diff --git a/internals/scripts/package.json b/internals/scripts/package.json index 33004634c..acdc393f7 100644 --- a/internals/scripts/package.json +++ b/internals/scripts/package.json @@ -19,12 +19,19 @@ "write:docs:models": "wireit", "write:docs:api": "wireit", "start:guide": "wireit", + "validate": "wireit", "update:npm-dependencies": "wireit", "build": "wireit", "test:run": "wireit", "test": "wireit" }, "wireit": { + "validate": { + "command": "node ./dist/bin/validate/index.js", + "dependencies": [ + "build" + ] + }, "write:docs:check-tense": { "command": "node ./dist/bin/write/docs/check-tense.js", "dependencies": [ diff --git a/internals/scripts/src/bin/validate/index.ts b/internals/scripts/src/bin/validate/index.ts new file mode 100644 index 000000000..f664661d4 --- /dev/null +++ b/internals/scripts/src/bin/validate/index.ts @@ -0,0 +1,84 @@ +import path from 'path'; +import fs from 'fs'; +import { getPackageJSON, JSONSchema } from '@internals/common/package-json'; +import { sync } from 'glob'; +import { ROOT_DIR } from '@internals/common/constants'; +import { output } from '@internals/common/logger'; +import { parseArgs } from 'node:util'; + +/**** + * Utility methods + */ +const getKeysOfObj = (json: JSONSchema, keys: string[]): Partial => { + return keys.reduce((obj, jsonKey) => { + if (json[jsonKey]) { + return { + ...obj, + [jsonKey]: json[jsonKey], + } + }; + return obj; + }, {}); +}; +const getObjAsArray = (obj: Partial): string[] => { + return Object.values(obj).reduce((arr, file) => { + if (typeof file === 'string') { + return arr.concat(file); + } + return arr.concat(getObjAsArray(file)); + }, [] as string[]); +}; + +export const extractAllFilesFromPackageJSON = async (packagePath: string): Promise => { + const packageJSON = await getPackageJSON(packagePath); + return getObjAsArray(getKeysOfObj(packageJSON, [ + 'exports', + 'main', + 'module', + 'types', + 'umd:main', + ])); +}; + +/**** + * Main function + */ + +const validateBuild = async (packageName: string): Promise> => { + const packagePath = path.resolve(ROOT_DIR, packageName); + const files = new Set([ + ...(await extractAllFilesFromPackageJSON(packagePath)), + ].map(file => path.resolve(packagePath, file))); + const packageDistPath = path.resolve(packagePath, 'dist'); + files.forEach(file => { + if (!fs.existsSync(path.resolve(packageDistPath, file))) { + const existingFiles: string[] = sync(path.resolve(packageDistPath, '**/*')); + throw new Error([ + `File ${file} was not built or does not exist.`, + existingFiles.length === 0 ? 'No existing files were found' : `Existing files include: \n${existingFiles.map(f => ` - ${f}`).join('\n')}`, + `Files we are checking include: \n${Array.from(files).map(f => ` - ${f}`).join('\n')}`, + ].join('\n')); + } + }); + return files; +}; + +const main = async () => { + const { + positionals: [ + src, + ] + } = parseArgs({ + allowPositionals: true, + }); + + const checkedFiles = Array.from(await validateBuild(src)); + output([ + 'The following files are present: ', + ...checkedFiles.map(file => { + return ` - ${file}`; + }), + ].join('\n')) +}; + +main(); diff --git a/internals/upscaler-cli/src/lib/cli/CLI.ts b/internals/upscaler-cli/src/lib/cli/CLI.ts index 798599569..50bb52aee 100644 --- a/internals/upscaler-cli/src/lib/cli/CLI.ts +++ b/internals/upscaler-cli/src/lib/cli/CLI.ts @@ -30,7 +30,6 @@ const { readFileSync, } = fsExtra; // "update:tfjs": "pnpm __run_command ./package-scripts/update-tfjs.ts", // "update:dependency": "pnpm __run_command ./package-scripts/update-dependency.ts", // "update:npm:dependencies": "pnpm __run_command ./package-scripts/update-npm-dependencies.ts", - // "validate:build": "pnpm __run_command ./package-scripts/validate-build.ts" // }, diff --git a/internals/upscaler-cli/src/lib/cli/index.ts b/internals/upscaler-cli/src/lib/cli/index.ts index fa97eb8de..9074ba6b4 100644 --- a/internals/upscaler-cli/src/lib/cli/index.ts +++ b/internals/upscaler-cli/src/lib/cli/index.ts @@ -29,7 +29,6 @@ const { readFileSync, } = fsExtra; // "update:tfjs": "pnpm __run_command ./package-scripts/update-tfjs.ts", // "update:dependency": "pnpm __run_command ./package-scripts/update-dependency.ts", // "update:npm:dependencies": "pnpm __run_command ./package-scripts/update-npm-dependencies.ts", - // "validate:build": "pnpm __run_command ./package-scripts/validate-build.ts" // }, diff --git a/models/default-model/package.json b/models/default-model/package.json index 7ce02f4c4..2ac52df0e 100644 --- a/models/default-model/package.json +++ b/models/default-model/package.json @@ -69,7 +69,7 @@ "command": "pnpm lint && pnpm build && pnpm validate:build" }, "validate:build": { - "command": "ts-node ../../scripts/package-scripts/validate-build.ts models/default-model" + "command": "pnpm --filter @internals/scripts validate models/default-model" }, "scaffold": { "command": "node -e 'const fs = require(\"fs\"); const {name, version} = require(\"./package.json\"); fs.writeFileSync(\"./src/constants.generated.ts\", `export const NAME = \"${name}\";\\nexport const VERSION=\"${version}\"`, \"utf-8\");'", diff --git a/models/esrgan-legacy/package.json b/models/esrgan-legacy/package.json index 118c6dc6f..390fd8f8a 100755 --- a/models/esrgan-legacy/package.json +++ b/models/esrgan-legacy/package.json @@ -111,7 +111,7 @@ "command": "pnpm lint && pnpm build && pnpm validate:build" }, "validate:build": { - "command": "ts-node ../../scripts/package-scripts/validate-build.ts models/esrgan-legacy" + "command": "pnpm --filter @internals/scripts validate models/esrgan-legacy" }, "build": { "command": "pnpm build:cjs && pnpm build:esm && pnpm build:umd", diff --git a/models/esrgan-medium/package.json b/models/esrgan-medium/package.json index bb42a2cb5..4cbd6d9b2 100644 --- a/models/esrgan-medium/package.json +++ b/models/esrgan-medium/package.json @@ -114,7 +114,7 @@ "command": "pnpm lint && pnpm build && pnpm validate:build" }, "validate:build": { - "command": "ts-node ../../scripts/package-scripts/validate-build.ts models/esrgan-medium" + "command": "pnpm --filter @internals/scripts validate models/esrgan-medium" }, "build": { "command": "pnpm build:cjs && pnpm build:esm && pnpm build:umd", diff --git a/models/esrgan-slim/package.json b/models/esrgan-slim/package.json index 70bdf7243..da17f2718 100644 --- a/models/esrgan-slim/package.json +++ b/models/esrgan-slim/package.json @@ -114,7 +114,7 @@ "command": "pnpm lint && pnpm build && pnpm validate:build" }, "validate:build": { - "command": "ts-node ../../scripts/package-scripts/validate-build.ts models/esrgan-slim" + "command": "pnpm --filter @internals/scripts validate models/esrgan-slim" }, "build": { "command": "pnpm build:cjs && pnpm build:esm && pnpm build:umd", diff --git a/models/esrgan-thick/package.json b/models/esrgan-thick/package.json index b209f39a1..f8441cead 100644 --- a/models/esrgan-thick/package.json +++ b/models/esrgan-thick/package.json @@ -107,7 +107,7 @@ "command": "pnpm lint && pnpm build && pnpm validate:build" }, "validate:build": { - "command": "ts-node ../../scripts/package-scripts/validate-build.ts models/esrgan-thick" + "command": "pnpm --filter @internals/scripts validate models/esrgan-thick" }, "build": { "command": "pnpm build:cjs && pnpm build:esm && pnpm build:umd", diff --git a/models/pixel-upsampler/package.json b/models/pixel-upsampler/package.json index ef0be1aff..ca9121a6d 100644 --- a/models/pixel-upsampler/package.json +++ b/models/pixel-upsampler/package.json @@ -77,7 +77,7 @@ "command": "pnpm lint && pnpm build && pnpm validate:build" }, "validate:build": { - "command": "ts-node ../../scripts/package-scripts/validate-build.ts models/pixel-upsampler" + "command": "pnpm --filter @internals/scripts validate models/pixel-upsampler" }, "build": { "command": "pnpm build:cjs && pnpm build:esm && pnpm build:umd", diff --git a/scripts/package-scripts/validate-build.ts b/scripts/package-scripts/validate-build.ts index 462c50ed7..e5c7004d0 100644 --- a/scripts/package-scripts/validate-build.ts +++ b/scripts/package-scripts/validate-build.ts @@ -5,8 +5,6 @@ import { getPackageJSON, JSONSchema } from './utils/packages'; import { sync } from 'glob'; const ROOT = path.resolve(__dirname, '../..'); -const UPSCALER_JS = path.resolve(ROOT, 'packages/upscalerjs'); -const UPSCALER_JS_DIST = path.resolve(UPSCALER_JS, 'dist'); /**** * Utility methods