-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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(publish): display uncommitted changes in check-working-tree #2066
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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` |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Brilliant example, thanks! |
||
|
||
```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. |
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); | ||
}); | ||
}); | ||
}); |
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)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ooh, fancy. |
||
|
||
const transformOutput = o(filterEmpty, o(splitOnNewLine, colorizeStats)); | ||
|
||
function collectUncommitted(options = {}) { | ||
return exec("git", "status -s", options).then(transformOutput); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This module looks fine as-is, though I am a little concerned that the Anyway, we can cross that bridge when we come to it, if at all. |
||
} | ||
|
||
function sync(options = {}) { | ||
const stdout = execSync("git", "status -s", options); | ||
return transformOutput(stdout); | ||
} |
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.
Actually, this is automatically generated during
lerna publish --conventional-commits
. I appreciate the motivation, though!