Skip to content

Commit

Permalink
refactor: changed from CLI command to using API library
Browse files Browse the repository at this point in the history
Signed-off-by: Youngone Lee <youngone.lee@accenture.com>
  • Loading branch information
Leeyoungone committed Feb 1, 2022
1 parent 3ec1714 commit 46fbfc9
Show file tree
Hide file tree
Showing 6 changed files with 3,427 additions and 3,100 deletions.
12 changes: 1 addition & 11 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -236,14 +236,4 @@ jobs:
run: DOCKER_BUILDKIT=1 docker build ./whitepaper/ -f ./whitepaper/Dockerfile

- name: Print Disk Usage Reports
run: df ; echo "" ; docker system df

check-package-json:
name: check-package-json
runs-on: ubuntu-20.04
steps:
- name: Checkout code
uses: actions/checkout@v2.3.4

- name: Check sorted package json
run: yarn run sort-json-check
run: df ; echo "" ; docker system df
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
]
},
"scripts": {
"sort-json": "npx sort-package-json 'packages/*/package.json' 'package.json' 'examples/cactus-*/package.json' 'extensions/*/package.json' && yarn run sort-json-check",
"sort-json-check": "npx sort-package-json 'packages/*/package.json' 'package.json' 'examples/cactus-*/package.json' 'extensions/*/package.json' --check",
"run-ci": "./tools/ci.sh",
"reset:node-modules": "del-cli '**/node_modules'",
"reset:git": "git clean -f -X",
Expand Down Expand Up @@ -107,8 +105,8 @@
"git-cz": "4.7.6",
"globby": "12.0.0",
"google-protobuf": "3.18.0-rc.2",
"grpc-tools": "1.11.2",
"grpc_tools_node_protoc_ts": "5.3.1",
"grpc-tools": "1.11.2",
"husky": "7.0.1",
"inquirer": "8.1.2",
"jest": "27.0.6",
Expand All @@ -130,6 +128,7 @@
"run-time-error": "1.4.0",
"secp256k1": "4.0.2",
"shebang-loader": "0.0.1",
"sort-package-json": "1.53.1",
"source-map-loader": "3.0.0",
"stream-browserify": "3.0.0",
"tap": "15.0.9",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
"license": "Apache-2.0",
"scripts": {
"build": "npm run codegen && npm run build-ts && npm run build:dev:backend:postbuild",
"codegen": "npm run copy-utility-assets && rm -fr ./dist/wallet",
"build-ts": "tsc",
"build:dev:backend:postbuild": "npm run copy-static-assets",
"copy-utility-assets": "ts-node copyUtilityAssets.ts",
"copy-static-assets": "ts-node copyStaticAssets.ts"
"codegen": "npm run copy-utility-assets && rm -fr ./dist/wallet",
"copy-static-assets": "ts-node copyStaticAssets.ts",
"copy-utility-assets": "ts-node copyUtilityAssets.ts"
},
"dependencies": {
"@types/node": "^14.14.5",
Expand Down
74 changes: 74 additions & 0 deletions tools/custom-checks/check-package-json-sort.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
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];
}
8 changes: 8 additions & 0 deletions tools/custom-checks/run-custom-checks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import esMain from "es-main";
import { checkOpenApiJsonSpecs } from "./check-open-api-json-specs";
import { checkPackageJsonSort } from "./check-package-json-sort";
import { checkSiblingDepVersionConsistency } from "./check-sibling-dep-version-consistency";

export async function runCustomChecks(
Expand Down Expand Up @@ -31,6 +32,13 @@ export async function runCustomChecks(
overallSuccess = overallSuccess && success;
}

{
const req = { argv, env };
const [success, errors] = await checkPackageJsonSort(req);
overallErrors = overallErrors.concat(errors);
overallSuccess = overallSuccess && success;
}

if (!overallSuccess) {
overallErrors.forEach((it) => console.error(it));
} else {
Expand Down
Loading

0 comments on commit 46fbfc9

Please sign in to comment.