diff --git a/src/createSwEnvJsFile.ts b/src/createSwEnvJsFile.ts index 5243443..139e010 100644 --- a/src/createSwEnvJsFile.ts +++ b/src/createSwEnvJsFile.ts @@ -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; @@ -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)); + + // 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" + ); + } } diff --git a/src/vite-plugin.ts b/src/vite-plugin.ts index 6fa21f0..3b80e22 100644 --- a/src/vite-plugin.ts +++ b/src/vite-plugin.ts @@ -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");