diff --git a/package.json b/package.json index dcd442ebb2..68ad6a6a69 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,7 @@ "tools:get-latest-sem-ver-git-tag": "TS_NODE_PROJECT=./tools/tsconfig.json node --abort-on-uncaught-exception --loader ts-node/esm --experimental-specifier-resolution=node --no-warnings ./tools/get-latest-sem-ver-git-tag.ts", "tools:generate-sbom": "TS_NODE_PROJECT=tools/tsconfig.json node --experimental-json-modules --trace-deprecation --experimental-modules --abort-on-uncaught-exception --loader ts-node/esm --experimental-specifier-resolution=node ./tools/generate-sbom.ts", "tools:fix-pkg-npm-scope": "TS_NODE_PROJECT=tools/tsconfig.json node --experimental-json-modules --trace-deprecation --experimental-modules --abort-on-uncaught-exception --loader ts-node/esm --experimental-specifier-resolution=node ./tools/custom-checks/check-pkg-npm-scope.ts", + "tools:sort-package-json": "TS_NODE_PROJECT=tools/tsconfig.json node --experimental-json-modules --trace-deprecation --experimental-modules --abort-on-uncaught-exception --loader ts-node/esm --experimental-specifier-resolution=node ./tools/sort-package-json.ts", "generate-api-server-config": "node ./tools/generate-api-server-config.js", "sync-ts-config": "TS_NODE_PROJECT=tools/tsconfig.json node --experimental-json-modules --loader ts-node/esm ./tools/sync-npm-deps-to-tsc-projects.ts", "start:api-server": "node ./packages/cactus-cmd-api-server/dist/lib/main/typescript/cmd/cactus-api.js --config-file=.config.json", diff --git a/tools/custom-checks/check-package-json-sort.ts b/tools/custom-checks/check-package-json-sort.ts index 1f2c35b6f0..79d6eac5fe 100644 --- a/tools/custom-checks/check-package-json-sort.ts +++ b/tools/custom-checks/check-package-json-sort.ts @@ -5,16 +5,18 @@ import { globby, Options as GlobbyOptions } from "globby"; import { RuntimeError } from "run-time-error"; import { isStdLibRecord } from "./is-std-lib-record"; import { sortPackageJson } from "sort-package-json"; - +import lernaCfg from "../../lerna.json" assert { type: "json" }; export interface ICheckPackageJsonSort { readonly argv: string[]; readonly env: NodeJS.ProcessEnv; } /** - * Sorts and checks that all the package.json files within the Cactus project - * Note: this only sorts and checks the package.json files that are within the - * `packages` folder for this proeject + * Sorts and checks that all the package.json files within the Cacti project + * + * Note: this sorts every package.json file that are included in the workspace + * globs (see lerna.json and package.json files in the **root** directory of the + * project) * * @returns An array with the first item being a boolean indicating * 1) success (`true`) or 2) failure (`false`) @@ -45,11 +47,21 @@ export async function checkPackageJsonSort( ignore: ["**/node_modules"], }; - const DEFAULT_GLOB = "**/cactus-*/package.json"; + const includeGlobs = lernaCfg.packages.map((x) => x.concat("/package.json")); + + const pkgJsonPaths = await globby(includeGlobs, globbyOpts); + + const notSortedFilesPaths: string[] = []; - const pkgJsonPaths = await globby(DEFAULT_GLOB, globbyOpts); - sortPackageJson(pkgJsonPaths); + for (const pkg of pkgJsonPaths) { + const json = await fs.readJSON(pkg); + const sortedJson = await sortPackageJson(json); + if (JSON.stringify(json) !== JSON.stringify(sortedJson)) { + notSortedFilesPaths.push(pkg); + } + } + console.log("%s notSortedFiles=%o", TAG, notSortedFilesPaths); const errors: string[] = []; const checks = pkgJsonPaths.map(async (pathRel) => { @@ -66,6 +78,9 @@ export async function checkPackageJsonSort( if (!isStdLibRecord(pkgJson)) { return; } + if (notSortedFilesPaths.includes(pathRel)) { + errors.push(`ERROR: Packages ${pathRel} is not sorted`); + } }); await Promise.all(checks); diff --git a/tools/sort-package-json.ts b/tools/sort-package-json.ts new file mode 100755 index 0000000000..95b449d27e --- /dev/null +++ b/tools/sort-package-json.ts @@ -0,0 +1,43 @@ +import fs from "fs-extra"; +import path from "path"; +import { fileURLToPath } from "url"; +import { globby, Options as GlobbyOptions } from "globby"; +import { sortPackageJson } from "sort-package-json"; +import lernaCfg from "../lerna.json" assert { type: "json" }; + +export interface ICheckPackageJsonSort { + readonly argv: string[]; + readonly env: NodeJS.ProcessEnv; +} + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); +const SCRIPT_DIR = __dirname; +const PROJECT_DIR = path.join(SCRIPT_DIR, "../"); +const globbyOpts: GlobbyOptions = { + cwd: PROJECT_DIR, + ignore: ["**/node_modules"], +}; + +export async function sortPackages(): Promise { + const TAG = "[tools/sort-package-json.ts]"; + const __filename = fileURLToPath(import.meta.url); + const __dirname = path.dirname(__filename); + const SCRIPT_DIR = __dirname; + const PROJECT_DIR = path.join(SCRIPT_DIR, "../../"); + console.log(`${TAG} SCRIPT_DIR=${SCRIPT_DIR}`); + console.log(`${TAG} PROJECT_DIR=${PROJECT_DIR}`); + + const includeGlobs = lernaCfg.packages.map((x) => x.concat("/package.json")); + + const pkgJsonPaths = await globby(includeGlobs, globbyOpts); + for (const pkg of pkgJsonPaths) { + const json = await fs.readJSON(pkg); + const sortedJson = await sortPackageJson(json); + + const sortedJsonString = JSON.stringify(sortedJson, null, 2).concat("\n"); + fs.writeFile(pkg, sortedJsonString); + } +} + +sortPackages();