|
| 1 | +const { build } = require("@ncpa0cpl/nodepack"); |
| 2 | +const { toJsonSchema, toTsType, getMetadata } = require("dilswer"); |
| 3 | +const path = require("path"); |
| 4 | +const fs = require("fs/promises"); |
| 5 | + |
| 6 | +const p = (loc) => path.resolve(__dirname, "..", loc); |
| 7 | + |
| 8 | +async function main() { |
| 9 | + try { |
| 10 | + await Promise.all([ |
| 11 | + // Build main package |
| 12 | + await build({ |
| 13 | + target: "es2020", |
| 14 | + srcDir: p("src"), |
| 15 | + outDir: p("dist"), |
| 16 | + tsConfig: p("tsconfig.json"), |
| 17 | + formats: ["cjs", "esm", "legacy"], |
| 18 | + declarations: true, |
| 19 | + exclude: [/\/polyfills\//], |
| 20 | + isomorphicImports: { |
| 21 | + "./config/eval-js-config/eval-js-config.ts": { |
| 22 | + js: "./config/eval-js-config/eval-js-config.cjs.ts", |
| 23 | + cjs: "./config/eval-js-config/eval-js-config.cjs.ts", |
| 24 | + mjs: "./config/eval-js-config/eval-js-config.esm.ts", |
| 25 | + }, |
| 26 | + "./get-dirpath/get-dirpath.ts": { |
| 27 | + js: "./get-dirpath/get-dirpath.cjs.ts", |
| 28 | + cjs: "./get-dirpath/get-dirpath.cjs.ts", |
| 29 | + mjs: "./get-dirpath/get-dirpath.esm.ts", |
| 30 | + }, |
| 31 | + }, |
| 32 | + }), |
| 33 | + // Build polyfill packages |
| 34 | + await build({ |
| 35 | + target: "ESNext", |
| 36 | + srcDir: p("src/polyfills"), |
| 37 | + outDir: p("polyfills"), |
| 38 | + tsConfig: p("tsconfig.json"), |
| 39 | + formats: ["esm"], |
| 40 | + exclude: [/\.d\.ts$/, /index.ts/, /\.json$/], |
| 41 | + }), |
| 42 | + ]); |
| 43 | + |
| 44 | + const { ConfigSchema } = require(p("dist/cjs/config/config-schema.cjs")); |
| 45 | + |
| 46 | + const configJsonSchema = toJsonSchema(ConfigSchema, { |
| 47 | + additionalProperties: false, |
| 48 | + incompatibleTypes: "omit", |
| 49 | + }); |
| 50 | + |
| 51 | + await fs.writeFile( |
| 52 | + p("dist/config-schema.json"), |
| 53 | + JSON.stringify(configJsonSchema, null, 2), |
| 54 | + ); |
| 55 | + |
| 56 | + const configTsType = toTsType(ConfigSchema, { |
| 57 | + mode: "named-expanded", |
| 58 | + onDuplicateName: "rename", |
| 59 | + declaration: true, |
| 60 | + getExternalTypeImport: (type) => { |
| 61 | + const metadata = getMetadata(type); |
| 62 | + if (metadata.extra && metadata.extra.extraType === "external-import") { |
| 63 | + return metadata.extra; |
| 64 | + } |
| 65 | + if (metadata.extra && metadata.extra.typeDef) { |
| 66 | + return { |
| 67 | + typeName: metadata.extra.typeDef, |
| 68 | + }; |
| 69 | + } |
| 70 | + }, |
| 71 | + }); |
| 72 | + |
| 73 | + await fs.writeFile(p("dist/types/config/config-type.d.ts"), configTsType); |
| 74 | + } catch (e) { |
| 75 | + console.error(e); |
| 76 | + process.exit(1); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +main(); |
0 commit comments