From 727df0c22f96c47cc91fa461a9ec4bb2616915e9 Mon Sep 17 00:00:00 2001 From: Jonatan Nilsson Date: Mon, 7 Sep 2020 01:25:03 +0000 Subject: [PATCH] Finished implementing core installer --- .gitignore | 3 +++ core/core.mjs | 47 +++++++++++++++++++++++++++++++++++++++++++---- core/util.mjs | 17 +++++++++++++---- 3 files changed, 59 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index fb68d27..de2449f 100644 --- a/.gitignore +++ b/.gitignore @@ -106,3 +106,6 @@ dist # Custom ignore db.json package-lock.json + +app/* +manage/* diff --git a/core/core.mjs b/core/core.mjs index f5427e9..2692136 100644 --- a/core/core.mjs +++ b/core/core.mjs @@ -111,14 +111,53 @@ export default class Core extends EventEmitter{ if (fs.existsSync(getPathFromRoot('./app/' + version.name))) { await runCommand('rmdir', ['/S', '/Q', `"${getPathFromRoot('./app/' + version.name)}"`]) } - await fsp.mkdir(getPathFromRoot('./app/' + version.name)) + try { + await fsp.mkdir(getPathFromRoot('./app/' + version.name)) + } catch(err) { + if (err.code !== 'EEXIST') { + throw err + } + } + // await fsp.mkdir(getPathFromRoot('./app/' + version.name + '/node_modules')) this.logActive(name, active, `Downloading ${version.name} (${version.url}) to ${version.name + '/' + version.name + '.zip'}\n`) let filePath = getPathFromRoot('./app/' + version.name + '/' + version.name + '.zip') await request(version.url, filePath) this.logActive(name, active, `Downloading finished, starting extraction\n`) - await runCommand('"C:\\Program Files\\7-Zip\\7z.exe"', ['e', `"${filePath}"`], this.logActive.bind(this, name, active)) - // await request(version) - // request(config[name + 'Repository']) + await runCommand( + '"C:\\Program Files\\7-Zip\\7z.exe"', + ['x', `"${filePath}"`], + getPathFromRoot('./app/' + version.name + '/'), + this.logActive.bind(this, name, active) + ) + + if (!fs.existsSync(getPathFromRoot('./app/' + version.name + '/index.mjs'))) { + this.logActive(name, active, `\nERROR: Missing index.mjs in the folder, exiting\n`) + throw new Error(`Missing index.mjs in ${getPathFromRoot('./app/' + version.name + '/index.mjs')}`) + } + + this.logActive(name, active, `\nStarting npm install\n`) + + await runCommand( + 'npm.cmd', + ['install', '--production', '--no-optional', '--no-package-lock', '--no-audit'], + getPathFromRoot('./app/' + version.name + '/'), + this.logActive.bind(this, name, active) + ) + + this.logActive(name, active, `\nInstalled:\n`) + + await runCommand( + 'npm.cmd', + ['list'], + getPathFromRoot('./app/' + version.name + '/'), + this.logActive.bind(this, name, active) + ) + + await this._db.set(`core.${name}LatestInstalled`, version.name) + .write() + this.emit('dbupdated', {}) + + this.logActive(name, active, `\nSuccessfully installed ${version.name}\n`) } async startProgram(name) { diff --git a/core/util.mjs b/core/util.mjs index 5e1c870..39c10f0 100644 --- a/core/util.mjs +++ b/core/util.mjs @@ -7,21 +7,30 @@ export function getPathFromRoot(add) { return path.join(__dirname,'../', add) } -export function runCommand(command, options = [], stream = function() {}) { +export function runCommand(command, options = [], folder = null, stream = function() {}) { + console.log(command, options.join(' ')) return new Promise(function(res, rej) { - let processor = spawn(command, options, {shell: true}) + let processor = spawn(command, options, { + shell: true, + cwd: folder, + }) + let timeOuter = setTimeout(function() { + processor.stdin.write('n\n') + }, 250) processor.stdout.on('data', function(data) { stream(data.toString()) - processor.stdin.write('n') + processor.stdin.write('n\n') }) processor.stderr.on('data', function(data) { stream(data.toString()) - processor.stdin.write('n') + processor.stdin.write('n\n') }) processor.on('error', function(err) { + clearInterval(timeOuter) rej(err) }) processor.on('exit', function (code) { + clearInterval(timeOuter) res(code) }) })