@strapi/typescript-utils 5.50.1 → 5.50.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/compile.d.ts +5 -0
- package/dist/compile.d.ts.map +1 -0
- package/dist/compile.js +12 -0
- package/dist/compile.js.map +1 -0
- package/dist/compile.mjs +10 -0
- package/dist/compile.mjs.map +1 -0
- package/dist/compilers/basic.d.ts +11 -0
- package/dist/compilers/basic.d.ts.map +1 -0
- package/dist/compilers/basic.js +58 -0
- package/dist/compilers/basic.js.map +1 -0
- package/dist/compilers/basic.mjs +36 -0
- package/dist/compilers/basic.mjs.map +1 -0
- package/dist/compilers/index.d.ts +3 -0
- package/dist/compilers/index.d.ts.map +1 -0
- package/dist/compilers/index.js +8 -0
- package/dist/compilers/index.js.map +1 -0
- package/dist/compilers/index.mjs +3 -0
- package/dist/compilers/index.mjs.map +1 -0
- package/dist/generators/common/imports.d.ts +5 -0
- package/dist/generators/common/imports.d.ts.map +1 -0
- package/dist/generators/common/imports.js +42 -0
- package/dist/generators/common/imports.js.map +1 -0
- package/dist/generators/common/imports.mjs +19 -0
- package/dist/generators/common/imports.mjs.map +1 -0
- package/dist/generators/common/index.d.ts +4 -0
- package/dist/generators/common/index.d.ts.map +1 -0
- package/dist/generators/common/models/attributes.d.ts +17 -0
- package/dist/generators/common/models/attributes.d.ts.map +1 -0
- package/dist/generators/common/models/attributes.js +152 -0
- package/dist/generators/common/models/attributes.js.map +1 -0
- package/dist/generators/common/models/attributes.mjs +124 -0
- package/dist/generators/common/models/attributes.mjs.map +1 -0
- package/dist/generators/common/models/index.d.ts +7 -0
- package/dist/generators/common/models/index.d.ts.map +1 -0
- package/dist/generators/common/models/mappers.d.ts +9 -0
- package/dist/generators/common/models/mappers.d.ts.map +1 -0
- package/dist/generators/common/models/mappers.js +227 -0
- package/dist/generators/common/models/mappers.js.map +1 -0
- package/dist/generators/common/models/mappers.mjs +202 -0
- package/dist/generators/common/models/mappers.mjs.map +1 -0
- package/dist/generators/common/models/schema.d.ts +7 -0
- package/dist/generators/common/models/schema.d.ts.map +1 -0
- package/dist/generators/common/models/schema.js +79 -0
- package/dist/generators/common/models/schema.js.map +1 -0
- package/dist/generators/common/models/schema.mjs +57 -0
- package/dist/generators/common/models/schema.mjs.map +1 -0
- package/dist/generators/common/models/utils.d.ts +54 -0
- package/dist/generators/common/models/utils.d.ts.map +1 -0
- package/dist/generators/common/models/utils.js +109 -0
- package/dist/generators/common/models/utils.js.map +1 -0
- package/dist/generators/common/models/utils.mjs +81 -0
- package/dist/generators/common/models/utils.mjs.map +1 -0
- package/dist/generators/components/index.d.ts +9 -0
- package/dist/generators/components/index.d.ts.map +1 -0
- package/dist/generators/components/index.js +76 -0
- package/dist/generators/components/index.js.map +1 -0
- package/dist/generators/components/index.mjs +54 -0
- package/dist/generators/components/index.mjs.map +1 -0
- package/dist/generators/constants.d.ts +3 -0
- package/dist/generators/constants.d.ts.map +1 -0
- package/dist/generators/constants.js +8 -0
- package/dist/generators/constants.js.map +1 -0
- package/dist/generators/constants.mjs +5 -0
- package/dist/generators/constants.mjs.map +1 -0
- package/dist/generators/content-types/index.d.ts +9 -0
- package/dist/generators/content-types/index.d.ts.map +1 -0
- package/dist/generators/content-types/index.js +76 -0
- package/dist/generators/content-types/index.js.map +1 -0
- package/dist/generators/content-types/index.mjs +54 -0
- package/dist/generators/content-types/index.mjs.map +1 -0
- package/dist/generators/index.d.ts +23 -0
- package/dist/generators/index.d.ts.map +1 -0
- package/dist/generators/index.js +81 -0
- package/dist/generators/index.js.map +1 -0
- package/dist/generators/index.mjs +74 -0
- package/dist/generators/index.mjs.map +1 -0
- package/dist/generators/utils.d.ts +52 -0
- package/dist/generators/utils.d.ts.map +1 -0
- package/dist/generators/utils.js +164 -0
- package/dist/generators/utils.js.map +1 -0
- package/dist/generators/utils.mjs +131 -0
- package/dist/generators/utils.mjs.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +28 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +14 -0
- package/dist/index.mjs.map +1 -0
- package/dist/utils/format-host.d.ts +3 -0
- package/dist/utils/format-host.d.ts.map +1 -0
- package/dist/utils/format-host.js +33 -0
- package/dist/utils/format-host.js.map +1 -0
- package/dist/utils/format-host.mjs +11 -0
- package/dist/utils/format-host.mjs.map +1 -0
- package/dist/utils/get-config-path.d.ts +10 -0
- package/dist/utils/get-config-path.d.ts.map +1 -0
- package/dist/utils/get-config-path.js +43 -0
- package/dist/utils/get-config-path.js.map +1 -0
- package/dist/utils/get-config-path.mjs +18 -0
- package/dist/utils/get-config-path.mjs.map +1 -0
- package/dist/utils/index.d.ts +9 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/is-using-typescript-sync.d.ts +5 -0
- package/dist/utils/is-using-typescript-sync.d.ts.map +1 -0
- package/dist/utils/is-using-typescript-sync.js +20 -0
- package/dist/utils/is-using-typescript-sync.js.map +1 -0
- package/dist/utils/is-using-typescript-sync.mjs +14 -0
- package/dist/utils/is-using-typescript-sync.mjs.map +1 -0
- package/dist/utils/is-using-typescript.d.ts +5 -0
- package/dist/utils/is-using-typescript.d.ts.map +1 -0
- package/dist/utils/is-using-typescript.js +20 -0
- package/dist/utils/is-using-typescript.js.map +1 -0
- package/dist/utils/is-using-typescript.mjs +14 -0
- package/dist/utils/is-using-typescript.mjs.map +1 -0
- package/dist/utils/report-diagnostics.d.ts +6 -0
- package/dist/utils/report-diagnostics.d.ts.map +1 -0
- package/dist/utils/report-diagnostics.js +37 -0
- package/dist/utils/report-diagnostics.js.map +1 -0
- package/dist/utils/report-diagnostics.mjs +15 -0
- package/dist/utils/report-diagnostics.mjs.map +1 -0
- package/dist/utils/resolve-config-options.d.ts +12 -0
- package/dist/utils/resolve-config-options.d.ts.map +1 -0
- package/dist/utils/resolve-config-options.js +39 -0
- package/dist/utils/resolve-config-options.js.map +1 -0
- package/dist/utils/resolve-config-options.mjs +17 -0
- package/dist/utils/resolve-config-options.mjs.map +1 -0
- package/dist/utils/resolve-outdir-sync.d.ts +5 -0
- package/dist/utils/resolve-outdir-sync.d.ts.map +1 -0
- package/dist/utils/resolve-outdir-sync.js +19 -0
- package/dist/utils/resolve-outdir-sync.js.map +1 -0
- package/dist/utils/resolve-outdir-sync.mjs +13 -0
- package/dist/utils/resolve-outdir-sync.mjs.map +1 -0
- package/dist/utils/resolve-outdir.d.ts +5 -0
- package/dist/utils/resolve-outdir.d.ts.map +1 -0
- package/dist/utils/resolve-outdir.js +19 -0
- package/dist/utils/resolve-outdir.js.map +1 -0
- package/dist/utils/resolve-outdir.mjs +13 -0
- package/dist/utils/resolve-outdir.mjs.map +1 -0
- package/package.json +25 -10
- package/.eslintrc.cjs +0 -16
- package/index.d.ts +0 -5
- package/jest.config.js +0 -6
- package/lib/__tests__/generators/schemas/attributes.test.js +0 -903
- package/lib/__tests__/generators/schemas/imports.test.js +0 -56
- package/lib/__tests__/generators/schemas/utils.test.js +0 -314
- package/lib/__tests__/generators/utils.test.js +0 -43
- package/lib/compile.js +0 -10
- package/lib/compilers/basic.js +0 -50
- package/lib/compilers/index.js +0 -7
- package/lib/generators/common/imports.js +0 -34
- package/lib/generators/common/index.js +0 -9
- package/lib/generators/common/models/attributes.js +0 -198
- package/lib/generators/common/models/index.js +0 -15
- package/lib/generators/common/models/mappers.js +0 -144
- package/lib/generators/common/models/schema.js +0 -93
- package/lib/generators/common/models/utils.js +0 -178
- package/lib/generators/components/index.js +0 -74
- package/lib/generators/constants.js +0 -6
- package/lib/generators/content-types/index.js +0 -74
- package/lib/generators/index.js +0 -122
- package/lib/generators/utils.js +0 -217
- package/lib/index.js +0 -13
- package/lib/utils/format-host.js +0 -15
- package/lib/utils/get-config-path.js +0 -29
- package/lib/utils/index.js +0 -21
- package/lib/utils/is-using-typescript-sync.js +0 -19
- package/lib/utils/is-using-typescript.js +0 -17
- package/lib/utils/report-diagnostics.js +0 -19
- package/lib/utils/resolve-config-options.js +0 -23
- package/lib/utils/resolve-outdir-sync.js +0 -18
- package/lib/utils/resolve-outdir.js +0 -18
- package/lint-staged.config.mjs +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compile.d.ts","sourceRoot":"","sources":["../src/compile.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAEvD,eAAO,MAAM,OAAO,GAClB,QAAQ,MAAM,EACd,oBAAwB;IAAE,aAAa,CAAC,EAAE,aAAa,CAAA;CAAO,KAC7D,OAAO,CAAC,IAAI,CAId,CAAC"}
|
package/dist/compile.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var basic = require('./compilers/basic.js');
|
|
4
|
+
var getConfigPath = require('./utils/get-config-path.js');
|
|
5
|
+
|
|
6
|
+
const compile = async (srcDir, { configOptions = {} } = {})=>{
|
|
7
|
+
const configPath = getConfigPath.getConfigPath(srcDir);
|
|
8
|
+
basic.run(configPath, configOptions);
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
exports.compile = compile;
|
|
12
|
+
//# sourceMappingURL=compile.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compile.js","sources":["../src/compile.ts"],"sourcesContent":["import * as compilers from './compilers';\nimport { getConfigPath } from './utils/get-config-path';\nimport type { ConfigOptions } from './compilers/basic';\n\nexport const compile = async (\n srcDir: string,\n { configOptions = {} }: { configOptions?: ConfigOptions } = {}\n): Promise<void> => {\n const configPath = getConfigPath(srcDir);\n\n compilers.basic.run(configPath as string, configOptions);\n};\n"],"names":["compile","srcDir","configOptions","configPath","getConfigPath","compilers"],"mappings":";;;;;AAIO,MAAMA,OAAAA,GAAU,OACrBC,MAAAA,EACA,EAAEC,aAAAA,GAAgB,EAAE,EAAqC,GAAG,EAAE,GAAA;AAE9D,IAAA,MAAMC,aAAaC,2BAAAA,CAAcH,MAAAA,CAAAA;AAEjCI,IAAAA,SAAmB,CAACF,UAAAA,EAAsBD,aAAAA,CAAAA;AAC5C;;;;"}
|
package/dist/compile.mjs
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { run } from './compilers/basic.mjs';
|
|
2
|
+
import { getConfigPath } from './utils/get-config-path.mjs';
|
|
3
|
+
|
|
4
|
+
const compile = async (srcDir, { configOptions = {} } = {})=>{
|
|
5
|
+
const configPath = getConfigPath(srcDir);
|
|
6
|
+
run(configPath, configOptions);
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export { compile };
|
|
10
|
+
//# sourceMappingURL=compile.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compile.mjs","sources":["../src/compile.ts"],"sourcesContent":["import * as compilers from './compilers';\nimport { getConfigPath } from './utils/get-config-path';\nimport type { ConfigOptions } from './compilers/basic';\n\nexport const compile = async (\n srcDir: string,\n { configOptions = {} }: { configOptions?: ConfigOptions } = {}\n): Promise<void> => {\n const configPath = getConfigPath(srcDir);\n\n compilers.basic.run(configPath as string, configOptions);\n};\n"],"names":["compile","srcDir","configOptions","configPath","getConfigPath","compilers"],"mappings":";;;AAIO,MAAMA,OAAAA,GAAU,OACrBC,MAAAA,EACA,EAAEC,aAAAA,GAAgB,EAAE,EAAqC,GAAG,EAAE,GAAA;AAE9D,IAAA,MAAMC,aAAaC,aAAAA,CAAcH,MAAAA,CAAAA;AAEjCI,IAAAA,GAAmB,CAACF,UAAAA,EAAsBD,aAAAA,CAAAA;AAC5C;;;;"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import * as ts from 'typescript';
|
|
2
|
+
export interface ConfigOptions {
|
|
3
|
+
fileNames?: string[];
|
|
4
|
+
options?: ts.CompilerOptions;
|
|
5
|
+
ignoreDiagnostics?: boolean;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Default TS -> JS Compilation for Strapi
|
|
9
|
+
*/
|
|
10
|
+
export declare function run(tsConfigPath: string, configOptions?: ConfigOptions): void;
|
|
11
|
+
//# sourceMappingURL=basic.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"basic.d.ts","sourceRoot":"","sources":["../../src/compilers/basic.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,YAAY,CAAC;AAMjC,MAAM,WAAW,aAAa;IAC5B,SAAS,CAAC,EAAE,MAAM,EAAE,CAAC;IACrB,OAAO,CAAC,EAAE,EAAE,CAAC,eAAe,CAAC;IAC7B,iBAAiB,CAAC,EAAE,OAAO,CAAC;CAC7B;AAED;;GAEG;AACH,wBAAgB,GAAG,CAAC,YAAY,EAAE,MAAM,EAAE,aAAa,GAAE,aAAkB,QA+B1E"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var ts = require('typescript');
|
|
4
|
+
var lodash = require('lodash');
|
|
5
|
+
var reportDiagnostics = require('../utils/report-diagnostics.js');
|
|
6
|
+
var resolveConfigOptions = require('../utils/resolve-config-options.js');
|
|
7
|
+
|
|
8
|
+
function _interopNamespace(e) {
|
|
9
|
+
if (e && e.__esModule) return e;
|
|
10
|
+
var n = Object.create(null);
|
|
11
|
+
if (e) {
|
|
12
|
+
Object.keys(e).forEach(function (k) {
|
|
13
|
+
if (k !== 'default') {
|
|
14
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
15
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
16
|
+
enumerable: true,
|
|
17
|
+
get: function () { return e[k]; }
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
n.default = e;
|
|
23
|
+
return Object.freeze(n);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
var ts__namespace = /*#__PURE__*/_interopNamespace(ts);
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Default TS -> JS Compilation for Strapi
|
|
30
|
+
*/ function run(tsConfigPath, configOptions = {}) {
|
|
31
|
+
const { ignoreDiagnostics = false } = configOptions;
|
|
32
|
+
// Parse the tsconfig.json file & resolve the configuration options
|
|
33
|
+
const { fileNames, options, projectReferences } = resolveConfigOptions.resolveConfigOptions(tsConfigPath);
|
|
34
|
+
const compilerOptions = lodash.merge(options, configOptions.options);
|
|
35
|
+
if (ignoreDiagnostics) {
|
|
36
|
+
Object.assign(compilerOptions, {
|
|
37
|
+
noEmit: false,
|
|
38
|
+
noEmitOnError: false
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
const program = ts__namespace.createProgram({
|
|
42
|
+
rootNames: configOptions.fileNames ? configOptions.fileNames : fileNames,
|
|
43
|
+
projectReferences,
|
|
44
|
+
options: compilerOptions
|
|
45
|
+
});
|
|
46
|
+
const emitResults = program.emit();
|
|
47
|
+
const diagnostics = ts__namespace.sortAndDeduplicateDiagnostics(ts__namespace.getPreEmitDiagnostics(program).concat(emitResults.diagnostics));
|
|
48
|
+
if (!ignoreDiagnostics && diagnostics.length > 0) {
|
|
49
|
+
reportDiagnostics.reportDiagnostics(diagnostics);
|
|
50
|
+
}
|
|
51
|
+
// If the compilation failed and diagnostics are not ignored, exit early
|
|
52
|
+
if (!ignoreDiagnostics && emitResults.emitSkipped) {
|
|
53
|
+
throw new Error('TypeScript compilation failed');
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
exports.run = run;
|
|
58
|
+
//# sourceMappingURL=basic.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"basic.js","sources":["../../src/compilers/basic.ts"],"sourcesContent":["import * as ts from 'typescript';\nimport { merge } from 'lodash';\n\nimport { reportDiagnostics } from '../utils/report-diagnostics';\nimport { resolveConfigOptions } from '../utils/resolve-config-options';\n\nexport interface ConfigOptions {\n fileNames?: string[];\n options?: ts.CompilerOptions;\n ignoreDiagnostics?: boolean;\n}\n\n/**\n * Default TS -> JS Compilation for Strapi\n */\nexport function run(tsConfigPath: string, configOptions: ConfigOptions = {}) {\n const { ignoreDiagnostics = false } = configOptions;\n // Parse the tsconfig.json file & resolve the configuration options\n const { fileNames, options, projectReferences } = resolveConfigOptions(tsConfigPath);\n\n const compilerOptions = merge(options, configOptions.options);\n\n if (ignoreDiagnostics) {\n Object.assign(compilerOptions, { noEmit: false, noEmitOnError: false });\n }\n\n const program = ts.createProgram({\n rootNames: configOptions.fileNames ? configOptions.fileNames : fileNames,\n projectReferences,\n options: compilerOptions,\n });\n\n const emitResults = program.emit();\n\n const diagnostics = ts.sortAndDeduplicateDiagnostics(\n ts.getPreEmitDiagnostics(program).concat(emitResults.diagnostics)\n );\n\n if (!ignoreDiagnostics && diagnostics.length > 0) {\n reportDiagnostics(diagnostics);\n }\n\n // If the compilation failed and diagnostics are not ignored, exit early\n if (!ignoreDiagnostics && emitResults.emitSkipped) {\n throw new Error('TypeScript compilation failed');\n }\n}\n"],"names":["run","tsConfigPath","configOptions","ignoreDiagnostics","fileNames","options","projectReferences","resolveConfigOptions","compilerOptions","merge","Object","assign","noEmit","noEmitOnError","program","ts","createProgram","rootNames","emitResults","emit","diagnostics","sortAndDeduplicateDiagnostics","getPreEmitDiagnostics","concat","length","reportDiagnostics","emitSkipped","Error"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAYA;;AAEC,IACM,SAASA,GAAAA,CAAIC,YAAoB,EAAEC,aAAAA,GAA+B,EAAE,EAAA;AACzE,IAAA,MAAM,EAAEC,iBAAAA,GAAoB,KAAK,EAAE,GAAGD,aAAAA;;IAEtC,MAAM,EAAEE,SAAS,EAAEC,OAAO,EAAEC,iBAAiB,EAAE,GAAGC,yCAAAA,CAAqBN,YAAAA,CAAAA;AAEvE,IAAA,MAAMO,eAAAA,GAAkBC,YAAAA,CAAMJ,OAAAA,EAASH,aAAAA,CAAcG,OAAO,CAAA;AAE5D,IAAA,IAAIF,iBAAAA,EAAmB;QACrBO,MAAAA,CAAOC,MAAM,CAACH,eAAAA,EAAiB;YAAEI,MAAAA,EAAQ,KAAA;YAAOC,aAAAA,EAAe;AAAM,SAAA,CAAA;AACvE,IAAA;IAEA,MAAMC,OAAAA,GAAUC,aAAAA,CAAGC,aAAa,CAAC;AAC/BC,QAAAA,SAAAA,EAAWf,aAAAA,CAAcE,SAAS,GAAGF,aAAAA,CAAcE,SAAS,GAAGA,SAAAA;AAC/DE,QAAAA,iBAAAA;QACAD,OAAAA,EAASG;AACX,KAAA,CAAA;IAEA,MAAMU,WAAAA,GAAcJ,QAAQK,IAAI,EAAA;IAEhC,MAAMC,WAAAA,GAAcL,aAAAA,CAAGM,6BAA6B,CAClDN,aAAAA,CAAGO,qBAAqB,CAACR,OAAAA,CAAAA,CAASS,MAAM,CAACL,WAAAA,CAAYE,WAAW,CAAA,CAAA;AAGlE,IAAA,IAAI,CAACjB,iBAAAA,IAAqBiB,WAAAA,CAAYI,MAAM,GAAG,CAAA,EAAG;QAChDC,mCAAAA,CAAkBL,WAAAA,CAAAA;AACpB,IAAA;;AAGA,IAAA,IAAI,CAACjB,iBAAAA,IAAqBe,WAAAA,CAAYQ,WAAW,EAAE;AACjD,QAAA,MAAM,IAAIC,KAAAA,CAAM,+BAAA,CAAA;AAClB,IAAA;AACF;;;;"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import * as ts from 'typescript';
|
|
2
|
+
import { merge } from 'lodash';
|
|
3
|
+
import { reportDiagnostics } from '../utils/report-diagnostics.mjs';
|
|
4
|
+
import { resolveConfigOptions } from '../utils/resolve-config-options.mjs';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Default TS -> JS Compilation for Strapi
|
|
8
|
+
*/ function run(tsConfigPath, configOptions = {}) {
|
|
9
|
+
const { ignoreDiagnostics = false } = configOptions;
|
|
10
|
+
// Parse the tsconfig.json file & resolve the configuration options
|
|
11
|
+
const { fileNames, options, projectReferences } = resolveConfigOptions(tsConfigPath);
|
|
12
|
+
const compilerOptions = merge(options, configOptions.options);
|
|
13
|
+
if (ignoreDiagnostics) {
|
|
14
|
+
Object.assign(compilerOptions, {
|
|
15
|
+
noEmit: false,
|
|
16
|
+
noEmitOnError: false
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
const program = ts.createProgram({
|
|
20
|
+
rootNames: configOptions.fileNames ? configOptions.fileNames : fileNames,
|
|
21
|
+
projectReferences,
|
|
22
|
+
options: compilerOptions
|
|
23
|
+
});
|
|
24
|
+
const emitResults = program.emit();
|
|
25
|
+
const diagnostics = ts.sortAndDeduplicateDiagnostics(ts.getPreEmitDiagnostics(program).concat(emitResults.diagnostics));
|
|
26
|
+
if (!ignoreDiagnostics && diagnostics.length > 0) {
|
|
27
|
+
reportDiagnostics(diagnostics);
|
|
28
|
+
}
|
|
29
|
+
// If the compilation failed and diagnostics are not ignored, exit early
|
|
30
|
+
if (!ignoreDiagnostics && emitResults.emitSkipped) {
|
|
31
|
+
throw new Error('TypeScript compilation failed');
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export { run };
|
|
36
|
+
//# sourceMappingURL=basic.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"basic.mjs","sources":["../../src/compilers/basic.ts"],"sourcesContent":["import * as ts from 'typescript';\nimport { merge } from 'lodash';\n\nimport { reportDiagnostics } from '../utils/report-diagnostics';\nimport { resolveConfigOptions } from '../utils/resolve-config-options';\n\nexport interface ConfigOptions {\n fileNames?: string[];\n options?: ts.CompilerOptions;\n ignoreDiagnostics?: boolean;\n}\n\n/**\n * Default TS -> JS Compilation for Strapi\n */\nexport function run(tsConfigPath: string, configOptions: ConfigOptions = {}) {\n const { ignoreDiagnostics = false } = configOptions;\n // Parse the tsconfig.json file & resolve the configuration options\n const { fileNames, options, projectReferences } = resolveConfigOptions(tsConfigPath);\n\n const compilerOptions = merge(options, configOptions.options);\n\n if (ignoreDiagnostics) {\n Object.assign(compilerOptions, { noEmit: false, noEmitOnError: false });\n }\n\n const program = ts.createProgram({\n rootNames: configOptions.fileNames ? configOptions.fileNames : fileNames,\n projectReferences,\n options: compilerOptions,\n });\n\n const emitResults = program.emit();\n\n const diagnostics = ts.sortAndDeduplicateDiagnostics(\n ts.getPreEmitDiagnostics(program).concat(emitResults.diagnostics)\n );\n\n if (!ignoreDiagnostics && diagnostics.length > 0) {\n reportDiagnostics(diagnostics);\n }\n\n // If the compilation failed and diagnostics are not ignored, exit early\n if (!ignoreDiagnostics && emitResults.emitSkipped) {\n throw new Error('TypeScript compilation failed');\n }\n}\n"],"names":["run","tsConfigPath","configOptions","ignoreDiagnostics","fileNames","options","projectReferences","resolveConfigOptions","compilerOptions","merge","Object","assign","noEmit","noEmitOnError","program","ts","createProgram","rootNames","emitResults","emit","diagnostics","sortAndDeduplicateDiagnostics","getPreEmitDiagnostics","concat","length","reportDiagnostics","emitSkipped","Error"],"mappings":";;;;;AAYA;;AAEC,IACM,SAASA,GAAAA,CAAIC,YAAoB,EAAEC,aAAAA,GAA+B,EAAE,EAAA;AACzE,IAAA,MAAM,EAAEC,iBAAAA,GAAoB,KAAK,EAAE,GAAGD,aAAAA;;IAEtC,MAAM,EAAEE,SAAS,EAAEC,OAAO,EAAEC,iBAAiB,EAAE,GAAGC,oBAAAA,CAAqBN,YAAAA,CAAAA;AAEvE,IAAA,MAAMO,eAAAA,GAAkBC,KAAAA,CAAMJ,OAAAA,EAASH,aAAAA,CAAcG,OAAO,CAAA;AAE5D,IAAA,IAAIF,iBAAAA,EAAmB;QACrBO,MAAAA,CAAOC,MAAM,CAACH,eAAAA,EAAiB;YAAEI,MAAAA,EAAQ,KAAA;YAAOC,aAAAA,EAAe;AAAM,SAAA,CAAA;AACvE,IAAA;IAEA,MAAMC,OAAAA,GAAUC,EAAAA,CAAGC,aAAa,CAAC;AAC/BC,QAAAA,SAAAA,EAAWf,aAAAA,CAAcE,SAAS,GAAGF,aAAAA,CAAcE,SAAS,GAAGA,SAAAA;AAC/DE,QAAAA,iBAAAA;QACAD,OAAAA,EAASG;AACX,KAAA,CAAA;IAEA,MAAMU,WAAAA,GAAcJ,QAAQK,IAAI,EAAA;IAEhC,MAAMC,WAAAA,GAAcL,EAAAA,CAAGM,6BAA6B,CAClDN,EAAAA,CAAGO,qBAAqB,CAACR,OAAAA,CAAAA,CAASS,MAAM,CAACL,WAAAA,CAAYE,WAAW,CAAA,CAAA;AAGlE,IAAA,IAAI,CAACjB,iBAAAA,IAAqBiB,WAAAA,CAAYI,MAAM,GAAG,CAAA,EAAG;QAChDC,iBAAAA,CAAkBL,WAAAA,CAAAA;AACpB,IAAA;;AAGA,IAAA,IAAI,CAACjB,iBAAAA,IAAqBe,WAAAA,CAAYQ,WAAW,EAAE;AACjD,QAAA,MAAM,IAAIC,KAAAA,CAAM,+BAAA,CAAA;AAClB,IAAA;AACF;;;;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/compilers/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC;AAEjC,OAAO,EAAE,KAAK,EAAE,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"imports.d.ts","sourceRoot":"","sources":["../../../src/generators/common/imports.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,YAAY,CAAC;AAMjC,wBAAgB,UAAU,IAAI,MAAM,EAAE,CAErC;AAED,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAM5C;AAED,wBAAgB,wBAAwB,IAAI,EAAE,CAAC,iBAAiB,EAAE,CAajE"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var ts = require('typescript');
|
|
4
|
+
|
|
5
|
+
function _interopNamespace(e) {
|
|
6
|
+
if (e && e.__esModule) return e;
|
|
7
|
+
var n = Object.create(null);
|
|
8
|
+
if (e) {
|
|
9
|
+
Object.keys(e).forEach(function (k) {
|
|
10
|
+
if (k !== 'default') {
|
|
11
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
12
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function () { return e[k]; }
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
n.default = e;
|
|
20
|
+
return Object.freeze(n);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
var ts__namespace = /*#__PURE__*/_interopNamespace(ts);
|
|
24
|
+
|
|
25
|
+
const { factory } = ts__namespace;
|
|
26
|
+
const imports = [];
|
|
27
|
+
function addImport(type) {
|
|
28
|
+
const hasType = imports.includes(type);
|
|
29
|
+
if (!hasType) {
|
|
30
|
+
imports.push(type);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
function generateImportDefinition() {
|
|
34
|
+
const formattedImports = imports.sort().map((key)=>factory.createImportSpecifier(false, undefined, factory.createIdentifier(key)));
|
|
35
|
+
return [
|
|
36
|
+
factory.createImportDeclaration(undefined, factory.createImportClause(true, undefined, factory.createNamedImports(formattedImports)), factory.createStringLiteral('@strapi/strapi'), undefined)
|
|
37
|
+
];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
exports.addImport = addImport;
|
|
41
|
+
exports.generateImportDefinition = generateImportDefinition;
|
|
42
|
+
//# sourceMappingURL=imports.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"imports.js","sources":["../../../src/generators/common/imports.ts"],"sourcesContent":["import * as ts from 'typescript';\n\nconst { factory } = ts;\n\nconst imports: string[] = [];\n\nexport function getImports(): string[] {\n return imports;\n}\n\nexport function addImport(type: string): void {\n const hasType = imports.includes(type);\n\n if (!hasType) {\n imports.push(type);\n }\n}\n\nexport function generateImportDefinition(): ts.ImportDeclaration[] {\n const formattedImports = imports\n .sort()\n .map((key) => factory.createImportSpecifier(false, undefined, factory.createIdentifier(key)));\n\n return [\n factory.createImportDeclaration(\n undefined,\n factory.createImportClause(true, undefined, factory.createNamedImports(formattedImports)),\n factory.createStringLiteral('@strapi/strapi'),\n undefined\n ),\n ];\n}\n"],"names":["factory","ts","imports","addImport","type","hasType","includes","push","generateImportDefinition","formattedImports","sort","map","key","createImportSpecifier","undefined","createIdentifier","createImportDeclaration","createImportClause","createNamedImports","createStringLiteral"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAEA,MAAM,EAAEA,OAAO,EAAE,GAAGC,aAAAA;AAEpB,MAAMC,UAAoB,EAAE;AAMrB,SAASC,UAAUC,IAAY,EAAA;IACpC,MAAMC,OAAAA,GAAUH,OAAAA,CAAQI,QAAQ,CAACF,IAAAA,CAAAA;AAEjC,IAAA,IAAI,CAACC,OAAAA,EAAS;AACZH,QAAAA,OAAAA,CAAQK,IAAI,CAACH,IAAAA,CAAAA;AACf,IAAA;AACF;AAEO,SAASI,wBAAAA,GAAAA;AACd,IAAA,MAAMC,gBAAAA,GAAmBP,OAAAA,CACtBQ,IAAI,EAAA,CACJC,GAAG,CAAC,CAACC,GAAAA,GAAQZ,OAAAA,CAAQa,qBAAqB,CAAC,KAAA,EAAOC,SAAAA,EAAWd,OAAAA,CAAQe,gBAAgB,CAACH,GAAAA,CAAAA,CAAAA,CAAAA;IAEzF,OAAO;AACLZ,QAAAA,OAAAA,CAAQgB,uBAAuB,CAC7BF,SAAAA,EACAd,OAAAA,CAAQiB,kBAAkB,CAAC,IAAA,EAAMH,SAAAA,EAAWd,OAAAA,CAAQkB,kBAAkB,CAACT,gBAAAA,CAAAA,CAAAA,EACvET,OAAAA,CAAQmB,mBAAmB,CAAC,gBAAA,CAAA,EAC5BL,SAAAA;AAEH,KAAA;AACH;;;;;"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import * as ts from 'typescript';
|
|
2
|
+
|
|
3
|
+
const { factory } = ts;
|
|
4
|
+
const imports = [];
|
|
5
|
+
function addImport(type) {
|
|
6
|
+
const hasType = imports.includes(type);
|
|
7
|
+
if (!hasType) {
|
|
8
|
+
imports.push(type);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
function generateImportDefinition() {
|
|
12
|
+
const formattedImports = imports.sort().map((key)=>factory.createImportSpecifier(false, undefined, factory.createIdentifier(key)));
|
|
13
|
+
return [
|
|
14
|
+
factory.createImportDeclaration(undefined, factory.createImportClause(true, undefined, factory.createNamedImports(formattedImports)), factory.createStringLiteral('@strapi/strapi'), undefined)
|
|
15
|
+
];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export { addImport, generateImportDefinition };
|
|
19
|
+
//# sourceMappingURL=imports.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"imports.mjs","sources":["../../../src/generators/common/imports.ts"],"sourcesContent":["import * as ts from 'typescript';\n\nconst { factory } = ts;\n\nconst imports: string[] = [];\n\nexport function getImports(): string[] {\n return imports;\n}\n\nexport function addImport(type: string): void {\n const hasType = imports.includes(type);\n\n if (!hasType) {\n imports.push(type);\n }\n}\n\nexport function generateImportDefinition(): ts.ImportDeclaration[] {\n const formattedImports = imports\n .sort()\n .map((key) => factory.createImportSpecifier(false, undefined, factory.createIdentifier(key)));\n\n return [\n factory.createImportDeclaration(\n undefined,\n factory.createImportClause(true, undefined, factory.createNamedImports(formattedImports)),\n factory.createStringLiteral('@strapi/strapi'),\n undefined\n ),\n ];\n}\n"],"names":["factory","ts","imports","addImport","type","hasType","includes","push","generateImportDefinition","formattedImports","sort","map","key","createImportSpecifier","undefined","createIdentifier","createImportDeclaration","createImportClause","createNamedImports","createStringLiteral"],"mappings":";;AAEA,MAAM,EAAEA,OAAO,EAAE,GAAGC,EAAAA;AAEpB,MAAMC,UAAoB,EAAE;AAMrB,SAASC,UAAUC,IAAY,EAAA;IACpC,MAAMC,OAAAA,GAAUH,OAAAA,CAAQI,QAAQ,CAACF,IAAAA,CAAAA;AAEjC,IAAA,IAAI,CAACC,OAAAA,EAAS;AACZH,QAAAA,OAAAA,CAAQK,IAAI,CAACH,IAAAA,CAAAA;AACf,IAAA;AACF;AAEO,SAASI,wBAAAA,GAAAA;AACd,IAAA,MAAMC,gBAAAA,GAAmBP,OAAAA,CACtBQ,IAAI,EAAA,CACJC,GAAG,CAAC,CAACC,GAAAA,GAAQZ,OAAAA,CAAQa,qBAAqB,CAAC,KAAA,EAAOC,SAAAA,EAAWd,OAAAA,CAAQe,gBAAgB,CAACH,GAAAA,CAAAA,CAAAA,CAAAA;IAEzF,OAAO;AACLZ,QAAAA,OAAAA,CAAQgB,uBAAuB,CAC7BF,SAAAA,EACAd,OAAAA,CAAQiB,kBAAkB,CAAC,IAAA,EAAMH,SAAAA,EAAWd,OAAAA,CAAQkB,kBAAkB,CAACT,gBAAAA,CAAAA,CAAAA,EACvET,OAAAA,CAAQmB,mBAAmB,CAAC,gBAAA,CAAA,EAC5BL,SAAAA;AAEH,KAAA;AACH;;;;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/generators/common/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,UAAU,CAAC;AACnC,OAAO,KAAK,OAAO,MAAM,WAAW,CAAC;AAErC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import * as ts from 'typescript';
|
|
2
|
+
import type { Attribute, Schema } from './utils';
|
|
3
|
+
import { mappers } from './mappers';
|
|
4
|
+
/**
|
|
5
|
+
* Create the base type node for a given attribute
|
|
6
|
+
*/
|
|
7
|
+
export declare const getAttributeType: (attributeName: string, attribute: Attribute, uid?: string) => ts.TypeReferenceNode | null;
|
|
8
|
+
/**
|
|
9
|
+
* Collect every modifier node from an attribute
|
|
10
|
+
*/
|
|
11
|
+
export declare const getAttributeModifiers: (attribute: Attribute) => ts.TypeNode[];
|
|
12
|
+
/**
|
|
13
|
+
* Generate a property signature node for a given attribute
|
|
14
|
+
*/
|
|
15
|
+
export declare const attributeToPropertySignature: (schema: Schema, attributeName: string, attribute: Attribute) => ts.PropertySignature | null;
|
|
16
|
+
export { mappers };
|
|
17
|
+
//# sourceMappingURL=attributes.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"attributes.d.ts","sourceRoot":"","sources":["../../../../src/generators/common/models/attributes.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,YAAY,CAAC;AAKjC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AACjD,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAIpC;;GAEG;AACH,eAAO,MAAM,gBAAgB,GAC3B,eAAe,MAAM,EACrB,WAAW,SAAS,EACpB,MAAM,MAAM,KACX,EAAE,CAAC,iBAAiB,GAAG,IAezB,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,qBAAqB,GAAI,WAAW,SAAS,KAAG,EAAE,CAAC,QAAQ,EA4HvE,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,4BAA4B,GACvC,QAAQ,MAAM,EACd,eAAe,MAAM,EACrB,WAAW,SAAS,KACnB,EAAE,CAAC,iBAAiB,GAAG,IAiBzB,CAAC;AAEF,OAAO,EAAE,OAAO,EAAE,CAAC"}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var ts = require('typescript');
|
|
4
|
+
var _ = require('lodash/fp');
|
|
5
|
+
var imports = require('../imports.js');
|
|
6
|
+
var utils = require('./utils.js');
|
|
7
|
+
var mappers = require('./mappers.js');
|
|
8
|
+
|
|
9
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
10
|
+
|
|
11
|
+
function _interopNamespace(e) {
|
|
12
|
+
if (e && e.__esModule) return e;
|
|
13
|
+
var n = Object.create(null);
|
|
14
|
+
if (e) {
|
|
15
|
+
Object.keys(e).forEach(function (k) {
|
|
16
|
+
if (k !== 'default') {
|
|
17
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
18
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
19
|
+
enumerable: true,
|
|
20
|
+
get: function () { return e[k]; }
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
n.default = e;
|
|
26
|
+
return Object.freeze(n);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
var ts__namespace = /*#__PURE__*/_interopNamespace(ts);
|
|
30
|
+
var ___default = /*#__PURE__*/_interopDefault(_);
|
|
31
|
+
|
|
32
|
+
const { factory } = ts__namespace;
|
|
33
|
+
/**
|
|
34
|
+
* Create the base type node for a given attribute
|
|
35
|
+
*/ const getAttributeType = (attributeName, attribute, uid)=>{
|
|
36
|
+
if (!Object.keys(mappers.mappers).includes(attribute.type)) {
|
|
37
|
+
console.warn(`"${attributeName}" attribute from "${uid}" has an invalid type: "${attribute.type}"`);
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
const [attributeType, typeParams] = mappers.mappers[attribute.type]({
|
|
41
|
+
uid,
|
|
42
|
+
attribute,
|
|
43
|
+
attributeName
|
|
44
|
+
});
|
|
45
|
+
// Make sure the schema namespace is imported
|
|
46
|
+
imports.addImport(utils.NAMESPACES.Schema);
|
|
47
|
+
return utils.getTypeNode(attributeType, typeParams);
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* Collect every modifier node from an attribute
|
|
51
|
+
*/ const getAttributeModifiers = (attribute)=>{
|
|
52
|
+
const modifiers = [];
|
|
53
|
+
// Required
|
|
54
|
+
if (attribute.required) {
|
|
55
|
+
modifiers.push(factory.createTypeReferenceNode(factory.createIdentifier(utils.withAttributeNamespace('Required'))));
|
|
56
|
+
}
|
|
57
|
+
// Private
|
|
58
|
+
if (attribute.private) {
|
|
59
|
+
modifiers.push(factory.createTypeReferenceNode(factory.createIdentifier(utils.withAttributeNamespace('Private'))));
|
|
60
|
+
}
|
|
61
|
+
// Unique
|
|
62
|
+
if (attribute.unique) {
|
|
63
|
+
modifiers.push(factory.createTypeReferenceNode(factory.createIdentifier(utils.withAttributeNamespace('Unique'))));
|
|
64
|
+
}
|
|
65
|
+
// Configurable
|
|
66
|
+
if (attribute.configurable) {
|
|
67
|
+
modifiers.push(factory.createTypeReferenceNode(factory.createIdentifier(utils.withAttributeNamespace('Configurable'))));
|
|
68
|
+
}
|
|
69
|
+
// Custom field
|
|
70
|
+
if (attribute.customField) {
|
|
71
|
+
const customFieldUid = factory.createStringLiteral(attribute.customField);
|
|
72
|
+
const typeArguments = [
|
|
73
|
+
customFieldUid
|
|
74
|
+
];
|
|
75
|
+
if (attribute.options) {
|
|
76
|
+
typeArguments.push(utils.toTypeLiteral(attribute.options));
|
|
77
|
+
}
|
|
78
|
+
modifiers.push(factory.createTypeReferenceNode(factory.createIdentifier(utils.withAttributeNamespace('CustomField')), typeArguments));
|
|
79
|
+
}
|
|
80
|
+
// Plugin Options
|
|
81
|
+
if (!___default.default.isEmpty(attribute.pluginOptions)) {
|
|
82
|
+
modifiers.push(factory.createTypeReferenceNode(factory.createIdentifier(utils.withAttributeNamespace('SetPluginOptions')), // Transform the pluginOptions object into an object literal expression
|
|
83
|
+
[
|
|
84
|
+
utils.toTypeLiteral(attribute.pluginOptions)
|
|
85
|
+
]));
|
|
86
|
+
}
|
|
87
|
+
// Min / Max
|
|
88
|
+
if (!___default.default.isNil(attribute.min) || !___default.default.isNil(attribute.max)) {
|
|
89
|
+
const minMaxProperties = ___default.default.pick([
|
|
90
|
+
'min',
|
|
91
|
+
'max'
|
|
92
|
+
], attribute);
|
|
93
|
+
const { min, max } = minMaxProperties;
|
|
94
|
+
const typeofMin = typeof min;
|
|
95
|
+
const typeofMax = typeof max;
|
|
96
|
+
// Throws error if min/max exist but have different types to prevent unexpected behavior
|
|
97
|
+
if (min !== undefined && max !== undefined && typeofMin !== typeofMax) {
|
|
98
|
+
throw new Error('typeof min/max values mismatch');
|
|
99
|
+
}
|
|
100
|
+
let typeKeyword;
|
|
101
|
+
// use 'string'
|
|
102
|
+
if (typeofMin === 'string' || typeofMax === 'string') {
|
|
103
|
+
typeKeyword = ts__namespace.SyntaxKind.StringKeyword;
|
|
104
|
+
} else if (typeofMin === 'number' || typeofMax === 'number') {
|
|
105
|
+
typeKeyword = ts__namespace.SyntaxKind.NumberKeyword;
|
|
106
|
+
} else {
|
|
107
|
+
throw new Error(`Invalid data type for min/max options. Must be string, number or undefined, but found { min: ${min} (${typeofMin}), max: ${max} (${typeofMax}) }`);
|
|
108
|
+
}
|
|
109
|
+
modifiers.push(factory.createTypeReferenceNode(factory.createIdentifier(utils.withAttributeNamespace('SetMinMax')), [
|
|
110
|
+
utils.toTypeLiteral(minMaxProperties),
|
|
111
|
+
factory.createKeywordTypeNode(typeKeyword)
|
|
112
|
+
]));
|
|
113
|
+
}
|
|
114
|
+
// Min length / Max length
|
|
115
|
+
if (!___default.default.isNil(attribute.minLength) || !___default.default.isNil(attribute.maxLength)) {
|
|
116
|
+
const minMaxProperties = ___default.default.pick([
|
|
117
|
+
'minLength',
|
|
118
|
+
'maxLength'
|
|
119
|
+
], attribute);
|
|
120
|
+
modifiers.push(factory.createTypeReferenceNode(factory.createIdentifier(utils.withAttributeNamespace('SetMinMaxLength')), [
|
|
121
|
+
utils.toTypeLiteral(minMaxProperties)
|
|
122
|
+
]));
|
|
123
|
+
}
|
|
124
|
+
// Default (ignore if default is a function)
|
|
125
|
+
if (!___default.default.isNil(attribute.default) && !___default.default.isFunction(attribute.default)) {
|
|
126
|
+
const defaultLiteral = utils.toTypeLiteral(attribute.default);
|
|
127
|
+
modifiers.push(factory.createTypeReferenceNode(factory.createIdentifier(utils.withAttributeNamespace('DefaultTo')), [
|
|
128
|
+
defaultLiteral
|
|
129
|
+
]));
|
|
130
|
+
}
|
|
131
|
+
return modifiers;
|
|
132
|
+
};
|
|
133
|
+
/**
|
|
134
|
+
* Generate a property signature node for a given attribute
|
|
135
|
+
*/ const attributeToPropertySignature = (schema, attributeName, attribute)=>{
|
|
136
|
+
const baseType = getAttributeType(attributeName, attribute, schema.uid);
|
|
137
|
+
if (baseType === null) {
|
|
138
|
+
return null;
|
|
139
|
+
}
|
|
140
|
+
const modifiers = getAttributeModifiers(attribute);
|
|
141
|
+
const nodes = [
|
|
142
|
+
baseType,
|
|
143
|
+
...modifiers
|
|
144
|
+
];
|
|
145
|
+
return factory.createPropertySignature(undefined, factory.createIdentifier(attributeName), undefined, factory.createIntersectionTypeNode(nodes));
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
exports.mappers = mappers.mappers;
|
|
149
|
+
exports.attributeToPropertySignature = attributeToPropertySignature;
|
|
150
|
+
exports.getAttributeModifiers = getAttributeModifiers;
|
|
151
|
+
exports.getAttributeType = getAttributeType;
|
|
152
|
+
//# sourceMappingURL=attributes.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"attributes.js","sources":["../../../../src/generators/common/models/attributes.ts"],"sourcesContent":["import * as ts from 'typescript';\nimport _ from 'lodash/fp';\n\nimport { addImport } from '../imports';\nimport { getTypeNode, toTypeLiteral, withAttributeNamespace, NAMESPACES } from './utils';\nimport type { Attribute, Schema } from './utils';\nimport { mappers } from './mappers';\n\nconst { factory } = ts;\n\n/**\n * Create the base type node for a given attribute\n */\nexport const getAttributeType = (\n attributeName: string,\n attribute: Attribute,\n uid?: string\n): ts.TypeReferenceNode | null => {\n if (!Object.keys(mappers).includes(attribute.type)) {\n console.warn(\n `\"${attributeName}\" attribute from \"${uid}\" has an invalid type: \"${attribute.type}\"`\n );\n\n return null;\n }\n\n const [attributeType, typeParams] = mappers[attribute.type]({ uid, attribute, attributeName });\n\n // Make sure the schema namespace is imported\n addImport(NAMESPACES.Schema);\n\n return getTypeNode(attributeType, typeParams);\n};\n\n/**\n * Collect every modifier node from an attribute\n */\nexport const getAttributeModifiers = (attribute: Attribute): ts.TypeNode[] => {\n const modifiers: ts.TypeNode[] = [];\n\n // Required\n if (attribute.required) {\n modifiers.push(\n factory.createTypeReferenceNode(factory.createIdentifier(withAttributeNamespace('Required')))\n );\n }\n\n // Private\n if (attribute.private) {\n modifiers.push(\n factory.createTypeReferenceNode(factory.createIdentifier(withAttributeNamespace('Private')))\n );\n }\n\n // Unique\n if (attribute.unique) {\n modifiers.push(\n factory.createTypeReferenceNode(factory.createIdentifier(withAttributeNamespace('Unique')))\n );\n }\n\n // Configurable\n if (attribute.configurable) {\n modifiers.push(\n factory.createTypeReferenceNode(\n factory.createIdentifier(withAttributeNamespace('Configurable'))\n )\n );\n }\n\n // Custom field\n if (attribute.customField) {\n const customFieldUid = factory.createStringLiteral(attribute.customField);\n const typeArguments: any[] = [customFieldUid];\n\n if (attribute.options) {\n typeArguments.push(toTypeLiteral(attribute.options));\n }\n\n modifiers.push(\n factory.createTypeReferenceNode(\n factory.createIdentifier(withAttributeNamespace('CustomField')),\n typeArguments\n )\n );\n }\n\n // Plugin Options\n if (!_.isEmpty(attribute.pluginOptions)) {\n modifiers.push(\n factory.createTypeReferenceNode(\n factory.createIdentifier(withAttributeNamespace('SetPluginOptions')),\n // Transform the pluginOptions object into an object literal expression\n [toTypeLiteral(attribute.pluginOptions)]\n )\n );\n }\n\n // Min / Max\n if (!_.isNil(attribute.min) || !_.isNil(attribute.max)) {\n const minMaxProperties = _.pick(['min', 'max'], attribute);\n const { min, max } = minMaxProperties;\n\n const typeofMin = typeof min;\n const typeofMax = typeof max;\n\n // Throws error if min/max exist but have different types to prevent unexpected behavior\n if (min !== undefined && max !== undefined && typeofMin !== typeofMax) {\n throw new Error('typeof min/max values mismatch');\n }\n\n let typeKeyword: any;\n\n // use 'string'\n if (typeofMin === 'string' || typeofMax === 'string') {\n typeKeyword = ts.SyntaxKind.StringKeyword;\n }\n // use 'number'\n else if (typeofMin === 'number' || typeofMax === 'number') {\n typeKeyword = ts.SyntaxKind.NumberKeyword;\n }\n // invalid type\n else {\n throw new Error(\n `Invalid data type for min/max options. Must be string, number or undefined, but found { min: ${min} (${typeofMin}), max: ${max} (${typeofMax}) }`\n );\n }\n\n modifiers.push(\n factory.createTypeReferenceNode(\n factory.createIdentifier(withAttributeNamespace('SetMinMax')),\n [toTypeLiteral(minMaxProperties), factory.createKeywordTypeNode(typeKeyword)]\n )\n );\n }\n\n // Min length / Max length\n if (!_.isNil(attribute.minLength) || !_.isNil(attribute.maxLength)) {\n const minMaxProperties = _.pick(['minLength', 'maxLength'], attribute);\n\n modifiers.push(\n factory.createTypeReferenceNode(\n factory.createIdentifier(withAttributeNamespace('SetMinMaxLength')),\n [toTypeLiteral(minMaxProperties)]\n )\n );\n }\n\n // Default (ignore if default is a function)\n if (!_.isNil(attribute.default) && !_.isFunction(attribute.default)) {\n const defaultLiteral = toTypeLiteral(attribute.default);\n\n modifiers.push(\n factory.createTypeReferenceNode(\n factory.createIdentifier(withAttributeNamespace('DefaultTo')),\n [defaultLiteral]\n )\n );\n }\n\n return modifiers;\n};\n\n/**\n * Generate a property signature node for a given attribute\n */\nexport const attributeToPropertySignature = (\n schema: Schema,\n attributeName: string,\n attribute: Attribute\n): ts.PropertySignature | null => {\n const baseType = getAttributeType(attributeName, attribute, schema.uid);\n\n if (baseType === null) {\n return null;\n }\n\n const modifiers = getAttributeModifiers(attribute);\n\n const nodes = [baseType, ...modifiers];\n\n return factory.createPropertySignature(\n undefined,\n factory.createIdentifier(attributeName),\n undefined,\n factory.createIntersectionTypeNode(nodes)\n );\n};\n\nexport { mappers };\n"],"names":["factory","ts","getAttributeType","attributeName","attribute","uid","Object","keys","mappers","includes","type","console","warn","attributeType","typeParams","addImport","NAMESPACES","Schema","getTypeNode","getAttributeModifiers","modifiers","required","push","createTypeReferenceNode","createIdentifier","withAttributeNamespace","private","unique","configurable","customField","customFieldUid","createStringLiteral","typeArguments","options","toTypeLiteral","_","isEmpty","pluginOptions","isNil","min","max","minMaxProperties","pick","typeofMin","typeofMax","undefined","Error","typeKeyword","SyntaxKind","StringKeyword","NumberKeyword","createKeywordTypeNode","minLength","maxLength","default","isFunction","defaultLiteral","attributeToPropertySignature","schema","baseType","nodes","createPropertySignature","createIntersectionTypeNode"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAQA,MAAM,EAAEA,OAAO,EAAE,GAAGC,aAAAA;AAEpB;;AAEC,IACM,MAAMC,gBAAAA,GAAmB,CAC9BC,eACAC,SAAAA,EACAC,GAAAA,GAAAA;IAEA,IAAI,CAACC,OAAOC,IAAI,CAACC,iBAASC,QAAQ,CAACL,SAAAA,CAAUM,IAAI,CAAA,EAAG;AAClDC,QAAAA,OAAAA,CAAQC,IAAI,CACV,CAAC,CAAC,EAAET,aAAAA,CAAc,kBAAkB,EAAEE,GAAAA,CAAI,wBAAwB,EAAED,SAAAA,CAAUM,IAAI,CAAC,CAAC,CAAC,CAAA;QAGvF,OAAO,IAAA;AACT,IAAA;IAEA,MAAM,CAACG,eAAeC,UAAAA,CAAW,GAAGN,eAAO,CAACJ,SAAAA,CAAUM,IAAI,CAAC,CAAC;AAAEL,QAAAA,GAAAA;AAAKD,QAAAA,SAAAA;AAAWD,QAAAA;AAAc,KAAA,CAAA;;AAG5FY,IAAAA,iBAAAA,CAAUC,iBAAWC,MAAM,CAAA;AAE3B,IAAA,OAAOC,kBAAYL,aAAAA,EAAeC,UAAAA,CAAAA;AACpC;AAEA;;IAGO,MAAMK,qBAAAA,GAAwB,CAACf,SAAAA,GAAAA;AACpC,IAAA,MAAMgB,YAA2B,EAAE;;IAGnC,IAAIhB,SAAAA,CAAUiB,QAAQ,EAAE;QACtBD,SAAAA,CAAUE,IAAI,CACZtB,OAAAA,CAAQuB,uBAAuB,CAACvB,OAAAA,CAAQwB,gBAAgB,CAACC,4BAAAA,CAAuB,UAAA,CAAA,CAAA,CAAA,CAAA;AAEpF,IAAA;;IAGA,IAAIrB,SAAAA,CAAUsB,OAAO,EAAE;QACrBN,SAAAA,CAAUE,IAAI,CACZtB,OAAAA,CAAQuB,uBAAuB,CAACvB,OAAAA,CAAQwB,gBAAgB,CAACC,4BAAAA,CAAuB,SAAA,CAAA,CAAA,CAAA,CAAA;AAEpF,IAAA;;IAGA,IAAIrB,SAAAA,CAAUuB,MAAM,EAAE;QACpBP,SAAAA,CAAUE,IAAI,CACZtB,OAAAA,CAAQuB,uBAAuB,CAACvB,OAAAA,CAAQwB,gBAAgB,CAACC,4BAAAA,CAAuB,QAAA,CAAA,CAAA,CAAA,CAAA;AAEpF,IAAA;;IAGA,IAAIrB,SAAAA,CAAUwB,YAAY,EAAE;QAC1BR,SAAAA,CAAUE,IAAI,CACZtB,OAAAA,CAAQuB,uBAAuB,CAC7BvB,OAAAA,CAAQwB,gBAAgB,CAACC,4BAAAA,CAAuB,cAAA,CAAA,CAAA,CAAA,CAAA;AAGtD,IAAA;;IAGA,IAAIrB,SAAAA,CAAUyB,WAAW,EAAE;AACzB,QAAA,MAAMC,cAAAA,GAAiB9B,OAAAA,CAAQ+B,mBAAmB,CAAC3B,UAAUyB,WAAW,CAAA;AACxE,QAAA,MAAMG,aAAAA,GAAuB;AAACF,YAAAA;AAAe,SAAA;QAE7C,IAAI1B,SAAAA,CAAU6B,OAAO,EAAE;AACrBD,YAAAA,aAAAA,CAAcV,IAAI,CAACY,mBAAAA,CAAc9B,SAAAA,CAAU6B,OAAO,CAAA,CAAA;AACpD,QAAA;QAEAb,SAAAA,CAAUE,IAAI,CACZtB,OAAAA,CAAQuB,uBAAuB,CAC7BvB,OAAAA,CAAQwB,gBAAgB,CAACC,4BAAAA,CAAuB,aAAA,CAAA,CAAA,EAChDO,aAAAA,CAAAA,CAAAA;AAGN,IAAA;;AAGA,IAAA,IAAI,CAACG,kBAAAA,CAAEC,OAAO,CAAChC,SAAAA,CAAUiC,aAAa,CAAA,EAAG;QACvCjB,SAAAA,CAAUE,IAAI,CACZtB,OAAAA,CAAQuB,uBAAuB,CAC7BvB,QAAQwB,gBAAgB,CAACC,4BAAAA,CAAuB,kBAAA,CAAA,CAAA;AAEhD,QAAA;AAACS,YAAAA,mBAAAA,CAAc9B,UAAUiC,aAAa;AAAE,SAAA,CAAA,CAAA;AAG9C,IAAA;;AAGA,IAAA,IAAI,CAACF,kBAAAA,CAAEG,KAAK,CAAClC,SAAAA,CAAUmC,GAAG,CAAA,IAAK,CAACJ,kBAAAA,CAAEG,KAAK,CAAClC,SAAAA,CAAUoC,GAAG,CAAA,EAAG;QACtD,MAAMC,gBAAAA,GAAmBN,kBAAAA,CAAEO,IAAI,CAAC;AAAC,YAAA,KAAA;AAAO,YAAA;SAAM,EAAEtC,SAAAA,CAAAA;AAChD,QAAA,MAAM,EAAEmC,GAAG,EAAEC,GAAG,EAAE,GAAGC,gBAAAA;AAErB,QAAA,MAAME,YAAY,OAAOJ,GAAAA;AACzB,QAAA,MAAMK,YAAY,OAAOJ,GAAAA;;AAGzB,QAAA,IAAID,GAAAA,KAAQM,SAAAA,IAAaL,GAAAA,KAAQK,SAAAA,IAAaF,cAAcC,SAAAA,EAAW;AACrE,YAAA,MAAM,IAAIE,KAAAA,CAAM,gCAAA,CAAA;AAClB,QAAA;QAEA,IAAIC,WAAAA;;QAGJ,IAAIJ,SAAAA,KAAc,QAAA,IAAYC,SAAAA,KAAc,QAAA,EAAU;YACpDG,WAAAA,GAAc9C,aAAAA,CAAG+C,UAAU,CAACC,aAAa;AAC3C,QAAA,CAAA,MAEK,IAAIN,SAAAA,KAAc,QAAA,IAAYC,SAAAA,KAAc,QAAA,EAAU;YACzDG,WAAAA,GAAc9C,aAAAA,CAAG+C,UAAU,CAACE,aAAa;QAC3C,CAAA,MAEK;AACH,YAAA,MAAM,IAAIJ,KAAAA,CACR,CAAC,6FAA6F,EAAEP,IAAI,EAAE,EAAEI,SAAAA,CAAU,QAAQ,EAAEH,GAAAA,CAAI,EAAE,EAAEI,SAAAA,CAAU,GAAG,CAAC,CAAA;AAEtJ,QAAA;QAEAxB,SAAAA,CAAUE,IAAI,CACZtB,OAAAA,CAAQuB,uBAAuB,CAC7BvB,OAAAA,CAAQwB,gBAAgB,CAACC,4BAAAA,CAAuB,WAAA,CAAA,CAAA,EAChD;YAACS,mBAAAA,CAAcO,gBAAAA,CAAAA;AAAmBzC,YAAAA,OAAAA,CAAQmD,qBAAqB,CAACJ,WAAAA;AAAa,SAAA,CAAA,CAAA;AAGnF,IAAA;;AAGA,IAAA,IAAI,CAACZ,kBAAAA,CAAEG,KAAK,CAAClC,SAAAA,CAAUgD,SAAS,CAAA,IAAK,CAACjB,kBAAAA,CAAEG,KAAK,CAAClC,SAAAA,CAAUiD,SAAS,CAAA,EAAG;QAClE,MAAMZ,gBAAAA,GAAmBN,kBAAAA,CAAEO,IAAI,CAAC;AAAC,YAAA,WAAA;AAAa,YAAA;SAAY,EAAEtC,SAAAA,CAAAA;QAE5DgB,SAAAA,CAAUE,IAAI,CACZtB,OAAAA,CAAQuB,uBAAuB,CAC7BvB,OAAAA,CAAQwB,gBAAgB,CAACC,4BAAAA,CAAuB,iBAAA,CAAA,CAAA,EAChD;YAACS,mBAAAA,CAAcO,gBAAAA;AAAkB,SAAA,CAAA,CAAA;AAGvC,IAAA;;AAGA,IAAA,IAAI,CAACN,kBAAAA,CAAEG,KAAK,CAAClC,SAAAA,CAAUkD,OAAO,CAAA,IAAK,CAACnB,kBAAAA,CAAEoB,UAAU,CAACnD,SAAAA,CAAUkD,OAAO,CAAA,EAAG;QACnE,MAAME,cAAAA,GAAiBtB,mBAAAA,CAAc9B,SAAAA,CAAUkD,OAAO,CAAA;QAEtDlC,SAAAA,CAAUE,IAAI,CACZtB,OAAAA,CAAQuB,uBAAuB,CAC7BvB,OAAAA,CAAQwB,gBAAgB,CAACC,4BAAAA,CAAuB,WAAA,CAAA,CAAA,EAChD;AAAC+B,YAAAA;AAAe,SAAA,CAAA,CAAA;AAGtB,IAAA;IAEA,OAAOpC,SAAAA;AACT;AAEA;;AAEC,IACM,MAAMqC,4BAAAA,GAA+B,CAC1CC,QACAvD,aAAAA,EACAC,SAAAA,GAAAA;AAEA,IAAA,MAAMuD,QAAAA,GAAWzD,gBAAAA,CAAiBC,aAAAA,EAAeC,SAAAA,EAAWsD,OAAOrD,GAAG,CAAA;AAEtE,IAAA,IAAIsD,aAAa,IAAA,EAAM;QACrB,OAAO,IAAA;AACT,IAAA;AAEA,IAAA,MAAMvC,YAAYD,qBAAAA,CAAsBf,SAAAA,CAAAA;AAExC,IAAA,MAAMwD,KAAAA,GAAQ;AAACD,QAAAA,QAAAA;AAAavC,QAAAA,GAAAA;AAAU,KAAA;IAEtC,OAAOpB,OAAAA,CAAQ6D,uBAAuB,CACpChB,SAAAA,EACA7C,OAAAA,CAAQwB,gBAAgB,CAACrB,aAAAA,CAAAA,EACzB0C,SAAAA,EACA7C,OAAAA,CAAQ8D,0BAA0B,CAACF,KAAAA,CAAAA,CAAAA;AAEvC;;;;;;;"}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import * as ts from 'typescript';
|
|
2
|
+
import _ from 'lodash/fp';
|
|
3
|
+
import { addImport } from '../imports.mjs';
|
|
4
|
+
import { getTypeNode, withAttributeNamespace, toTypeLiteral, NAMESPACES } from './utils.mjs';
|
|
5
|
+
import { mappers } from './mappers.mjs';
|
|
6
|
+
|
|
7
|
+
const { factory } = ts;
|
|
8
|
+
/**
|
|
9
|
+
* Create the base type node for a given attribute
|
|
10
|
+
*/ const getAttributeType = (attributeName, attribute, uid)=>{
|
|
11
|
+
if (!Object.keys(mappers).includes(attribute.type)) {
|
|
12
|
+
console.warn(`"${attributeName}" attribute from "${uid}" has an invalid type: "${attribute.type}"`);
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
const [attributeType, typeParams] = mappers[attribute.type]({
|
|
16
|
+
uid,
|
|
17
|
+
attribute,
|
|
18
|
+
attributeName
|
|
19
|
+
});
|
|
20
|
+
// Make sure the schema namespace is imported
|
|
21
|
+
addImport(NAMESPACES.Schema);
|
|
22
|
+
return getTypeNode(attributeType, typeParams);
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Collect every modifier node from an attribute
|
|
26
|
+
*/ const getAttributeModifiers = (attribute)=>{
|
|
27
|
+
const modifiers = [];
|
|
28
|
+
// Required
|
|
29
|
+
if (attribute.required) {
|
|
30
|
+
modifiers.push(factory.createTypeReferenceNode(factory.createIdentifier(withAttributeNamespace('Required'))));
|
|
31
|
+
}
|
|
32
|
+
// Private
|
|
33
|
+
if (attribute.private) {
|
|
34
|
+
modifiers.push(factory.createTypeReferenceNode(factory.createIdentifier(withAttributeNamespace('Private'))));
|
|
35
|
+
}
|
|
36
|
+
// Unique
|
|
37
|
+
if (attribute.unique) {
|
|
38
|
+
modifiers.push(factory.createTypeReferenceNode(factory.createIdentifier(withAttributeNamespace('Unique'))));
|
|
39
|
+
}
|
|
40
|
+
// Configurable
|
|
41
|
+
if (attribute.configurable) {
|
|
42
|
+
modifiers.push(factory.createTypeReferenceNode(factory.createIdentifier(withAttributeNamespace('Configurable'))));
|
|
43
|
+
}
|
|
44
|
+
// Custom field
|
|
45
|
+
if (attribute.customField) {
|
|
46
|
+
const customFieldUid = factory.createStringLiteral(attribute.customField);
|
|
47
|
+
const typeArguments = [
|
|
48
|
+
customFieldUid
|
|
49
|
+
];
|
|
50
|
+
if (attribute.options) {
|
|
51
|
+
typeArguments.push(toTypeLiteral(attribute.options));
|
|
52
|
+
}
|
|
53
|
+
modifiers.push(factory.createTypeReferenceNode(factory.createIdentifier(withAttributeNamespace('CustomField')), typeArguments));
|
|
54
|
+
}
|
|
55
|
+
// Plugin Options
|
|
56
|
+
if (!_.isEmpty(attribute.pluginOptions)) {
|
|
57
|
+
modifiers.push(factory.createTypeReferenceNode(factory.createIdentifier(withAttributeNamespace('SetPluginOptions')), // Transform the pluginOptions object into an object literal expression
|
|
58
|
+
[
|
|
59
|
+
toTypeLiteral(attribute.pluginOptions)
|
|
60
|
+
]));
|
|
61
|
+
}
|
|
62
|
+
// Min / Max
|
|
63
|
+
if (!_.isNil(attribute.min) || !_.isNil(attribute.max)) {
|
|
64
|
+
const minMaxProperties = _.pick([
|
|
65
|
+
'min',
|
|
66
|
+
'max'
|
|
67
|
+
], attribute);
|
|
68
|
+
const { min, max } = minMaxProperties;
|
|
69
|
+
const typeofMin = typeof min;
|
|
70
|
+
const typeofMax = typeof max;
|
|
71
|
+
// Throws error if min/max exist but have different types to prevent unexpected behavior
|
|
72
|
+
if (min !== undefined && max !== undefined && typeofMin !== typeofMax) {
|
|
73
|
+
throw new Error('typeof min/max values mismatch');
|
|
74
|
+
}
|
|
75
|
+
let typeKeyword;
|
|
76
|
+
// use 'string'
|
|
77
|
+
if (typeofMin === 'string' || typeofMax === 'string') {
|
|
78
|
+
typeKeyword = ts.SyntaxKind.StringKeyword;
|
|
79
|
+
} else if (typeofMin === 'number' || typeofMax === 'number') {
|
|
80
|
+
typeKeyword = ts.SyntaxKind.NumberKeyword;
|
|
81
|
+
} else {
|
|
82
|
+
throw new Error(`Invalid data type for min/max options. Must be string, number or undefined, but found { min: ${min} (${typeofMin}), max: ${max} (${typeofMax}) }`);
|
|
83
|
+
}
|
|
84
|
+
modifiers.push(factory.createTypeReferenceNode(factory.createIdentifier(withAttributeNamespace('SetMinMax')), [
|
|
85
|
+
toTypeLiteral(minMaxProperties),
|
|
86
|
+
factory.createKeywordTypeNode(typeKeyword)
|
|
87
|
+
]));
|
|
88
|
+
}
|
|
89
|
+
// Min length / Max length
|
|
90
|
+
if (!_.isNil(attribute.minLength) || !_.isNil(attribute.maxLength)) {
|
|
91
|
+
const minMaxProperties = _.pick([
|
|
92
|
+
'minLength',
|
|
93
|
+
'maxLength'
|
|
94
|
+
], attribute);
|
|
95
|
+
modifiers.push(factory.createTypeReferenceNode(factory.createIdentifier(withAttributeNamespace('SetMinMaxLength')), [
|
|
96
|
+
toTypeLiteral(minMaxProperties)
|
|
97
|
+
]));
|
|
98
|
+
}
|
|
99
|
+
// Default (ignore if default is a function)
|
|
100
|
+
if (!_.isNil(attribute.default) && !_.isFunction(attribute.default)) {
|
|
101
|
+
const defaultLiteral = toTypeLiteral(attribute.default);
|
|
102
|
+
modifiers.push(factory.createTypeReferenceNode(factory.createIdentifier(withAttributeNamespace('DefaultTo')), [
|
|
103
|
+
defaultLiteral
|
|
104
|
+
]));
|
|
105
|
+
}
|
|
106
|
+
return modifiers;
|
|
107
|
+
};
|
|
108
|
+
/**
|
|
109
|
+
* Generate a property signature node for a given attribute
|
|
110
|
+
*/ const attributeToPropertySignature = (schema, attributeName, attribute)=>{
|
|
111
|
+
const baseType = getAttributeType(attributeName, attribute, schema.uid);
|
|
112
|
+
if (baseType === null) {
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
const modifiers = getAttributeModifiers(attribute);
|
|
116
|
+
const nodes = [
|
|
117
|
+
baseType,
|
|
118
|
+
...modifiers
|
|
119
|
+
];
|
|
120
|
+
return factory.createPropertySignature(undefined, factory.createIdentifier(attributeName), undefined, factory.createIntersectionTypeNode(nodes));
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
export { attributeToPropertySignature, getAttributeModifiers, getAttributeType, mappers };
|
|
124
|
+
//# sourceMappingURL=attributes.mjs.map
|