Skip to content

Commit

Permalink
fix(version): writing to lerna.json5 should keep json5 format (#768)
Browse files Browse the repository at this point in the history
* fix(version): writing to lerna.json5 should save as JSON5 stringified
  • Loading branch information
ghiscoding authored Nov 20, 2023
1 parent 6736842 commit fd66852
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 29 deletions.
6 changes: 3 additions & 3 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"request": "launch",
"runtimeExecutable": "node",
"runtimeArgs": ["--nolazy", "-r", "tsm"],
"args": ["packages/cli/src/cli.ts", "publish", "from-package", "--dry-run"],
"args": ["packages/cli/dist/cli.js", "publish", "from-package", "--dry-run"],
"cwd": "${workspaceRoot}",
"console": "integratedTerminal",
"internalConsoleOptions": "openOnSessionStart",
Expand All @@ -51,7 +51,7 @@
"request": "launch",
"runtimeExecutable": "node",
"runtimeArgs": ["--nolazy", "-r", "tsm"],
"args": ["packages/cli/src/cli.ts", "exec", "--scope", " @lerna-lite/core", "echo", "hello"],
"args": ["packages/cli/dist/cli.js", "exec", "--scope", " @lerna-lite/core", "echo", "hello"],
"cwd": "${workspaceRoot}",
"console": "integratedTerminal",
"internalConsoleOptions": "openOnSessionStart",
Expand All @@ -63,7 +63,7 @@
"request": "launch",
"runtimeExecutable": "node",
"runtimeArgs": ["--nolazy", "-r", "tsm"],
"args": ["packages/cli/src/cli.ts", "run", "pack-tarball", "--stream", "--no-bail"],
"args": ["packages/cli/dist/cli.js", "run", "pack-tarball", "--stream", "--no-bail"],
"cwd": "${workspaceRoot}",
"console": "integratedTerminal",
"internalConsoleOptions": "openOnSessionStart",
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ npm install @lerna-lite/publish -D

You want to see a project demo? Sure... you're looking at it 😉

Yes indeed, this project was originally created as an NPM Workspace and later migrated to a [pnpm workspaces](https://pnpm.io/workspaces) for the sole purpose of demoing and testing its own code. All changelogs and versions are created and published by the lib itself, how sweet is that? You can also see that this project has its own [`lerna.jsonc`](https://github.com/lerna-lite/lerna-lite/blob/main/lerna.jsonc) config file as well to run properly (take a look to see how it works).
Yes indeed, this project was originally created as an NPM Workspace and later migrated to a [pnpm workspaces](https://pnpm.io/workspaces) for the sole purpose of demoing and testing its own code. All changelogs and versions are created and published by the lib itself, how sweet is that? You can also see that this project has its own [`lerna.json`](https://github.com/lerna-lite/lerna-lite/blob/main/lerna.json) config file as well to run properly (take a look to see how it works).

### See it in Action 🎦

Expand Down
17 changes: 4 additions & 13 deletions lerna.jsonc → lerna.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
{
"$schema": "node_modules/@lerna-lite/cli/schemas/lerna-schema.json",
// or from a CDN:: "$schema": "https://raw.githubusercontent.com/lerna-lite/lerna-lite/main/packages/cli/schemas/lerna-schema.json",
"version": "2.7.1",
"loglevel": "verbose",
"npmClient": "pnpm",
"command": {
"publish": {
"cleanupTempFiles": true,
"removePackageFields": [
"devDependencies",
"scripts"
]
"removePackageFields": ["devDependencies", "scripts"]
},
"version": {
"conventionalCommits": true,
Expand All @@ -20,13 +18,6 @@
}
},
"changelogPreset": "conventional-changelog-conventionalcommits",
"ignoreChanges": [
"**/__fixtures__/**",
"**/__tests__/**",
"**/helpers/**",
"**/*.md"
],
"packages": [
"packages/*"
]
"ignoreChanges": ["**/__fixtures__/**", "**/__tests__/**", "**/helpers/**", "**/*.md"],
"packages": ["packages/*"]
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"build:pnpm": "pnpm -r --stream build",
"init-cmd": "lerna init --independent --exact --use-workspaces",
"changed-cmd": "lerna changed --all",
"lerna-init": "lerna init --use-workspaces",
"diff-cmd": "lerna diff",
"list-cmd": "lerna list --all",
"exec-win": "lerna exec --scope {@lerna-lite/cli,@lerna-lite/core} -- cross-env-shell echo hello from package: $LERNA_PACKAGE_NAME",
Expand Down Expand Up @@ -88,7 +89,7 @@
"@typescript-eslint/eslint-plugin": "^6.11.0",
"@typescript-eslint/parser": "^6.11.0",
"@vitest/coverage-v8": "^1.0.0-beta.5",
"conventional-changelog-conventionalcommits": "^6.1.0",
"conventional-changelog-conventionalcommits": "^7.0.2",
"cross-env": "^7.0.3",
"eslint": "^8.54.0",
"eslint-config-prettier": "^9.0.0",
Expand Down
31 changes: 30 additions & 1 deletion packages/core/src/project/__tests__/project.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { afterEach, beforeAll, describe, expect, it } from 'vitest';
import { afterEach, beforeAll, describe, expect, it, Mock, vi } from 'vitest';
import { outputFile, remove, writeJson } from 'fs-extra/esm';
import { basename, dirname, join, resolve as pathResolve } from 'node:path';
import { fileURLToPath } from 'node:url';

vi.mock('write-json-file');
vi.mock('node:fs');
import { writeFileSync } from 'node:fs';
import { writeJsonFile } from 'write-json-file';

// remove quotes around top-level strings
expect.addSnapshotSerializer({
test(val) {
Expand Down Expand Up @@ -119,6 +124,30 @@ describe('Project', () => {
`);
});

it('can write to lerna.jsonc config as json format', async () => {
(writeJsonFile as Mock).mockResolvedValue({});
const cwd = await initFixture('lerna-jsonc-config');
const project = new Project(cwd);

project.serializeConfig();
expect(writeJsonFile).toHaveBeenCalledWith(
expect.stringContaining('lerna.jsonc'),
{ version: '1.0.0' },
{
indent: 2,
detectIndent: true,
}
);
});

it('can write to lerna.json5 config as json5 format', async () => {
const cwd = await initFixture('lerna-json5-config');
const project = new Project(cwd);

project.serializeConfig();
expect(writeFileSync).toHaveBeenCalledWith(expect.stringContaining('lerna.json5'), expect.stringContaining(`version: '1.0.0'`));
});

it('errors when lerna.json is irrecoverably invalid JSON', async () => {
const cwd = await initFixture('invalid-lerna-json-irrecoverable');

Expand Down
20 changes: 15 additions & 5 deletions packages/core/src/project/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { globbySync } from 'globby';
import globParent from 'glob-parent';
import JSON5 from 'json5';
import log from 'npmlog';
import { writeFileSync } from 'node:fs';
import { basename, dirname, join, normalize, resolve as pathResolve } from 'node:path';
import pMap from 'p-map';
import { loadJsonFile, loadJsonFileSync } from 'load-json-file';
Expand Down Expand Up @@ -270,11 +271,20 @@ export class Project {
}
}

serializeConfig() {
// TODO: might be package.json prop
return writeJsonFile(this.rootConfigLocation, this.config, { indent: 2, detectIndent: true }).then(
() => this.rootConfigLocation
);
serializeConfig(): Promise<string> {
if (this.rootConfigLocation.endsWith('lerna.json5')) {
// for .json5, we must use JSON stringify method then save as regular text file
return new Promise((resolve) => {
writeFileSync(this.rootConfigLocation, JSON5.stringify(this.config, { space: 2 }));
resolve(this.rootConfigLocation);
});
} else {
// TODO: might be package.json prop
// for .json/.jsonc we can use `write-json-file` lib
return writeJsonFile(this.rootConfigLocation, this.config, { indent: 2, detectIndent: true }).then(
() => this.rootConfigLocation
);
}
}
}

Expand Down
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

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

0 comments on commit fd66852

Please sign in to comment.