Skip to content

Commit

Permalink
fix(use-env-prefix): consider that env file does not exist (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
tyankatsu0105 authored Feb 13, 2021
1 parent 139befe commit 6cd3b45
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
15 changes: 10 additions & 5 deletions lib/rules/use-env-prefix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 11 additions & 0 deletions tests/lib/rules/fixtures/use-env-prefix/valid/03/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
plugins: [
{
use: "@gridsome/source-plugin",
options: {
username: "user",
password: "password",
},
},
],
};
15 changes: 15 additions & 0 deletions tests/lib/rules/use-env-prefix.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
{
Expand Down

0 comments on commit 6cd3b45

Please sign in to comment.