Skip to content

Commit 351539b

Browse files
Add check for multiple packages (for monorepo) (#36)
* Add check for multiple packages (for monorepo)
1 parent 5851190 commit 351539b

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

index.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,19 @@ const checkForWorkspaces = require("./lib/checks/workspaces");
44
const hasMSBridgeConfig = require("./lib/checks/msbridge");
55
const hasYarnLock = require("./lib/checks/yarn");
66
const { FatalError } = require("./lib/errors");
7-
const { getPackageLock, getPackage } = require("./lib/utils");
7+
const { getPackageLock, getAllPackages } = require("./lib/utils");
88

99
function lint() {
1010
const pkgLock = getPackageLock();
11-
const pkgJson = getPackage();
11+
const allPkgs = getAllPackages();
1212
const errors = [];
1313
errors.push(hasYarnLock());
1414
errors.push(checkLockVersion(pkgLock));
15-
errors.push(checkForPreRelease(pkgJson));
16-
errors.push(checkForWorkspaces(pkgJson));
15+
allPkgs.forEach((pkg) => {
16+
console.log(`Checking ${pkg.filename}`);
17+
errors.push(checkForPreRelease(pkg.content));
18+
errors.push(checkForWorkspaces(pkg.content));
19+
});
1720
errors.push(hasMSBridgeConfig());
1821
for (const error of errors) {
1922
if (error instanceof FatalError) {

lib/checks/preReleases.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ function checkForPreRelease(pkgJson = {}) {
88
} = pkgJson;
99
const deps = { ...dependencies, ...devDependencies, ...peerDependencies };
1010
for (const [name, version] of Object.entries(deps)) {
11-
if (/^@mobsuccess-devops/.test(name) && /-pr-(\d+)\.(\d+)$/.test(version)) {
11+
if (
12+
/^@mobsuccess-devops/.test(name) &&
13+
/-?pr-(\d+)(\.(\d+))*$/.test(version) // matches -pr-1 or -pr-1.0 or -pr-1.0.0 or pr-1
14+
) {
1215
return new FatalError(
1316
`Unexpected pre-release dependency found: ${name}@${version}\n`
1417
);

lib/utils.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const { spawnSync } = require("child_process");
12
const { readFileSync } = require("fs");
23

34
function getPackage() {
@@ -8,6 +9,32 @@ function getPackage() {
89
}
910
}
1011

12+
/**
13+
* Return the list of all package.json files, except for those in node_modules folder. This is useful for monorepos.
14+
*/
15+
function getAllPackages() {
16+
const result = spawnSync(
17+
"find",
18+
[".", "-name", "package.json", "-not", "-path", "./node_modules/*"],
19+
{ stdio: ["ignore", "pipe", "ignore"] }
20+
);
21+
if (result.status !== 0) {
22+
throw new Error(`find command failed with status ${result.status}`);
23+
}
24+
const stdout = result.stdout.toString("utf-8");
25+
let filenames = stdout.split("\n").filter((line) => line.length > 0);
26+
filenames = filenames.map((filename) => filename.replace("./", ""));
27+
const filesContent = filenames.map((filename) => {
28+
return JSON.parse(readFileSync(filename).toString("utf-8"));
29+
});
30+
return filesContent.map((fileContent, index) => {
31+
return {
32+
filename: filenames[index],
33+
content: fileContent,
34+
};
35+
});
36+
}
37+
1138
function getPackageLock() {
1239
try {
1340
return JSON.parse(readFileSync("package-lock.json").toString("utf-8"));
@@ -18,5 +45,6 @@ function getPackageLock() {
1845

1946
module.exports = {
2047
getPackage,
48+
getAllPackages,
2149
getPackageLock,
2250
};

0 commit comments

Comments
 (0)