Skip to content

Commit

Permalink
fix: 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.

Closes: hyperledger-cacti#2356
Signed-off-by: Tomasz Awramski <tomasz.awramski@fujitsu.com
  • Loading branch information
rwat17 committed Aug 22, 2023
1 parent 08afe1a commit b3fa9d5
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 3 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/sort-package-json-check.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: sort-package-json-check
on:
push:
# Publish `main` as Docker `latest` image.
branches:
- main

# Publish `v1.2.3` tags as releases.
tags:
- v*

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true


jobs:
sort-package-json-check:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: "14"
- run: npm install --global sort-package-json
- run: ./tools/ci-package-json-check.sh
24 changes: 21 additions & 3 deletions tools/custom-checks/check-package-json-sort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ 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";

// eslint-disable-next-line prettier/prettier
import lernaCfg from "../../lerna.json" assert { type: "json" };
export interface ICheckPackageJsonSort {
readonly argv: string[];
readonly env: NodeJS.ProcessEnv;
Expand Down Expand Up @@ -47,9 +48,23 @@ export async function checkPackageJsonSort(

const DEFAULT_GLOB = "**/cactus-*/package.json";

const pkgJsonPaths = await globby(DEFAULT_GLOB, globbyOpts);
sortPackageJson(pkgJsonPaths);
const includeGlobs = lernaCfg.packages.map((x) => x.concat("/package.json"));

const pkgJsonPaths = await globby(includeGlobs, globbyOpts);

const notSortedFilesPaths: string[] = [];

for (const pckg of pkgJsonPaths) {
const json = await fs.readJSON(pckg);
const sortredJson = await sortPackageJson(json);

if (JSON.stringify(json) !== JSON.stringify(sortredJson)) {
notSortedFilesPaths.push(pckg);
}

// fs.writeFile(pckg, JSON.stringify(sortredJson, null, 2));
}
console.log("notSortedFiles", notSortedFilesPaths);
const errors: string[] = [];

const checks = pkgJsonPaths.map(async (pathRel) => {
Expand All @@ -66,6 +81,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
48 changes: 48 additions & 0 deletions tools/sort-package-json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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";
// eslint-disable-next-line prettier/prettier
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"],
};

const DEFAULT_GLOB = "**/cactus-*/package.json";


export async function sortPackages(
): Promise<any> {
const TAG = "[tools/check-package-json-sort.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 pckg of pkgJsonPaths) {
const json = await fs.readJSON(pckg);
const sortredJson = await sortPackageJson(json);

fs.writeFile(pckg, JSON.stringify(sortredJson, null, 2));

}
}

sortPackages()

0 comments on commit b3fa9d5

Please sign in to comment.