diff --git a/.github/workflows/sort-package-json-check.yaml b/.github/workflows/sort-package-json-check.yaml new file mode 100644 index 0000000000..f9a8d25dcd --- /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 0000000000..57fdb5606d --- /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 1f2c35b6f0..0000000000 --- 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]; -}