Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(repair): add lerna repair command (#3302)
- Loading branch information
1 parent
a248165
commit aae1a2b
Showing
12 changed files
with
296 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// @ts-check | ||
|
||
"use strict"; | ||
|
||
const path = require("path"); | ||
const { loggingOutput } = require("@lerna-test/helpers/logging-output"); | ||
|
||
// file under test | ||
const lernaRepair = require("@lerna-test/helpers").commandRunner(require("../commands/repair/command")); | ||
|
||
describe("repair", () => { | ||
it("should output the result of running migrations", async () => { | ||
// project fixture is irrelevant, no actual changes are made | ||
await lernaRepair(path.resolve(__dirname, "../../.."))(); | ||
|
||
expect(loggingOutput("info")).toContain("No changes were necessary. This workspace is up to date!"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// @ts-check | ||
|
||
"use strict"; | ||
|
||
/** | ||
* @see https://github.com/yargs/yargs/blob/master/docs/advanced.md#providing-a-command-module | ||
*/ | ||
exports.command = "repair"; | ||
|
||
exports.describe = "Runs automated migrations to repair the state of a lerna repo"; | ||
|
||
exports.handler = function handler(argv) { | ||
// eslint-disable-next-line global-require | ||
return require(".")(argv); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// @ts-check | ||
|
||
"use strict"; | ||
|
||
const { Command } = require("@lerna/command"); | ||
const log = require("npmlog"); | ||
const { executeMigrations } = require("nx/src/command-line/migrate"); | ||
const migrationsJson = require("../../migrations.json"); | ||
|
||
module.exports = factory; | ||
|
||
function factory(argv) { | ||
return new RepairCommand(argv); | ||
} | ||
|
||
class RepairCommand extends Command { | ||
// eslint-disable-next-line class-methods-use-this | ||
initialize() {} | ||
|
||
async execute() { | ||
const verbose = log.level === "verbose"; | ||
|
||
const lernaMigrations = Object.entries(migrationsJson.generators).map(([name, migration]) => { | ||
return /** @type {const} */ ({ | ||
package: "lerna", | ||
cli: "nx", | ||
name, | ||
description: migration.description, | ||
version: migration.version, | ||
}); | ||
}); | ||
|
||
const migrationsThatMadeNoChanges = await executeMigrations( | ||
process.cwd(), | ||
lernaMigrations, | ||
verbose, | ||
false, | ||
"" | ||
); | ||
|
||
if (migrationsThatMadeNoChanges.length < lernaMigrations.length) { | ||
// @ts-ignore | ||
this.logger.info("repair", `Successfully repaired your configuration. This workspace is up to date!`); | ||
} else { | ||
// @ts-ignore | ||
this.logger.info("repair", `No changes were necessary. This workspace is up to date!`); | ||
} | ||
} | ||
} | ||
|
||
module.exports.RepairCommand = RepairCommand; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"generators": { | ||
"noop": { | ||
"cli": "nx", | ||
"version": "5.3.0", | ||
"description": "Noop example migration, can be removed once a real migration is created", | ||
"implementation": "./migrations/noop/noop" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// @ts-check | ||
|
||
// eslint-disable-next-line no-unused-vars | ||
exports.default = async function generator(tree) { | ||
// This is a noop migration just to show how one would be written until the first real implementation is in place. | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// @ts-check | ||
|
||
const { createTreeWithEmptyWorkspace } = require("@nrwl/devkit/testing"); | ||
const { default: noopMigration } = require("./noop"); | ||
|
||
describe("noop migration", () => { | ||
let tree; | ||
|
||
beforeEach(() => { | ||
tree = createTreeWithEmptyWorkspace(); | ||
}); | ||
|
||
it("should be runnable and not throw", async () => { | ||
await expect(noopMigration(tree)).resolves.toBeUndefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { Fixture } from "../../utils/fixture"; | ||
import { normalizeEnvironment } from "../../utils/snapshot-serializer-utils"; | ||
|
||
expect.addSnapshotSerializer({ | ||
serialize(str) { | ||
return normalizeEnvironment(str); | ||
}, | ||
test(val) { | ||
return val != null && typeof val === "string"; | ||
}, | ||
}); | ||
|
||
describe("lerna-repair", () => { | ||
let fixture: Fixture; | ||
|
||
beforeAll(async () => { | ||
fixture = await Fixture.create({ | ||
name: "lerna-repair", | ||
packageManager: "npm", | ||
initializeGit: true, | ||
runLernaInit: true, | ||
installDependencies: true, | ||
}); | ||
}); | ||
afterAll(() => fixture.destroy()); | ||
|
||
it("should run any existing migrations", async () => { | ||
const output = await fixture.lerna("repair"); | ||
|
||
expect(output.combinedOutput).toMatchInlineSnapshot(` | ||
lerna notice cli v999.9.9-e2e.0 | ||
lerna info repair No changes were necessary. This workspace is up to date! | ||
`); | ||
}); | ||
}); |
Oops, something went wrong.