-
Notifications
You must be signed in to change notification settings - Fork 283
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: changed from CLI command to using API library
Signed-off-by: Youngone Lee <youngone.lee@accenture.com>
- Loading branch information
1 parent
8b85293
commit 305ef98
Showing
7 changed files
with
2,527 additions
and
2,298 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.