forked from hyperledger-cacti/cacti
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ci(github): add check to validate exported types being correct
Primary Changes --------------- 1. Added get-all-tgz-path.ts to get all tgz files path 2. Added run-attw-on-tgz.ts to run attw on each tgz filepath 3. Added are-the-types-wrong as part of the custom-checks mechanism Fixes: hyperledger-cacti#3140 Signed-off-by: ruzell22 <[email protected]>
- Loading branch information
Showing
6 changed files
with
365 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
"approveformyorg", | ||
"askar", | ||
"Askar", | ||
"attw", | ||
"Authz", | ||
"authzn", | ||
"AWSSM", | ||
|
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,49 @@ | ||
import path from "path"; | ||
import { fileURLToPath } from "url"; | ||
import { globby, Options as GlobbyOptions } from "globby"; | ||
import lernaCfg from "../../lerna.json" assert { type: "json" }; | ||
|
||
/** | ||
* Interface for the response of the getAllTgzPath function. | ||
* @property {Array<string>} relativePaths - An array of relative paths to the | ||
* package directories. | ||
*/ | ||
|
||
export interface IGetAllTgzPathResponse { | ||
readonly relativePaths: Readonly<Array<string>>; | ||
} | ||
|
||
/** | ||
* Asynchronous function to get all tgz filepaths in a Lerna monorepo. | ||
* @returns {Promise<IGetAllTgzPathResponse>} A promise that resolves to an | ||
* object containing the arrays of relative paths to the all tgz files. | ||
*/ | ||
|
||
export async function getAllTgzPath(): Promise<IGetAllTgzPathResponse> { | ||
const TAG = "[tools/get-all-tgz-path.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 globbyOpts: GlobbyOptions = { | ||
cwd: PROJECT_DIR, | ||
onlyFiles: true, | ||
expandDirectories: false, | ||
ignore: ["**/node_modules"], | ||
}; | ||
|
||
const tgzFilesPattern = lernaCfg.packages.map( | ||
(pkg: string) => `${pkg}/**/hyperledger-*.tgz`, | ||
); | ||
|
||
const tgzFilesRelative = await globby(tgzFilesPattern, globbyOpts); | ||
console.log("%s Found %s tgz files.", TAG, tgzFilesRelative.length); | ||
|
||
return { | ||
relativePaths: tgzFilesRelative, | ||
}; | ||
} |
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,83 @@ | ||
import esMain from "es-main"; | ||
import { getAllTgzPath } from "./get-all-tgz-path"; | ||
import { exec } from "child_process"; | ||
import { exit } from "process"; | ||
import { promisify } from "node:util"; | ||
import { rm } from "fs"; | ||
|
||
const execPromise = promisify(exec); | ||
|
||
async function cleanUpTgzFiles(): Promise<void> { | ||
const TAG = "[tools/custom-checks/run-attw-on-tgz.ts]"; | ||
console.log(`${TAG} Cleaning up existing .tgz files...`); | ||
|
||
const { relativePaths: tgzFilesRelative } = await getAllTgzPath(); | ||
|
||
for (const filePath of tgzFilesRelative) { | ||
await rm(filePath, { recursive: true, force: true }, () => {}); | ||
console.log(`${TAG} Deleted ${filePath}`); | ||
} | ||
} | ||
|
||
export async function runAttwOnTgz(): Promise<[boolean, string[]]> { | ||
await cleanUpTgzFiles(); | ||
|
||
const TAG = "[tools/custom-checks/run-attw-on-tgz.ts]"; | ||
await execCommand("yarn lerna exec 'npm pack'", true); | ||
console.log(`${TAG} Packaging .tgz files`); | ||
|
||
console.log(`${TAG} Fetching .tgz file paths.`); | ||
const { relativePaths: tgzFilesRelative } = await getAllTgzPath(); | ||
|
||
const attwFailedPackages: string[] = []; | ||
|
||
for (const filePath of tgzFilesRelative) { | ||
try { | ||
const output = await execCommand("attw", filePath); | ||
console.log(output); | ||
} catch (error: any) { | ||
attwFailedPackages.push( | ||
`ERROR ${filePath}: ${error.message} ${error.output}`, | ||
); | ||
} | ||
} | ||
|
||
const success = attwFailedPackages.length === 0; | ||
return [success, attwFailedPackages]; | ||
} | ||
|
||
async function execCommand( | ||
binaryName: string, | ||
argument: string | boolean, | ||
): Promise<string> { | ||
let command; | ||
if (typeof argument === "boolean") { | ||
command = binaryName; | ||
} else if (typeof argument === "string") { | ||
command = `${binaryName} ./${argument}`; | ||
} else { | ||
throw new Error("Invalid arguments for execCommand"); | ||
} | ||
|
||
try { | ||
const { stdout } = await execPromise(command); | ||
return stdout; | ||
} catch (error: any) { | ||
const { stdout, stderr } = error; | ||
const output = | ||
(stdout ? `stdout:\n${stdout}` : "") + | ||
(stderr ? `\nstderr:\n${stderr}` : ""); | ||
error.output = output; | ||
throw error; | ||
} | ||
} | ||
|
||
if (esMain(import.meta)) { | ||
const [success, attwFailedPackages] = await runAttwOnTgz(); | ||
if (!success) { | ||
console.log("Types are wrong for these packages:"); | ||
console.log(attwFailedPackages); | ||
exit(1); | ||
} | ||
exit(0); | ||
} |
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.