prismicio / prismic-ts-codegen

A Prismic model-to-TypeScript-type generator.
Apache License 2.0
18 stars 6 forks source link

use config file as argument #31

Closed maapteh closed 2 years ago

maapteh commented 2 years ago

Is your feature request related to a problem? Please describe.

For our package/component in our root we have one prismicCodegen.config.ts file which generates the types from the url (production custom types). But in that same root we want to be able to generate the types from our checked-in model. So we can automate diffing when user changed something without us knowing from a component perspective.

Describe the solution you'd like

So just like other toolings (like jest etc) i want to be able to pass the fileName to the generator where to find its config. At the moment the name is hardcoded in DEFAULT_CONFIG_PATHS Since that argument can be optional this will not require us to make a breaking change

Describe alternatives you've considered

I considered that i wont need to use the local type to generate its types, but i would like to have the code in our checkin since CMS can not guarentee versioning.

maapteh commented 2 years ago

I can create this feature request in code, but first would like to know if you want it :)

angeloashmore commented 2 years ago

Hey @maapteh, great suggestion! The CLI actually supports this today using the --config / -c option:

npx prismic-ts-codegen --config myOtherConfig.ts

The --config option is helpful in situations like the one you described, or if you are querying content from multiple Prismic repositories.

DEFAULT_CONFIG_PATHS is only used if --config is not passed.


If you are already aware of this option but it is not working, let me know and I will investigate. 🙂

maapteh commented 2 years ago

Well for that option you need to tweak the file itself as well. Since it expects a module, so when you read your doc quickly you think why. So my config .ts file needs to be:

const dotenv = require('dotenv')

dotenv.config({ path: '.env' })

// from our production environment we take the typings
const config = {
  output: './src/types/@generated/prismic.ts',
  repositoryName: 'snip',
  customTypesAPIToken: process.env.PRISMIC_CUSTOM_TYPES_API_TOKEN,
  models: {
    fetchFromRepository: true,
  },
}

// reason: codegen wants setup like this
// eslint-disable-next-line import/no-default-export
// export default config

module.exports = config

But it still doesnt want what i thought, because my single Page component in our checkout has slices and these references are not in my codebase :) So the option is handy but then i still need ALL my components in the checkout. Thanks!

angeloashmore commented 2 years ago

@maapteh Thanks for the extra explanation. There was a bug in the CLI that caused custom configuration files to be loaded differently than the default file.

This should be fixed as of v0.1.4. You can use export default config in the configuration file now. module.exports is not recommended, but should continue to work.

You can update with the following command:

npm install --save-dev prismic-ts-codegen@latest

Then update your types:

# With the default config path
npx prismic-ts-codegen

# With the custom config path
npx prismic-ts-codegen --config path/to/config.ts

Based on your description, it sounds like you need two configuration files outputting to different files. Once those files are generated, you can perform a diff to determine inconsistencies.

I think you already have these two files written, but to be clear, each file should have the following config:

If you are still experiencing issues with this workflow, let me know and I'll try to help. 🙂

maapteh commented 2 years ago

Ah oke, so 1.0.4 makes me indeed use same syntax for the config file.

So I used the https://customtypes.prismic.io/customtypes endpoint to get all custom types locally in one json file. Then i generate them with:

const config: Config = {
  // we will never store this in our repo!
  output: './tmp/local.ts',
  models: ['./prismic-custom-types/index.json'], <= generated from customtypes endpoint!!!
}

which results in a file, nut no typings other then:

// Code generated by prismic-ts-codegen. DO NOT EDIT.

import type * as prismicT from '@prismicio/types'

type Simplify<T> = {
  [KeyType in keyof T]: T[KeyType]
}
angeloashmore commented 2 years ago

Hey @maapteh, sorry, I missed your notification.

You don't need to use the /customtypes API endpoint manually. The CLI will pull the types for you.

Your two configuration files should look like the following:

File 1: Uses models from Custom Types API

// prismicConfig-fromAPI.config.ts

import type { Config } from 'prismic-ts-codegen'
import dotenv from 'dotenv'

dotenv.config({ path: '.env' })

const config: Config = {
  output: './src/types/@generated/prismic-from-api.ts',
  repositoryName: 'snip',
  customTypesAPIToken: process.env.PRISMIC_CUSTOM_TYPES_API_TOKEN,
  models: {
    fetchFromRepository: true,
  },
  clientIntegration: {
    includeCreateClientInterface: false,
    includeContentNamespace: false,
  },
}

export default config

Command:

npx prismic-ts-codegen --config prismicConfig-fromAPI.config.ts

File 2: Uses local models

// prismicConfig.config.ts

import type { Config } from 'prismic-ts-codegen'

const config: Config = {
  output: './src/types/@generated/prismic.ts',
  models: ['./customtypes/**/index.json', './slices/**/model.json'],
}

export default config

Command:

npx prismic-ts-codegen

This configuration assumes you are using Slice Machine. If you save your models in a different location, update the models paths accordingly.


Let me know if that doesn't work and I'll try to help. 🙂