From 336d8bd98020ddad8fafdf889bb2b6afe02aa4e5 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Mon, 7 Dec 2020 20:04:02 +0000 Subject: [PATCH] use async fs methods in apply.js should avoid potential race conditions --- pkg/apply.js | 44 +++++++++++++++++++++----------------------- 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/pkg/apply.js b/pkg/apply.js index 6b7f3ca..63132a8 100644 --- a/pkg/apply.js +++ b/pkg/apply.js @@ -66,7 +66,7 @@ module.exports = async function ({ overwrite_version, friendly_errors } = {}) { console.info(' ...unpacking app.asar.'); const asar_bak = path.resolve(`${__notion}/app.asar.bak`); extractAll(check_app.executable, `${path.resolve(`${__notion}/app`)}`); - if (await fs.pathExists(asar_bak)) fs.remove(asar_bak); + if (await fs.pathExists(asar_bak)) await fs.remove(asar_bak); await fs.move(check_app.executable, asar_bak); } else { console.info(' ...backing up default app.'); @@ -104,7 +104,7 @@ module.exports = async function ({ overwrite_version, friendly_errors } = {}) { process.platform === 'darwin' && (await fs.pathExists(path.resolve(`${__notion}/../Info.plist`))) ) { - fs.copy( + await fs.copy( path.resolve(`${__dirname}/Info.plist`), path.resolve(`${__notion}/../Info.plist`), { overwrite: true } @@ -124,27 +124,25 @@ module.exports = async function ({ overwrite_version, friendly_errors } = {}) { if (insertion_target === 'main/main.js') { // https://github.com/notion-enhancer/notion-enhancer/issues/160 // patch the notion:// url scheme/protocol to work on linux - fs.readFile(insertion_file, 'utf8', (err, data) => { - if (err) throw err; - fs.writeFile( - insertion_file, - data - .replace( - /process.platform === "win32"/g, - 'process.platform === "win32" || process.platform === "linux"' - ) - .replace( - /else \{[\s\n]+const win = createWindow_1\.createWindow\(relativeUrl\);/g, - 'else if (relativeUrl) { const win = createWindow_1.createWindow(relativeUrl);' - ), - 'utf8', - (err) => { - if (err) throw err; - } - ); - }); + const data = await fs.readFile(insertion_file, 'utf8'); + await fs.writeFile( + insertion_file, + data + .replace( + /process.platform === "win32"/g, + 'process.platform === "win32" || process.platform === "linux"' + ) + .replace( + /else \{[\s\n]+const win = createWindow_1\.createWindow\(relativeUrl\);/g, + 'else if (relativeUrl) { const win = createWindow_1.createWindow(relativeUrl);' + ), + 'utf8', + (err) => { + if (err) throw err; + } + ); } - fs.appendFile( + await fs.appendFile( insertion_file, `\n\n//notion-enhancer\nrequire('notion-enhancer/pkg/loader.js')(__filename, exports);` ); @@ -153,7 +151,7 @@ module.exports = async function ({ overwrite_version, friendly_errors } = {}) { // not resolved, nothing else in apply process depends on it // so it's just a "let it do its thing" console.info(' ...recording enhancement version.'); - fs.outputFile( + await fs.outputFile( path.resolve(`${__notion}/app/ENHANCER_VERSION.txt`), version );