From 1ac602732bce82b22bbe581c3f47c52dc232c7f2 Mon Sep 17 00:00:00 2001 From: Minteck Date: Wed, 16 Mar 2022 16:52:22 +0100 Subject: CC: #1 --- commands/aliases.json | 22 +-- commands/info.js | 288 ++++++++++++++--------------- commands/install.js | 400 ++++++++++++++++++++--------------------- commands/installable.js | 200 ++++++++++----------- commands/installed.js | 184 +++++++++---------- commands/list.js | 196 ++++++++++---------- commands/purge.js | 156 ++++++++-------- commands/reinstall.js | 58 +++--- commands/remove.js | 238 ++++++++++++------------ commands/update.js | 54 +++--- commands/upgrade.js | 470 ++++++++++++++++++++++++------------------------ 11 files changed, 1133 insertions(+), 1133 deletions(-) (limited to 'commands') diff --git a/commands/aliases.json b/commands/aliases.json index 9a5754c..667eca9 100644 --- a/commands/aliases.json +++ b/commands/aliases.json @@ -1,12 +1,12 @@ -{ - "install": [ "i", "a", "add" ], - "reinstall": [ "ri", "reset", "rs" ], - "remove": [ "r", "del", "rm", "delete", "uninstall", "u" ], - "update": [ "ud", "fetch", "refresh", "reload", "rl" ], - "upgrade": [ "ug" ], - "purge": [ "p", "ori", "ors", "oreset", "oreinstall" ], - "info": [ "inf", "view", "v", "if" ], - "list": [ "l", "ls", "all" ], - "installed": [ "il", "lil", "lsil", "allil" ], - "installable": [ "ia", "lia", "lsia", "allia" ] +{ + "install": [ "i", "a", "add" ], + "reinstall": [ "ri", "reset", "rs" ], + "remove": [ "r", "del", "rm", "delete", "uninstall", "u" ], + "update": [ "ud", "fetch", "refresh", "reload", "rl" ], + "upgrade": [ "ug" ], + "purge": [ "p", "ori", "ors", "oreset", "oreinstall" ], + "info": [ "inf", "view", "v", "if" ], + "list": [ "l", "ls", "all" ], + "installed": [ "il", "lil", "lsil", "allil" ], + "installable": [ "ia", "lia", "lsia", "allia" ] } \ No newline at end of file diff --git a/commands/info.js b/commands/info.js index 49b3162..b88e2b3 100644 --- a/commands/info.js +++ b/commands/info.js @@ -1,145 +1,145 @@ -/* - * MIT License - * - * Copyright (c) 2022- Minteck - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - * - */ - -module.exports = async (argv) => { - const installed = JSON.parse(fs.readFileSync(home + "/installed.json").toString()); - let pkgInstalled = false - if (installed.map(i => i.id).includes(argv.package)) { - pkgInstalled = true; - } - - let packages = JSON.parse(fs.readFileSync(home + "/repository/list.json").toString()); - - if (!packages.includes(argv.package)) { - die(c.red("error: ") + "package '" + argv.package + "' not in repository"); - } - - let dir = argv.package.substring(0, 1).replace(/[^a-zA-Z0-9]/gm, "#"); - let pkg = JSON.parse(fs.readFileSync(home + "/repository/" + dir + "/" + argv.package + ".json").toString()); - - let compatible = true; - let replacement = null; - if (os.platform() === "win32" && pkg.platforms.windows === 0) { - compatible = false; - if (typeof pkg.replaced.windows === "string") { - replacement = pkg.replaced.windows - } - } else if (os.platform() === "linux" && pkg.platforms.linux === 0) { - compatible = false; - if (typeof pkg.replaced.linux === "string") { - replacement = pkg.replaced.linux - } - } else if (os.platform() === "darwin" && pkg.platforms.mac === 0) { - compatible = false; - if (typeof pkg.replaced.mac === "string") { - replacement = pkg.replaced.mac - } - } - - let experimental = false; - if (os.platform() === "win32" && pkg.platforms.windows === 1) { - experimental = true; - } else if (os.platform() === "linux" && pkg.platforms.linux === 1) { - experimental = true; - } else if (os.platform() === "darwin" && pkg.platforms.mac === 1) { - experimental = true; - } - - let version = pkg.verdata.latest; - let publisher = pkg.verdata.publisher.name; - let publisherMail = pkg.verdata.publisher.email; - let date = moment(pkg.verdata.date).fromNow(); - - let signed = false; - let signInfo = ""; - let verified = false; - - if (pkg.sign.signed) { - signed = true; - verified = pkg.sign.verified; - if (pkg.sign.signer.name && pkg.sign.signer.email && pkg.sign.key) { - signInfo = pkg.sign.signer.name + " <" + pkg.sign.signer.email + "> " + c.gray("(" + pkg.sign.key + ")"); - } else if (pkg.sign.signer.name && pkg.sign.key) { - signInfo = pkg.sign.signer.name + c.gray(" (" + pkg.sign.key + ")"); - } else if (pkg.sign.signer.email && pkg.sign.key) { - signInfo = pkg.sign.signer.email + c.gray(" (" + pkg.sign.key + ")"); - } else if (pkg.sign.key) { - signInfo = pkg.sign.key; - } - } - - console.log(c.bold(pkg.name + ": " + pkg.description)) - console.log(" " + c.magentaBright("version:") + " " + version); - console.log(" " + c.magentaBright("last update:") + " " + date); - console.log(" " + c.magentaBright("publisher:") + " " + publisher + " <" + publisherMail + ">"); - - if (signed) { - if (verified) { - console.log(" " + c.magentaBright("security:") + " " + c.green("verified") + "\n " + signInfo); - } else { - console.log(" " + c.magentaBright("security:") + " " + c.yellow("unverified") + "\n " + signInfo); - } - } else { - console.log(" " + c.magentaBright("security:") + " " + c.red.inverse("unsafe")); - } - - if (pkgInstalled) { - if (experimental) { - console.log(" " + c.magentaBright("state:") + " " + c.cyan("installed") + " (" + moment(installed.filter(i => i.id === pkg.id)[0].date).fromNow() + "), " + c.yellow("experimental")) - } else { - console.log(" " + c.magentaBright("state:") + " " + c.cyan("installed") + " (" + moment(installed.filter(i => i.id === pkg.id)[0].date).fromNow() + ")") - } - } else { - if (compatible) { - if (experimental) { - console.log(" " + c.magentaBright("state:") + " " + c.green("compatible") + ", " + c.yellow("experimental")); - } else { - console.log(" " + c.magentaBright("state:") + " " + c.green("compatible")); - } - } else { - if (typeof replacement === "string") { - console.log(" " + c.magentaBright("state:") + " " + c.red("incompatible") + " (replaced by: " + replacement + ")"); - } else { - console.log(" " + c.magentaBright("state:") + " " + c.red("incompatible") + " (no replacement)"); - } - } - } - - if (typeof pkg.deprecated === "string") { - if (pkg.deprecated.toString().trim() !== "") { - console.log(" " + c.magentaBright("support:") + " " + c.red("unsupported") + " (" + pkg.deprecated + ")"); - } else { - console.log(" " + c.magentaBright("support:") + " " + c.red("unsupported")); - } - } else if (typeof pkg.extended === "string") { - if (pkg.extended.toString().trim() !== "") { - console.log(" " + c.magentaBright("support:") + " " + c.yellow("extended") + " (" + pkg.extended + ")"); - } else { - console.log(" " + c.magentaBright("support:") + " " + c.yellow("extended")); - } - } else { - console.log(" " + c.magentaBright("support:") + " " + c.green("supported")); - } +/* + * MIT License + * + * Copyright (c) 2022- Minteck + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + */ + +module.exports = async (argv) => { + const installed = JSON.parse(fs.readFileSync(home + "/installed.json").toString()); + let pkgInstalled = false + if (installed.map(i => i.id).includes(argv.package)) { + pkgInstalled = true; + } + + let packages = JSON.parse(fs.readFileSync(home + "/repository/list.json").toString()); + + if (!packages.includes(argv.package)) { + die(c.red("error: ") + "package '" + argv.package + "' not in repository"); + } + + let dir = argv.package.substring(0, 1).replace(/[^a-zA-Z0-9]/gm, "#"); + let pkg = JSON.parse(fs.readFileSync(home + "/repository/" + dir + "/" + argv.package + ".json").toString()); + + let compatible = true; + let replacement = null; + if (os.platform() === "win32" && pkg.platforms.windows === 0) { + compatible = false; + if (typeof pkg.replaced.windows === "string") { + replacement = pkg.replaced.windows + } + } else if (os.platform() === "linux" && pkg.platforms.linux === 0) { + compatible = false; + if (typeof pkg.replaced.linux === "string") { + replacement = pkg.replaced.linux + } + } else if (os.platform() === "darwin" && pkg.platforms.mac === 0) { + compatible = false; + if (typeof pkg.replaced.mac === "string") { + replacement = pkg.replaced.mac + } + } + + let experimental = false; + if (os.platform() === "win32" && pkg.platforms.windows === 1) { + experimental = true; + } else if (os.platform() === "linux" && pkg.platforms.linux === 1) { + experimental = true; + } else if (os.platform() === "darwin" && pkg.platforms.mac === 1) { + experimental = true; + } + + let version = pkg.verdata.latest; + let publisher = pkg.verdata.publisher.name; + let publisherMail = pkg.verdata.publisher.email; + let date = moment(pkg.verdata.date).fromNow(); + + let signed = false; + let signInfo = ""; + let verified = false; + + if (pkg.sign.signed) { + signed = true; + verified = pkg.sign.verified; + if (pkg.sign.signer.name && pkg.sign.signer.email && pkg.sign.key) { + signInfo = pkg.sign.signer.name + " <" + pkg.sign.signer.email + "> " + c.gray("(" + pkg.sign.key + ")"); + } else if (pkg.sign.signer.name && pkg.sign.key) { + signInfo = pkg.sign.signer.name + c.gray(" (" + pkg.sign.key + ")"); + } else if (pkg.sign.signer.email && pkg.sign.key) { + signInfo = pkg.sign.signer.email + c.gray(" (" + pkg.sign.key + ")"); + } else if (pkg.sign.key) { + signInfo = pkg.sign.key; + } + } + + console.log(c.bold(pkg.name + ": " + pkg.description)) + console.log(" " + c.magentaBright("version:") + " " + version); + console.log(" " + c.magentaBright("last update:") + " " + date); + console.log(" " + c.magentaBright("publisher:") + " " + publisher + " <" + publisherMail + ">"); + + if (signed) { + if (verified) { + console.log(" " + c.magentaBright("security:") + " " + c.green("verified") + "\n " + signInfo); + } else { + console.log(" " + c.magentaBright("security:") + " " + c.yellow("unverified") + "\n " + signInfo); + } + } else { + console.log(" " + c.magentaBright("security:") + " " + c.red.inverse("unsafe")); + } + + if (pkgInstalled) { + if (experimental) { + console.log(" " + c.magentaBright("state:") + " " + c.cyan("installed") + " (" + moment(installed.filter(i => i.id === pkg.id)[0].date).fromNow() + "), " + c.yellow("experimental")) + } else { + console.log(" " + c.magentaBright("state:") + " " + c.cyan("installed") + " (" + moment(installed.filter(i => i.id === pkg.id)[0].date).fromNow() + ")") + } + } else { + if (compatible) { + if (experimental) { + console.log(" " + c.magentaBright("state:") + " " + c.green("compatible") + ", " + c.yellow("experimental")); + } else { + console.log(" " + c.magentaBright("state:") + " " + c.green("compatible")); + } + } else { + if (typeof replacement === "string") { + console.log(" " + c.magentaBright("state:") + " " + c.red("incompatible") + " (replaced by: " + replacement + ")"); + } else { + console.log(" " + c.magentaBright("state:") + " " + c.red("incompatible") + " (no replacement)"); + } + } + } + + if (typeof pkg.deprecated === "string") { + if (pkg.deprecated.toString().trim() !== "") { + console.log(" " + c.magentaBright("support:") + " " + c.red("unsupported") + " (" + pkg.deprecated + ")"); + } else { + console.log(" " + c.magentaBright("support:") + " " + c.red("unsupported")); + } + } else if (typeof pkg.extended === "string") { + if (pkg.extended.toString().trim() !== "") { + console.log(" " + c.magentaBright("support:") + " " + c.yellow("extended") + " (" + pkg.extended + ")"); + } else { + console.log(" " + c.magentaBright("support:") + " " + c.yellow("extended")); + } + } else { + console.log(" " + c.magentaBright("support:") + " " + c.green("supported")); + } } \ No newline at end of file diff --git a/commands/install.js b/commands/install.js index 0448f59..837f9ef 100644 --- a/commands/install.js +++ b/commands/install.js @@ -1,201 +1,201 @@ -/* - * MIT License - * - * Copyright (c) 2022- Minteck - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - * - */ - -module.exports = async (argv) => { - const installed = JSON.parse(fs.readFileSync(home + "/installed.json").toString()); - if (installed.map(i => i.id).includes(argv.package)) { - die(c.red("error: ") + "package '" + argv.package + "' is already installed (version " + installed.filter(i => i.id === argv.package)[0].version + ", installed " + moment(installed.filter(i => i.id === argv.package)[0].date).fromNow() + ")"); - } - - let spinner = ora("Reading package lists...").start(); - let packages = JSON.parse(fs.readFileSync(home + "/repository/list.json").toString()); - spinner.succeed("Reading packages lists... done"); - - if (!packages.includes(argv.package)) { - die(c.red("error: ") + "package '" + argv.package + "' not in repository"); - } - - let dir = argv.package.substring(0, 1).replace(/[^a-zA-Z0-9]/gm, "#"); - let pkg = JSON.parse(fs.readFileSync(home + "/repository/" + dir + "/" + argv.package + ".json").toString()); - - let replacement = null; - if (os.platform() === "win32" && pkg.platforms.windows === 0) { - if (typeof pkg.replaced.windows === "string") { - replacement = pkg.replaced.windows - } - } else if (os.platform() === "linux" && pkg.platforms.linux === 0) { - if (typeof pkg.replaced.linux === "string") { - replacement = pkg.replaced.linux - } - } else if (os.platform() === "darwin" && pkg.platforms.mac === 0) { - if (typeof pkg.replaced.mac === "string") { - replacement = pkg.replaced.mac - } - } - - if (os.platform() === "win32" && pkg.platforms.windows === 0) { - if (typeof replacement === "string") { - die(c.red("error: ") + "package '" + argv.package + "' not available on platform 'win32', replaced by '" + replacement + "'"); - } else { - die(c.red("error: ") + "package '" + argv.package + "' not available on platform 'win32', no replacement available"); - } - } else if (os.platform() === "linux" && pkg.platforms.linux === 0) { - if (typeof replacement === "string") { - die(c.red("error: ") + "package '" + argv.package + "' not available on platform 'linux', replaced by '" + replacement + "'"); - } else { - die(c.red("error: ") + "package '" + argv.package + "' not available on platform 'linux', no replacement available"); - } - } else if (os.platform() === "darwin" && pkg.platforms.mac === 0) { - if (typeof replacement === "string") { - die(c.red("error: ") + "package '" + argv.package + "' not available on platform 'macos', replaced by '" + replacement + "'"); - } else { - die(c.red("error: ") + "package '" + argv.package + "' not available on platform 'macos', no replacement available"); - } - } - - if (os.platform() === "win32" && pkg.platforms.windows === 1) { - console.log(c.yellow("warn: ") + "package '" + argv.package + "' is experimental on platform 'win32'"); - } else if (os.platform() === "linux" && pkg.platforms.linux === 1) { - console.log(c.yellow("warn: ") + "package '" + argv.package + "' is experimental on platform 'linux'"); - } else if (os.platform() === "darwin" && pkg.platforms.mac === 1) { - console.log(c.yellow("warn: ") + "package '" + argv.package + "' 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 '" + argv.package + "' 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") - - let version = pkg.verdata.latest; - let publisher = pkg.verdata.publisher.name; - let publisherMail = pkg.verdata.publisher.email; - let date = moment(pkg.verdata.date).fromNow(); - - let signed = false; - let signInfo = ""; - let verified = false; - - if (pkg.sign.signed) { - signed = true; - verified = pkg.sign.verified; - if (pkg.sign.signer.name && pkg.sign.signer.email && pkg.sign.key) { - signInfo = pkg.sign.signer.name + " <" + pkg.sign.signer.email + "> " + c.gray("(" + pkg.sign.key + ")"); - } else if (pkg.sign.signer.name && pkg.sign.key) { - signInfo = pkg.sign.signer.name + c.gray(" (" + pkg.sign.key + ")"); - } else if (pkg.sign.signer.email && pkg.sign.key) { - signInfo = pkg.sign.signer.email + c.gray(" (" + pkg.sign.key + ")"); - } else if (pkg.sign.key) { - signInfo = pkg.sign.key; - } - } - - console.log(" Installing '" + pkg.name + "'...") - console.log(" version: " + version); - console.log(" release: " + date); - console.log(" publisher: " + publisher + " <" + publisherMail + ">"); - - if (signed) { - if (verified) { - console.log(" security: " + c.green("verified") + " " + signInfo); - } else { - console.log(" security: " + c.yellow("unverified") + " " + signInfo); - } - } else { - console.log(" security: " + c.red.inverse("unsafe")); - } - - if (typeof pkg.deprecated === "string") { - if (pkg.deprecated.toString().trim() !== "") { - console.log(c.yellow("warn: ") + "package '" + pkg.id + "' has been marked as deprecated: " + pkg.deprecated); - } else { - console.log(c.yellow("warn: ") + "package '" + pkg.id + "' has been marked as deprecated"); - } - } - - try { - if (!signed && !(await prompts.confirm({ - message: "This package is unsafe, installing it may damage your system. Are you sure you want to continue?", - initial: false - }))) { - die(); - } - } catch (e) { - die(); - } - - if (fs.existsSync(home + "/buildroot")) { fs.rmSync(home + "/buildroot", { recursive: true }) } - require('../hooks/clone')(pkg.repo, pkg.branch, () => { - spinner = ora("Extracting package...").start(); - fs.renameSync(home + "/buildroot", home + "/packages/" + pkg.id); - installed.push({ - id: pkg.id, - date: new Date().toISOString(), - version, - files: require('../hooks/files')(pkg.id) - }) - fs.writeFileSync(os.homedir() + "/.twilight/installed.json", JSON.stringify(installed)); - spinner.succeed("Extracting package... done"); - console.log(" Size change: +" + require('../hooks/size')(pkg.id)); - - let exec = argv.package; - if (typeof JSON.parse(fs.readFileSync(home + "/repository/" + dir + "/" + argv.package + ".json").toString()).execname === "string") { - exec = JSON.parse(fs.readFileSync(home + "/repository/" + dir + "/" + argv.package + ".json").toString()).execname; - } - - if (os.platform() === "win32" && typeof pkg.executable.windows === "string") { - fs.writeFileSync(os.homedir() + "/.twilight/binaries/" + exec + ".bat", pkg.executable.windows); - } else if (os.platform() === "linux" && typeof pkg.executable.linux === "string") { - fs.writeFileSync(os.homedir() + "/.twilight/binaries/" + exec, pkg.executable.linux); - require('child_process').spawnSync("chmod", [ "+x", os.homedir() + "/.twilight/binaries/" + exec ]) - } else if (os.platform() === "darwin" && typeof pkg.executable.mac === "string") { - fs.writeFileSync(os.homedir() + "/.twilight/binaries/" + exec, pkg.executable.mac); - require('child_process').spawnSync("chmod", [ "+x", os.homedir() + "/.twilight/binaries/" + exec ]) - } - - 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"); - } - }) +/* + * MIT License + * + * Copyright (c) 2022- Minteck + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + */ + +module.exports = async (argv) => { + const installed = JSON.parse(fs.readFileSync(home + "/installed.json").toString()); + if (installed.map(i => i.id).includes(argv.package)) { + die(c.red("error: ") + "package '" + argv.package + "' is already installed (version " + installed.filter(i => i.id === argv.package)[0].version + ", installed " + moment(installed.filter(i => i.id === argv.package)[0].date).fromNow() + ")"); + } + + let spinner = ora("Reading package lists...").start(); + let packages = JSON.parse(fs.readFileSync(home + "/repository/list.json").toString()); + spinner.succeed("Reading packages lists... done"); + + if (!packages.includes(argv.package)) { + die(c.red("error: ") + "package '" + argv.package + "' not in repository"); + } + + let dir = argv.package.substring(0, 1).replace(/[^a-zA-Z0-9]/gm, "#"); + let pkg = JSON.parse(fs.readFileSync(home + "/repository/" + dir + "/" + argv.package + ".json").toString()); + + let replacement = null; + if (os.platform() === "win32" && pkg.platforms.windows === 0) { + if (typeof pkg.replaced.windows === "string") { + replacement = pkg.replaced.windows + } + } else if (os.platform() === "linux" && pkg.platforms.linux === 0) { + if (typeof pkg.replaced.linux === "string") { + replacement = pkg.replaced.linux + } + } else if (os.platform() === "darwin" && pkg.platforms.mac === 0) { + if (typeof pkg.replaced.mac === "string") { + replacement = pkg.replaced.mac + } + } + + if (os.platform() === "win32" && pkg.platforms.windows === 0) { + if (typeof replacement === "string") { + die(c.red("error: ") + "package '" + argv.package + "' not available on platform 'win32', replaced by '" + replacement + "'"); + } else { + die(c.red("error: ") + "package '" + argv.package + "' not available on platform 'win32', no replacement available"); + } + } else if (os.platform() === "linux" && pkg.platforms.linux === 0) { + if (typeof replacement === "string") { + die(c.red("error: ") + "package '" + argv.package + "' not available on platform 'linux', replaced by '" + replacement + "'"); + } else { + die(c.red("error: ") + "package '" + argv.package + "' not available on platform 'linux', no replacement available"); + } + } else if (os.platform() === "darwin" && pkg.platforms.mac === 0) { + if (typeof replacement === "string") { + die(c.red("error: ") + "package '" + argv.package + "' not available on platform 'macos', replaced by '" + replacement + "'"); + } else { + die(c.red("error: ") + "package '" + argv.package + "' not available on platform 'macos', no replacement available"); + } + } + + if (os.platform() === "win32" && pkg.platforms.windows === 1) { + console.log(c.yellow("warn: ") + "package '" + argv.package + "' is experimental on platform 'win32'"); + } else if (os.platform() === "linux" && pkg.platforms.linux === 1) { + console.log(c.yellow("warn: ") + "package '" + argv.package + "' is experimental on platform 'linux'"); + } else if (os.platform() === "darwin" && pkg.platforms.mac === 1) { + console.log(c.yellow("warn: ") + "package '" + argv.package + "' 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 '" + argv.package + "' 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") + + let version = pkg.verdata.latest; + let publisher = pkg.verdata.publisher.name; + let publisherMail = pkg.verdata.publisher.email; + let date = moment(pkg.verdata.date).fromNow(); + + let signed = false; + let signInfo = ""; + let verified = false; + + if (pkg.sign.signed) { + signed = true; + verified = pkg.sign.verified; + if (pkg.sign.signer.name && pkg.sign.signer.email && pkg.sign.key) { + signInfo = pkg.sign.signer.name + " <" + pkg.sign.signer.email + "> " + c.gray("(" + pkg.sign.key + ")"); + } else if (pkg.sign.signer.name && pkg.sign.key) { + signInfo = pkg.sign.signer.name + c.gray(" (" + pkg.sign.key + ")"); + } else if (pkg.sign.signer.email && pkg.sign.key) { + signInfo = pkg.sign.signer.email + c.gray(" (" + pkg.sign.key + ")"); + } else if (pkg.sign.key) { + signInfo = pkg.sign.key; + } + } + + console.log(" Installing '" + pkg.name + "'...") + console.log(" version: " + version); + console.log(" release: " + date); + console.log(" publisher: " + publisher + " <" + publisherMail + ">"); + + if (signed) { + if (verified) { + console.log(" security: " + c.green("verified") + " " + signInfo); + } else { + console.log(" security: " + c.yellow("unverified") + " " + signInfo); + } + } else { + console.log(" security: " + c.red.inverse("unsafe")); + } + + if (typeof pkg.deprecated === "string") { + if (pkg.deprecated.toString().trim() !== "") { + console.log(c.yellow("warn: ") + "package '" + pkg.id + "' has been marked as deprecated: " + pkg.deprecated); + } else { + console.log(c.yellow("warn: ") + "package '" + pkg.id + "' has been marked as deprecated"); + } + } + + try { + if (!signed && !(await prompts.confirm({ + message: "This package is unsafe, installing it may damage your system. Are you sure you want to continue?", + initial: false + }))) { + die(); + } + } catch (e) { + die(); + } + + if (fs.existsSync(home + "/buildroot")) { fs.rmSync(home + "/buildroot", { recursive: true }) } + require('../hooks/clone')(pkg.repo, pkg.branch, () => { + spinner = ora("Extracting package...").start(); + fs.renameSync(home + "/buildroot", home + "/packages/" + pkg.id); + installed.push({ + id: pkg.id, + date: new Date().toISOString(), + version, + files: require('../hooks/files')(pkg.id) + }) + fs.writeFileSync(os.homedir() + "/.twilight/installed.json", JSON.stringify(installed)); + spinner.succeed("Extracting package... done"); + console.log(" Size change: +" + require('../hooks/size')(pkg.id)); + + let exec = argv.package; + if (typeof JSON.parse(fs.readFileSync(home + "/repository/" + dir + "/" + argv.package + ".json").toString()).execname === "string") { + exec = JSON.parse(fs.readFileSync(home + "/repository/" + dir + "/" + argv.package + ".json").toString()).execname; + } + + if (os.platform() === "win32" && typeof pkg.executable.windows === "string") { + fs.writeFileSync(os.homedir() + "/.twilight/binaries/" + exec + ".bat", pkg.executable.windows); + } else if (os.platform() === "linux" && typeof pkg.executable.linux === "string") { + fs.writeFileSync(os.homedir() + "/.twilight/binaries/" + exec, pkg.executable.linux); + require('child_process').spawnSync("chmod", [ "+x", os.homedir() + "/.twilight/binaries/" + exec ]) + } else if (os.platform() === "darwin" && typeof pkg.executable.mac === "string") { + fs.writeFileSync(os.homedir() + "/.twilight/binaries/" + exec, pkg.executable.mac); + require('child_process').spawnSync("chmod", [ "+x", os.homedir() + "/.twilight/binaries/" + exec ]) + } + + 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"); + } + }) } \ No newline at end of file diff --git a/commands/installable.js b/commands/installable.js index 00d1ecb..2c80d58 100644 --- a/commands/installable.js +++ b/commands/installable.js @@ -1,101 +1,101 @@ -/* - * MIT License - * - * Copyright (c) 2022- Minteck - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - * - */ - -module.exports = async () => { - let packages = JSON.parse(fs.readFileSync(home + "/repository/list.json").toString()).sort(); - let installed = JSON.parse(fs.readFileSync(home + "/installed.json").toString()).map(i => i.id); - let installs = JSON.parse(fs.readFileSync(home + "/installed.json").toString()); - - let signs = {}; - let dates = {}; - let platforms = {}; - - for (let pkg of packages) { - let dir = pkg.substring(0, 1).replace(/[^a-zA-Z0-9]/gm, "#"); - let pack = JSON.parse(fs.readFileSync(home + "/repository/" + dir + "/" + pkg + ".json").toString()); - - let signed = false; - let signInfo = ""; - let verified = false; - - if (pack.sign.signed) { - signed = true; - verified = pack.sign.verified; - if (pack.sign.signer.name && pack.sign.signer.email && pack.sign.key) { - signInfo = pack.sign.signer.name + " <" + pack.sign.signer.email + "> " + c.gray("(" + pack.sign.key + ")"); - } else if (pack.sign.signer.name && pack.sign.key) { - signInfo = pack.sign.signer.name + c.gray(" (" + pack.sign.key + ")"); - } else if (pkg.sign.signer.email && pkg.sign.key) { - signInfo = pkg.sign.signer.email + c.gray(" (" + pkg.sign.key + ")"); - } else if (pkg.sign.key) { - signInfo = pkg.sign.key; - } - } - - if (signed) { - if (verified) { - signs[pack.id] = c.green("verified"); - } else { - signs[pack.id] = c.yellow("unverified"); - } - } else { - signs[pack.id] = c.red("unsafe"); - } - - if (pack.platforms.windows === 2 && pack.platforms.linux === 2 && pack.platforms.mac === 2) { - platforms[pack.id] = c.cyan("all"); - } else if (pack.platforms.windows === 1 && pack.platforms.linux === 1 && pack.platforms.mac === 1) { - platforms[pack.id] = c.yellow("all"); - } else { - platforms[pack.id] = ""; - if (pack.platforms.windows === 1) { platforms[pack.id] += c.yellow("win32") + "," } - if (pack.platforms.windows === 2) { platforms[pack.id] += c.cyan("win32") + "," } - if (pack.platforms.linux === 1) { platforms[pack.id] += c.yellow("linux") + "," } - if (pack.platforms.linux === 2) { platforms[pack.id] += c.cyan("linux") + "," } - if (pack.platforms.mac === 1) { platforms[pack.id] += c.yellow("macos") + "," } - if (pack.platforms.mac === 2) { platforms[pack.id] += c.cyan("macos") + "," } - - if (platforms[pack.id].endsWith(",")) { platforms[pack.id] = platforms[pack.id].substring(0, platforms[pack.id].length - 1) } - } - } - - for (let pkg of packages) { - let dir = pkg.substring(0, 1).replace(/[^a-zA-Z0-9]/gm, "#"); - let pack = JSON.parse(fs.readFileSync(home + "/repository/" + dir + "/" + pkg + ".json").toString()); - - installable = true; - if (os.platform() === "win32" && pack.platforms.windows === 0) { - installable = false; - } else if (os.platform() === "linux" && pack.platforms.linux === 0) { - installable = false; - } else if (os.platform() === "darwin" && pack.platforms.mac === 0) { - installable = false; - } - - if (installable) { - console.log(c.green(pack.id) + "/" + signs[pack.id] + " " + pack.verdata.latest + " " + platforms[pack.id]) - } - } +/* + * MIT License + * + * Copyright (c) 2022- Minteck + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + */ + +module.exports = async () => { + let packages = JSON.parse(fs.readFileSync(home + "/repository/list.json").toString()).sort(); + let installed = JSON.parse(fs.readFileSync(home + "/installed.json").toString()).map(i => i.id); + let installs = JSON.parse(fs.readFileSync(home + "/installed.json").toString()); + + let signs = {}; + let dates = {}; + let platforms = {}; + + for (let pkg of packages) { + let dir = pkg.substring(0, 1).replace(/[^a-zA-Z0-9]/gm, "#"); + let pack = JSON.parse(fs.readFileSync(home + "/repository/" + dir + "/" + pkg + ".json").toString()); + + let signed = false; + let signInfo = ""; + let verified = false; + + if (pack.sign.signed) { + signed = true; + verified = pack.sign.verified; + if (pack.sign.signer.name && pack.sign.signer.email && pack.sign.key) { + signInfo = pack.sign.signer.name + " <" + pack.sign.signer.email + "> " + c.gray("(" + pack.sign.key + ")"); + } else if (pack.sign.signer.name && pack.sign.key) { + signInfo = pack.sign.signer.name + c.gray(" (" + pack.sign.key + ")"); + } else if (pkg.sign.signer.email && pkg.sign.key) { + signInfo = pkg.sign.signer.email + c.gray(" (" + pkg.sign.key + ")"); + } else if (pkg.sign.key) { + signInfo = pkg.sign.key; + } + } + + if (signed) { + if (verified) { + signs[pack.id] = c.green("verified"); + } else { + signs[pack.id] = c.yellow("unverified"); + } + } else { + signs[pack.id] = c.red("unsafe"); + } + + if (pack.platforms.windows === 2 && pack.platforms.linux === 2 && pack.platforms.mac === 2) { + platforms[pack.id] = c.cyan("all"); + } else if (pack.platforms.windows === 1 && pack.platforms.linux === 1 && pack.platforms.mac === 1) { + platforms[pack.id] = c.yellow("all"); + } else { + platforms[pack.id] = ""; + if (pack.platforms.windows === 1) { platforms[pack.id] += c.yellow("win32") + "," } + if (pack.platforms.windows === 2) { platforms[pack.id] += c.cyan("win32") + "," } + if (pack.platforms.linux === 1) { platforms[pack.id] += c.yellow("linux") + "," } + if (pack.platforms.linux === 2) { platforms[pack.id] += c.cyan("linux") + "," } + if (pack.platforms.mac === 1) { platforms[pack.id] += c.yellow("macos") + "," } + if (pack.platforms.mac === 2) { platforms[pack.id] += c.cyan("macos") + "," } + + if (platforms[pack.id].endsWith(",")) { platforms[pack.id] = platforms[pack.id].substring(0, platforms[pack.id].length - 1) } + } + } + + for (let pkg of packages) { + let dir = pkg.substring(0, 1).replace(/[^a-zA-Z0-9]/gm, "#"); + let pack = JSON.parse(fs.readFileSync(home + "/repository/" + dir + "/" + pkg + ".json").toString()); + + installable = true; + if (os.platform() === "win32" && pack.platforms.windows === 0) { + installable = false; + } else if (os.platform() === "linux" && pack.platforms.linux === 0) { + installable = false; + } else if (os.platform() === "darwin" && pack.platforms.mac === 0) { + installable = false; + } + + if (installable) { + console.log(c.green(pack.id) + "/" + signs[pack.id] + " " + pack.verdata.latest + " " + platforms[pack.id]) + } + } } \ No newline at end of file diff --git a/commands/installed.js b/commands/installed.js index 11a7616..c22e538 100644 --- a/commands/installed.js +++ b/commands/installed.js @@ -1,93 +1,93 @@ -/* - * MIT License - * - * Copyright (c) 2022- Minteck - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - * - */ - -module.exports = async () => { - let packages = JSON.parse(fs.readFileSync(home + "/repository/list.json").toString()).sort(); - let installed = JSON.parse(fs.readFileSync(home + "/installed.json").toString()).map(i => i.id); - let installs = JSON.parse(fs.readFileSync(home + "/installed.json").toString()); - - let signs = {}; - let dates = {}; - let platforms = {}; - - for (let pkg of packages) { - let dir = pkg.substring(0, 1).replace(/[^a-zA-Z0-9]/gm, "#"); - let pack = JSON.parse(fs.readFileSync(home + "/repository/" + dir + "/" + pkg + ".json").toString()); - - let signed = false; - let signInfo = ""; - let verified = false; - - if (pack.sign.signed) { - signed = true; - verified = pack.sign.verified; - if (pack.sign.signer.name && pack.sign.signer.email && pack.sign.key) { - signInfo = pack.sign.signer.name + " <" + pack.sign.signer.email + "> " + c.gray("(" + pack.sign.key + ")"); - } else if (pack.sign.signer.name && pack.sign.key) { - signInfo = pack.sign.signer.name + c.gray(" (" + pack.sign.key + ")"); - } else if (pkg.sign.signer.email && pkg.sign.key) { - signInfo = pkg.sign.signer.email + c.gray(" (" + pkg.sign.key + ")"); - } else if (pkg.sign.key) { - signInfo = pkg.sign.key; - } - } - - if (signed) { - if (verified) { - signs[pack.id] = c.green("verified"); - } else { - signs[pack.id] = c.yellow("unverified"); - } - } else { - signs[pack.id] = c.red("unsafe"); - } - - if (pack.platforms.windows === 2 && pack.platforms.linux === 2 && pack.platforms.mac === 2) { - platforms[pack.id] = c.cyan("all"); - } else if (pack.platforms.windows === 1 && pack.platforms.linux === 1 && pack.platforms.mac === 1) { - platforms[pack.id] = c.yellow("all"); - } else { - platforms[pack.id] = ""; - if (pack.platforms.windows === 1) { platforms[pack.id] += c.yellow("win32") + "," } - if (pack.platforms.windows === 2) { platforms[pack.id] += c.cyan("win32") + "," } - if (pack.platforms.linux === 1) { platforms[pack.id] += c.yellow("linux") + "," } - if (pack.platforms.linux === 2) { platforms[pack.id] += c.cyan("linux") + "," } - if (pack.platforms.mac === 1) { platforms[pack.id] += c.yellow("macos") + "," } - if (pack.platforms.mac === 2) { platforms[pack.id] += c.cyan("macos") + "," } - - if (platforms[pack.id].endsWith(",")) { platforms[pack.id] = platforms[pack.id].substring(0, platforms[pack.id].length - 1) } - } - } - - for (let pkg of packages) { - let dir = pkg.substring(0, 1).replace(/[^a-zA-Z0-9]/gm, "#"); - let pack = JSON.parse(fs.readFileSync(home + "/repository/" + dir + "/" + pkg + ".json").toString()); - - let add = ""; - if (installed.includes(pack.id)) { - console.log(c.green(pack.id) + "/" + signs[pack.id] + " " + pack.verdata.latest + " " + platforms[pack.id] + add) - } - } +/* + * MIT License + * + * Copyright (c) 2022- Minteck + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + */ + +module.exports = async () => { + let packages = JSON.parse(fs.readFileSync(home + "/repository/list.json").toString()).sort(); + let installed = JSON.parse(fs.readFileSync(home + "/installed.json").toString()).map(i => i.id); + let installs = JSON.parse(fs.readFileSync(home + "/installed.json").toString()); + + let signs = {}; + let dates = {}; + let platforms = {}; + + for (let pkg of packages) { + let dir = pkg.substring(0, 1).replace(/[^a-zA-Z0-9]/gm, "#"); + let pack = JSON.parse(fs.readFileSync(home + "/repository/" + dir + "/" + pkg + ".json").toString()); + + let signed = false; + let signInfo = ""; + let verified = false; + + if (pack.sign.signed) { + signed = true; + verified = pack.sign.verified; + if (pack.sign.signer.name && pack.sign.signer.email && pack.sign.key) { + signInfo = pack.sign.signer.name + " <" + pack.sign.signer.email + "> " + c.gray("(" + pack.sign.key + ")"); + } else if (pack.sign.signer.name && pack.sign.key) { + signInfo = pack.sign.signer.name + c.gray(" (" + pack.sign.key + ")"); + } else if (pkg.sign.signer.email && pkg.sign.key) { + signInfo = pkg.sign.signer.email + c.gray(" (" + pkg.sign.key + ")"); + } else if (pkg.sign.key) { + signInfo = pkg.sign.key; + } + } + + if (signed) { + if (verified) { + signs[pack.id] = c.green("verified"); + } else { + signs[pack.id] = c.yellow("unverified"); + } + } else { + signs[pack.id] = c.red("unsafe"); + } + + if (pack.platforms.windows === 2 && pack.platforms.linux === 2 && pack.platforms.mac === 2) { + platforms[pack.id] = c.cyan("all"); + } else if (pack.platforms.windows === 1 && pack.platforms.linux === 1 && pack.platforms.mac === 1) { + platforms[pack.id] = c.yellow("all"); + } else { + platforms[pack.id] = ""; + if (pack.platforms.windows === 1) { platforms[pack.id] += c.yellow("win32") + "," } + if (pack.platforms.windows === 2) { platforms[pack.id] += c.cyan("win32") + "," } + if (pack.platforms.linux === 1) { platforms[pack.id] += c.yellow("linux") + "," } + if (pack.platforms.linux === 2) { platforms[pack.id] += c.cyan("linux") + "," } + if (pack.platforms.mac === 1) { platforms[pack.id] += c.yellow("macos") + "," } + if (pack.platforms.mac === 2) { platforms[pack.id] += c.cyan("macos") + "," } + + if (platforms[pack.id].endsWith(",")) { platforms[pack.id] = platforms[pack.id].substring(0, platforms[pack.id].length - 1) } + } + } + + for (let pkg of packages) { + let dir = pkg.substring(0, 1).replace(/[^a-zA-Z0-9]/gm, "#"); + let pack = JSON.parse(fs.readFileSync(home + "/repository/" + dir + "/" + pkg + ".json").toString()); + + let add = ""; + if (installed.includes(pack.id)) { + console.log(c.green(pack.id) + "/" + signs[pack.id] + " " + pack.verdata.latest + " " + platforms[pack.id] + add) + } + } } \ No newline at end of file diff --git a/commands/list.js b/commands/list.js index 40f5b65..871f821 100644 --- a/commands/list.js +++ b/commands/list.js @@ -1,99 +1,99 @@ -/* - * MIT License - * - * Copyright (c) 2022- Minteck - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - * - */ - -module.exports = async () => { - let packages = JSON.parse(fs.readFileSync(home + "/repository/list.json").toString()).sort(); - let installed = JSON.parse(fs.readFileSync(home + "/installed.json").toString()).map(i => i.id); - let installs = JSON.parse(fs.readFileSync(home + "/installed.json").toString()); - - let signs = {}; - let dates = {}; - let platforms = {}; - - for (let pkg of packages) { - let dir = pkg.substring(0, 1).replace(/[^a-zA-Z0-9]/gm, "#"); - let pack = JSON.parse(fs.readFileSync(home + "/repository/" + dir + "/" + pkg + ".json").toString()); - - let signed = false; - let signInfo = ""; - let verified = false; - - if (pack.sign.signed) { - signed = true; - verified = pack.sign.verified; - if (pack.sign.signer.name && pack.sign.signer.email && pack.sign.key) { - signInfo = pack.sign.signer.name + " <" + pack.sign.signer.email + "> " + c.gray("(" + pack.sign.key + ")"); - } else if (pack.sign.signer.name && pack.sign.key) { - signInfo = pack.sign.signer.name + c.gray(" (" + pack.sign.key + ")"); - } else if (pkg.sign.signer.email && pkg.sign.key) { - signInfo = pkg.sign.signer.email + c.gray(" (" + pkg.sign.key + ")"); - } else if (pkg.sign.key) { - signInfo = pkg.sign.key; - } - } - - if (signed) { - if (verified) { - signs[pack.id] = c.green("verified"); - } else { - signs[pack.id] = c.yellow("unverified"); - } - } else { - signs[pack.id] = c.red("unsafe"); - } - - if (pack.platforms.windows === 2 && pack.platforms.linux === 2 && pack.platforms.mac === 2) { - platforms[pack.id] = c.cyan("all"); - } else if (pack.platforms.windows === 1 && pack.platforms.linux === 1 && pack.platforms.mac === 1) { - platforms[pack.id] = c.yellow("all"); - } else { - platforms[pack.id] = ""; - if (pack.platforms.windows === 1) { platforms[pack.id] += c.yellow("win32") + "," } - if (pack.platforms.windows === 2) { platforms[pack.id] += c.cyan("win32") + "," } - if (pack.platforms.linux === 1) { platforms[pack.id] += c.yellow("linux") + "," } - if (pack.platforms.linux === 2) { platforms[pack.id] += c.cyan("linux") + "," } - if (pack.platforms.mac === 1) { platforms[pack.id] += c.yellow("macos") + "," } - if (pack.platforms.mac === 2) { platforms[pack.id] += c.cyan("macos") + "," } - - if (platforms[pack.id].endsWith(",")) { platforms[pack.id] = platforms[pack.id].substring(0, platforms[pack.id].length - 1) } - } - } - - for (let pkg of packages) { - let dir = pkg.substring(0, 1).replace(/[^a-zA-Z0-9]/gm, "#"); - let pack = JSON.parse(fs.readFileSync(home + "/repository/" + dir + "/" + pkg + ".json").toString()); - - let add = ""; - if (installed.includes(pack.id)) { - if (installs.filter(i => i.id === pack.id)[0].version !== pack.verdata.latest) { - add = c.gray(" [upgradable]"); - } else { - add = c.gray(" [installed]"); - } - } - - console.log(c.green(pack.id) + "/" + signs[pack.id] + " " + pack.verdata.latest + " " + platforms[pack.id] + add) - } +/* + * MIT License + * + * Copyright (c) 2022- Minteck + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + */ + +module.exports = async () => { + let packages = JSON.parse(fs.readFileSync(home + "/repository/list.json").toString()).sort(); + let installed = JSON.parse(fs.readFileSync(home + "/installed.json").toString()).map(i => i.id); + let installs = JSON.parse(fs.readFileSync(home + "/installed.json").toString()); + + let signs = {}; + let dates = {}; + let platforms = {}; + + for (let pkg of packages) { + let dir = pkg.substring(0, 1).replace(/[^a-zA-Z0-9]/gm, "#"); + let pack = JSON.parse(fs.readFileSync(home + "/repository/" + dir + "/" + pkg + ".json").toString()); + + let signed = false; + let signInfo = ""; + let verified = false; + + if (pack.sign.signed) { + signed = true; + verified = pack.sign.verified; + if (pack.sign.signer.name && pack.sign.signer.email && pack.sign.key) { + signInfo = pack.sign.signer.name + " <" + pack.sign.signer.email + "> " + c.gray("(" + pack.sign.key + ")"); + } else if (pack.sign.signer.name && pack.sign.key) { + signInfo = pack.sign.signer.name + c.gray(" (" + pack.sign.key + ")"); + } else if (pkg.sign.signer.email && pkg.sign.key) { + signInfo = pkg.sign.signer.email + c.gray(" (" + pkg.sign.key + ")"); + } else if (pkg.sign.key) { + signInfo = pkg.sign.key; + } + } + + if (signed) { + if (verified) { + signs[pack.id] = c.green("verified"); + } else { + signs[pack.id] = c.yellow("unverified"); + } + } else { + signs[pack.id] = c.red("unsafe"); + } + + if (pack.platforms.windows === 2 && pack.platforms.linux === 2 && pack.platforms.mac === 2) { + platforms[pack.id] = c.cyan("all"); + } else if (pack.platforms.windows === 1 && pack.platforms.linux === 1 && pack.platforms.mac === 1) { + platforms[pack.id] = c.yellow("all"); + } else { + platforms[pack.id] = ""; + if (pack.platforms.windows === 1) { platforms[pack.id] += c.yellow("win32") + "," } + if (pack.platforms.windows === 2) { platforms[pack.id] += c.cyan("win32") + "," } + if (pack.platforms.linux === 1) { platforms[pack.id] += c.yellow("linux") + "," } + if (pack.platforms.linux === 2) { platforms[pack.id] += c.cyan("linux") + "," } + if (pack.platforms.mac === 1) { platforms[pack.id] += c.yellow("macos") + "," } + if (pack.platforms.mac === 2) { platforms[pack.id] += c.cyan("macos") + "," } + + if (platforms[pack.id].endsWith(",")) { platforms[pack.id] = platforms[pack.id].substring(0, platforms[pack.id].length - 1) } + } + } + + for (let pkg of packages) { + let dir = pkg.substring(0, 1).replace(/[^a-zA-Z0-9]/gm, "#"); + let pack = JSON.parse(fs.readFileSync(home + "/repository/" + dir + "/" + pkg + ".json").toString()); + + let add = ""; + if (installed.includes(pack.id)) { + if (installs.filter(i => i.id === pack.id)[0].version !== pack.verdata.latest) { + add = c.gray(" [upgradable]"); + } else { + add = c.gray(" [installed]"); + } + } + + console.log(c.green(pack.id) + "/" + signs[pack.id] + " " + pack.verdata.latest + " " + platforms[pack.id] + add) + } } \ No newline at end of file diff --git a/commands/purge.js b/commands/purge.js index 96f8b75..f698bdc 100644 --- a/commands/purge.js +++ b/commands/purge.js @@ -1,79 +1,79 @@ -/* - * MIT License - * - * Copyright (c) 2022- Minteck - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - * - */ - -module.exports = async (argv) => { - const installed = JSON.parse(fs.readFileSync(home + "/installed.json").toString()); - if (!installed.map(i => i.id).includes(argv.package)) { - die(c.red("error: ") + "package '" + argv.package + "' is not installed"); - } - - let spinner = ora("Reading package lists...").start(); - let packages = JSON.parse(fs.readFileSync(home + "/repository/list.json").toString()); - spinner.succeed("Reading packages lists... done"); - - let dir = argv.package.substring(0, 1).replace(/[^a-zA-Z0-9]/gm, "#"); - - if (!packages.includes(argv.package)) { - console.log(c.yellow("warn: ") + "package '" + argv.package + "' not in repository anymore"); - name = argv.package; - } else { - name = JSON.parse(fs.readFileSync(home + "/repository/" + dir + "/" + argv.package + ".json").toString()).name; - } - - instInfo = installed.filter(i => i.id === argv.package)[0]; - - console.log(" Purging '" + name + "'...") - console.log(" version: " + instInfo.version); - console.log(" installed: " + moment(instInfo.date).fromNow()); - - try { - if (!(await prompts.confirm({ - message: "Purging this package will delete all associated data. Are you sure you want to continue?", - initial: false - }))) { - die(); - } - } catch (e) { - die(); - } - - spinner = ora("Reading files list...").start(); - files1 = instInfo.files; - files2 = require('../hooks/files')(instInfo.id); - filesR = files2.filter(f => !files1.includes(f)); - spinner.succeed("Reading files list... done"); - if (filesR.length === 0) { - die(c.red("error: ") + "this installation of package '" + argv.package + "' cannot be purged, use 'twi reinstall' instead"); - } - - spinner = ora("Removing user files...").start(); - let index = 0 - for (let file of filesR) { - spinner.text = "Removing user files... " + Math.round((index / filesR.length) * 100) + "%"; - fs.rmSync(home + "/packages/" + argv.package + "/" + file); - index++; - } - spinner.succeed("Removing user files... done"); +/* + * MIT License + * + * Copyright (c) 2022- Minteck + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + */ + +module.exports = async (argv) => { + const installed = JSON.parse(fs.readFileSync(home + "/installed.json").toString()); + if (!installed.map(i => i.id).includes(argv.package)) { + die(c.red("error: ") + "package '" + argv.package + "' is not installed"); + } + + let spinner = ora("Reading package lists...").start(); + let packages = JSON.parse(fs.readFileSync(home + "/repository/list.json").toString()); + spinner.succeed("Reading packages lists... done"); + + let dir = argv.package.substring(0, 1).replace(/[^a-zA-Z0-9]/gm, "#"); + + if (!packages.includes(argv.package)) { + console.log(c.yellow("warn: ") + "package '" + argv.package + "' not in repository anymore"); + name = argv.package; + } else { + name = JSON.parse(fs.readFileSync(home + "/repository/" + dir + "/" + argv.package + ".json").toString()).name; + } + + instInfo = installed.filter(i => i.id === argv.package)[0]; + + console.log(" Purging '" + name + "'...") + console.log(" version: " + instInfo.version); + console.log(" installed: " + moment(instInfo.date).fromNow()); + + try { + if (!(await prompts.confirm({ + message: "Purging this package will delete all associated data. Are you sure you want to continue?", + initial: false + }))) { + die(); + } + } catch (e) { + die(); + } + + spinner = ora("Reading files list...").start(); + files1 = instInfo.files; + files2 = require('../hooks/files')(instInfo.id); + filesR = files2.filter(f => !files1.includes(f)); + spinner.succeed("Reading files list... done"); + if (filesR.length === 0) { + die(c.red("error: ") + "this installation of package '" + argv.package + "' cannot be purged, use 'twi reinstall' instead"); + } + + spinner = ora("Removing user files...").start(); + let index = 0 + for (let file of filesR) { + spinner.text = "Removing user files... " + Math.round((index / filesR.length) * 100) + "%"; + fs.rmSync(home + "/packages/" + argv.package + "/" + file); + index++; + } + spinner.succeed("Removing user files... done"); } \ No newline at end of file diff --git a/commands/reinstall.js b/commands/reinstall.js index 497979f..dbadf8a 100644 --- a/commands/reinstall.js +++ b/commands/reinstall.js @@ -1,30 +1,30 @@ -/* - * MIT License - * - * Copyright (c) 2022- Minteck - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - * - */ - -module.exports = async (argv) => { - await require('./remove')(argv, true); - fs.writeFileSync(os.homedir() + "/.twilight/installed.json", JSON.stringify(JSON.parse(fs.readFileSync(os.homedir() + "/.twilight/installed.json").toString()).filter(i => i !== null))); - await require('./install')(argv); +/* + * MIT License + * + * Copyright (c) 2022- Minteck + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + */ + +module.exports = async (argv) => { + await require('./remove')(argv, true); + fs.writeFileSync(os.homedir() + "/.twilight/installed.json", JSON.stringify(JSON.parse(fs.readFileSync(os.homedir() + "/.twilight/installed.json").toString()).filter(i => i !== null))); + await require('./install')(argv); } \ No newline at end of file diff --git a/commands/remove.js b/commands/remove.js index bde1e81..dc26eae 100644 --- a/commands/remove.js +++ b/commands/remove.js @@ -1,120 +1,120 @@ -/* - * MIT License - * - * Copyright (c) 2022- Minteck - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - * - */ - -module.exports = async (argv, reinstalling) => { - if (reinstalling === undefined) { - reinstalling = false; - } - - const installed = JSON.parse(fs.readFileSync(home + "/installed.json").toString()); - if (!installed.map(i => i.id).includes(argv.package)) { - die(c.red("error: ") + "package '" + argv.package + "' is not installed"); - } - - let spinner = ora("Reading package lists...").start(); - let packages = JSON.parse(fs.readFileSync(home + "/repository/list.json").toString()); - spinner.succeed("Reading packages lists... done"); - let updated = false; - - let dir = argv.package.substring(0, 1).replace(/[^a-zA-Z0-9]/gm, "#"); - - if (!packages.includes(argv.package)) { - console.log(c.yellow("warn: ") + "package '" + argv.package + "' not in repository anymore, unable to fetch for name"); - name = argv.package; - } else { - name = JSON.parse(fs.readFileSync(home + "/repository/" + dir + "/" + argv.package + ".json").toString()).name; - } - - if (argv.package === "twilight") { - die(c.red("error: ") + "package 'twilight' is system package and cannot be uninstalled, use 'twilight-setup' instead"); - } - - instInfo = installed.filter(i => i.id === argv.package)[0]; - - console.log(" Uninstalling '" + name + "'...") - console.log(" version: " + instInfo.version); - console.log(" installed: " + moment(instInfo.date).fromNow()); - console.log(" size: " + require('../hooks/size.js')(argv.package)); - - if (reinstalling) { - try { - if (!(await prompts.confirm({ - message: "Reinstalling this package will delete all associated data. Are you sure you want to continue?", - initial: false - }))) { - die(); - } - } catch (e) { - die(); - } - } else { - try { - if (!(await prompts.confirm({ - message: "Uninstalling this package will also delete all associated data. Are you sure you want to continue?", - initial: false - }))) { - die(); - } - } catch (e) { - die(); - } - } - - spinner = ora("Removing package...").start(); - 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)); - - let exec = argv.package; - if (!packages.includes(argv.package)) { - exec = argv.package; - } else if (typeof JSON.parse(fs.readFileSync(home + "/repository/" + dir + "/" + argv.package + ".json").toString()).execname === "string") { - exec = JSON.parse(fs.readFileSync(home + "/repository/" + dir + "/" + argv.package + ".json").toString()).execname; - } - if (fs.existsSync(os.homedir() + "/.twilight/binaries/" + exec + ".bat")) fs.unlinkSync(os.homedir() + "/.twilight/binaries/" + exec + ".bat"); - if (fs.existsSync(os.homedir() + "/.twilight/binaries/" + exec + ".sh")) fs.unlinkSync(os.homedir() + "/.twilight/binaries/" + exec + ".sh"); - if (fs.existsSync(os.homedir() + "/.twilight/binaries/" + exec)) fs.unlinkSync(os.homedir() + "/.twilight/binaries/" + exec); - - spinner.succeed("Removing package... done"); - - if (!packages.includes(argv.package)) { - console.log(c.yellow("warn: ") + "package '" + argv.package + "' not in repository anymore, unable to fetch for post-remove hooks"); - } else { - ppr = JSON.parse(fs.readFileSync(home + "/repository/" + dir + "/" + argv.package + ".json").toString()).postremove; - - let postremove = []; - if (os.platform() === "win32") postremove = ppr.windows; - if (os.platform() === "linux") postremove = ppr.linux; - if (os.platform() === "darwin") postremove = ppr.mac; - - if (postremove.length > 0) { - spinner = ora("Running post-remove hooks...").start(); - for (let hook of postremove) { - require('child_process').execSync(hook, { stdio: "inherit" }) - } - spinner.succeed("Running post-remove hooks... done"); - } - } +/* + * MIT License + * + * Copyright (c) 2022- Minteck + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + */ + +module.exports = async (argv, reinstalling) => { + if (reinstalling === undefined) { + reinstalling = false; + } + + const installed = JSON.parse(fs.readFileSync(home + "/installed.json").toString()); + if (!installed.map(i => i.id).includes(argv.package)) { + die(c.red("error: ") + "package '" + argv.package + "' is not installed"); + } + + let spinner = ora("Reading package lists...").start(); + let packages = JSON.parse(fs.readFileSync(home + "/repository/list.json").toString()); + spinner.succeed("Reading packages lists... done"); + let updated = false; + + let dir = argv.package.substring(0, 1).replace(/[^a-zA-Z0-9]/gm, "#"); + + if (!packages.includes(argv.package)) { + console.log(c.yellow("warn: ") + "package '" + argv.package + "' not in repository anymore, unable to fetch for name"); + name = argv.package; + } else { + name = JSON.parse(fs.readFileSync(home + "/repository/" + dir + "/" + argv.package + ".json").toString()).name; + } + + if (argv.package === "twilight") { + die(c.red("error: ") + "package 'twilight' is system package and cannot be uninstalled, use 'twilight-setup' instead"); + } + + instInfo = installed.filter(i => i.id === argv.package)[0]; + + console.log(" Uninstalling '" + name + "'...") + console.log(" version: " + instInfo.version); + console.log(" installed: " + moment(instInfo.date).fromNow()); + console.log(" size: " + require('../hooks/size.js')(argv.package)); + + if (reinstalling) { + try { + if (!(await prompts.confirm({ + message: "Reinstalling this package will delete all associated data. Are you sure you want to continue?", + initial: false + }))) { + die(); + } + } catch (e) { + die(); + } + } else { + try { + if (!(await prompts.confirm({ + message: "Uninstalling this package will also delete all associated data. Are you sure you want to continue?", + initial: false + }))) { + die(); + } + } catch (e) { + die(); + } + } + + spinner = ora("Removing package...").start(); + 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)); + + let exec = argv.package; + if (!packages.includes(argv.package)) { + exec = argv.package; + } else if (typeof JSON.parse(fs.readFileSync(home + "/repository/" + dir + "/" + argv.package + ".json").toString()).execname === "string") { + exec = JSON.parse(fs.readFileSync(home + "/repository/" + dir + "/" + argv.package + ".json").toString()).execname; + } + if (fs.existsSync(os.homedir() + "/.twilight/binaries/" + exec + ".bat")) fs.unlinkSync(os.homedir() + "/.twilight/binaries/" + exec + ".bat"); + if (fs.existsSync(os.homedir() + "/.twilight/binaries/" + exec + ".sh")) fs.unlinkSync(os.homedir() + "/.twilight/binaries/" + exec + ".sh"); + if (fs.existsSync(os.homedir() + "/.twilight/binaries/" + exec)) fs.unlinkSync(os.homedir() + "/.twilight/binaries/" + exec); + + spinner.succeed("Removing package... done"); + + if (!packages.includes(argv.package)) { + console.log(c.yellow("warn: ") + "package '" + argv.package + "' not in repository anymore, unable to fetch for post-remove hooks"); + } else { + ppr = JSON.parse(fs.readFileSync(home + "/repository/" + dir + "/" + argv.package + ".json").toString()).postremove; + + let postremove = []; + if (os.platform() === "win32") postremove = ppr.windows; + if (os.platform() === "linux") postremove = ppr.linux; + if (os.platform() === "darwin") postremove = ppr.mac; + + if (postremove.length > 0) { + spinner = ora("Running post-remove hooks...").start(); + for (let hook of postremove) { + require('child_process').execSync(hook, { stdio: "inherit" }) + } + spinner.succeed("Running post-remove hooks... done"); + } + } } \ No newline at end of file diff --git a/commands/update.js b/commands/update.js index 3b1bae8..ad34165 100644 --- a/commands/update.js +++ b/commands/update.js @@ -1,28 +1,28 @@ -/* - * MIT License - * - * Copyright (c) 2022- Minteck - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - * - */ - -module.exports = async (argv) => { - await require('../hooks/update')(); +/* + * MIT License + * + * Copyright (c) 2022- Minteck + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + */ + +module.exports = async (argv) => { + await require('../hooks/update')(); } \ No newline at end of file diff --git a/commands/upgrade.js b/commands/upgrade.js index 60f47fc..ac36fd9 100644 --- a/commands/upgrade.js +++ b/commands/upgrade.js @@ -1,236 +1,236 @@ -/* - * MIT License - * - * Copyright (c) 2022- Minteck - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - * - */ - -async function processQueue() { - let pack = installed.filter(i => i.id === queue[0])[0]; - let dir = pack.id.substring(0, 1).replace(/[^a-zA-Z0-9]/gm, "#"); - let pkg = JSON.parse(fs.readFileSync(home + "/repository/" + dir + "/" + pack.id + ".json").toString()); - let installable = true; - - let version = pkg.verdata.latest; - let publisher = pkg.verdata.publisher.name; - let publisherMail = pkg.verdata.publisher.email; - let date = moment(pkg.verdata.date).fromNow(); - - let signed = false; - let signInfo = ""; - let verified = false; - - if (pkg.sign.signed) { - signed = true; - verified = pkg.sign.verified; - if (pkg.sign.signer.name && pkg.sign.signer.email && pkg.sign.key) { - signInfo = pkg.sign.signer.name + " <" + pkg.sign.signer.email + "> " + c.gray("(" + pkg.sign.key + ")"); - } else if (pkg.sign.signer.name && pkg.sign.key) { - signInfo = pkg.sign.signer.name + c.gray(" (" + pkg.sign.key + ")"); - } else if (pkg.sign.signer.email && pkg.sign.key) { - signInfo = pkg.sign.signer.email + c.gray(" (" + pkg.sign.key + ")"); - } else if (pkg.sign.key) { - signInfo = pkg.sign.key; - } - } - - 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); - console.log(" publisher: " + publisher + " <" + publisherMail + ">"); - - if (signed) { - if (verified) { - console.log(" security: " + c.green("verified") + " " + signInfo); - } else { - console.log(" security: " + c.yellow("unverified") + " " + signInfo); - } - } else { - console.log(" security: " + c.red.inverse("unsafe")); - } - - try { - if (!signed && !(await prompts.confirm({ - message: "This package is unsafe, installing it may damage your system. Are you sure you want to continue?", - initial: false - }))) { - installable = false; - } - } catch (e) { - installable = false; - } - - if (installable) { - if (fs.existsSync(home + "/buildroot")) { fs.rmSync(home + "/buildroot", { recursive: true }) } - require('../hooks/clone')(pkg.repo, pkg.branch, async () => { - spinner = ora("Extracting package...").start(); - if (fs.existsSync(home + "/packages/" + pkg.id + "--update-" + version)) fs.rmSync(home + "/packages/" + pkg.id + "--update-" + version, { recursive: true }) - fs.renameSync(home + "/buildroot", home + "/packages/" + pkg.id + "--update-" + version); - let change = require('../hooks/diff')(pkg.id, pkg.id + "--update-" + version); - require('../hooks/apply_update')(pkg.id, pkg.id + "--update-" + version); - fs.rmSync(home + "/packages/" + pkg.id + "--update-" + version, { recursive: true }); - delete installed[installed.map(i => i.id).indexOf(pkg.id)]; - installed.push({ - id: pkg.id, - date: new Date().toISOString(), - version, - files: require('../hooks/files')(pkg.id) - }) - fs.writeFileSync(os.homedir() + "/.twilight/installed.json", JSON.stringify(installed)); - spinner.succeed("Extracting package... done"); - console.log(" Size change: " + change); - - let exec = pkg.id; - if (typeof JSON.parse(fs.readFileSync(home + "/repository/" + dir + "/" + pkg.id + ".json").toString()).execname === "string") { - exec = JSON.parse(fs.readFileSync(home + "/repository/" + dir + "/" + pkg.id + ".json").toString()).execname; - } - - 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/" + exec, pkg.executable.linux); - require('child_process').spawnSync("chmod", [ "+x", os.homedir() + "/.twilight/binaries/" + exec ]) - } else if (os.platform() === "darwin" && typeof pkg.executable.mac === "string") { - fs.writeFileSync(os.homedir() + "/.twilight/binaries/" + exec, pkg.executable.mac); - require('child_process').spawnSync("chmod", [ "+x", os.homedir() + "/.twilight/binaries/" + exec ]) - } - - 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"); - } - - queue.shift(); - if (queue.length > 0) await processQueue(); - }) - } -} - -module.exports = async (argv) => { - global.installed = JSON.parse(fs.readFileSync(home + "/installed.json").toString()); - - let spinner = ora("Reading package lists...").start(); - let packages = JSON.parse(fs.readFileSync(home + "/repository/list.json").toString()); - spinner.succeed("Reading packages lists... done"); - let updated = false; - let affected = false; - global.queue = []; - - for (let pack of installed) { - if (!packages.includes(pack.id)) { - console.log(c.yellow("warn: ") + "package '" + pack.id + "' not in repository anymore"); - } else { - let installable = true; - let dir = pack.id.substring(0, 1).replace(/[^a-zA-Z0-9]/gm, "#"); - 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'"); - } - - if (installable) { - let version = pkg.verdata.latest; - let publisher = pkg.verdata.publisher.name; - let publisherMail = pkg.verdata.publisher.email; - let date = moment(pkg.verdata.date).fromNow(); - - let signed = false; - let signInfo = ""; - let verified = false; - - if (pkg.sign.signed) { - signed = true; - verified = pkg.sign.verified; - if (pkg.sign.signer.name && pkg.sign.signer.email && pkg.sign.key) { - signInfo = pkg.sign.signer.name + " <" + pkg.sign.signer.email + "> " + c.gray("(" + pkg.sign.key + ")"); - } else if (pkg.sign.signer.name && pkg.sign.key) { - signInfo = pkg.sign.signer.name + c.gray(" (" + pkg.sign.key + ")"); - } else if (pkg.sign.signer.email && pkg.sign.key) { - signInfo = pkg.sign.signer.email + c.gray(" (" + pkg.sign.key + ")"); - } else if (pkg.sign.key) { - signInfo = pkg.sign.key; - } - } - - if (pack.version !== version) { - queue.push(pack.id); - affected = true; - } else if (argv.package !== undefined && argv.package === pack.id) { - die(c.red("error: ") + "package '" + argv.package + "' is up to date"); - } - } else if (argv.package !== undefined && argv.package === pack.id) { - die(c.red("error: ") + "package '" + argv.package + "' cannot be updated"); - } - } - } - } - - if (!updated) { - die(c.red("error: ") + "package '" + argv.package + "' not installed"); - } - - if (!affected) { - die("All packages are up to date"); - } - - await processQueue(); +/* + * MIT License + * + * Copyright (c) 2022- Minteck + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + */ + +async function processQueue() { + let pack = installed.filter(i => i.id === queue[0])[0]; + let dir = pack.id.substring(0, 1).replace(/[^a-zA-Z0-9]/gm, "#"); + let pkg = JSON.parse(fs.readFileSync(home + "/repository/" + dir + "/" + pack.id + ".json").toString()); + let installable = true; + + let version = pkg.verdata.latest; + let publisher = pkg.verdata.publisher.name; + let publisherMail = pkg.verdata.publisher.email; + let date = moment(pkg.verdata.date).fromNow(); + + let signed = false; + let signInfo = ""; + let verified = false; + + if (pkg.sign.signed) { + signed = true; + verified = pkg.sign.verified; + if (pkg.sign.signer.name && pkg.sign.signer.email && pkg.sign.key) { + signInfo = pkg.sign.signer.name + " <" + pkg.sign.signer.email + "> " + c.gray("(" + pkg.sign.key + ")"); + } else if (pkg.sign.signer.name && pkg.sign.key) { + signInfo = pkg.sign.signer.name + c.gray(" (" + pkg.sign.key + ")"); + } else if (pkg.sign.signer.email && pkg.sign.key) { + signInfo = pkg.sign.signer.email + c.gray(" (" + pkg.sign.key + ")"); + } else if (pkg.sign.key) { + signInfo = pkg.sign.key; + } + } + + 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); + console.log(" publisher: " + publisher + " <" + publisherMail + ">"); + + if (signed) { + if (verified) { + console.log(" security: " + c.green("verified") + " " + signInfo); + } else { + console.log(" security: " + c.yellow("unverified") + " " + signInfo); + } + } else { + console.log(" security: " + c.red.inverse("unsafe")); + } + + try { + if (!signed && !(await prompts.confirm({ + message: "This package is unsafe, installing it may damage your system. Are you sure you want to continue?", + initial: false + }))) { + installable = false; + } + } catch (e) { + installable = false; + } + + if (installable) { + if (fs.existsSync(home + "/buildroot")) { fs.rmSync(home + "/buildroot", { recursive: true }) } + require('../hooks/clone')(pkg.repo, pkg.branch, async () => { + spinner = ora("Extracting package...").start(); + if (fs.existsSync(home + "/packages/" + pkg.id + "--update-" + version)) fs.rmSync(home + "/packages/" + pkg.id + "--update-" + version, { recursive: true }) + fs.renameSync(home + "/buildroot", home + "/packages/" + pkg.id + "--update-" + version); + let change = require('../hooks/diff')(pkg.id, pkg.id + "--update-" + version); + require('../hooks/apply_update')(pkg.id, pkg.id + "--update-" + version); + fs.rmSync(home + "/packages/" + pkg.id + "--update-" + version, { recursive: true }); + delete installed[installed.map(i => i.id).indexOf(pkg.id)]; + installed.push({ + id: pkg.id, + date: new Date().toISOString(), + version, + files: require('../hooks/files')(pkg.id) + }) + fs.writeFileSync(os.homedir() + "/.twilight/installed.json", JSON.stringify(installed)); + spinner.succeed("Extracting package... done"); + console.log(" Size change: " + change); + + let exec = pkg.id; + if (typeof JSON.parse(fs.readFileSync(home + "/repository/" + dir + "/" + pkg.id + ".json").toString()).execname === "string") { + exec = JSON.parse(fs.readFileSync(home + "/repository/" + dir + "/" + pkg.id + ".json").toString()).execname; + } + + 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/" + exec, pkg.executable.linux); + require('child_process').spawnSync("chmod", [ "+x", os.homedir() + "/.twilight/binaries/" + exec ]) + } else if (os.platform() === "darwin" && typeof pkg.executable.mac === "string") { + fs.writeFileSync(os.homedir() + "/.twilight/binaries/" + exec, pkg.executable.mac); + require('child_process').spawnSync("chmod", [ "+x", os.homedir() + "/.twilight/binaries/" + exec ]) + } + + 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"); + } + + queue.shift(); + if (queue.length > 0) await processQueue(); + }) + } +} + +module.exports = async (argv) => { + global.installed = JSON.parse(fs.readFileSync(home + "/installed.json").toString()); + + let spinner = ora("Reading package lists...").start(); + let packages = JSON.parse(fs.readFileSync(home + "/repository/list.json").toString()); + spinner.succeed("Reading packages lists... done"); + let updated = false; + let affected = false; + global.queue = []; + + for (let pack of installed) { + if (!packages.includes(pack.id)) { + console.log(c.yellow("warn: ") + "package '" + pack.id + "' not in repository anymore"); + } else { + let installable = true; + let dir = pack.id.substring(0, 1).replace(/[^a-zA-Z0-9]/gm, "#"); + 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'"); + } + + if (installable) { + let version = pkg.verdata.latest; + let publisher = pkg.verdata.publisher.name; + let publisherMail = pkg.verdata.publisher.email; + let date = moment(pkg.verdata.date).fromNow(); + + let signed = false; + let signInfo = ""; + let verified = false; + + if (pkg.sign.signed) { + signed = true; + verified = pkg.sign.verified; + if (pkg.sign.signer.name && pkg.sign.signer.email && pkg.sign.key) { + signInfo = pkg.sign.signer.name + " <" + pkg.sign.signer.email + "> " + c.gray("(" + pkg.sign.key + ")"); + } else if (pkg.sign.signer.name && pkg.sign.key) { + signInfo = pkg.sign.signer.name + c.gray(" (" + pkg.sign.key + ")"); + } else if (pkg.sign.signer.email && pkg.sign.key) { + signInfo = pkg.sign.signer.email + c.gray(" (" + pkg.sign.key + ")"); + } else if (pkg.sign.key) { + signInfo = pkg.sign.key; + } + } + + if (pack.version !== version) { + queue.push(pack.id); + affected = true; + } else if (argv.package !== undefined && argv.package === pack.id) { + die(c.red("error: ") + "package '" + argv.package + "' is up to date"); + } + } else if (argv.package !== undefined && argv.package === pack.id) { + die(c.red("error: ") + "package '" + argv.package + "' cannot be updated"); + } + } + } + } + + if (!updated) { + die(c.red("error: ") + "package '" + argv.package + "' not installed"); + } + + if (!affected) { + die("All packages are up to date"); + } + + await processQueue(); } \ No newline at end of file -- cgit