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
- Loading branch information
1 parent
add4356
commit 81c660b
Showing
13 changed files
with
189 additions
and
29 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
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.