Skip to content

Commit 6cd3b45

Browse files
fix(use-env-prefix): consider that env file does not exist (#119)
1 parent 139befe commit 6cd3b45

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

lib/rules/use-env-prefix.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,20 @@ export = createRule<[Options], MessageIds>({
5151
create(context) {
5252
const path = getPathFromProjectRoot(context.getFilename(), process.cwd());
5353

54-
const pathsForBrowserfileOption = context.options[0]
55-
?.pathsForBrowserfile || ["src/**/*"];
56-
const envPathOption = context.options[0]?.envPath || ".env";
54+
const options = {
55+
pathsForBrowserfile: context.options[0]?.pathsForBrowserfile || [
56+
"src/**/*",
57+
],
58+
envPath: context.options[0]?.envPath || ".env",
59+
};
5760

58-
const isClientfile = pathsForBrowserfileOption.some((clientPath) =>
61+
const isClientfile = options.pathsForBrowserfile.some((clientPath) =>
5962
minimatch(path, clientPath)
6063
);
6164

62-
const envSource = Fs.readFileSync(envPathOption, { encoding: "utf8" });
65+
if (!Fs.existsSync(options.envPath)) return {};
66+
const envSource = Fs.readFileSync(options.envPath, { encoding: "utf8" });
67+
6368
const parsedEnvSource = new Env(envSource).parse();
6469

6570
return {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module.exports = {
2+
plugins: [
3+
{
4+
use: "@gridsome/source-plugin",
5+
options: {
6+
username: "user",
7+
password: "password",
8+
},
9+
},
10+
],
11+
};

tests/lib/rules/use-env-prefix.spec.ts

+15
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,21 @@ tester.run("use-env-prefix", rule, {
4444
},
4545
],
4646
},
47+
{
48+
filename: path.join(process.cwd(), "src/client.js"),
49+
...loadFixture({
50+
fixtureDirectory: "valid/03",
51+
filenames: {
52+
code: "client.js",
53+
},
54+
}),
55+
options: [
56+
{
57+
pathsForBrowserfile: ["src/**/*"],
58+
envPath: "tests/lib/rules/fixtures/use-env-prefix/valid/03/.env",
59+
},
60+
],
61+
},
4762
],
4863
invalid: [
4964
{

0 commit comments

Comments
 (0)