Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check for multiple packages (for monorepo) #36

Merged
merged 3 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Vraiment utile le com ?

) {
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,
};