diff --git a/src/config/constants.js b/src/config/constants.js index c3d6249..ed73fac 100644 --- a/src/config/constants.js +++ b/src/config/constants.js @@ -1,4 +1,8 @@ const { join } = require('path'); +const fs = require('fs'); +const os = require('os'); + +let _tmpdir; const constants = { platforms: { @@ -12,13 +16,20 @@ const constants = { noProtoExitCode: 4 } }, - homedir: join(require('os').homedir(), '.protocol-registry'), + homedir: join(os.homedir(), '.protocol-registry'), urlArgument: { win32: '%1', win32InScript: '%~1%', linux: '%u', // eslint-disable-next-line quotes darwin: `" & this_URL & "` - } + }, + get tmpdir() { + if (!fs.existsSync(_tmpdir)) { + _tmpdir = fs.mkdtempSync(`${os.tmpdir()}/register-protocol-`); + } + return _tmpdir; + }, }; + module.exports = constants; diff --git a/src/linux/index.js b/src/linux/index.js index e47f611..129f9a8 100644 --- a/src/linux/index.js +++ b/src/linux/index.js @@ -72,10 +72,10 @@ const register = async (options, cb) => { } const desktopFileName = `${protocol}.desktop`; - const desktopFilePath = join(__dirname, '../../temp', desktopFileName); + const desktopFilePath = join(constants.tmpdir, desktopFileName); const desktopTemplate = join(__dirname, './templates', 'desktop.ejs'); const scriptTemplate = join(__dirname, './templates', 'script.ejs'); - const scriptFilePath = join(__dirname, '../../temp', 'script.sh'); + const scriptFilePath = join(constants.tmpdir, 'script.sh'); command = await preProcessCommands( protocol, @@ -116,11 +116,11 @@ const register = async (options, cb) => { }); if (scriptResult.code != 0 || scriptResult.stderr) throw new Error(scriptResult.stderr); - - fs.unlinkSync(scriptFilePath); } catch (e) { if (!cb) throw e; res = e; + } finally { + fs.rmSync(constants.tmpdir, { recursive: true, force: true }); } if (cb) return cb(res); }; diff --git a/src/macos/index.js b/src/macos/index.js index 1b368c3..2de150b 100644 --- a/src/macos/index.js +++ b/src/macos/index.js @@ -77,19 +77,15 @@ const register = async (options, cb) => { const plistMutator = join(__dirname, 'plistMutator.js'); const appTemplate = join(__dirname, './templates', 'app.ejs'); - const appSource = join(__dirname, '../../temp', `app-${protocol}.txt`); + const appSource = join(constants.tmpdir, `app-${protocol}.txt`); const appPath = join(homedir, `APP-${protocol}.app`); const urlAppTemplate = join(__dirname, './templates', 'url-app.ejs'); - const urlAppSource = join( - __dirname, - '../../temp', - `URL-${protocol}.txt` - ); + const urlAppSource = join(constants.tmpdir,`URL-${protocol}.txt`); const urlAppPath = join(homedir, `URL-${protocol}.app`); const scriptTemplate = join(__dirname, './templates', 'script.ejs'); - const scriptFilePath = join(__dirname, '../../temp', 'script.sh'); + const scriptFilePath = join(constants.tmpdir, 'script.sh'); const appSourceContent = await new Promise((resolve, reject) => { ejs.renderFile( @@ -142,13 +138,11 @@ const register = async (options, cb) => { }); if (scriptResult.code != 0 || scriptResult.stderr) throw new Error(scriptResult.stderr); - - fs.unlinkSync(scriptFilePath); - fs.unlinkSync(urlAppSource); - fs.unlinkSync(appSource); } catch (e) { if (!cb) throw e; res = e; + } finally { + fs.rmSync(constants.tmpdir, { recursive: true, force: true }); } if (cb) return cb(res); }; diff --git a/temp/.gitkeep b/temp/.gitkeep deleted file mode 100644 index e69de29..0000000