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

Adding a support system for proxy setting to download #255

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
4 changes: 2 additions & 2 deletions src/commands/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ module.exports.register = (program) => {
.command('create <binaryName>')
.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();
});
}

6 changes: 3 additions & 3 deletions src/commands/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
});
}

20 changes: 10 additions & 10 deletions src/modules/creator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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...`);
Expand All @@ -20,21 +20,21 @@ 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);
}

config.update('cli.binaryName', binaryName);
config.update('modes.window.title', binaryName);

if(frontendlib.containsFrontendLibApp()) {
if (frontendlib.containsFrontendLibApp()) {
await frontendlib.runCommand('initCommand');
}

Expand Down
66 changes: 42 additions & 24 deletions src/modules/downloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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...');
Expand All @@ -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);
Expand All @@ -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;

Expand All @@ -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);
Expand All @@ -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...');
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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...');
Expand All @@ -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');
Expand All @@ -184,21 +203,20 @@ 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 --` +
` download @neutralinojs/lib from your Node package manager.`);
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}`);
fse.copySync(`.tmp/neutralino.d.ts`
, `./${clientLibrary.replace(/[.][a-z]*$/, '.d.ts')}`);
utils.clearDirectory('.tmp');
}