Skip to content

Commit

Permalink
fix(publish): revert auto-copying of assets to custom contents/direct…
Browse files Browse the repository at this point in the history
…ory (#3732)
  • Loading branch information
JamesHenry authored Jun 15, 2023
1 parent 273ed54 commit 70d4438
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
14 changes: 6 additions & 8 deletions e2e/publish/src/custom-publish-directories.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ describe("lerna-publish-custom-publish-directories", () => {
await fixture.updateJson("packages/package-1/package.json", (pkg) => ({
...pkg,
main: "main.js",
scripts: { build: "cp ./lib/package-1.js ../../dist/packages/package-1/lib/main.js" },
scripts: {
// Case where user manually ensures package.json is present inn publish directory
build:
"cp ./lib/package-1.js ../../dist/packages/package-1/lib/main.js && cp ./package.json ../../dist/packages/package-1/package.json",
},
lerna: {
command: {
publish: { directory: "../../dist/packages/package-1" },
Expand Down Expand Up @@ -140,10 +144,6 @@ describe("lerna-publish-custom-publish-directories", () => {
lerna WARN ENOLICENSE One way to fix this is to add a LICENSE.md file to the root of this repository.
lerna WARN ENOLICENSE See https://choosealicense.com for additional guidance.
lerna verb getCurrentSHA {FULL_COMMIT_SHA}
lerna verb publish Expanded asset glob package.json into files ["package.json"]
lerna verb publish Expanded asset glob README.md into files ["README.md"]
lerna verb publish Copying asset packages/package-1/package.json to dist/packages/package-1/package.json
lerna verb publish Copying asset packages/package-1/README.md to dist/packages/package-1/README.md
lerna verb pack-directory dist/packages/package-1
lerna verb packed dist/packages/package-1
lerna verb publish Expanded asset glob package.json into files ["package.json"]
Expand All @@ -167,7 +167,6 @@ describe("lerna-publish-custom-publish-directories", () => {
lerna notice === Tarball Contents ===
lerna notice 99B lib/main.js
lerna notice XXXkb package.json
lerna notice 119B README.md
lerna notice === Tarball Details ===
lerna notice name: package-1
lerna notice version: XX.XX.XX
Expand All @@ -176,7 +175,7 @@ describe("lerna-publish-custom-publish-directories", () => {
lerna notice unpacked size: XXX.XXX kb
lerna notice shasum: {FULL_COMMIT_SHA}
lerna notice integrity: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
lerna notice total files: 3
lerna notice total files: 2
lerna notice
lerna verb publish package-2
lerna success published package-2 XX.XX.XX
Expand Down Expand Up @@ -229,7 +228,6 @@ describe("lerna-publish-custom-publish-directories", () => {
expect(files.sort().join("\n")).toMatchInlineSnapshot(`
packages
packages/package-1
packages/package-1/README.md
packages/package-1/lib
packages/package-1/lib/main.js
packages/package-1/package.json
Expand Down
7 changes: 6 additions & 1 deletion libs/commands/publish/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1099,14 +1099,19 @@ class PublishCommand extends Command {
}

private async copyAssets(pkg: Package) {
// Do not copy assets if no custom assets are defined (root level config will have been applied to the pkg by this point)
if (!pkg.lernaConfig?.command?.publish?.assets) {
return;
}

if (normalize(pkg.location) === normalize(pkg.contents)) {
// no need to copy assets if publishing from the source location
return;
}

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

const assets = pkg.lernaConfig?.command?.publish?.assets || ["package.json", "README.md"];
const assets = pkg.lernaConfig?.command?.publish?.assets;
const filesToCopy: {
from: string;
to: string;
Expand Down
12 changes: 7 additions & 5 deletions website/docs/concepts/configuring-published-files.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ This is only useful for publishing if the packages in your monorepo have a simpl

In v7, we introduced a more powerful, more focused `--directory` option for `lerna publish`. Please prefer that over the `--contents` option, which will likely be deprecated in future.

## `--directory"`
## `--directory`

In v7, we introduced a more powerful, more focused `--directory` option for `lerna publish`.

Expand Down Expand Up @@ -73,6 +73,11 @@ An example configuration for a package that publishes from a `dist/packages/foo`
}
```

:::info
You will need to make sure that your custom directory location contains a valid `package.json` which will be used for the registry publish. You could create this via a lifecycle script such as `prepare`, `prepublishOnly`, or `prepack` if you need more complex custom logic involved, or simply have it copied for you from the package's source automatically by configuring it as an asset. See the upcoming section on **Including Additional Assets in Published Packages** for full details.

:::

If you wanted to make one of your packages behave like a standard lerna package and publish from source, you could override its publish config like so:

```json
Expand All @@ -83,8 +88,7 @@ If you wanted to make one of your packages behave like a standard lerna package
"lerna": {
"command": {
"publish": {
"directory": ".",
"assets": []
"directory": "."
}
}
}
Expand All @@ -95,8 +99,6 @@ If you wanted to make one of your packages behave like a standard lerna package

Lerna can copy files from your source directory to the directory specified for publishing. Just as with the `directory` option, this can be configured in the `lerna.json` (including using dynamic placeholders within asset definitions), or within the `package.json` of a particular package.

By default, Lerna will copy the `README.md` and `package.json` files. If you decided to override the `assets` configuration, you must make sure to include `"package.json"` in your asset definitions.

Regardless of which file it is configured in, the `"assets"` property should be an array of glob patterns or objects with a `"from"` and `"to"` property. The `"from"` property should be a specific file or glob pattern that matches files in the source directory, and the `"to"` property is the path to copy the file to within the publish directory.

This example package builds its output to a root `dist/packages/bar` directory. Lerna is configured to copy additional files to this directory, then publish the contents of `dist/packages/bar` to npm.
Expand Down

0 comments on commit 70d4438

Please sign in to comment.