Skip to content

Commit

Permalink
ci(custom-checks): fix broken package.json sort verification
Browse files Browse the repository at this point in the history
- Remove not working check-package-json-sort.ts script.
- Add ci action to check for unsorted package.json files.

Peter's changes:
1. Changed the commit message in a way that it won't show up in the
release notes among the bug fixes (the section that is meant for production
bug-fixes not tooling bug-fixes)
2. Fixed typos in the .ts scripts
3. Applied the prettier/eslint formatting, deleted unused variables
4. Added a convenience script to the root package.json that runs the
package.json sorter
5. Fixed the sorter script so that it appends a newline character after
the sorted JSON string when writing back to the file. This is needed
because when you run `yarn install` or `npm install` they both like to
close the package.json file's with an empty line last so we have to mimic
that in order to avoid spamming all future PRs with these line changes.
One problem that this might still have is that on different operating
systems the newline character might be different and it also depends on
the git configuration in effect (you can configure Windows' git to use
Unix line endings for example). So it's not trivial how to fix it but
we'll cross that bridge when we get to it.

Closes: #2356

Co-authored-by: Peter Somogyvari <peter.somogyvari@accenture.com>

Signed-off-by: Tomasz Awramski <tomasz.awramski@fujitsu.com
Signed-off-by: Peter Somogyvari <peter.somogyvari@accenture.com>
  • Loading branch information
rwat17 authored and petermetz committed Aug 25, 2023
1 parent 127896a commit 71a3b1c
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 7 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
29 changes: 22 additions & 7 deletions tools/custom-checks/check-package-json-sort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down Expand Up @@ -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) => {
Expand All @@ -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);
Expand Down
43 changes: 43 additions & 0 deletions tools/sort-package-json.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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();

0 comments on commit 71a3b1c

Please sign in to comment.