Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create temp directory for temporary files #45

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.