From 6cd3b4586593a1e6e1abbed93d4cf52cf6eb0f33 Mon Sep 17 00:00:00 2001 From: tyankatsu Date: Sat, 13 Feb 2021 14:15:34 +0900 Subject: [PATCH] fix(use-env-prefix): consider that env file does not exist (#119) --- lib/rules/use-env-prefix.ts | 15 ++++++++++----- .../fixtures/use-env-prefix/valid/03/client.js | 11 +++++++++++ tests/lib/rules/use-env-prefix.spec.ts | 15 +++++++++++++++ 3 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 tests/lib/rules/fixtures/use-env-prefix/valid/03/client.js diff --git a/lib/rules/use-env-prefix.ts b/lib/rules/use-env-prefix.ts index c7dcd3f..19fb171 100644 --- a/lib/rules/use-env-prefix.ts +++ b/lib/rules/use-env-prefix.ts @@ -51,15 +51,20 @@ export = createRule<[Options], MessageIds>({ create(context) { const path = getPathFromProjectRoot(context.getFilename(), process.cwd()); - const pathsForBrowserfileOption = context.options[0] - ?.pathsForBrowserfile || ["src/**/*"]; - const envPathOption = context.options[0]?.envPath || ".env"; + const options = { + pathsForBrowserfile: context.options[0]?.pathsForBrowserfile || [ + "src/**/*", + ], + envPath: context.options[0]?.envPath || ".env", + }; - const isClientfile = pathsForBrowserfileOption.some((clientPath) => + const isClientfile = options.pathsForBrowserfile.some((clientPath) => minimatch(path, clientPath) ); - const envSource = Fs.readFileSync(envPathOption, { encoding: "utf8" }); + if (!Fs.existsSync(options.envPath)) return {}; + const envSource = Fs.readFileSync(options.envPath, { encoding: "utf8" }); + const parsedEnvSource = new Env(envSource).parse(); return { diff --git a/tests/lib/rules/fixtures/use-env-prefix/valid/03/client.js b/tests/lib/rules/fixtures/use-env-prefix/valid/03/client.js new file mode 100644 index 0000000..c88479c --- /dev/null +++ b/tests/lib/rules/fixtures/use-env-prefix/valid/03/client.js @@ -0,0 +1,11 @@ +module.exports = { + plugins: [ + { + use: "@gridsome/source-plugin", + options: { + username: "user", + password: "password", + }, + }, + ], +}; diff --git a/tests/lib/rules/use-env-prefix.spec.ts b/tests/lib/rules/use-env-prefix.spec.ts index 64de435..807cd2b 100644 --- a/tests/lib/rules/use-env-prefix.spec.ts +++ b/tests/lib/rules/use-env-prefix.spec.ts @@ -44,6 +44,21 @@ tester.run("use-env-prefix", rule, { }, ], }, + { + filename: path.join(process.cwd(), "src/client.js"), + ...loadFixture({ + fixtureDirectory: "valid/03", + filenames: { + code: "client.js", + }, + }), + options: [ + { + pathsForBrowserfile: ["src/**/*"], + envPath: "tests/lib/rules/fixtures/use-env-prefix/valid/03/.env", + }, + ], + }, ], invalid: [ {