Skip to content

Commit

Permalink
fix(version): recognize .prettierignore when formatting files (#3482)
Browse files Browse the repository at this point in the history
  • Loading branch information
fahslaj committed Jan 5, 2023
1 parent 008b995 commit 4e2c7a9
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 27 deletions.
Expand Up @@ -67,13 +67,9 @@ diff --git a/lerna.json b/lerna.json
index SHA..SHA 100644
--- a/lerna.json
+++ b/lerna.json
@@ -2,4 +2,2 @@
@@ -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 @@ -142,13 +138,9 @@ diff --git a/lerna.json b/lerna.json
index SHA..SHA 100644
--- a/lerna.json
+++ b/lerna.json
@@ -2,4 +2,2 @@
@@ -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 @@ -285,13 +277,9 @@ diff --git a/lerna.json b/lerna.json
index SHA..SHA 100644
--- a/lerna.json
+++ b/lerna.json
@@ -2,4 +2,2 @@
@@ -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 @@ -350,13 +338,9 @@ diff --git a/lerna.json b/lerna.json
index SHA..SHA 100644
--- a/lerna.json
+++ b/lerna.json
@@ -2,4 +2,2 @@
@@ -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 @@ -375,13 +359,9 @@ diff --git a/lerna.json b/lerna.json
index SHA..SHA 100644
--- a/lerna.json
+++ b/lerna.json
@@ -2,4 +2,2 @@
@@ -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
15 changes: 13 additions & 2 deletions commands/version/lib/git-add.js
Expand Up @@ -35,9 +35,20 @@ function maybeFormatFile(filePath) {
return;
}
const config = resolvedPrettier.resolveConfig.sync(filePath);
const ignorePath = path.join(workspaceRoot, ".prettierignore");
const fullFilePath = path.join(workspaceRoot, filePath);

if (resolvedPrettier.getFileInfo.sync(fullFilePath, { ignorePath }).ignored) {
log.silly("version", `Skipped applying prettier to ignored file: ${filePath}`);
return;
}
try {
const input = fs.readFileSync(filePath, "utf8");
fs.writeFileSync(filePath, resolvedPrettier.format(input, { ...config, filepath: filePath }), "utf8");
const input = fs.readFileSync(fullFilePath, "utf8");
fs.writeFileSync(
fullFilePath,
resolvedPrettier.format(input, { ...config, filepath: fullFilePath }),
"utf8"
);
log.silly("version", `Successfully applied prettier to updated file: ${filePath}`);
} catch {
log.silly("version", `Failed to apply prettier to updated file: ${filePath}`);
Expand Down
122 changes: 122 additions & 0 deletions e2e/version/src/formatting.spec.ts
@@ -0,0 +1,122 @@
import { Fixture, normalizeEnvironment } from "@lerna/e2e-utils";
import { writeFile } from "fs-extra";

expect.addSnapshotSerializer({
serialize(str: string) {
return normalizeEnvironment(str).replaceAll(/"author": ".*"/g, 'author: "XXXX"');
},
test(val: string) {
return val != null && typeof val === "string";
},
});

describe("lerna-version-formatting", () => {
let fixture: Fixture;

beforeEach(async () => {
fixture = await Fixture.create({
e2eRoot: process.env.E2E_ROOT,
name: "lerna-version-formatting",
packageManager: "npm",
initializeGit: true,
runLernaInit: true,
installDependencies: false,
});

await fixture.updateJson("package.json", (json: { devDependencies: Record<string, unknown> }) => ({
...json,
devDependencies: {
...json.devDependencies,
prettier: "^2.2.1",
},
}));
await fixture.install();

await writeFile(
fixture.getWorkspacePath(".prettierrc"),
JSON.stringify({
tabWidth: 6,
})
);
await writeFile(fixture.getWorkspacePath(".prettierignore"), "node_modules");

await fixture.lerna("create package-a -y");

await fixture.createInitialGitCommit();
await fixture.exec("git push origin test-main");
});
afterEach(() => fixture.destroy());

it("should format file if not in .prettierignore.", async () => {
await fixture.lerna("version patch -y");

const packageJson = await fixture.readWorkspaceFile("packages/package-a/package.json");

expect(packageJson).toMatchInlineSnapshot(`
{
"name": "package-a",
"version": "0.0.1",
"description": "Now I’m the model of a modern major general / The venerated Virginian veteran whose men are all / Lining up, to put me up on a pedestal / Writin’ letters to relatives / Embellishin’ my elegance and eloquence / But the elephant is in the room / The truth is in ya face when ya hear the British cannons go / BOOM",
"keywords": [],
author: "XXXX",
"license": "ISC",
"main": "lib/package-a.js",
"directories": {
"lib": "lib",
"test": "__tests__"
},
"files": [
"lib"
],
"repository": {
"type": "git",
"url": "/tmp/lerna-e2e/lerna-version-formatting/origin.git"
},
"scripts": {
"test": "node ./__tests__/package-a.test.js"
}
}
`);
});

it("should not format file if is in .prettierignore.", async () => {
await writeFile(
fixture.getWorkspacePath(".prettierignore"),
"node_modules\npackages/package-a/package.json"
);
await fixture.exec("git add .prettierignore");
await fixture.exec("git commit -m 'chore: add package-lock.json to .prettierignore'");

await fixture.lerna("version patch -y");

const packageJson = await fixture.readWorkspaceFile("packages/package-a/package.json");

expect(packageJson).toMatchInlineSnapshot(`
{
"name": "package-a",
"version": "0.0.1",
"description": "Now I’m the model of a modern major general / The venerated Virginian veteran whose men are all / Lining up, to put me up on a pedestal / Writin’ letters to relatives / Embellishin’ my elegance and eloquence / But the elephant is in the room / The truth is in ya face when ya hear the British cannons go / BOOM",
"keywords": [],
author: "XXXX",
"license": "ISC",
"main": "lib/package-a.js",
"directories": {
"lib": "lib",
"test": "__tests__"
},
"files": [
"lib"
],
"repository": {
"type": "git",
"url": "/tmp/lerna-e2e/lerna-version-formatting/origin.git"
},
"scripts": {
"test": "node ./__tests__/package-a.test.js"
}
}
`);
});
});

0 comments on commit 4e2c7a9

Please sign in to comment.