Skip to content

Commit cfd573a

Browse files
author
Leo Dumon
authored
feat(publish): support full file path for --summary-file (lerna#4039)
1 parent 2076e4b commit cfd573a

File tree

3 files changed

+48
-7
lines changed

3 files changed

+48
-7
lines changed

libs/commands/publish/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,8 @@ Useful in [Continuous integration (CI)](https://en.wikipedia.org/wiki/Continuous
334334
lerna publish --canary --yes --summary-file
335335
# Will create a summary file in the provided directory, i.e. `./some/other/dir/lerna-publish-summary.json`
336336
lerna publish --canary --yes --summary-file ./some/other/dir
337-
337+
# Will create a summary file with the provided name, i.e. `./some/other/dir/my-summary.json`
338+
lerna publish --canary --yes --summary-file ./some/other/dir/my-summary.json
338339
```
339340

340341
When run with this flag, a json summary report will be generated after all packages have been successfully published (see below for an example).

libs/commands/publish/src/index.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -415,16 +415,15 @@ class PublishCommand extends Command {
415415
output("Successfully published:");
416416

417417
if (this.options.summaryFile !== undefined) {
418-
// create a json object and output it to a file location.
419-
const filePath = this.options.summaryFile
420-
? `${this.options.summaryFile}/lerna-publish-summary.json`
421-
: "./lerna-publish-summary.json";
418+
const filePath = this.getSummaryFilePath();
419+
422420
const jsonObject = publishedPackagesSorted.map((pkg) => {
423421
return {
424422
packageName: pkg.name,
425423
version: pkg.version,
426424
};
427425
});
426+
428427
output(jsonObject);
429428
try {
430429
fs.writeFileSync(filePath, JSON.stringify(jsonObject));
@@ -1220,6 +1219,28 @@ class PublishCommand extends Command {
12201219
}
12211220
}
12221221
}
1222+
1223+
private getSummaryFilePath(): string {
1224+
if (this.options.summaryFile === undefined) {
1225+
throw new Error("summaryFile options is not defined. Unable to get path.");
1226+
}
1227+
1228+
if (this.options.summaryFile === "") {
1229+
return path.join(process.cwd(), "./lerna-publish-summary.json");
1230+
}
1231+
1232+
const normalizedPath = path.normalize(this.options.summaryFile);
1233+
1234+
if (normalizedPath === "") {
1235+
throw new Error("summaryFile is not a valid path.");
1236+
}
1237+
1238+
if (normalizedPath.endsWith(".json")) {
1239+
return path.join(process.cwd(), normalizedPath);
1240+
}
1241+
1242+
return path.join(process.cwd(), normalizedPath, "lerna-publish-summary.json");
1243+
}
12231244
}
12241245

12251246
module.exports.PublishCommand = PublishCommand;

libs/commands/publish/src/lib/publish-command.spec.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ Map {
382382
];
383383
expect(fsSpy).toHaveBeenCalled();
384384
expect(fsSpy).toHaveBeenCalledWith(
385-
"./outputs/lerna-publish-summary.json",
385+
path.join(process.cwd(), "outputs/lerna-publish-summary.json"),
386386
JSON.stringify(expectedJsonResponse)
387387
);
388388
});
@@ -392,6 +392,25 @@ Map {
392392
const fsSpy = jest.spyOn(fsmain, "writeFileSync");
393393
await lernaPublish(cwd)("--summary-file");
394394

395+
const expectedJsonResponse = [
396+
{ packageName: "package-1", version: "1.0.1" },
397+
{ packageName: "package-2", version: "1.0.1" },
398+
{ packageName: "package-3", version: "1.0.1" },
399+
{ packageName: "package-4", version: "1.0.1" },
400+
];
401+
402+
expect(fsSpy).toHaveBeenCalled();
403+
expect(fsSpy).toHaveBeenCalledWith(
404+
path.join(process.cwd(), "./lerna-publish-summary.json"),
405+
JSON.stringify(expectedJsonResponse)
406+
);
407+
});
408+
409+
it("creates the summary file in the provided file path", async () => {
410+
const cwd = await initFixture("normal");
411+
const fsSpy = jest.spyOn(fsmain, "writeFileSync");
412+
await lernaPublish(cwd)("--summary-file", "./outputs/lerna-publish-summary.json");
413+
395414
const expectedJsonResponse = [
396415
{ packageName: "package-1", version: "1.0.1" },
397416
{ packageName: "package-2", version: "1.0.1" },
@@ -400,7 +419,7 @@ Map {
400419
];
401420
expect(fsSpy).toHaveBeenCalled();
402421
expect(fsSpy).toHaveBeenCalledWith(
403-
"./lerna-publish-summary.json",
422+
path.join(process.cwd(), "outputs/lerna-publish-summary.json"),
404423
JSON.stringify(expectedJsonResponse)
405424
);
406425
});

0 commit comments

Comments
 (0)