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/custom-checks/check-package-json-sort.ts b/tools/custom-checks/check-package-json-sort.ts index 1f2c35b6f02..d27d5e39b71 100644 --- a/tools/custom-checks/check-package-json-sort.ts +++ b/tools/custom-checks/check-package-json-sort.ts @@ -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; @@ -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) => { @@ -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); diff --git a/tools/sort-package-json.ts b/tools/sort-package-json.ts new file mode 100755 index 00000000000..8eb02ecf389 --- /dev/null +++ b/tools/sort-package-json.ts @@ -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 { + 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() \ No newline at end of file