diff options
-rw-r--r-- | commands/install.js | 8 | ||||
-rw-r--r-- | commands/remove.js | 7 | ||||
-rw-r--r-- | commands/upgrade.js | 99 | ||||
-rw-r--r-- | index.js | 176 |
4 files changed, 180 insertions, 110 deletions
diff --git a/commands/install.js b/commands/install.js index b65725d..6f0df0c 100644 --- a/commands/install.js +++ b/commands/install.js @@ -145,6 +145,14 @@ module.exports = async (argv) => { spinner.succeed("Extracting package... done"); console.log(" Size change: +" + require('../hooks/size')(pkg.id)); + if (os.platform() === "win32" && typeof pkg.executable.windows === "string") { + fs.writeFileSync(os.homedir() + "/.twilight/binaries/" + pkg.id + ".bat", pkg.executable.windows); + } else if (os.platform() === "linux" && typeof pkg.executable.linux === "string") { + fs.writeFileSync(os.homedir() + "/.twilight/binaries/" + pkg.id + ".sh", pkg.executable.linux); + } else if (os.platform() === "darwin" && typeof pkg.executable.mac === "string") { + fs.writeFileSync(os.homedir() + "/.twilight/binaries/" + pkg.id + ".sh", pkg.executable.mac); + } + let postinstall = []; if (os.platform() === "win32") postinstall = pkg.postinstall.windows; if (os.platform() === "linux") postinstall = pkg.postinstall.linux; diff --git a/commands/remove.js b/commands/remove.js index 56fc847..081d87d 100644 --- a/commands/remove.js +++ b/commands/remove.js @@ -57,6 +57,13 @@ module.exports = async (argv, reinstalling) => { fs.rmSync(home + "/packages/" + argv.package, { recursive: true }); delete installed[installed.map(i => i.id).indexOf(argv.package)]; fs.writeFileSync(os.homedir() + "/.twilight/installed.json", JSON.stringify(installed)); + + if (os.platform() === "win32") { + if (fs.existsSync(os.homedir() + "/.twilight/binaries/" + argv.package + ".bat")) fs.unlinkSync(os.homedir() + "/.twilight/binaries/" + argv.package + ".bat"); + } else { + if (fs.existsSync(os.homedir() + "/.twilight/binaries/" + argv.package + ".sh")) fs.unlinkSync(os.homedir() + "/.twilight/binaries/" + argv.package + ".sh"); + } + spinner.succeed("Removing package... done"); if (!packages.includes(argv.package)) { diff --git a/commands/upgrade.js b/commands/upgrade.js index 7dcdb4a..18e5adc 100644 --- a/commands/upgrade.js +++ b/commands/upgrade.js @@ -5,6 +5,7 @@ module.exports = async (argv) => { let packages = JSON.parse(fs.readFileSync(home + "/repository/list.json").toString()); spinner.succeed("Reading packages lists... done"); let updated = false; + let affected = false; for (let pack of installed) { if (!packages.includes(pack.id)) { @@ -15,40 +16,6 @@ module.exports = async (argv) => { let pkg = JSON.parse(fs.readFileSync(home + "/repository/" + dir + "/" + pack.id + ".json").toString()); if ((argv.package !== undefined && argv.package === pack.id) || argv.package === undefined) { updated = true; - if (os.platform() === "win32" && pkg.platforms.windows === 0) { - console.log(c.yellow("warn: ") + "package '" + pack.id + "' not available on platform 'win32' anymore"); - installable = false; - } else if (os.platform() === "linux" && pkg.platforms.linux === 0) { - console.log(c.yellow("warn: ") + "package '" + pack.id + "' not available on platform 'linux' anymore"); - installable = false; - } else if (os.platform() === "darwin" && pkg.platforms.mac === 0) { - console.log(c.yellow("warn: ") + "package '" + pack.id + "' not available on platform 'macos' anymore"); - installable = false; - } - - if (os.platform() === "win32" && pkg.platforms.windows === 1) { - console.log(c.yellow("warn: ") + "package '" + pack.id + "' is experimental on platform 'win32'"); - } else if (os.platform() === "linux" && pkg.platforms.linux === 1) { - console.log(c.yellow("warn: ") + "package '" + pack.id + "' is experimental on platform 'linux'"); - } else if (os.platform() === "darwin" && pkg.platforms.mac === 1) { - console.log(c.yellow("warn: ") + "package '" + pack.id + "' is experimental on platform 'macos'"); - } - - spinner = ora("Checking dependencies...").start(); - for (let dependency of pkg.depends) { - let cmd = "which"; - if (os.platform() === "win32") { cmd = "where"; } - try { - if (require('child_process').spawnSync(cmd, [dependency]).status !== 0) { - spinner.fail("Checking dependencies... failed") - die(c.red("error: ") + "package '" + pack.id + "' depends on '" + dependency + "' which is not installed"); - } - } catch (e) { - spinner.fail("Checking dependencies... failed") - die(c.red("error: ") + "unable to check for '" + dependency + "'"); - } - } - spinner.succeed("Checking dependencies... done") if (installable) { let version = pkg.verdata.latest; @@ -75,6 +42,43 @@ module.exports = async (argv) => { } if (pack.version !== version) { + let affected = true; + + if (os.platform() === "win32" && pkg.platforms.windows === 0) { + console.log(c.yellow("warn: ") + "package '" + pack.id + "' not available on platform 'win32' anymore"); + installable = false; + } else if (os.platform() === "linux" && pkg.platforms.linux === 0) { + console.log(c.yellow("warn: ") + "package '" + pack.id + "' not available on platform 'linux' anymore"); + installable = false; + } else if (os.platform() === "darwin" && pkg.platforms.mac === 0) { + console.log(c.yellow("warn: ") + "package '" + pack.id + "' not available on platform 'macos' anymore"); + installable = false; + } + + if (os.platform() === "win32" && pkg.platforms.windows === 1) { + console.log(c.yellow("warn: ") + "package '" + pack.id + "' is experimental on platform 'win32'"); + } else if (os.platform() === "linux" && pkg.platforms.linux === 1) { + console.log(c.yellow("warn: ") + "package '" + pack.id + "' is experimental on platform 'linux'"); + } else if (os.platform() === "darwin" && pkg.platforms.mac === 1) { + console.log(c.yellow("warn: ") + "package '" + pack.id + "' is experimental on platform 'macos'"); + } + + spinner = ora("Checking dependencies...").start(); + for (let dependency of pkg.depends) { + let cmd = "which"; + if (os.platform() === "win32") { cmd = "where"; } + try { + if (require('child_process').spawnSync(cmd, [dependency]).status !== 0) { + spinner.fail("Checking dependencies... failed") + die(c.red("error: ") + "package '" + pack.id + "' depends on '" + dependency + "' which is not installed"); + } + } catch (e) { + spinner.fail("Checking dependencies... failed") + die(c.red("error: ") + "unable to check for '" + dependency + "'"); + } + } + spinner.succeed("Checking dependencies... done") + console.log(" Installing '" + pkg.name + "'...") console.log(" version: " + pack.version + " -> " + version); console.log(" release: " + date); @@ -120,12 +124,31 @@ module.exports = async (argv) => { fs.writeFileSync(os.homedir() + "/.twilight/installed.json", JSON.stringify(installed)); spinner.succeed("Extracting package... done"); console.log(" Size change: " + change); + + if (os.platform() === "win32" && typeof pkg.executable.windows === "string") { + fs.writeFileSync(os.homedir() + "/.twilight/binaries/" + pkg.id + ".bat", pkg.executable.windows); + } else if (os.platform() === "linux" && typeof pkg.executable.linux === "string") { + fs.writeFileSync(os.homedir() + "/.twilight/binaries/" + pkg.id + ".sh", pkg.executable.linux); + } else if (os.platform() === "darwin" && typeof pkg.executable.mac === "string") { + fs.writeFileSync(os.homedir() + "/.twilight/binaries/" + pkg.id + ".sh", pkg.executable.mac); + } + + let postinstall = []; + if (os.platform() === "win32") postinstall = pkg.postinstall.windows; + if (os.platform() === "linux") postinstall = pkg.postinstall.linux; + if (os.platform() === "darwin") postinstall = pkg.postinstall.mac; + + if (postinstall.length > 0) { + spinner = ora("Running post-install hooks...").start(); + for (let hook of postinstall) { + require('child_process').execSync(hook, { stdio: "inherit" }) + } + spinner.succeed("Running post-install hooks... done"); + } }) } } else if (argv.package !== undefined && argv.package === pack.id) { die(c.red("error: ") + "package '" + argv.package + "' is up to date"); - } else { - console.log(" up to date"); } } else if (argv.package !== undefined && argv.package === pack.id) { die(c.red("error: ") + "package '" + argv.package + "' cannot be updated"); @@ -137,4 +160,8 @@ module.exports = async (argv) => { if (!updated) { die(c.red("error: ") + "package '" + argv.package + "' not installed"); } + + if (!affected) { + die("All packages are up to date"); + } }
\ No newline at end of file @@ -1,88 +1,116 @@ -(async () => { - global.yargs = require('yargs/yargs'); - const { hideBin } = require('yargs/helpers'); - global.git = require('simple-git'); - global.ora = (await import('ora')).default; +process.on('uncaughtException', async (e) => { global.c = (await import('chalk')).default; - global.fs = require('fs'); - global.os = require('os'); - global.axios = require('axios'); - global.moment = require('moment'); - global.prompts = require('prompts').prompts; - - if (!fs.existsSync(os.homedir() + "/.twilight")) { - fs.mkdirSync(os.homedir() + "/.twilight") + if (!fs.existsSync(os.homedir() + "/.twilight/crashes")) { + fs.mkdirSync(os.homedir() + "/.twilight/crashes") } - if (!fs.existsSync(os.homedir() + "/.twilight/packages")) { - fs.mkdirSync(os.homedir() + "/.twilight/packages") - } + let date = new Date().toISOString().replace(/[^a-zA-Z0-9-]/gm, "-"); + fs.writeFileSync(require('os').homedir() + "/.twilight/crashes/" + date + ".txt", e.stack); - if (!fs.existsSync(os.homedir() + "/.twilight/installed.json")) { - fs.writeFileSync(os.homedir() + "/.twilight/installed.json", "[]"); - } else { - fs.writeFileSync(os.homedir() + "/.twilight/installed.json", JSON.stringify(JSON.parse(fs.readFileSync(os.homedir() + "/.twilight/installed.json").toString()).filter(i => i !== null))); - } + console.log(c.red("error:") + " an internal error occurred, did you forget to run 'twi update'?"); + console.log(" additionally, a crash report has been saved to:\n " + require('os').homedir() + (require('os').platform() === "win32" ? "\\" : "/") + ".twilight" + (require('os').platform() === "win32" ? "\\" : "/") + "crashes" + (require('os').platform() === "win32" ? "\\" : "/") + date + ".txt") + process.exit(2); +}) - global.home = os.homedir() + "/.twilight"; +try { + (async () => { + global.yargs = require('yargs/yargs'); + const { hideBin } = require('yargs/helpers'); + global.git = require('simple-git'); + global.ora = (await import('ora')).default; + global.c = (await import('chalk')).default; + global.fs = require('fs'); + global.os = require('os'); + global.axios = require('axios'); + global.moment = require('moment'); + global.prompts = require('prompts').prompts; - global.die = (text, code) => { - fs.rmSync(home + "/runtime.pid"); - if (text) { - console.log(text); - if (code) { - process.exit(code); - } else { - process.exit(); - } + + if (!fs.existsSync(os.homedir() + "/.twilight")) { + fs.mkdirSync(os.homedir() + "/.twilight") + } + + if (!fs.existsSync(os.homedir() + "/.twilight/packages")) { + fs.mkdirSync(os.homedir() + "/.twilight/packages") + } + + if (!fs.existsSync(os.homedir() + "/.twilight/crashes")) { + fs.mkdirSync(os.homedir() + "/.twilight/crashes") + } + + if (!fs.existsSync(os.homedir() + "/.twilight/binaries")) { + fs.mkdirSync(os.homedir() + "/.twilight/binaries") + } + + if (!fs.existsSync(os.homedir() + "/.twilight/installed.json")) { + fs.writeFileSync(os.homedir() + "/.twilight/installed.json", "[]"); } else { - if (code) { - process.exit(code); + fs.writeFileSync(os.homedir() + "/.twilight/installed.json", JSON.stringify(JSON.parse(fs.readFileSync(os.homedir() + "/.twilight/installed.json").toString()).filter(i => i !== null))); + } + + global.home = os.homedir() + "/.twilight"; + + global.die = (text, code) => { + fs.rmSync(home + "/runtime.pid"); + if (text) { + console.log(text); + if (code) { + process.exit(code); + } else { + process.exit(); + } } else { - process.exit(); + if (code) { + process.exit(code); + } else { + process.exit(); + } } } - } - let pargv = process.argv; - pargv[1] = "twi"; - - global.argv = yargs(pargv.slice(2)) - .command("install <package>", "Install a package") - .command("reinstall <package>", "Online reinstall an installed package") - .command("remove <package>", "Delete a package") - .command("update", "Fetches the repository") - .command("upgrade [package]", "Update one or all package(s)") - .command("purge [package]", "Offline reinstall an installed package") - .command("info <package>", "Get info about a package") - .command("list", "List all packages in the repository") - .command("installed", "List all installed packages") - .command("installable", "List all compatible packages") - .help() - .alias("help", "h") - .alias("version", "V") - .example("twi install neutron", "Install Neutron") - .example("twi update", "Update all packages") - .example("twi info ponyfind", "Show info about Ponyfind") - .strictCommands() - .demandCommand(1) - .epilog("Twilight Package Manager v" + require('./package.json').version) - .usage('Usage: twi <command> [arguments...]') - .argv; - - if (fs.existsSync(home + "/runtime.pid")) { - let pid = fs.readFileSync(home + "/runtime.pid") - 1 + 1; - try { - process.kill(pid, 0); - die(c.red("error:") + " another instance is running (" + pid + ")"); - } catch (e) { - console.log(c.yellow("warn:") + " process was stopped unexpectedly"); + let pargv = process.argv; + pargv[1] = "twi"; + + global.argv = yargs(pargv.slice(2)) + .command("install <package>", "Install a package") + .command("reinstall <package>", "Online reinstall an installed package") + .command("remove <package>", "Delete a package") + .command("update", "Fetches the repository") + .command("upgrade [package]", "Update one or all package(s)") + .command("purge [package]", "Offline reinstall an installed package") + .command("info <package>", "Get info about a package") + .command("list", "List all packages in the repository") + .command("installed", "List all installed packages") + .command("installable", "List all compatible packages") + .help() + .alias("help", "h") + .alias("version", "V") + .example("twi install neutron", "Install Neutron") + .example("twi update", "Update all packages") + .example("twi info ponyfind", "Show info about Ponyfind") + .strictCommands() + .demandCommand(1) + .epilog("Twilight Package Manager v" + require('./package.json').version) + .usage('Usage: twi <command> [arguments...]') + .argv; + + if (fs.existsSync(home + "/runtime.pid")) { + let pid = fs.readFileSync(home + "/runtime.pid") - 1 + 1; + try { + process.kill(pid, 0); + die(c.red("error:") + " another instance is running (" + pid + ")"); + } catch (e) { + console.log(c.yellow("warn:") + " process was stopped unexpectedly"); + } } - } - fs.writeFileSync(home + "/runtime.pid", process.pid.toString()); - let command = argv._[0]; - await require('./commands/' + command)(argv); + fs.writeFileSync(home + "/runtime.pid", process.pid.toString()); + let command = argv._[0]; + await require('./commands/' + command)(argv); - fs.rmSync(home + "/runtime.pid"); -})()
\ No newline at end of file + fs.rmSync(home + "/runtime.pid"); + })() +} catch (e) { + console.log("error: an internal error occurred, did you forget to run 'twi update'?"); + process.exit(2); +}
\ No newline at end of file |