Skip to content

Commit

Permalink
Create temp directory for temporary files
Browse files Browse the repository at this point in the history
  • Loading branch information
panther7 committed Nov 17, 2023
1 parent f9f5cc1 commit 2c32cdc
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
15 changes: 13 additions & 2 deletions src/config/constants.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
const { join } = require('path');
const fs = require('fs');
const os = require('os');

let _tmpdir;

const constants = {
platforms: {
Expand All @@ -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;
8 changes: 4 additions & 4 deletions src/linux/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
};
Expand Down
16 changes: 5 additions & 11 deletions src/macos/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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);
};
Expand Down
Empty file removed temp/.gitkeep
Empty file.

0 comments on commit 2c32cdc

Please sign in to comment.