Skip to content

Commit

Permalink
Don't crash if no write permission on the swEnv.js file
Browse files Browse the repository at this point in the history
  • Loading branch information
garronej committed Sep 2, 2024
1 parent d3270f5 commit 2c5f3ec
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
18 changes: 17 additions & 1 deletion src/createSwEnvJsFile.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as fs from "fs";
import { nameOfTheGlobal } from "./constants";
import { join as pathJoin } from "path";
import { assert } from "tsafe/assert";
import { is } from "tsafe/is";

type Params = {
distDirPath: string;
Expand Down Expand Up @@ -41,5 +43,19 @@ export function createSwEnvJsFile(params: Params) {
`self.${nameOfTheGlobal} = env;`
].join("\n");

fs.writeFileSync(pathJoin(distDirPath, "swEnv.js"), Buffer.from(swEnv, "utf8"));
try {
fs.writeFileSync(pathJoin(distDirPath, "swEnv.js"), Buffer.from(swEnv, "utf8"));
} catch (error) {
assert(is<Error>(error));

// Test if permission error

if (!error.message.includes("permission denied")) {
throw error;
}

console.log(
"Not enough permissions to update the swEnv.js file. It's not an issue if you don't explicitly use it"
);
}
}
2 changes: 1 addition & 1 deletion src/vite-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ export function viteEnvs(params?: {
`self.${nameOfTheGlobal} = env;`,
`"`,
``,
`echo "$swEnv_script" > "$DIR/swEnv.js"`,
`echo "$swEnv_script" > "$DIR/swEnv.js" || echo "Not enough permissions to write to $DIR/swEnv.js"`,
``
].join("\n");

Expand Down

0 comments on commit 2c5f3ec

Please sign in to comment.