diff --git a/src/commands/create.js b/src/commands/create.js index 6451d8d..bd7975f 100644 --- a/src/commands/create.js +++ b/src/commands/create.js @@ -6,9 +6,9 @@ module.exports.register = (program) => { .command('create ') .description('creates an app based on template (neutralinojs/neutralinojs-minimal by default)') .option('-t, --template [template]') + .option('-p, --proxy [proxy]', 'specify proxy URL for downloading templates') .action(async (binaryName, command) => { - await creator.createApp(binaryName, command.template); + await creator.createApp(binaryName, command.template, command.proxy); utils.showArt(); }); } - diff --git a/src/commands/update.js b/src/commands/update.js index 6b3e472..b4a0355 100644 --- a/src/commands/update.js +++ b/src/commands/update.js @@ -6,13 +6,13 @@ module.exports.register = (program) => { .command('update') .description('updates neutralinojs binaries, client library, and TypeScript definitions') .option('-l, --latest') + .option('-p, --proxy [proxy]', 'specify proxy URL for downloading resources') .action(async (command) => { utils.checkCurrentProject(); - await downloader.downloadAndUpdateBinaries(command.latest); - await downloader.downloadAndUpdateClient(command.latest); + await downloader.downloadAndUpdateBinaries(command.latest, command.proxy); + await downloader.downloadAndUpdateClient(command.latest, command.proxy); utils.showArt(); utils.log('Run "neu version" to see installed version details.'); }); } - diff --git a/src/modules/creator.js b/src/modules/creator.js index bf26fbe..4ac59cf 100644 --- a/src/modules/creator.js +++ b/src/modules/creator.js @@ -6,12 +6,12 @@ const downloader = require('./downloader'); const frontendlib = require('../modules/frontendlib'); const utils = require('../utils'); -module.exports.createApp = async (binaryName, template) => { +module.exports.createApp = async (binaryName, template, proxy) => { if (fs.existsSync(`./${binaryName}`)) { utils.error('App name already exists'); process.exit(1); } - if(!template) { + if (!template) { template = 'neutralinojs/neutralinojs-minimal'; } utils.log(`Downloading ${template} template to ${binaryName} directory...`); @@ -20,13 +20,13 @@ module.exports.createApp = async (binaryName, template) => { process.chdir(binaryName); // Change the path context for the following methods try { - await downloader.downloadTemplate(template); - await downloader.downloadAndUpdateBinaries(); - await downloader.downloadAndUpdateClient(); - } - catch(err) { - utils.error('Unable to download resources from internet.' + - ' Please check your internet connection and template URLs.'); + // Pass proxy information to downloader functions + await downloader.downloadTemplate(template, proxy); + await downloader.downloadAndUpdateBinaries(false, proxy); + await downloader.downloadAndUpdateClient(false, proxy); + } catch (err) { + utils.error('Unable to download resources from the internet.' + + ' Please check your internet connection and template URLs.'); fse.removeSync(`../${binaryName}`); process.exit(1); } @@ -34,7 +34,7 @@ module.exports.createApp = async (binaryName, template) => { config.update('cli.binaryName', binaryName); config.update('modes.window.title', binaryName); - if(frontendlib.containsFrontendLibApp()) { + if (frontendlib.containsFrontendLibApp()) { await frontendlib.runCommand('initCommand'); } diff --git a/src/modules/downloader.js b/src/modules/downloader.js index 189468c..358328f 100644 --- a/src/modules/downloader.js +++ b/src/modules/downloader.js @@ -8,7 +8,7 @@ const decompress = require('decompress'); let cachedLatestClientVersion = null; -let getLatestVersion = (repo) => { +let getLatestVersion = (repo, proxy) => { return new Promise((resolve, reject) => { function fallback() { utils.warn('Unable to fetch the latest version tag from GitHub. Using nightly releases...'); @@ -18,6 +18,9 @@ let getLatestVersion = (repo) => { let opt = { headers: {'User-Agent': 'Neutralinojs CLI'} }; + if (proxy) { + opt.proxy = proxy; + } https.get(constants.remote.releasesApiUrl.replace('{repo}', repo), opt, function (response) { let body = ''; response.on('data', (data) => body += data); @@ -43,19 +46,19 @@ let getScriptExtension = () => { return clientLibrary.includes('.mjs') ? 'mjs' : 'js'; } -let getBinaryDownloadUrl = async (latest) => { +let getBinaryDownloadUrl = async (latest, proxy) => { const configObj = config.get(); let version = configObj.cli.binaryVersion; if(!version || latest) { - version = await getLatestVersion('neutralinojs'); + version = await getLatestVersion('neutralinojs', proxy); config.update('cli.binaryVersion', version); } return constants.remote.binariesUrl .replace(/{tag}/g, utils.getVersionTag(version)); } -let getClientDownloadUrl = async (latest, types = false) => { +let getClientDownloadUrl = async (latest, proxy, types = false) => { const configObj = config.get(); let version = configObj.cli.clientVersion; @@ -64,7 +67,7 @@ let getClientDownloadUrl = async (latest, types = false) => { version = cachedLatestClientVersion; } else { - version = await getLatestVersion('neutralino.js'); + version = await getLatestVersion('neutralino.js', proxy); } cachedLatestClientVersion = version; config.update('cli.clientVersion', version); @@ -75,23 +78,27 @@ let getClientDownloadUrl = async (latest, types = false) => { .replace(/{tag}/g, utils.getVersionTag(version)); } -let getTypesDownloadUrl = (latest) => { - return getClientDownloadUrl(latest, true); +let getTypesDownloadUrl = (latest, proxy) => { + return getClientDownloadUrl(latest, proxy, true); } let getRepoNameFromTemplate = (template) => { return template.split('/')[1]; } -let downloadBinariesFromRelease = (latest) => { +let downloadBinariesFromRelease = (latest, proxy) => { return new Promise((resolve, reject) => { fs.mkdirSync('.tmp', { recursive: true }); const zipFilename = '.tmp/binaries.zip'; const file = fs.createWriteStream(zipFilename); utils.log('Downloading Neutralinojs binaries..'); - getBinaryDownloadUrl(latest) + getBinaryDownloadUrl(latest, proxy) .then((url) => { - https.get(url, function (response) { + const options = {}; + if (proxy) { + options.proxy = proxy; + } + https.get(url, options, function (response) { response.pipe(file); response.on('end', () => { utils.log('Extracting binaries.zip file...'); @@ -104,14 +111,18 @@ let downloadBinariesFromRelease = (latest) => { }); } -let downloadClientFromRelease = (latest) => { +let downloadClientFromRelease = (latest, proxy) => { return new Promise((resolve, reject) => { fs.mkdirSync('.tmp', { recursive: true }); const file = fs.createWriteStream('.tmp/neutralino.' + getScriptExtension()); utils.log('Downloading the Neutralinojs client..'); - getClientDownloadUrl(latest) + getClientDownloadUrl(latest, proxy) .then((url) => { - https.get(url, function (response) { + const options = {}; + if (proxy) { + options.proxy = proxy; + } + https.get(url, options, function (response) { response.pipe(file); file.on('finish', () => { file.close(); @@ -122,15 +133,19 @@ let downloadClientFromRelease = (latest) => { }); } -let downloadTypesFromRelease = (latest) => { +let downloadTypesFromRelease = (latest, proxy) => { return new Promise((resolve, reject) => { fs.mkdirSync('.tmp', { recursive: true }); const file = fs.createWriteStream('.tmp/neutralino.d.ts'); utils.log('Downloading the Neutralinojs types..'); - getTypesDownloadUrl(latest) + getTypesDownloadUrl(latest, proxy) .then((url) => { - https.get(url, function (response) { + const options = {}; + if (proxy) { + options.proxy = proxy; + } + https.get(url, options, function (response) { response.pipe(file); file.on('finish', () => { file.close(); @@ -141,13 +156,17 @@ let downloadTypesFromRelease = (latest) => { }); } -module.exports.downloadTemplate = (template) => { +module.exports.downloadTemplate = (template, proxy) => { return new Promise((resolve, reject) => { let templateUrl = constants.remote.templateUrl.replace('{template}', template); fs.mkdirSync('.tmp', { recursive: true }); const zipFilename = '.tmp/template.zip'; const file = fs.createWriteStream(zipFilename); - https.get(templateUrl, function (response) { + const options = {}; + if (proxy) { + options.proxy = proxy; + } + https.get(templateUrl, options, function (response) { response.pipe(file); response.on('end', () => { utils.log('Extracting template zip file...'); @@ -163,8 +182,8 @@ module.exports.downloadTemplate = (template) => { }); } -module.exports.downloadAndUpdateBinaries = async (latest = false) => { - await downloadBinariesFromRelease(latest); +module.exports.downloadAndUpdateBinaries = async (latest = false, proxy) => { + await downloadBinariesFromRelease(latest, proxy); utils.log('Finalizing and cleaning temp. files.'); if(!fse.existsSync('bin')) fse.mkdirSync('bin'); @@ -184,7 +203,7 @@ module.exports.downloadAndUpdateBinaries = async (latest = false) => { utils.clearDirectory('.tmp'); } -module.exports.downloadAndUpdateClient = async (latest = false) => { +module.exports.downloadAndUpdateClient = async (latest = false, proxy) => { const configObj = config.get(); if(!configObj.cli.clientLibrary) { utils.log(`neu CLI won't download the client library --` + @@ -192,8 +211,8 @@ module.exports.downloadAndUpdateClient = async (latest = false) => { return; } const clientLibrary = utils.trimPath(configObj.cli.clientLibrary); - await downloadClientFromRelease(latest); - await downloadTypesFromRelease(latest); + await downloadClientFromRelease(latest, proxy); + await downloadTypesFromRelease(latest, proxy); utils.log('Finalizing and cleaning temp. files...'); fse.copySync(`.tmp/${constants.files.clientLibraryPrefix + getScriptExtension()}` , `./${clientLibrary}`); @@ -201,4 +220,3 @@ module.exports.downloadAndUpdateClient = async (latest = false) => { , `./${clientLibrary.replace(/[.][a-z]*$/, '.d.ts')}`); utils.clearDirectory('.tmp'); } -