Skip to content

Commit

Permalink
feat(version): add --sync-dist-version option (#3787)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Austin Fahsl <austin@fahsl.io>
  • Loading branch information
matheo and fahslaj committed Aug 30, 2023
1 parent 9065d57 commit ba8b946
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 58 deletions.
61 changes: 58 additions & 3 deletions e2e/publish/src/custom-publish-directories.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ describe("lerna-publish-custom-publish-directories", () => {
lerna verb rootPath /tmp/lerna-e2e/lerna-publish-custom-publish-directories/lerna-workspace
lerna verb session XXXXXXXX
lerna verb user-agent lerna/999.9.9-e2e.0/<user agent>
lerna verb git-describe undefined => "vXX.XX.XX-0-gXXXXXXXX"
lerna verb silly Interpolated string "../../dist/packages/package-1" for node "package-1" to produce "../../dist/packages/package-1"
lerna verb silly Interpolated string "../../dist/packages/package-2" for node "package-2" to produce "../../dist/packages/package-2"
lerna verb silly Interpolated string "package.json" for node "package-2" to produce "package.json"
Expand All @@ -130,6 +129,7 @@ describe("lerna-publish-custom-publish-directories", () => {
lerna verb silly Interpolated string "assets" for node "package-2" to produce "assets"
lerna verb silly Interpolated string "../../CONTRIBUTING.md" for node "package-2" to produce "../../CONTRIBUTING.md"
lerna verb silly Interpolated string "./" for node "package-2" to produce "./"
lerna verb git-describe undefined => "vXX.XX.XX-0-gXXXXXXXX"
Found 3 packages to publish:
- package-1 => XX.XX.XX
Expand Down Expand Up @@ -311,13 +311,13 @@ describe("lerna-publish-custom-publish-directories", () => {
lerna verb rootPath /tmp/lerna-e2e/lerna-publish-custom-publish-directories/lerna-workspace
lerna verb session XXXXXXXX
lerna verb user-agent lerna/999.9.9-e2e.0/<user agent>
lerna verb git-describe undefined => "vXX.XX.XX-0-gXXXXXXXX"
lerna verb silly Interpolated string "{workspaceRoot}/dist/{projectRoot}" for node "package-1" to produce "/tmp/lerna-e2e/lerna-publish-custom-publish-directories/lerna-workspace/dist/packages/package-1"
lerna verb silly Interpolated string "package.json" for node "package-1" to produce "package.json"
lerna verb silly Interpolated string "{projectName}.txt" for node "package-1" to produce "package-1.txt"
lerna verb silly Interpolated string "{workspaceRoot}/dist/{projectRoot}" for node "package-2" to produce "/tmp/lerna-e2e/lerna-publish-custom-publish-directories/lerna-workspace/dist/packages/package-2"
lerna verb silly Interpolated string "package.json" for node "package-2" to produce "package.json"
lerna verb silly Interpolated string "{projectName}.txt" for node "package-2" to produce "package-2.txt"
lerna verb git-describe undefined => "vXX.XX.XX-0-gXXXXXXXX"
Found 2 packages to publish:
- package-1 => XX.XX.XX
Expand Down Expand Up @@ -472,11 +472,11 @@ describe("lerna-publish-custom-publish-directories", () => {
lerna verb rootPath /tmp/lerna-e2e/lerna-publish-custom-publish-directories/lerna-workspace
lerna verb session XXXXXXXX
lerna verb user-agent lerna/999.9.9-e2e.0/<user agent>
lerna verb git-describe undefined => "vXX.XX.XX-0-gXXXXXXXX"
lerna verb silly Interpolated string "{workspaceRoot}/dist/{projectRoot}" for node "package-1" to produce "/tmp/lerna-e2e/lerna-publish-custom-publish-directories/lerna-workspace/dist/packages/package-1"
lerna verb silly Interpolated string "package.json" for node "package-1" to produce "package.json"
lerna verb silly Interpolated string "{projectName}.txt" for node "package-1" to produce "package-1.txt"
lerna verb silly Interpolated string "." for node "package-2" to produce "."
lerna verb git-describe undefined => "vXX.XX.XX-0-gXXXXXXXX"
Found 2 packages to publish:
- package-1 => XX.XX.XX
Expand Down Expand Up @@ -555,4 +555,59 @@ describe("lerna-publish-custom-publish-directories", () => {
`);
});
});

describe("version", () => {
it("should sync the version of the contents directory", async () => {
await fixture.updateJson("lerna.json", (lernaJson) => {
return {
...lernaJson,
command: {
publish: {
directory: "{workspaceRoot}/dist/{projectRoot}",
syncDistVersion: true,
},
},
};
});

const version = randomVersion();

await fixture.lerna("create package-1 -y");
await fixture.updateJson("packages/package-1/package.json", (pkg) => ({
...pkg,
main: "main.js",
version,
scripts: {
build: "cp ./lib/package-1.js ../../dist/packages/package-1/lib/main.js",
copyManifest: "cp ./package.json ../../dist/packages/package-1/package.json",
},
}));

// Make sure dist location exists before running mini build target
await ensureDir(fixture.getWorkspacePath("dist/packages/package-1/lib"));

await fixture.exec("git checkout -b test-main");
await writeFile(fixture.getWorkspacePath(".gitignore"), "node_modules\n.DS_Store\ndist");
await fixture.exec("git add .gitignore");
await fixture.exec("git add .");
await fixture.exec('git commit -m "initial-commit"');
await fixture.exec("git push origin test-main");

await fixture.lerna("run build,copyManifest");

await fixture.lerna(
`publish ${version} --registry=http://localhost:4872 --loglevel verbose --concurrency 1 -y`
);

const distVersion = JSON.parse(
await fixture.readWorkspaceFile("dist/packages/package-1/package.json")
).version;

await fixture.exec(`npm unpublish --force package-1@${version} --registry=http://localhost:4872`);

expect(JSON.parse(await fixture.readWorkspaceFile("packages/package-1/package.json")).version).toEqual(
distVersion
);
});
});
});
2 changes: 1 addition & 1 deletion libs/commands/publish/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ Subdirectory to publish. Must apply to ALL packages, and MUST contain a package.
Package lifecycles will still be run in the original leaf directory.
You should probably use one of those lifecycles (`prepare`, `prepublishOnly`, or `prepack`) to _create_ the subdirectory and whatnot.

See [Configuring Published Files](https://lerna.js.org/docs/concepts#configuring-published-files) for more information on configuring files to publish to npm.
See [Configuring Published Files](https://lerna.js.org/docs/concepts/configuring-published-files) for more information on configuring files to publish to npm.

```sh
lerna publish --contents dist
Expand Down
106 changes: 53 additions & 53 deletions libs/commands/publish/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,58 @@ class PublishCommand extends Command {

this.projectsWithPackage = Object.values(this.projectGraph.nodes).filter((node) => !!node.package);

/**
* Determine the relevant configuration for the packages.
* If applicable, using either root level or package level config
*/
this.projectsWithPackage.map((node) => {
const interpolateStr = (str) => {
const res = interpolate(str, {
projectRoot: node.data.root,
projectName: node.name,
workspaceRoot: this.project.rootPath,
});
this.logger.verbose(
"silly",
`Interpolated string "%s" for node "%s" to produce "%s"`,
str,
node.name,
res
);
return res;
};

const pkg = getPackage(node);

if (this.options.contents) {
pkg.contents = this.options.contents;
}

if (pkg.lernaConfig?.command?.publish?.directory) {
// Package level
pkg.contents = interpolateStr(pkg.lernaConfig.command.publish.directory);
} else if (this.project.config.command?.publish?.["directory"]) {
// Root level
pkg.contents = interpolateStr(this.project.config.command.publish["directory"]);
}

if (pkg.lernaConfig?.command?.publish?.assets) {
// Package level
pkg.lernaConfig.command.publish.assets = pkg.lernaConfig.command.publish.assets.map((asset) =>
interpolateAsset(asset, interpolateStr)
);
} else if (this.project.config.command?.publish?.["assets"]) {
// Root level
const assets = this.project.config.command?.publish?.["assets"].map((asset) =>
interpolateAsset(asset, interpolateStr)
);
pkg.lernaConfig = pkg.lernaConfig || {};
pkg.lernaConfig.command = pkg.lernaConfig.command || {};
pkg.lernaConfig.command.publish = pkg.lernaConfig.command.publish || {};
pkg.lernaConfig.command.publish.assets = assets;
}
});

let result: {
updates?: ProjectGraphProjectNodeWithPackage[];
updatesVersions?: [string, string][];
Expand Down Expand Up @@ -304,59 +356,7 @@ class PublishCommand extends Command {
return asset;
}

/**
* Determine the relevant configuration for what gets published.
* If applicable, using either root level or package level config
*/
this.packagesToPublish = this.updates.map((node) => {
const interpolateStr = (str) => {
const res = interpolate(str, {
projectRoot: node.data.root,
projectName: node.name,
workspaceRoot: this.project.rootPath,
});
this.logger.verbose(
"silly",
`Interpolated string "%s" for node "%s" to produce "%s"`,
str,
node.name,
res
);
return res;
};

const pkg = getPackage(node);

if (this.options.contents) {
pkg.contents = this.options.contents;
}

if (pkg.lernaConfig?.command?.publish?.directory) {
// Package level
pkg.contents = interpolateStr(pkg.lernaConfig.command.publish.directory);
} else if (this.project.config.command?.publish?.["directory"]) {
// Root level
pkg.contents = interpolateStr(this.project.config.command.publish["directory"]);
}

if (pkg.lernaConfig?.command?.publish?.assets) {
// Package level
pkg.lernaConfig.command.publish.assets = pkg.lernaConfig.command.publish.assets.map((asset) =>
interpolateAsset(asset, interpolateStr)
);
} else if (this.project.config.command?.publish?.["assets"]) {
// Root level
const assets = this.project.config.command?.publish?.["assets"].map((asset) =>
interpolateAsset(asset, interpolateStr)
);
pkg.lernaConfig = pkg.lernaConfig || {};
pkg.lernaConfig.command = pkg.lernaConfig.command || {};
pkg.lernaConfig.command.publish = pkg.lernaConfig.command.publish || {};
pkg.lernaConfig.command.publish.assets = assets;
}

return pkg;
});
this.packagesToPublish = this.updates.map((node) => getPackage(node));

if (result.needsConfirmation) {
// only confirm for --canary, bump === "from-git",
Expand Down
5 changes: 5 additions & 0 deletions libs/commands/version/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ Running `lerna version --conventional-commits` without the above flags will rele
- [`--signoff-git-commit`](#--signoff-git-commit)
- [`--sign-git-commit`](#--sign-git-commit)
- [`--sign-git-tag`](#--sign-git-tag)
- [`--sync-dist-version`](#--sync-dist-version)
- [`--force-git-tag`](#--force-git-tag)
- [`--tag-version-prefix`](#--tag-version-prefix)
- [`--yes`](#--yes)
Expand Down Expand Up @@ -515,6 +516,10 @@ This option is analogous to the `npm version` [option](https://docs.npmjs.com/mi

This option is analogous to the `npm version` [option](https://docs.npmjs.com/misc/config#sign-git-tag) of the same name.

### `--sync-dist-version`

Updates the version of the `package.json` of the contents directory.

### `--force-git-tag`

This option replaces any existing tag instead of failing.
Expand Down
4 changes: 4 additions & 0 deletions libs/commands/version/src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ const command: CommandModule = {
hidden: true,
type: "boolean",
},
"sync-dist-version": {
describe: "Update the version of the package.json of the contents directory.",
type: "boolean",
},
// TODO: (major) make --no-granular-pathspec the default
"no-granular-pathspec": {
describe: "Do not stage changes file-by-file, but globally.",
Expand Down
8 changes: 7 additions & 1 deletion libs/commands/version/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ interface VersionCommandConfigOptions extends CommandConfigOptions {
commitHooks?: boolean;
gitRemote?: string;
gitTagVersion?: boolean;
syncDistVersion?: boolean;
granularPathspec?: boolean;
push?: boolean;
signGitCommit?: boolean;
Expand Down Expand Up @@ -621,6 +622,7 @@ class VersionCommand extends Command {
changelogPreset,
changelogEntryAdditionalMarkdown,
changelog = true,
syncDistVersion = false,
} = this.options;
const independentVersions = this.project.isIndependent();
const rootPath = this.project.manifest.location;
Expand Down Expand Up @@ -651,7 +653,11 @@ class VersionCommand extends Command {
// update dependencies
this.updateDependencies(node);

return Promise.all([updateLockfileVersion(pkg), pkg.serialize()]).then(([lockfilePath]) => {
return Promise.all([
updateLockfileVersion(pkg),
pkg.serialize(),
pkg.syncDistVersion(syncDistVersion),
]).then(([lockfilePath]) => {
// commit the updated manifest
changedFiles.add(pkg.manifestLocation);

Expand Down
18 changes: 18 additions & 0 deletions libs/core/src/lib/package.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { workspaceRoot } from "@nx/devkit";
import fs from "fs";
import loadJsonFile from "load-json-file";
import npa from "npm-package-arg";
import path from "path";
Expand Down Expand Up @@ -286,6 +287,23 @@ export class Package {
return writePkg(this.manifestLocation, this[PKG] as any).then(() => this);
}

/**
* Sync dist manifest version
*/
async syncDistVersion(doSync: boolean) {
if (doSync) {
const distPkg = path.join(this.contents, "package.json");

if (distPkg !== this.manifestLocation && fs.existsSync(distPkg)) {
const pkg = await loadJsonFile<RawManifest>(distPkg);
pkg.version = this[PKG].version;
await writePkg(distPkg, pkg as any);
}
}

return this;
}

getLocalDependency(
depName: string
): { collection: "dependencies" | "devDependencies" | "optionalDependencies"; spec: string } | null {
Expand Down
7 changes: 7 additions & 0 deletions packages/lerna/schemas/lerna-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1003,6 +1003,9 @@
"gitTagVersion": {
"$ref": "#/$defs/commandOptions/version/gitTagVersion"
},
"syncDistVersion": {
"$ref": "#/$defs/commandOptions/version/syncDistVersion"
},
"granularPathspec": {
"$ref": "#/$defs/commandOptions/shared/granularPathspec"
},
Expand Down Expand Up @@ -1700,6 +1703,10 @@
"type": "boolean",
"description": "During `lerna version`, when true, commit and tag version changes."
},
"syncDistVersion": {
"type": "boolean",
"description": "During `lerna version`, when true, updates the version of the contents directory."
},
"push": {
"type": "boolean",
"description": "During `lerna version`, when true, push tagged commit to git remote."
Expand Down

0 comments on commit ba8b946

Please sign in to comment.