Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pull validate command into @internals/scripts #1240

Merged
merged 1 commit into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions internals/scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
84 changes: 84 additions & 0 deletions internals/scripts/src/bin/validate/index.ts
Original file line number Diff line number Diff line change
@@ -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<JSONSchema> => {
return keys.reduce((obj, jsonKey) => {
if (json[jsonKey]) {
return {
...obj,
[jsonKey]: json[jsonKey],
}
};
return obj;
}, {});
};
const getObjAsArray = (obj: Partial<JSONSchema>): 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<string[]> => {
const packageJSON = await getPackageJSON(packagePath);
return getObjAsArray(getKeysOfObj(packageJSON, [
'exports',
'main',
'module',
'types',
'umd:main',
]));
};

/****
* Main function
*/

const validateBuild = async (packageName: string): Promise<Set<string>> => {
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();
1 change: 0 additions & 1 deletion internals/upscaler-cli/src/lib/cli/CLI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
// },


Expand Down
1 change: 0 additions & 1 deletion internals/upscaler-cli/src/lib/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
// },


Expand Down
2 changes: 1 addition & 1 deletion models/default-model/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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\");'",
Expand Down
2 changes: 1 addition & 1 deletion models/esrgan-legacy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion models/esrgan-medium/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion models/esrgan-slim/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion models/esrgan-thick/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion models/pixel-upsampler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 0 additions & 2 deletions scripts/package-scripts/validate-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading