Skip to content

Commit

Permalink
Add --immutable-types flag to strapi-generate-types
Browse files Browse the repository at this point in the history
and add .npmignore
  • Loading branch information
acomagu committed Oct 5, 2023
1 parent f400573 commit aaa3be8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.tgz
*.tsbuildinfo
12 changes: 7 additions & 5 deletions generate-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { pascalCase } from 'change-case';
import { models, components, AttributesSetting } from './strapi-settings.js';
import { destroy } from './strapi.js';

const isReadonly = process.argv.includes('--immutable-types');

const project = new ts.Project();
const sf = project.createSourceFile('models.ts', '', { overwrite: true });

Expand All @@ -16,7 +18,7 @@ for (const [name, modelSetting] of Object.entries(models)) {
{
name: modelSetting.primaryKey,
type: 'number',
isReadonly: true,
isReadonly,
},
...interfacePropertiesFromAttributesSetting(modelSetting.attributes),
],
Expand All @@ -31,7 +33,7 @@ sf.addTypeAlias({

sf.addInterface({
isExported: true,
properties: Object.entries(models).map(([name]) => ({ name: `'${name}'`, type: modelInterfaceName(name), isReadonly: true })),
properties: Object.entries(models).map(([name]) => ({ name: `'${name}'`, type: modelInterfaceName(name), isReadonly })),
name: 'ModelByName',
});

Expand Down Expand Up @@ -71,7 +73,7 @@ function interfacePropertiesFromAttributesSetting(attributes: AttributesSetting)
const base = {
name,
hasQuestionToken: isOptional,
isReadonly: true,
isReadonly,
} as const;
const optionalTypeSuffix = isOptional ? ' | null' : '';
if ('type' in attr) {
Expand Down Expand Up @@ -100,7 +102,7 @@ function interfacePropertiesFromAttributesSetting(attributes: AttributesSetting)
return [{
...base,
hasQuestionToken: attr.repeatable ? false : isOptional, // For repeatable component field, at least empty array is necessary.
type: attr.repeatable ? `readonly ${componentInterfaceName(attr.component)}[]` : (componentInterfaceName(attr.component) + optionalTypeSuffix),
type: attr.repeatable ? `${isReadonly ? 'readonly ' : ''}${componentInterfaceName(attr.component)}[]` : (componentInterfaceName(attr.component) + optionalTypeSuffix),
}];
case 'decimal':
return [{ ...base, type: 'number' + optionalTypeSuffix }];
Expand All @@ -114,7 +116,7 @@ function interfacePropertiesFromAttributesSetting(attributes: AttributesSetting)
}
if ('collection' in attr) {
// return [{ ...base, type: attr.collection === '*' ? 'any' : modelInterfaceName(attr.collection) + optionalTypeSuffix }];
return [{ ...base, type: 'readonly number[]' + optionalTypeSuffix }];
return [{ ...base, type: `${isReadonly ? 'readonly ' : ''}number[]` + optionalTypeSuffix }];
}
throw new Error(`Unknown attribute type: ${JSON.stringify(attr)}`);
})
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "strapi-data-migrator",
"version": "0.0.1",
"name": "@acomagu/strapi-data-migrator",
"version": "0.0.2",
"type": "module",
"bin": {
"strapi-generate-types": "./generate-types.js",
"strapi-dump-data": "./dump-data.js",
"strapi-import-data": "./import-data.js",
"strapi-generate-transformer": "./generate-transformer.js"
"strapi-generate-types": "generate-types.js",
"strapi-dump-data": "dump-data.js",
"strapi-import-data": "import-data.js",
"strapi-generate-transformer": "generate-transformer.js"
},
"dependencies": {
"chalk": "^5.3.0",
Expand Down

0 comments on commit aaa3be8

Please sign in to comment.