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 Oct 26, 2023
1 parent f9f5cc1 commit a5c93f4
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
8 changes: 6 additions & 2 deletions src/linux/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const ejs = require('ejs');
const os = require('os');
const fs = require('fs');
const { join } = require('path');
const shell = require('../utils/shell');
Expand Down Expand Up @@ -54,6 +55,7 @@ const checkifExists = async (protocol) => {

const register = async (options, cb) => {
let res = null;
const tmpDir = fs.mkdtempSync(`${os.tmpdir()}/register-protocol-`);
const validOptions = validator(registerSchema, options);
const {
protocol,
Expand All @@ -72,10 +74,10 @@ const register = async (options, cb) => {
}

const desktopFileName = `${protocol}.desktop`;
const desktopFilePath = join(__dirname, '../../temp', desktopFileName);
const desktopFilePath = join(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(tmpDir, 'script.sh');

command = await preProcessCommands(
protocol,
Expand Down Expand Up @@ -121,6 +123,8 @@ const register = async (options, cb) => {
} catch (e) {
if (!cb) throw e;
res = e;
} finally {
fs.rmSync(rmDir, { recursive: true, force: true });
}
if (cb) return cb(res);
};
Expand Down
14 changes: 7 additions & 7 deletions src/macos/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const ejs = require('ejs');
const os = require('os');
const fs = require('fs');
const { join } = require('path');
const shell = require('../utils/shell');
Expand Down Expand Up @@ -50,6 +51,7 @@ const checkifExists = async (protocol) => {

const register = async (options, cb) => {
let res = null;
const tmpDir = fs.mkdtempSync(`${os.tmpdir()}/register-protocol-`);
const validOptions = validator(registerSchema, options);
const {
protocol,
Expand Down Expand Up @@ -77,19 +79,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(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(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(tmpDir, 'script.sh');

const appSourceContent = await new Promise((resolve, reject) => {
ejs.renderFile(
Expand Down Expand Up @@ -149,6 +147,8 @@ const register = async (options, cb) => {
} catch (e) {
if (!cb) throw e;
res = e;
} finally {
fs.rmSync(rmDir, { recursive: true, force: true });
}
if (cb) return cb(res);
};
Expand Down
Empty file removed temp/.gitkeep
Empty file.

0 comments on commit a5c93f4

Please sign in to comment.