Skip to content

Commit

Permalink
feat(core): replace dotenv with dotenv-expand (#18622)
Browse files Browse the repository at this point in the history
Co-authored-by: FrozenPandaz <[email protected]>
  • Loading branch information
xiongemi and FrozenPandaz authored Aug 23, 2023
1 parent f5d55e3 commit 6a09105
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 4 deletions.
47 changes: 47 additions & 0 deletions e2e/nx-misc/src/extras.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,4 +273,51 @@ describe('Extra Nx Misc Tests', () => {
expect(output).not.toContain('Installed');
});
});

describe('Env File', () => {
it('should have the right env', () => {
const appName = uniq('app');
runCLI(
`generate @nx/react:app ${appName} --style=css --bundler=webpack --no-interactive`
);
updateFile(
'.env',
`FIRSTNAME="firstname"
LASTNAME="lastname"
NX_USERNAME=$FIRSTNAME $LASTNAME`
);
updateFile(
`apps/${appName}/src/app/app.tsx`,
`
import NxWelcome from './nx-welcome';
export function App() {
return (
<>
<NxWelcome title={process.env.NX_USERNAME} />
</>
);
}
export default App;
`
);
updateFile(
`apps/${appName}/src/app/app.spec.tsx`,
`import { render } from '@testing-library/react';
import App from './app';
describe('App', () => {
it('should have a greeting as the title', () => {
const { getByText } = render(<App />);
expect(getByText(/Welcome firstname lastname/gi)).toBeTruthy();
});
});
`
);
const unitTestsOutput = runCLI(`test ${appName}`);
expect(unitTestsOutput).toContain('Successfully ran target test');
});
});
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@
"czg": "^1.4.0",
"detect-port": "^1.5.1",
"dotenv": "~16.3.1",
"dotenv-expand": "^10.0.0",
"ejs": "^3.1.7",
"enhanced-resolve": "^5.8.3",
"esbuild": "^0.17.5",
Expand Down
4 changes: 3 additions & 1 deletion packages/nx/bin/nx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
} from '../src/utils/find-workspace-root';
import * as chalk from 'chalk';
import { config as loadDotEnvFile } from 'dotenv';
import { expand } from 'dotenv-expand';
import { initLocal } from './init-local';
import { output } from '../src/utils/output';
import {
Expand Down Expand Up @@ -109,9 +110,10 @@ function main() {
*/
function loadDotEnvFiles() {
for (const file of ['.local.env', '.env.local', '.env']) {
loadDotEnvFile({
const myEnv = loadDotEnvFile({
path: file,
});
expand(myEnv);
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/nx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"cli-spinners": "2.6.1",
"cliui": "^7.0.2",
"dotenv": "~16.3.1",
"dotenv-expand": "~10.0.0",
"enquirer": "~2.3.6",
"fast-glob": "3.2.7",
"figures": "3.2.0",
Expand Down
10 changes: 9 additions & 1 deletion packages/nx/src/tasks-runner/forked-process-task-runner.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { readFileSync, writeFileSync } from 'fs';
import { config as loadDotEnvFile } from 'dotenv';
import { expand } from 'dotenv-expand';
import { ChildProcess, fork, Serializable } from 'child_process';
import * as chalk from 'chalk';
import * as logTransformer from 'strong-log-transformer';
Expand Down Expand Up @@ -443,12 +444,19 @@ export class ForkedProcessTaskRunner {
];

for (const file of dotEnvFiles) {
loadDotEnvFile({
const myEnv = loadDotEnvFile({
path: file,
processEnv: environmentVariables,
// Do not override existing env variables as we load
override: false,
});
environmentVariables = {
...expand({
...myEnv,
ignoreProcessEnv: true, // Do not override existing env variables as we load
}).parsed,
...environmentVariables,
};
}

return environmentVariables;
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions tools/documentation/create-embeddings/src/main.mts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
// https://github.com/supabase-community/nextjs-openai-doc-search/blob/main/lib/generate-embeddings.ts

import { createClient } from '@supabase/supabase-js';
import * as dotenv from 'dotenv';
import { config as loadDotEnvFile } from 'dotenv';
import { expand } from 'dotenv-expand';
import { readFile } from 'fs/promises';
import 'openai';
import { Configuration, OpenAIApi } from 'openai';
Expand All @@ -24,7 +25,8 @@ import manifestsTags from '../../../../docs/generated/manifests/tags.json' asser

let identityMap = {};

dotenv.config();
const myEnv = loadDotEnvFile();
expand(myEnv);

type ProcessedMdx = {
checksum: string;
Expand Down

1 comment on commit 6a09105

@vercel
Copy link

@vercel vercel bot commented on 6a09105 Aug 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-five.vercel.app
nx-dev-nrwl.vercel.app
nx-dev-git-master-nrwl.vercel.app
nx.dev

Please sign in to comment.