Skip to content

Commit

Permalink
fix(run): allow for loading of env files to be skipped
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesHenry committed Oct 14, 2022
1 parent fd0befa commit 42943bd
Show file tree
Hide file tree
Showing 6 changed files with 227 additions and 56 deletions.
5 changes: 5 additions & 0 deletions commands/run/command.js
Expand Up @@ -78,6 +78,11 @@ exports.builder = (yargs) => {
describe: "When useNx is not false, show verbose output from dependent tasks.",
type: "boolean",
},
"load-env-files": {
group: "Command Options:",
describe: "When useNx is not false, automatically load .env files",
type: "boolean",
},
});

return filterOptions(yargs);
Expand Down
1 change: 1 addition & 0 deletions commands/run/index.js
Expand Up @@ -311,6 +311,7 @@ class RunCommand extends Command {

const extraOptions = {
excludeTaskDependencies: mimicLernaDefaultBehavior,
loadDotEnvFiles: this.options.loadEnvFiles ?? true,
};

return { targetDependencies, options, extraOptions };
Expand Down
2 changes: 1 addition & 1 deletion core/lerna/package.json
Expand Up @@ -63,7 +63,7 @@
"import-local": "^3.0.2",
"inquirer": "^8.2.4",
"npmlog": "^6.0.2",
"nx": ">=14.8.1 < 16",
"nx": ">=14.8.6 < 16",
"typescript": "^3 || ^4"
}
}
122 changes: 122 additions & 0 deletions e2e/tests/lerna-run/lerna-run-nx-env-files.ts
@@ -0,0 +1,122 @@
import { remove, writeFile } from "fs-extra";
import { Fixture } from "../../utils/fixture";
import { normalizeCommandOutput, normalizeEnvironment } from "../../utils/snapshot-serializer-utils";

expect.addSnapshotSerializer({
serialize(str: string) {
return normalizeCommandOutput(normalizeEnvironment(str));
},
test(val: string) {
return val != null && typeof val === "string";
},
});

describe("lerna-run-nx-env-files", () => {
let fixture: Fixture;

beforeEach(async () => {
fixture = await Fixture.create({
name: "lerna-run-nx-env-files",
packageManager: "npm",
initializeGit: true,
runLernaInit: true,
installDependencies: true,
/**
* Because lerna run involves spawning further child processes, the tests would be too flaky
* if we didn't force deterministic terminal output by appending stderr to stdout instead
* of interleaving them.
*/
forceDeterministicTerminalOutput: true,
});

await writeFile(fixture.getWorkspacePath(".env"), `SOMETHING_IN_ENV_FILE=some_value_here`, "utf-8");

await fixture.lerna("create package-1 -y");
await fixture.addScriptsToPackage({
packagePath: "packages/package-1",
scripts: {
"log-env-var": "echo $SOMETHING_IN_ENV_FILE",
},
});
});
afterEach(() => fixture.destroy());

describe("without nx enabled", () => {
it("should log an empty value", async () => {
// Enable legacy task runner
await fixture.overrideLernaConfig({
useNx: false,
});

const output = await fixture.lerna("run log-env-var -- --silent");

expect(output.combinedOutput).toMatchInlineSnapshot(`
lerna notice cli v999.9.9-e2e.0
lerna info Executing command in 1 package: "npm run log-env-var --silent"
lerna info run Ran npm script 'log-env-var' in 'package-X' in X.Xs:
lerna success run Ran npm script 'log-env-var' in 1 package in X.Xs:
lerna success - package-X
`);
});
});

describe("with nx enabled", () => {
it("should log a value by default", async () => {
await fixture.addNxJsonToWorkspace();

const output = await fixture.lerna("run log-env-var -- --silent");

expect(output.combinedOutput).toMatchInlineSnapshot(`
> Lerna (powered by Nx) Nx didn't recognize the following args: loadDotEnvFiles
When using '--' all executor args have to be defined after '--'.
> package-X:log-env-var --silent
> package-X@0.0.0 log-env-var
> echo $SOMETHING_IN_ENV_FILE "--silent"
some_value_here --silent
> Lerna (powered by Nx) Successfully ran target log-env-var for project package-X
lerna notice cli v999.9.9-e2e.0
`);
});

it("should log an empty value when --load-env-files=false", async () => {
await fixture.addNxJsonToWorkspace();

const output = await fixture.lerna("run log-env-var --load-env-files=false -- --silent");

expect(output.combinedOutput).toMatchInlineSnapshot(`
> Lerna (powered by Nx) Nx didn't recognize the following args: loadDotEnvFiles
When using '--' all executor args have to be defined after '--'.
> package-X:log-env-var --silent
> package-X@0.0.0 log-env-var
> echo $SOMETHING_IN_ENV_FILE "--silent"
--silent
> Lerna (powered by Nx) Successfully ran target log-env-var for project package-X
lerna notice cli v999.9.9-e2e.0
`);
});
});
});

0 comments on commit 42943bd

Please sign in to comment.