Skip to content

Commit

Permalink
fix(watch): remove npm scope from project names sent to nx
Browse files Browse the repository at this point in the history
  • Loading branch information
fahslaj committed Jan 3, 2023
1 parent db3de78 commit 9ac0d62
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
14 changes: 13 additions & 1 deletion core/lerna/commands/watch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ const { watch } = require("nx/src/command-line/watch");

module.exports = factory;

const getNxProjectNamesFromLernaPackageNames = (packageNames) => {
const { readNxJson } = require("nx/src/config/configuration");
const nxJson = readNxJson();
const nxConfiguredNpmScope = nxJson.npmScope;

return nxConfiguredNpmScope
? packageNames.map((name) => name.replace(`@${nxConfiguredNpmScope}/`, ""))
: packageNames;
};

function factory(argv) {
return new WatchCommand(argv);
}
Expand Down Expand Up @@ -38,12 +48,14 @@ class WatchCommand extends Command {
this.packagePlural
);

const projectNames = getNxProjectNamesFromLernaPackageNames(this.filteredPackages.map((p) => p.name));

await watch({
command: this.options.command,
projectNameEnvName: "LERNA_PACKAGE_NAME",
fileChangesEnvName: "LERNA_FILE_CHANGES",
includeDependentProjects: false, // dependent projects are accounted for via lerna filter options
projects: this.filteredPackages.map((p) => p.name),
projects: projectNames,
verbose: this.options.verbose,
});
}
Expand Down
18 changes: 9 additions & 9 deletions e2e/watch/src/watch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe("lerna-watch", () => {
lerna verb rootPath /tmp/lerna-e2e/lerna-watch/lerna-workspace
lerna info watch Executing command "echo watch triggered" on changes in 3 packages.
> NX running with args: {"command":"echo watch triggered","projectNameEnvName":"LERNA_PACKAGE_NAME","fileChangesEnvName":"LERNA_FILE_CHANGES","includeDependentProjects":false,"projects":["package-a","package-b","package-c"],"verbose":true}
> NX running with args: {"command":"echo watch triggered","projectNameEnvName":"LERNA_PACKAGE_NAME","fileChangesEnvName":"LERNA_FILE_CHANGES","includeDependentProjects":false,"projects":["package-a","package-b","@scope/package-c"],"verbose":true}
> NX starting watch process
Expand Down Expand Up @@ -98,9 +98,9 @@ describe("lerna-watch", () => {
});

describe("with --scope", () => {
it("should only watch only specified packages", async () => {
it("should watch only specified packages", async () => {
const getWatchResult = await fixture.lernaWatch(
'--scope="package-a" --scope="package-c" -- "echo watch triggered"'
'--scope="package-a" --scope="@scope/package-c" -- "echo watch triggered"'
);

await createFile(fixture.getWorkspacePath("packages/package-a/my-file.txt"));
Expand All @@ -117,11 +117,11 @@ describe("lerna-watch", () => {
expect(output.combinedOutput).toMatchInlineSnapshot(`
lerna notice cli v999.9.9-e2e.0
lerna verb rootPath /tmp/lerna-e2e/lerna-watch/lerna-workspace
lerna notice filter including ["package-a","package-c"]
lerna info filter [ 'package-a', 'package-c' ]
lerna notice filter including ["package-a","@scope/package-c"]
lerna info filter [ 'package-a', '@scope/package-c' ]
lerna info watch Executing command "echo watch triggered" on changes in 2 packages.
> NX running with args: {"command":"echo watch triggered","projectNameEnvName":"LERNA_PACKAGE_NAME","fileChangesEnvName":"LERNA_FILE_CHANGES","includeDependentProjects":false,"projects":["package-a","package-c"],"verbose":true}
> NX running with args: {"command":"echo watch triggered","projectNameEnvName":"LERNA_PACKAGE_NAME","fileChangesEnvName":"LERNA_FILE_CHANGES","includeDependentProjects":false,"projects":["package-a","@scope/package-c"],"verbose":true}
> NX starting watch process
Expand Down Expand Up @@ -242,7 +242,7 @@ describe("lerna-watch", () => {
lerna verb rootPath /tmp/lerna-e2e/lerna-watch/lerna-workspace
lerna info watch Executing command "echo $LERNA_PACKAGE_NAME: $LERNA_FILE_CHANGES" on changes in 3 packages.
> NX running with args: {"command":"echo $LERNA_PACKAGE_NAME: $LERNA_FILE_CHANGES","projectNameEnvName":"LERNA_PACKAGE_NAME","fileChangesEnvName":"LERNA_FILE_CHANGES","includeDependentProjects":false,"projects":["package-a","package-b","package-c"],"verbose":true}
> NX running with args: {"command":"echo $LERNA_PACKAGE_NAME: $LERNA_FILE_CHANGES","projectNameEnvName":"LERNA_PACKAGE_NAME","fileChangesEnvName":"LERNA_FILE_CHANGES","includeDependentProjects":false,"projects":["package-a","package-b","@scope/package-c"],"verbose":true}
> NX starting watch process
Expand Down Expand Up @@ -271,9 +271,9 @@ describe("lerna-watch", () => {
> NX no more commands to process
> NX about to run commands with these environments: [{"LERNA_PACKAGE_NAME":"package-c","LERNA_FILE_CHANGES":"packages/package-c/my-file.txt"}]
> NX about to run commands with these environments: [{"LERNA_PACKAGE_NAME":"@scope/package-c","LERNA_FILE_CHANGES":"packages/package-c/my-file.txt"}]
package-c: packages/package-c/my-file.txt
@scope/package-c: packages/package-c/my-file.txt
> NX running complete, processing the next batch
Expand Down

0 comments on commit 9ac0d62

Please sign in to comment.