From 5b9439d19ff46863d63d51d116ac616fc32a23d9 Mon Sep 17 00:00:00 2001 From: Tomasz Awramski Date: Tue, 23 May 2023 14:45:38 +0200 Subject: [PATCH] feat: add ci action to check for unsorter package.json files - Remove not working check-package-json-sort.ts script. - Add ci action to check for unsorted package.json files. Closes: #2356 Signed-off-by: Tomasz Awramski --- .../workflows/sort-package-json-check.yaml | 26 +++++++ tools/ci-package-json-check.sh | 20 +++++ .../custom-checks/check-package-json-sort.ts | 74 ------------------- 3 files changed, 46 insertions(+), 74 deletions(-) create mode 100644 .github/workflows/sort-package-json-check.yaml create mode 100644 tools/ci-package-json-check.sh delete mode 100644 tools/custom-checks/check-package-json-sort.ts diff --git a/.github/workflows/sort-package-json-check.yaml b/.github/workflows/sort-package-json-check.yaml new file mode 100644 index 00000000000..f9a8d25dcd7 --- /dev/null +++ b/.github/workflows/sort-package-json-check.yaml @@ -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 diff --git a/tools/ci-package-json-check.sh b/tools/ci-package-json-check.sh new file mode 100644 index 00000000000..57fdb5606dd --- /dev/null +++ b/tools/ci-package-json-check.sh @@ -0,0 +1,20 @@ +country=$(sort-package-json "**/cactus-*/package.json" --check ) + + +echo "${country}" + +if [[ "${country}" =~ 'not sorted' ]] +then +echo 1 'not sorted. +Run +$ find -wholename "**/cactus-*/package.json" -type f -not -path "**/node_modules/*" -exec sort-package-json {} +; +in cactus root.' + exit 1 +elif [[ "${country}" =~ 'already sorted' ]] +then +echo 0 'sorted' + exit 0 +else +echo 1 'some error occured' "${country}" + exit 1 +fi \ No newline at end of file diff --git a/tools/custom-checks/check-package-json-sort.ts b/tools/custom-checks/check-package-json-sort.ts deleted file mode 100644 index 1f2c35b6f02..00000000000 --- a/tools/custom-checks/check-package-json-sort.ts +++ /dev/null @@ -1,74 +0,0 @@ -import fs from "fs-extra"; -import path from "path"; -import { fileURLToPath } from "url"; -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"; - -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 - * - * @returns An array with the first item being a boolean indicating - * 1) success (`true`) or 2) failure (`false`) - */ -export async function checkPackageJsonSort( - req: ICheckPackageJsonSort, -): Promise<[boolean, string[]]> { - 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}`); - - if (!req) { - throw new RuntimeError(`req parameter cannot be falsy.`); - } - if (!req.argv) { - throw new RuntimeError(`req.argv cannot be falsy.`); - } - if (!req.env) { - throw new RuntimeError(`req.env cannot be falsy.`); - } - - const globbyOpts: GlobbyOptions = { - cwd: PROJECT_DIR, - ignore: ["**/node_modules"], - }; - - const DEFAULT_GLOB = "**/cactus-*/package.json"; - - const pkgJsonPaths = await globby(DEFAULT_GLOB, globbyOpts); - sortPackageJson(pkgJsonPaths); - - const errors: string[] = []; - - const checks = pkgJsonPaths.map(async (pathRel) => { - const filePathAbs = path.join(PROJECT_DIR, pathRel); - const pkgJson: unknown = await fs.readJSON(filePathAbs); - if (typeof pkgJson !== "object") { - errors.push(`ERROR: ${pathRel} package.json cannot be empty.`); - return; - } - if (!pkgJson) { - errors.push(`ERROR: ${pathRel} package.json cannot be empty.`); - return; - } - if (!isStdLibRecord(pkgJson)) { - return; - } - }); - - await Promise.all(checks); - - return [errors.length === 0, errors]; -}