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

fix(version): recognize .prettierignore when formatting files #3482

Merged
merged 5 commits into from Jan 5, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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"
}
}

`);
});
});