Skip to content

Commit

Permalink
feat: add migration for adding $schema, increase some strictness
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesHenry committed Jun 1, 2023
1 parent 834160c commit 73ceac3
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/lerna/migrations.json
@@ -1,5 +1,11 @@
{
"generators": {
"add-schema-config": {
"cli": "nx",
"version": "7.0.0-alpha.5",
"description": "Add `$schema` config to lerna.json if not already present to allow for IDE validation of lerna.json",
"implementation": "./dist/migrations/add-schema-config/add-schema-config"
},
"remove-invalid-init-config": {
"cli": "nx",
"version": "7.0.0-alpha.2",
Expand Down
1 change: 1 addition & 0 deletions packages/lerna/project.json
Expand Up @@ -79,6 +79,7 @@
"packages/lerna/src/commands/version/lib/prompt-version.ts",
"packages/lerna/src/commands/version/lib/remote-branch-exists.ts",
"packages/lerna/src/commands/version/lib/update-lockfile-version.ts",
"packages/lerna/src/migrations/add-schema-config/add-schema-config.ts",
"packages/lerna/src/migrations/remove-invalid-init-config/remove-invalid-init-config.ts",
"packages/lerna/src/migrations/remove-invalid-use-workspaces/remove-invalid-use-workspaces.ts",
"packages/lerna/src/migrations/remove-unnecessary-use-nx/remove-unnecessary-use-nx.ts",
Expand Down
6 changes: 6 additions & 0 deletions packages/lerna/schemas/lerna-schema.json
Expand Up @@ -3,7 +3,12 @@
"$id": "https://lerna.js.org/docs/getting-started#adding-lerna",
"title": "JSON schema for Lerna configuration",
"type": "object",
"additionalProperties": false,
"properties": {
"$schema": {
"type": "string",
"description": "The JSON schema version used to validate this configuration file"
},
"version": {
"type": "string",
"description": "The version of the repository, or \"independent\" for a repository with independently versioned packages.",
Expand All @@ -25,6 +30,7 @@
},
"command": {
"type": "object",
"additionalProperties": false,
"description": "Options for individual Lerna commands.",
"properties": {
"add": {
Expand Down
@@ -0,0 +1,33 @@
import { readJson, Tree, writeJson } from "@nx/devkit";
import { createTreeWithEmptyWorkspace } from "@nx/devkit/testing";
import addSchemaConfigMigration from "./add-schema-config";

describe("add-schema-config", () => {
let tree: Tree;

beforeEach(() => {
tree = createTreeWithEmptyWorkspace();
});

it(`should add "$schema" config if not already present`, async () => {
writeJson(tree, "lerna.json", {});
await addSchemaConfigMigration(tree);
expect(readJson(tree, "lerna.json")).toMatchInlineSnapshot(`
Object {
"$schema": "node_modules/lerna/schemas/lerna-schema.json",
}
`);
});

it(`should not not modify the "$schema" config if it is already present`, async () => {
writeJson(tree, "lerna.json", {
$schema: "./some/path/to/schema.json",
});
await addSchemaConfigMigration(tree);
expect(readJson(tree, "lerna.json")).toMatchInlineSnapshot(`
Object {
"$schema": "./some/path/to/schema.json",
}
`);
});
});
@@ -0,0 +1,12 @@
import { formatFiles, readJson, Tree, writeJson } from "@nx/devkit";

export default async function generator(tree: Tree) {
const lernaJson = readJson(tree, "lerna.json");

if (!lernaJson.$schema) {
lernaJson.$schema = "node_modules/lerna/schemas/lerna-schema.json";
writeJson(tree, "lerna.json", lernaJson);
}

await formatFiles(tree);
}

0 comments on commit 73ceac3

Please sign in to comment.