Skip to content

Commit

Permalink
chore: rework publish directory and assets (#3711)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesHenry committed Jun 7, 2023
1 parent d326fd7 commit e7e0489
Show file tree
Hide file tree
Showing 13 changed files with 651 additions and 134 deletions.
28 changes: 11 additions & 17 deletions CHANGELOG.md
Expand Up @@ -5,28 +5,22 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline

# [7.0.0-alpha.7](https://github.com/lerna/lerna/compare/7.0.0-alpha.0...7.0.0-alpha.7) (2023-06-05)


### Bug Fixes

* bump cosmiconfig to v8 ([#3701](https://github.com/lerna/lerna/issues/3701)) ([898923d](https://github.com/lerna/lerna/commit/898923d198319d76ed5e37e553bfe3b27e43604c))
* daemon communication ([e82618b](https://github.com/lerna/lerna/commit/e82618bf1a8aaea6d3d7f74f9afb8f1c5afe04ad))
* ensure repair command not blocked by config validation ([e237d58](https://github.com/lerna/lerna/commit/e237d585e63a2fb502a8958f15a1efe03d781c57))
* improve github client missing env var error ([ce4b352](https://github.com/lerna/lerna/commit/ce4b35213200b07e89a6ab09743f84aa60e5ca2c))
* internal cli.js should not be bundled ([53d73c6](https://github.com/lerna/lerna/commit/53d73c6aa9833e5a5bf60c2c78896456e77fab40))
* migration building/publishing issues ([27bf800](https://github.com/lerna/lerna/commit/27bf800b6e7670ea1ec5576fdf008e8d09897d4b))
* **publish:** use correct version in log messages ([#3702](https://github.com/lerna/lerna/issues/3702)) ([4be9188](https://github.com/lerna/lerna/commit/4be9188e68c5d4c320c0946e6e386cbee95a8efe))
* share project data when nesting commands ([#3709](https://github.com/lerna/lerna/issues/3709)) ([53e71e4](https://github.com/lerna/lerna/commit/53e71e4746604aa3fbaada3dad58b02904769759))
* support nx 16.3.1+ ([#3707](https://github.com/lerna/lerna/issues/3707)) ([647dbb5](https://github.com/lerna/lerna/commit/647dbb512bf6a873cab6362c434b420b18af0ad4))

- bump cosmiconfig to v8 ([#3701](https://github.com/lerna/lerna/issues/3701)) ([898923d](https://github.com/lerna/lerna/commit/898923d198319d76ed5e37e553bfe3b27e43604c))
- daemon communication ([e82618b](https://github.com/lerna/lerna/commit/e82618bf1a8aaea6d3d7f74f9afb8f1c5afe04ad))
- ensure repair command not blocked by config validation ([e237d58](https://github.com/lerna/lerna/commit/e237d585e63a2fb502a8958f15a1efe03d781c57))
- improve github client missing env var error ([ce4b352](https://github.com/lerna/lerna/commit/ce4b35213200b07e89a6ab09743f84aa60e5ca2c))
- internal cli.js should not be bundled ([53d73c6](https://github.com/lerna/lerna/commit/53d73c6aa9833e5a5bf60c2c78896456e77fab40))
- migration building/publishing issues ([27bf800](https://github.com/lerna/lerna/commit/27bf800b6e7670ea1ec5576fdf008e8d09897d4b))
- **publish:** use correct version in log messages ([#3702](https://github.com/lerna/lerna/issues/3702)) ([4be9188](https://github.com/lerna/lerna/commit/4be9188e68c5d4c320c0946e6e386cbee95a8efe))
- share project data when nesting commands ([#3709](https://github.com/lerna/lerna/issues/3709)) ([53e71e4](https://github.com/lerna/lerna/commit/53e71e4746604aa3fbaada3dad58b02904769759))
- support nx 16.3.1+ ([#3707](https://github.com/lerna/lerna/issues/3707)) ([647dbb5](https://github.com/lerna/lerna/commit/647dbb512bf6a873cab6362c434b420b18af0ad4))

### Features

* add migration for adding $schema, increase some strictness ([73ceac3](https://github.com/lerna/lerna/commit/73ceac3dc2cf0e1246d4433cb101d1e794b2cca3))
* **publish:** support custom directory per-package ([#3699](https://github.com/lerna/lerna/issues/3699)) ([9da575e](https://github.com/lerna/lerna/commit/9da575e9da221b8be4fbaa2fb1e7676b54d86d4f))




- add migration for adding $schema, increase some strictness ([73ceac3](https://github.com/lerna/lerna/commit/73ceac3dc2cf0e1246d4433cb101d1e794b2cca3))
- **publish:** support custom directory per-package ([#3699](https://github.com/lerna/lerna/issues/3699)) ([9da575e](https://github.com/lerna/lerna/commit/9da575e9da221b8be4fbaa2fb1e7676b54d86d4f))

# [7.0.0-alpha.6](https://github.com/lerna/lerna/compare/7.0.0-alpha.0...7.0.0-alpha.6) (2023-06-02)

Expand Down
381 changes: 353 additions & 28 deletions e2e/publish/src/custom-publish-directories.spec.ts

Large diffs are not rendered by default.

93 changes: 83 additions & 10 deletions libs/commands/publish/src/index.ts
@@ -1,4 +1,5 @@
import {
AssetDefinition,
collectProjectUpdates,
Command,
CommandConfigOptions,
Expand Down Expand Up @@ -46,6 +47,7 @@ import { getProjectsWithTaggedPackages } from "./lib/get-projects-with-tagged-pa
import { getProjectsWithUnpublishedPackages } from "./lib/get-projects-with-unpublished-packages";
import { getTwoFactorAuthRequired } from "./lib/get-two-factor-auth-required";
import { gitCheckout } from "./lib/git-checkout";
import { interpolate } from "./lib/interpolate";
import { removeTempLicenses } from "./lib/remove-temp-licenses";
import { verifyNpmPackageAccess } from "./lib/verify-npm-package-access";

Expand Down Expand Up @@ -288,17 +290,72 @@ class PublishCommand extends Command {
this.updates = this.filterPrivatePkgUpdates(result.updates);
this.updatesVersions = new Map(result.updatesVersions);

this.packagesToPublish = this.updates.map((node) => getPackage(node));
function interpolateAsset(asset: AssetDefinition, interpolationFn: (str: string) => string) {
if (typeof asset === "string") {
return interpolationFn(asset);
}
if (asset.from) {
asset.from = interpolationFn(asset.from);
}
if (asset.to) {
asset.to = interpolationFn(asset.to);
}
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);

// override directory to publish
for (const pkg of this.packagesToPublish) {
if (this.options.contents) {
pkg.contents = this.options.contents;
}
if (pkg.lernaConfig?.publish?.directory) {
pkg.contents = pkg.lernaConfig.publish?.directory;

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;
});

if (result.needsConfirmation) {
// only confirm for --canary, bump === "from-git",
Expand Down Expand Up @@ -1045,7 +1102,10 @@ class PublishCommand extends Command {
// no need to copy assets if publishing from the source location
return;
}
const assets = pkg.lernaConfig?.publish?.assets || ["package.json", "README.md"];

const _workspaceRoot = process.env["NX_WORKSPACE_ROOT_PATH"] || workspaceRoot;

const assets = pkg.lernaConfig?.command?.publish?.assets || ["package.json", "README.md"];
const filesToCopy: {
from: string;
to: string;
Expand Down Expand Up @@ -1091,12 +1151,25 @@ class PublishCommand extends Command {

for (const file of filesToCopy) {
if (normalize(file.from) === normalize(file.to)) {
this.logger.warn("EPUBLISHASSET", "Asset %s is already in package directory", file.from);
this.logger.warn(
"EPUBLISHASSET",
"Asset %s is already in package directory",
file.from.replace(`${_workspaceRoot}/`, "")
);
} else if (existsSync(file.from)) {
this.logger.verbose("publish", "Copying asset %s to %s", file.from, file.to);
this.logger.verbose(
"publish",
"Copying asset %s to %s",
file.from.replace(`${_workspaceRoot}/`, ""),
file.to.replace(`${_workspaceRoot}/`, "")
);
await copy(file.from, file.to);
} else {
this.logger.warn("EPUBLISHASSET", "Asset %s does not exist", file.from);
this.logger.warn(
"EPUBLISHASSET",
"Asset %s does not exist",
file.from.replace(`${_workspaceRoot}/`, "")
);
}
}
}
Expand Down
36 changes: 36 additions & 0 deletions libs/commands/publish/src/lib/interpolate.ts
@@ -0,0 +1,36 @@
import { workspaceRoot } from "@nx/devkit";

// Modified from https://github.com/nrwl/nx/blob/ef63dcb69b4f7806a2e47174804f59a19a6b3919/packages/nx/src/tasks-runner/utils.ts#L197
export function interpolate(template: string, data: any): string {
const _workspaceRoot = process.env["NX_WORKSPACE_ROOT_PATH"] || workspaceRoot;

if (template.includes("{workspaceRoot}", 1)) {
throw new Error(
`Config '${template}' is invalid. {workspaceRoot} can only be used at the beginning of the expression.`
);
}

if (data.projectRoot == "." && template.includes("{projectRoot}", 1)) {
throw new Error(
`Config '${template}' is invalid. When {projectRoot} is '.', it can only be used at the beginning of the expression.`
);
}

let res = template.replace("{workspaceRoot}", _workspaceRoot);

if (data.projectRoot == ".") {
res = res.replace("{projectRoot}/", "");
}

return res.replace(/{([\s\S]+?)}/g, (match: string) => {
let value = data;
const path = match.slice(1, -1).trim().split(".");
for (let idx = 0; idx < path.length; idx++) {
if (!value[path[idx]]) {
return match;
}
value = value[path[idx]];
}
return value;
});
}
20 changes: 10 additions & 10 deletions libs/core/src/index.ts
Expand Up @@ -3,40 +3,40 @@ export { addDependents } from "./lib/add-dependents";
export { checkWorkingTree, throwIfUncommitted } from "./lib/check-working-tree";
export * from "./lib/cli";
export {
collectProjects,
collectProjectUpdates,
ProjectCollectorOptions,
ProjectUpdateCollectorOptions,
collectProjectUpdates,
collectProjects,
} from "./lib/collect-updates";
export { Command, PreInitializedProjectData } from "./lib/command";
export { isGitInitialized } from "./lib/command/is-git-initialized";
export { applyBuildMetadata, recommendVersion, updateChangelog } from "./lib/conventional-commits";
export { describeRef, describeRefSync } from "./lib/describe-ref";
export { filterOptions, FilterOptions } from "./lib/filter-options";
export { FilterOptions, filterOptions } from "./lib/filter-options";
export { filterProjects } from "./lib/filter-projects";
export { getPackageManifestPath } from "./lib/get-package-manifest-path";
export * from "./lib/get-packages-for-option";
export { hasNpmVersion } from "./lib/has-npm-version";
export { listableFormatProjects } from "./lib/listable-format-projects";
export { listableOptions, ListableOptions } from "./lib/listable-options";
export { ListableOptions, listableOptions } from "./lib/listable-options";
export { logPacked } from "./lib/log-packed";
export { Conf } from "./lib/npm-conf/conf";
export { npmInstall, npmInstallDependencies } from "./lib/npm-install";
export { npmPublish } from "./lib/npm-publish";
export { npmRunScript, npmRunScriptStreaming } from "./lib/npm-run-script";
export { getOneTimePassword } from "./lib/otplease";
export { output } from "./lib/output";
export { packDirectory, Packed } from "./lib/pack-directory";
export { ExtendedNpaResult, Package, RawManifest } from "./lib/package";
export { Packed, packDirectory } from "./lib/pack-directory";
export { AssetDefinition, ExtendedNpaResult, Package, RawManifest } from "./lib/package";
export { prereleaseIdFromVersion } from "./lib/prerelease-id-from-version";
export { generateProfileOutputPath, Profiler } from "./lib/profiler";
export { CommandConfigOptions, Project, getPackages, LernaConfig } from "./lib/project";
export { Profiler, generateProfileOutputPath } from "./lib/profiler";
export { CommandConfigOptions, LernaConfig, Project, getPackages } from "./lib/project";
export {
getPackage,
isExternalNpmDependency,
ProjectGraphProjectNodeWithPackage,
ProjectGraphWithPackages,
ProjectGraphWorkspacePackageDependency,
getPackage,
isExternalNpmDependency,
} from "./lib/project-graph-with-packages";
export { promptConfirmation, promptSelectOne, promptTextInput } from "./lib/prompt";
export { pulseTillDone } from "./lib/pulse-till-done";
Expand Down
22 changes: 19 additions & 3 deletions libs/core/src/lib/package.ts
@@ -1,3 +1,4 @@
import { workspaceRoot } from "@nx/devkit";
import loadJsonFile from "load-json-file";
import npa from "npm-package-arg";
import path from "path";
Expand Down Expand Up @@ -41,10 +42,15 @@ function shallowCopy(json: any) {
}, {});
}

export type AssetDefinition = string | { from: string; to: string };

// Only a small subset of lerna configuration can be specified on a package's own package.json
export interface RawManifestLernaConfig {
publish?: {
directory?: string;
assets?: (string | { from: string; to: string })[];
command?: {
publish?: {
directory?: string;
assets?: AssetDefinition[];
};
};
}

Expand Down Expand Up @@ -153,6 +159,10 @@ export class Package {
return this[PKG].lerna;
}

set lernaConfig(config: RawManifestLernaConfig | undefined) {
this[PKG].lerna = config;
}

get bin() {
const pkg = this[PKG];
return typeof pkg.bin === "string"
Expand Down Expand Up @@ -208,6 +218,12 @@ export class Package {
}

set contents(subDirectory) {
// If absolute path starting with workspace root, set it directly, otherwise join with package location (historical behavior)
const _workspaceRoot = process.env["NX_WORKSPACE_ROOT_PATH"] || workspaceRoot;
if (subDirectory.startsWith(_workspaceRoot)) {
this[_contents] = subDirectory;
return;
}
this[_contents] = path.join(this.location, subDirectory);
}

Expand Down
4 changes: 3 additions & 1 deletion libs/core/src/lib/project/index.ts
Expand Up @@ -382,7 +382,9 @@ export class Project {
throw new ValidationError(
"EWORKSPACES",
dedent`
Lerna is expecting to able to resolve the "workspaces" configuration from your package manager in order to determine what packages to work on, but no "workspaces" config was found. Did you mean to specify a "packages" config manually in lerna.json instead?
Lerna is expecting to able to resolve the "workspaces" configuration from your package manager in order to determine what packages to work on, but no "workspaces" config was found.
(A) Did you mean to specify a "packages" config manually in lerna.json instead of using your workspaces config?
(B) Alternatively, if you are using pnpm as your package manager, make sure you set "npmClient": "pnpm" in your lerna.json so that lerna knows to read from the "pnpm-workspace.yaml" file instead of package.json.
See: https://lerna.js.org/docs/getting-started
`
);
Expand Down
11 changes: 3 additions & 8 deletions packages/child-process/CHANGELOG.md
Expand Up @@ -5,16 +5,11 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline

# [7.0.0-alpha.7](https://github.com/lerna/lerna/compare/7.0.0-alpha.0...7.0.0-alpha.7) (2023-06-05)


### Bug Fixes

* migration building/publishing issues ([27bf800](https://github.com/lerna/lerna/commit/27bf800b6e7670ea1ec5576fdf008e8d09897d4b))
* **publish:** use correct version in log messages ([#3702](https://github.com/lerna/lerna/issues/3702)) ([4be9188](https://github.com/lerna/lerna/commit/4be9188e68c5d4c320c0946e6e386cbee95a8efe))
* support nx 16.3.1+ ([#3707](https://github.com/lerna/lerna/issues/3707)) ([647dbb5](https://github.com/lerna/lerna/commit/647dbb512bf6a873cab6362c434b420b18af0ad4))




- migration building/publishing issues ([27bf800](https://github.com/lerna/lerna/commit/27bf800b6e7670ea1ec5576fdf008e8d09897d4b))
- **publish:** use correct version in log messages ([#3702](https://github.com/lerna/lerna/issues/3702)) ([4be9188](https://github.com/lerna/lerna/commit/4be9188e68c5d4c320c0946e6e386cbee95a8efe))
- support nx 16.3.1+ ([#3707](https://github.com/lerna/lerna/issues/3707)) ([647dbb5](https://github.com/lerna/lerna/commit/647dbb512bf6a873cab6362c434b420b18af0ad4))

# [7.0.0-alpha.6](https://github.com/lerna/lerna/compare/7.0.0-alpha.0...7.0.0-alpha.6) (2023-06-02)

Expand Down
13 changes: 4 additions & 9 deletions packages/legacy-package-management/CHANGELOG.md
Expand Up @@ -5,17 +5,12 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline

# [7.0.0-alpha.7](https://github.com/lerna/lerna/compare/7.0.0-alpha.0...7.0.0-alpha.7) (2023-06-05)


### Bug Fixes

* bump cosmiconfig to v8 ([#3701](https://github.com/lerna/lerna/issues/3701)) ([898923d](https://github.com/lerna/lerna/commit/898923d198319d76ed5e37e553bfe3b27e43604c))
* migration building/publishing issues ([27bf800](https://github.com/lerna/lerna/commit/27bf800b6e7670ea1ec5576fdf008e8d09897d4b))
* **publish:** use correct version in log messages ([#3702](https://github.com/lerna/lerna/issues/3702)) ([4be9188](https://github.com/lerna/lerna/commit/4be9188e68c5d4c320c0946e6e386cbee95a8efe))
* support nx 16.3.1+ ([#3707](https://github.com/lerna/lerna/issues/3707)) ([647dbb5](https://github.com/lerna/lerna/commit/647dbb512bf6a873cab6362c434b420b18af0ad4))




- bump cosmiconfig to v8 ([#3701](https://github.com/lerna/lerna/issues/3701)) ([898923d](https://github.com/lerna/lerna/commit/898923d198319d76ed5e37e553bfe3b27e43604c))
- migration building/publishing issues ([27bf800](https://github.com/lerna/lerna/commit/27bf800b6e7670ea1ec5576fdf008e8d09897d4b))
- **publish:** use correct version in log messages ([#3702](https://github.com/lerna/lerna/issues/3702)) ([4be9188](https://github.com/lerna/lerna/commit/4be9188e68c5d4c320c0946e6e386cbee95a8efe))
- support nx 16.3.1+ ([#3707](https://github.com/lerna/lerna/issues/3707)) ([647dbb5](https://github.com/lerna/lerna/commit/647dbb512bf6a873cab6362c434b420b18af0ad4))

# [7.0.0-alpha.6](https://github.com/lerna/lerna/compare/7.0.0-alpha.0...7.0.0-alpha.6) (2023-06-02)

Expand Down
11 changes: 3 additions & 8 deletions packages/legacy-structure/commands/create/CHANGELOG.md
Expand Up @@ -5,16 +5,11 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline

# [7.0.0-alpha.7](https://github.com/lerna/lerna/compare/7.0.0-alpha.0...7.0.0-alpha.7) (2023-06-05)


### Bug Fixes

* migration building/publishing issues ([27bf800](https://github.com/lerna/lerna/commit/27bf800b6e7670ea1ec5576fdf008e8d09897d4b))
* **publish:** use correct version in log messages ([#3702](https://github.com/lerna/lerna/issues/3702)) ([4be9188](https://github.com/lerna/lerna/commit/4be9188e68c5d4c320c0946e6e386cbee95a8efe))
* support nx 16.3.1+ ([#3707](https://github.com/lerna/lerna/issues/3707)) ([647dbb5](https://github.com/lerna/lerna/commit/647dbb512bf6a873cab6362c434b420b18af0ad4))




- migration building/publishing issues ([27bf800](https://github.com/lerna/lerna/commit/27bf800b6e7670ea1ec5576fdf008e8d09897d4b))
- **publish:** use correct version in log messages ([#3702](https://github.com/lerna/lerna/issues/3702)) ([4be9188](https://github.com/lerna/lerna/commit/4be9188e68c5d4c320c0946e6e386cbee95a8efe))
- support nx 16.3.1+ ([#3707](https://github.com/lerna/lerna/issues/3707)) ([647dbb5](https://github.com/lerna/lerna/commit/647dbb512bf6a873cab6362c434b420b18af0ad4))

# [7.0.0-alpha.6](https://github.com/lerna/lerna/compare/7.0.0-alpha.0...7.0.0-alpha.6) (2023-06-02)

Expand Down

0 comments on commit e7e0489

Please sign in to comment.