Skip to content

Commit

Permalink
Add check for multiple packages (for monorepo) (#36)
Browse files Browse the repository at this point in the history
* Add check for multiple packages (for monorepo)
  • Loading branch information
philippeauriach committed Aug 31, 2023
1 parent 5851190 commit 351539b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
11 changes: 7 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@ const checkForWorkspaces = require("./lib/checks/workspaces");
const hasMSBridgeConfig = require("./lib/checks/msbridge");
const hasYarnLock = require("./lib/checks/yarn");
const { FatalError } = require("./lib/errors");
const { getPackageLock, getPackage } = require("./lib/utils");
const { getPackageLock, getAllPackages } = require("./lib/utils");

function lint() {
const pkgLock = getPackageLock();
const pkgJson = getPackage();
const allPkgs = getAllPackages();
const errors = [];
errors.push(hasYarnLock());
errors.push(checkLockVersion(pkgLock));
errors.push(checkForPreRelease(pkgJson));
errors.push(checkForWorkspaces(pkgJson));
allPkgs.forEach((pkg) => {
console.log(`Checking ${pkg.filename}`);
errors.push(checkForPreRelease(pkg.content));
errors.push(checkForWorkspaces(pkg.content));
});
errors.push(hasMSBridgeConfig());
for (const error of errors) {
if (error instanceof FatalError) {
Expand Down
5 changes: 4 additions & 1 deletion lib/checks/preReleases.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ function checkForPreRelease(pkgJson = {}) {
} = pkgJson;
const deps = { ...dependencies, ...devDependencies, ...peerDependencies };
for (const [name, version] of Object.entries(deps)) {
if (/^@mobsuccess-devops/.test(name) && /-pr-(\d+)\.(\d+)$/.test(version)) {
if (
/^@mobsuccess-devops/.test(name) &&
/-?pr-(\d+)(\.(\d+))*$/.test(version) // matches -pr-1 or -pr-1.0 or -pr-1.0.0 or pr-1
) {
return new FatalError(
`Unexpected pre-release dependency found: ${name}@${version}\n`
);
Expand Down
28 changes: 28 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { spawnSync } = require("child_process");
const { readFileSync } = require("fs");

function getPackage() {
Expand All @@ -8,6 +9,32 @@ function getPackage() {
}
}

/**
* Return the list of all package.json files, except for those in node_modules folder. This is useful for monorepos.
*/
function getAllPackages() {
const result = spawnSync(
"find",
[".", "-name", "package.json", "-not", "-path", "./node_modules/*"],
{ stdio: ["ignore", "pipe", "ignore"] }
);
if (result.status !== 0) {
throw new Error(`find command failed with status ${result.status}`);
}
const stdout = result.stdout.toString("utf-8");
let filenames = stdout.split("\n").filter((line) => line.length > 0);
filenames = filenames.map((filename) => filename.replace("./", ""));
const filesContent = filenames.map((filename) => {
return JSON.parse(readFileSync(filename).toString("utf-8"));
});
return filesContent.map((fileContent, index) => {
return {
filename: filenames[index],
content: fileContent,
};
});
}

function getPackageLock() {
try {
return JSON.parse(readFileSync("package-lock.json").toString("utf-8"));
Expand All @@ -18,5 +45,6 @@ function getPackageLock() {

module.exports = {
getPackage,
getAllPackages,
getPackageLock,
};

0 comments on commit 351539b

Please sign in to comment.