Skip to content

Commit 70d4438

Browse files
authored
fix(publish): revert auto-copying of assets to custom contents/directory (#3732)
1 parent 273ed54 commit 70d4438

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

e2e/publish/src/custom-publish-directories.spec.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ describe("lerna-publish-custom-publish-directories", () => {
4343
await fixture.updateJson("packages/package-1/package.json", (pkg) => ({
4444
...pkg,
4545
main: "main.js",
46-
scripts: { build: "cp ./lib/package-1.js ../../dist/packages/package-1/lib/main.js" },
46+
scripts: {
47+
// Case where user manually ensures package.json is present inn publish directory
48+
build:
49+
"cp ./lib/package-1.js ../../dist/packages/package-1/lib/main.js && cp ./package.json ../../dist/packages/package-1/package.json",
50+
},
4751
lerna: {
4852
command: {
4953
publish: { directory: "../../dist/packages/package-1" },
@@ -140,10 +144,6 @@ describe("lerna-publish-custom-publish-directories", () => {
140144
lerna WARN ENOLICENSE One way to fix this is to add a LICENSE.md file to the root of this repository.
141145
lerna WARN ENOLICENSE See https://choosealicense.com for additional guidance.
142146
lerna verb getCurrentSHA {FULL_COMMIT_SHA}
143-
lerna verb publish Expanded asset glob package.json into files ["package.json"]
144-
lerna verb publish Expanded asset glob README.md into files ["README.md"]
145-
lerna verb publish Copying asset packages/package-1/package.json to dist/packages/package-1/package.json
146-
lerna verb publish Copying asset packages/package-1/README.md to dist/packages/package-1/README.md
147147
lerna verb pack-directory dist/packages/package-1
148148
lerna verb packed dist/packages/package-1
149149
lerna verb publish Expanded asset glob package.json into files ["package.json"]
@@ -167,7 +167,6 @@ describe("lerna-publish-custom-publish-directories", () => {
167167
lerna notice === Tarball Contents ===
168168
lerna notice 99B lib/main.js
169169
lerna notice XXXkb package.json
170-
lerna notice 119B README.md
171170
lerna notice === Tarball Details ===
172171
lerna notice name: package-1
173172
lerna notice version: XX.XX.XX
@@ -176,7 +175,7 @@ describe("lerna-publish-custom-publish-directories", () => {
176175
lerna notice unpacked size: XXX.XXX kb
177176
lerna notice shasum: {FULL_COMMIT_SHA}
178177
lerna notice integrity: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
179-
lerna notice total files: 3
178+
lerna notice total files: 2
180179
lerna notice
181180
lerna verb publish package-2
182181
lerna success published package-2 XX.XX.XX
@@ -229,7 +228,6 @@ describe("lerna-publish-custom-publish-directories", () => {
229228
expect(files.sort().join("\n")).toMatchInlineSnapshot(`
230229
packages
231230
packages/package-1
232-
packages/package-1/README.md
233231
packages/package-1/lib
234232
packages/package-1/lib/main.js
235233
packages/package-1/package.json

libs/commands/publish/src/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1099,14 +1099,19 @@ class PublishCommand extends Command {
10991099
}
11001100

11011101
private async copyAssets(pkg: Package) {
1102+
// Do not copy assets if no custom assets are defined (root level config will have been applied to the pkg by this point)
1103+
if (!pkg.lernaConfig?.command?.publish?.assets) {
1104+
return;
1105+
}
1106+
11021107
if (normalize(pkg.location) === normalize(pkg.contents)) {
11031108
// no need to copy assets if publishing from the source location
11041109
return;
11051110
}
11061111

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

1109-
const assets = pkg.lernaConfig?.command?.publish?.assets || ["package.json", "README.md"];
1114+
const assets = pkg.lernaConfig?.command?.publish?.assets;
11101115
const filesToCopy: {
11111116
from: string;
11121117
to: string;

website/docs/concepts/configuring-published-files.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ This is only useful for publishing if the packages in your monorepo have a simpl
2222

2323
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.
2424

25-
## `--directory"`
25+
## `--directory`
2626

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

@@ -73,6 +73,11 @@ An example configuration for a package that publishes from a `dist/packages/foo`
7373
}
7474
```
7575

76+
:::info
77+
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.
78+
79+
:::
80+
7681
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:
7782

7883
```json
@@ -83,8 +88,7 @@ If you wanted to make one of your packages behave like a standard lerna package
8388
"lerna": {
8489
"command": {
8590
"publish": {
86-
"directory": ".",
87-
"assets": []
91+
"directory": "."
8892
}
8993
}
9094
}
@@ -95,8 +99,6 @@ If you wanted to make one of your packages behave like a standard lerna package
9599

96100
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.
97101

98-
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.
99-
100102
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.
101103

102104
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.

0 commit comments

Comments
 (0)