Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(publish): Display uncommitted changes when validation fails (#2066)
* Add utils/collect-uncommitted package
- Loading branch information
Showing
10 changed files
with
275 additions
and
25 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 @@ | ||
# Change Log | ||
|
||
All notable changes to this project will be documented in this file. | ||
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. | ||
|
||
## Unpublished | ||
|
||
### Features | ||
|
||
* Create `[@lerna](https://github.com/lerna)/collect-uncommitted` |
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,25 @@ | ||
# `@lerna/collect-uncommitted` | ||
|
||
> Check git working tree status and collect uncommitted changes for display | ||
## Usage | ||
|
||
```js | ||
const collectUncommitted = require("@lerna/collect-uncommitted"); | ||
|
||
// values listed here are their defaults | ||
const options = { | ||
cwd: process.cwd(), | ||
}; | ||
|
||
(async () => { | ||
try { | ||
const results = await collectUncommitted(options); | ||
console.log(`Uncommitted changes on CWD ${options.cwd}: ${results.join("\n")}`); | ||
} catch (err) { | ||
console.error(err.message); | ||
} | ||
})(); | ||
``` | ||
|
||
Install [lerna](https://www.npmjs.com/package/lerna) for access to the `lerna` CLI. |
78 changes: 78 additions & 0 deletions
78
utils/collect-uncommitted/__tests__/collect-uncommitted.js
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,78 @@ | ||
"use strict"; | ||
|
||
jest.mock("@lerna/child-process"); | ||
|
||
const chalk = require("chalk"); | ||
const childProcess = require("@lerna/child-process"); | ||
const collectUncommitted = require("../lib/collect-uncommitted"); | ||
|
||
const stats = `AD file1 | ||
D file2 | ||
M path/to/file3 | ||
AM path/file4 | ||
MM path/file5 | ||
M file6 | ||
D file7 | ||
UU file8 | ||
?? file9`; | ||
|
||
const GREEN_A = chalk.green("A"); | ||
const GREEN_M = chalk.green("M"); | ||
const GREEN_D = chalk.green("D"); | ||
const RED_D = chalk.red("D"); | ||
const RED_M = chalk.red("M"); | ||
const RED_UU = chalk.red("UU"); | ||
const RED_QQ = chalk.red("??"); | ||
|
||
const colorizedAry = [ | ||
`${GREEN_A}${RED_D} file1`, | ||
` ${RED_D} file2`, | ||
` ${RED_M} path/to/file3`, | ||
`${GREEN_A}${RED_M} path/file4`, | ||
`${GREEN_M}${RED_M} path/file5`, | ||
`${GREEN_M} file6`, | ||
`${GREEN_D} file7`, | ||
`${RED_UU} file8`, | ||
`${RED_QQ} file9`, | ||
]; | ||
|
||
childProcess.exec.mockResolvedValue(stats); | ||
childProcess.execSync.mockReturnValue(stats); | ||
|
||
describe("collectUncommitted()", () => { | ||
it("resolves an array of uncommitted changes", async () => { | ||
const result = await collectUncommitted(); | ||
expect(childProcess.exec).toHaveBeenLastCalledWith("git", "status -s", {}); | ||
expect(result).toEqual(colorizedAry); | ||
}); | ||
|
||
it("empty array on clean repo", async () => { | ||
childProcess.exec.mockResolvedValueOnce(""); | ||
const result = await collectUncommitted(); | ||
expect(childProcess.exec).toHaveBeenLastCalledWith("git", "status -s", {}); | ||
expect(result).toEqual([]); | ||
}); | ||
|
||
it("accepts options.cwd", async () => { | ||
const options = { cwd: "foo" }; | ||
await collectUncommitted(options); | ||
|
||
expect(childProcess.exec).toHaveBeenLastCalledWith("git", "status -s", options); | ||
}); | ||
|
||
describe("collectUncommitted.sync()", () => { | ||
it("returns an array of uncommitted changes", async () => { | ||
const result = collectUncommitted.sync(); | ||
|
||
expect(childProcess.execSync).toHaveBeenLastCalledWith("git", "status -s", {}); | ||
expect(result).toEqual(colorizedAry); | ||
}); | ||
|
||
it("accepts options.cwd", async () => { | ||
const options = { cwd: "foo" }; | ||
collectUncommitted.sync(options); | ||
|
||
expect(childProcess.execSync).toHaveBeenLastCalledWith("git", "status -s", options); | ||
}); | ||
}); | ||
}); |
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,33 @@ | ||
"use strict"; | ||
|
||
const chalk = require("chalk"); | ||
const { exec, execSync } = require("@lerna/child-process"); | ||
|
||
module.exports = collectUncommitted; | ||
module.exports.sync = sync; | ||
|
||
const maybeColorize = colorize => s => (s !== " " ? colorize(s) : s); | ||
const cRed = maybeColorize(chalk.red); | ||
const cGreen = maybeColorize(chalk.green); | ||
|
||
const replaceStatus = (_, maybeGreen, maybeRed) => `${cGreen(maybeGreen)}${cRed(maybeRed)}`; | ||
|
||
const colorizeStats = stats => | ||
stats.replace(/^([^U]| )([A-Z]| )/gm, replaceStatus).replace(/^\?{2}|U{2}/gm, cRed("$&")); | ||
|
||
const splitOnNewLine = (string = "") => string.split("\n"); | ||
|
||
const filterEmpty = (strings = []) => strings.filter(s => s.length !== 0); | ||
|
||
const o = (l, r) => x => l(r(x)); | ||
|
||
const transformOutput = o(filterEmpty, o(splitOnNewLine, colorizeStats)); | ||
|
||
function collectUncommitted(options = {}) { | ||
return exec("git", "status -s", options).then(transformOutput); | ||
} | ||
|
||
function sync(options = {}) { | ||
const stdout = execSync("git", "status -s", options); | ||
return transformOutput(stdout); | ||
} |
Oops, something went wrong.