Skip to content

Commit

Permalink
feat(publish): Add --git-head option to preserve gitless `from-pack…
Browse files Browse the repository at this point in the history
…age` metadata

Fixes #1933
  • Loading branch information
evocateur committed Feb 14, 2019
1 parent df49bfc commit 3d18f2f
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 1 deletion.
14 changes: 14 additions & 0 deletions commands/publish/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ This is useful when a previous `lerna publish` failed to publish all packages to
- [`--canary`](#--canary)
- [`--contents <dir>`](#--contents-dir)
- [`--dist-tag <tag>`](#--dist-tag-tag)
- [`--git-head <sha>`](#--git-head-sha)
- [`--no-git-reset`](#--no-git-reset)
- [`--no-verify-access`](#--no-verify-access)
- [`--preid`](#--preid)
Expand Down Expand Up @@ -103,6 +104,19 @@ This option can be used to publish a [`prerelease`](http://carrot.is/coding/npm_
> Note: the `latest` tag is the one that is used when a user runs `npm install my-package`.
> To install a different tag, a user can run `npm install my-package@prerelease`.
### `--git-head <sha>`

Explicit SHA to set as [`gitHead`](https://git.io/fh7np) on manifests when packing tarballs, only allowed with [`from-package`](#bump-from-package) positional.

For example, when publishing from AWS CodeBuild (where `git` is not available),
you could use this option to pass the appropriate [environment variable](https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-env-vars.html) to use for this package metadata:

```sh
lerna publish from-package --git-head ${CODEBUILD_RESOLVED_SOURCE_VERSION}
```

Under all other circumstances, this value is derived from a local `git` command.

### `--no-git-reset`

By default, `lerna publish` ensures any changes to the working tree have been reset.
Expand Down
12 changes: 12 additions & 0 deletions commands/publish/__tests__/publish-canary.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,18 @@ test("publish --canary with dirty tree throws error", async () => {
expect.assertions(1);
});

test("publish --canary --git-head <sha> throws an error", async () => {
const cwd = await initFixture("normal");

try {
await lernaPublish(cwd)("--canary", "--git-head", "deadbeef");
} catch (err) {
expect(err.prefix).toBe("EGITHEAD");
}

expect.hasAssertions();
});

test("publish --canary --include-merged-tags calls git describe correctly", async () => {
const spy = jest.spyOn(childProcess, "exec");
const cwd = await initTaggedFixture("normal");
Expand Down
10 changes: 10 additions & 0 deletions commands/publish/__tests__/publish-command.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ describe("PublishCommand", () => {

expect.assertions(2);
});

it("errors when --git-head is passed without from-package positional", async () => {
try {
await lernaPublish(cwd)("--git-head", "deadbeef");
} catch (err) {
expect(err.message).toBe("--git-head is only allowed with 'from-package' positional");
}

expect.hasAssertions();
});
});

describe("with implied versioning", () => {
Expand Down
12 changes: 12 additions & 0 deletions commands/publish/__tests__/publish-from-git.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,16 @@ describe("publish from-git", () => {

expect.assertions(1);
});

it("throws an error when --git-head is passed", async () => {
const cwd = await initFixture("normal");

try {
await lernaPublish(cwd)("from-git", "--git-head", "deadbeef");
} catch (err) {
expect(err.prefix).toBe("EGITHEAD");
}

expect.hasAssertions();
});
});
11 changes: 11 additions & 0 deletions commands/publish/__tests__/publish-from-package.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,15 @@ describe("publish from-package", () => {
);
expect(logMessages).toContain("Unable to reset working tree changes, this probably isn't a git repo.");
});

it("accepts --git-head override", async () => {
getUnpublishedPackages.mockImplementationOnce(packageGraph => [packageGraph.get("package-1")]);

const cwd = await initFixture("independent");

await lernaPublish(cwd)("from-package", "--git-head", "deadbeef");

expect(npmPublish).toHaveBeenCalled();
expect(writePkg.updatedManifest("package-1").gitHead).toBe("deadbeef");
});
});
6 changes: 6 additions & 0 deletions commands/publish/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ exports.builder = yargs => {
type: "string",
requiresArg: true,
},
"git-head": {
describe:
"Explicit SHA to set as gitHead when packing tarballs, only allowed with 'from-package' positional.",
type: "string",
requiresArg: true,
},
registry: {
describe: "Use the specified registry for all npm client operations.",
type: "string",
Expand Down
7 changes: 6 additions & 1 deletion commands/publish/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const pReduce = require("p-reduce");
const semver = require("semver");

const Command = require("@lerna/command");
const ValidationError = require("@lerna/validation-error");
const describeRef = require("@lerna/describe-ref");
const checkWorkingTree = require("@lerna/check-working-tree");
const PromptUtilities = require("@lerna/prompt");
Expand Down Expand Up @@ -70,6 +71,10 @@ class PublishCommand extends Command {
this.logger.info("require-scripts", "enabled");
}

if (this.requiresGit && this.options.gitHead) {
throw new ValidationError("EGITHEAD", "--git-head is only allowed with 'from-package' positional");
}

// https://docs.npmjs.com/misc/config#save-prefix
this.savePrefix = this.options.exact ? "" : "^";

Expand Down Expand Up @@ -492,7 +497,7 @@ class PublishCommand extends Command {

annotateGitHead() {
try {
const gitHead = getCurrentSHA(this.execOpts);
const gitHead = this.options.gitHead || getCurrentSHA(this.execOpts);

for (const pkg of this.packagesToPublish) {
// provide gitHead property that is normally added during npm publish
Expand Down

0 comments on commit 3d18f2f

Please sign in to comment.