From 5143870d3b3132d15e6d8ecc83b284f1d36a4ce2 Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Mon, 22 Jun 2020 23:06:47 +0100 Subject: [PATCH] Tools: Improve building and testing app on Windows --- ElectronClient/gulpfile.js | 2 +- ElectronClient/tools/copyPluginAssets.js | 13 ++------ ElectronClient/tools/copyTinyMceLangs.js | 5 ++- Tools/gulp/tasks/copyLib.js | 10 ++++-- Tools/gulp/utils.js | 39 ++++++++++++++++-------- 5 files changed, 39 insertions(+), 30 deletions(-) diff --git a/ElectronClient/gulpfile.js b/ElectronClient/gulpfile.js index 291fa07603..1191c65eee 100644 --- a/ElectronClient/gulpfile.js +++ b/ElectronClient/gulpfile.js @@ -31,7 +31,7 @@ const buildSeries = [ // which makes the copyPluginAssets command fail. For that reason, // it's not possible to run watch on Windows while testing the desktop app. if (require('os').platform() === 'win32') { - buildSeries.push('tsc'); + // buildSeries.push('tsc'); } const buildParallel = [ diff --git a/ElectronClient/tools/copyPluginAssets.js b/ElectronClient/tools/copyPluginAssets.js index d4444c227a..21f4a27cbc 100644 --- a/ElectronClient/tools/copyPluginAssets.js +++ b/ElectronClient/tools/copyPluginAssets.js @@ -1,4 +1,4 @@ -const fs = require('fs-extra'); +const utils = require('../../Tools/gulp/utils'); async function main() { const rootDir = `${__dirname}/..`; @@ -10,16 +10,7 @@ async function main() { for (const destDir of destDirs) { console.info(`Copying to ${destDir}`); - - try { - await fs.remove(destDir); - await fs.copy(sourceDir, destDir); - } catch (error) { - // These calls randomly fail on Windows when the folders are being - // watch by TypeScript. As these files aren't always needed for - // development, only print a warning. - console.warn(`Could not copy to ${destDir}`, error); - } + await utils.copyDir(sourceDir, destDir); } } diff --git a/ElectronClient/tools/copyTinyMceLangs.js b/ElectronClient/tools/copyTinyMceLangs.js index 79342e088d..e9605b7f82 100644 --- a/ElectronClient/tools/copyTinyMceLangs.js +++ b/ElectronClient/tools/copyTinyMceLangs.js @@ -1,13 +1,12 @@ const fs = require('fs-extra'); const glob = require('glob'); +const utils = require('../../Tools/gulp/utils'); async function main() { const sourceDir = `${__dirname}/../../Modules/TinyMCE/langs`; const destDir = `${__dirname}/../node_modules/tinymce/langs`; console.info(`Copying ${sourceDir} => ${destDir}`); - await fs.remove(destDir); - await fs.mkdirp(destDir); - await fs.copy(sourceDir, destDir); + await utils.copyDir(sourceDir, destDir); const supportedLocales = glob.sync(`${sourceDir}/*.js`).map(s => { s = s.split('/'); diff --git a/Tools/gulp/tasks/copyLib.js b/Tools/gulp/tasks/copyLib.js index 85ae1c8868..57af201c3e 100644 --- a/Tools/gulp/tasks/copyLib.js +++ b/Tools/gulp/tasks/copyLib.js @@ -5,7 +5,13 @@ const rootDir = utils.rootDir(); module.exports = { src: `${rootDir}/ReactNativeClient/lib/**/*`, fn: async function() { - await utils.copyDir(`${rootDir}/ReactNativeClient/lib`, `${rootDir}/CliClient/build/lib`, { delete: false }); - await utils.copyDir(`${rootDir}/ReactNativeClient/lib`, `${rootDir}/ElectronClient/lib`, { delete: false }); + const copyOptions = { + excluded: [ + `${rootDir}/ReactNativeClient/lib/joplin-renderer/node_modules`, + ], + }; + + await utils.copyDir(`${rootDir}/ReactNativeClient/lib`, `${rootDir}/CliClient/build/lib`, copyOptions); + await utils.copyDir(`${rootDir}/ReactNativeClient/lib`, `${rootDir}/ElectronClient/lib`, copyOptions); }, }; diff --git a/Tools/gulp/utils.js b/Tools/gulp/utils.js index a916dd1e4b..4c3158b9a8 100644 --- a/Tools/gulp/utils.js +++ b/Tools/gulp/utils.js @@ -20,6 +20,14 @@ utils.execCommand = function(command) { return new Promise((resolve, reject) => { exec(command, { maxBuffer: 1024 * 1024 }, (error, stdout) => { if (error) { + + // Special case for robocopy, which will return an error code of "1" + // if successful - https://ss64.com/nt/robocopy-exit.html + if (command.indexOf('robocopy') === 0 && error.code <= 1) { + resolve(stdout.trim()); + return; + } + if (error.signal == 'SIGTERM') { resolve('Process was killed'); } else { @@ -76,8 +84,6 @@ utils.replaceFileText = async function(filePath, regex, toInsert) { }; utils.copyDir = async function(src, dest, options) { - const os = require('os'); - options = Object.assign({}, { excluded: [], delete: true, @@ -86,22 +92,21 @@ utils.copyDir = async function(src, dest, options) { src = utils.toSystemSlashes(src); dest = utils.toSystemSlashes(dest); - await fs.mkdirp(dest); + await utils.mkdir(dest); if (utils.isWindows()) { - let excludedFlag = ''; - let tempFile = null; + let cmd = ['robocopy']; + cmd.push(`"${src}"`); + cmd.push(`"${dest}"`); + cmd.push('/e'); + if (options.delete) cmd.push('/purge'); + if (options.excluded.length) { - tempFile = `${os.tmpdir()}\\xcopy_excluded_${Date.now()}.txt`; - await fs.writeFile(tempFile, options.excluded.join('\n')); - excludedFlag = `/EXCLUDE:${tempFile}`; + cmd.push('/xd'); + cmd = cmd.concat(options.excluded.map(p => `"${utils.toSystemSlashes(p)}"`).join(' ')); } - // TODO: add support for delete flag - - await utils.execCommand(`xcopy /C /I /H /R /Y /S ${excludedFlag} "${src}" "${dest}"`); - - if (tempFile) await fs.remove(tempFile); + await utils.execCommand(cmd.join(' ')); } else { let excludedFlag = ''; if (options.excluded.length) { @@ -117,6 +122,14 @@ utils.copyDir = async function(src, dest, options) { } }; +utils.mkdir = async function(dir) { + if (utils.isWindows()) { + return utils.execCommand(`if not exist "${utils.toSystemSlashes(dir)}" mkdir "${utils.toSystemSlashes(dir)}"`); + } else { + return fs.mkdir(dir); + } +}; + utils.copyFile = async function(src, dest) { await fs.copy(src, dest); };