Skip to content

Commit

Permalink
feat(version): apply prettier to updated files, if applicable (lerna#…
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesHenry authored and Ross Rhodes committed Jan 3, 2023
1 parent 114f6a3 commit d570bb4
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 8 deletions.
Expand Up @@ -67,9 +67,13 @@ diff --git a/lerna.json b/lerna.json
index SHA..SHA 100644
--- a/lerna.json
+++ b/lerna.json
@@ -2 +2 @@
@@ -2,4 +2,2 @@
- \\"version\\": \\"1.0.0\\",
- \\"packages\\": [
- \\"packages/*\\"
- ]
+ \\"version\\": \\"1.0.1\\",
+ \\"packages\\": [\\"packages/*\\"]
diff --git a/packages/package-1/package.json b/packages/package-1/package.json
index SHA..SHA 100644
--- a/packages/package-1/package.json
Expand Down Expand Up @@ -138,9 +142,13 @@ diff --git a/lerna.json b/lerna.json
index SHA..SHA 100644
--- a/lerna.json
+++ b/lerna.json
@@ -2 +2 @@
@@ -2,4 +2,2 @@
- \\"version\\": \\"1.0.0\\",
- \\"packages\\": [
- \\"packages/*\\"
- ]
+ \\"version\\": \\"1.0.1\\",
+ \\"packages\\": [\\"packages/*\\"]
diff --git a/packages/package-1/package.json b/packages/package-1/package.json
index SHA..SHA 100644
--- a/packages/package-1/package.json
Expand Down Expand Up @@ -277,9 +285,13 @@ diff --git a/lerna.json b/lerna.json
index SHA..SHA 100644
--- a/lerna.json
+++ b/lerna.json
@@ -2 +2 @@
@@ -2,4 +2,2 @@
- \\"version\\": \\"1.0.0\\",
- \\"packages\\": [
- \\"packages/*\\"
- ]
+ \\"version\\": \\"2.0.0\\",
+ \\"packages\\": [\\"packages/*\\"]
diff --git a/packages/package-1/package.json b/packages/package-1/package.json
index SHA..SHA 100644
--- a/packages/package-1/package.json
Expand Down Expand Up @@ -338,9 +350,13 @@ diff --git a/lerna.json b/lerna.json
index SHA..SHA 100644
--- a/lerna.json
+++ b/lerna.json
@@ -2 +2 @@
@@ -2,4 +2,2 @@
- \\"version\\": \\"1.0.0\\",
- \\"packages\\": [
- \\"packages/*\\"
- ]
+ \\"version\\": \\"1.1.0\\",
+ \\"packages\\": [\\"packages/*\\"]
diff --git a/packages/package-3/package.json b/packages/package-3/package.json
index SHA..SHA 100644
--- a/packages/package-3/package.json
Expand All @@ -359,9 +375,13 @@ diff --git a/lerna.json b/lerna.json
index SHA..SHA 100644
--- a/lerna.json
+++ b/lerna.json
@@ -2 +2 @@
@@ -2,4 +2,2 @@
- \\"version\\": \\"1.0.0\\",
- \\"packages\\": [
- \\"packages/*\\"
- ]
+ \\"version\\": \\"1.0.1\\",
+ \\"packages\\": [\\"packages/*\\"]
diff --git a/packages/package-1/package.json b/packages/package-1/package.json
index SHA..SHA 100644
--- a/packages/package-1/package.json
Expand Down
47 changes: 44 additions & 3 deletions commands/version/lib/git-add.js
@@ -1,22 +1,63 @@
"use strict";

const fs = require("fs");
const log = require("npmlog");
const path = require("path");
const slash = require("slash");
const { workspaceRoot } = require("@nrwl/devkit");
const childProcess = require("@lerna/child-process");

module.exports.gitAdd = gitAdd;

let resolvedPrettier;
function resolvePrettier() {
if (!resolvedPrettier) {
try {
// If the workspace has prettier installed, apply it to the updated files
const prettierPath = path.join(workspaceRoot, "node_modules", "prettier");
// eslint-disable-next-line import/no-dynamic-require, global-require
resolvedPrettier = require(prettierPath);
} catch {
return;
}
}
return resolvedPrettier;
}

function maybeFormatFile(filePath) {
const prettier = resolvePrettier();
if (!prettier) {
return;
}
const config = resolvedPrettier.resolveConfig.sync(filePath);
try {
const input = fs.readFileSync(filePath, "utf8");
fs.writeFileSync(filePath, resolvedPrettier.format(input, { ...config, filepath: filePath }), "utf8");
log.silly("version", `Successfully applied prettier to updated file: ${filePath}`);
} catch {
log.silly("version", `Failed to apply prettier to updated file: ${filePath}`);
}
}

/**
* @param {string[]} changedFiles
* @param {{ granularPathspec: boolean; }} gitOpts
* @param {import("@lerna/child-process").ExecOpts} execOpts
*/
function gitAdd(changedFiles, gitOpts, execOpts) {
let files = [];
for (const file of changedFiles) {
const filePath = slash(path.relative(execOpts.cwd, path.resolve(execOpts.cwd, file)));
maybeFormatFile(filePath);
if (gitOpts.granularPathspec) {
files.push(filePath);
}
}

// granular pathspecs should be relative to the git root, but that isn't necessarily where lerna lives
const files = gitOpts.granularPathspec
? changedFiles.map((file) => slash(path.relative(execOpts.cwd, path.resolve(execOpts.cwd, file))))
: ".";
if (!gitOpts.granularPathspec) {
files = ".";
}

log.silly("gitAdd", files);

Expand Down
1 change: 1 addition & 0 deletions commands/version/package.json
Expand Up @@ -49,6 +49,7 @@
"@lerna/run-topologically": "file:../../utils/run-topologically",
"@lerna/temp-write": "file:../../utils/temp-write",
"@lerna/validation-error": "file:../../core/validation-error",
"@nrwl/devkit": ">=14.8.1 < 16",
"chalk": "^4.1.0",
"dedent": "^0.7.0",
"load-json-file": "^6.2.0",
Expand Down
2 changes: 2 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d570bb4

Please sign in to comment.