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
feat(repair): add lerna repair command #3314
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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,29 @@ | ||
// @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.builder = (yargs) => { | ||
yargs.options({ | ||
/** | ||
* equivalent to --loglevel=verbose, but added explicitly here because the repair() | ||
* output will potentially contain instructions to run with --verbose | ||
*/ | ||
verbose: { | ||
hidden: true, | ||
type: "boolean", | ||
}, | ||
}); | ||
return yargs; | ||
}; | ||
|
||
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,56 @@ | ||
// @ts-check | ||
|
||
"use strict"; | ||
|
||
const { Command } = require("@lerna/command"); | ||
const log = require("npmlog"); | ||
const { repair } = require("nx/src/command-line/repair"); | ||
const migrationsJson = require("../../migrations.json"); | ||
|
||
module.exports = factory; | ||
|
||
function factory(argv) { | ||
return new RepairCommand(argv); | ||
} | ||
|
||
class RepairCommand extends Command { | ||
constructor(argv) { | ||
super(argv, { skipValidations: true }); | ||
} | ||
|
||
// eslint-disable-next-line class-methods-use-this | ||
initialize() {} | ||
|
||
async execute() { | ||
this.configureNxOutput(); | ||
const verbose = this.options?.verbose ? true : 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, | ||
}); | ||
}); | ||
|
||
await repair({ verbose }, lernaMigrations); | ||
} | ||
|
||
configureNxOutput() { | ||
try { | ||
// eslint-disable-next-line global-require | ||
const nxOutput = require("nx/src/utils/output"); | ||
nxOutput.output.cliName = "Lerna"; | ||
nxOutput.output.formatCommand = (taskId) => taskId; | ||
return nxOutput; | ||
} catch (err) { | ||
this.logger.error("There was a critical issue when trying to execute the repair command."); | ||
// Rethrow so that the lerna logger can automatically handle the unexpected error | ||
throw err; | ||
} | ||
} | ||
} | ||
|
||
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,38 @@ | ||
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 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Type: "intitialize" -> "initialize"