diff options
author | Minteck <contact@minteck.org> | 2022-02-12 12:09:34 +0100 |
---|---|---|
committer | Minteck <contact@minteck.org> | 2022-02-12 12:09:34 +0100 |
commit | 38e7fbe2f99a5d0b6a69e54ab3af7ebf451e8f4f (patch) | |
tree | 33266f2ba82dd1e22fe85cf77bc0b559e3917af8 /commands | |
parent | 01160246e4a0c0052181c72a53737e356ea7d02d (diff) | |
download | twilight-38e7fbe2f99a5d0b6a69e54ab3af7ebf451e8f4f.tar.gz twilight-38e7fbe2f99a5d0b6a69e54ab3af7ebf451e8f4f.tar.bz2 twilight-38e7fbe2f99a5d0b6a69e54ab3af7ebf451e8f4f.zip |
Update
Diffstat (limited to 'commands')
-rw-r--r-- | commands/info.js | 120 | ||||
-rw-r--r-- | commands/install.js | 48 | ||||
-rw-r--r-- | commands/installable.js | 10 | ||||
-rw-r--r-- | commands/installed.js | 10 | ||||
-rw-r--r-- | commands/list.js | 10 | ||||
-rw-r--r-- | commands/purge.js | 54 | ||||
-rw-r--r-- | commands/remove.js | 21 | ||||
-rw-r--r-- | commands/upgrade.js | 41 |
8 files changed, 267 insertions, 47 deletions
diff --git a/commands/info.js b/commands/info.js new file mode 100644 index 0000000..125c198 --- /dev/null +++ b/commands/info.js @@ -0,0 +1,120 @@ +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 c2da58a..b65725d 100644 --- a/commands/install.js +++ b/commands/install.js @@ -15,12 +15,39 @@ module.exports = async (argv) => { 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) { - die(c.red("error: ") + "package '" + argv.package + "' not available on platform 'win32'"); + 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) { - die(c.red("error: ") + "package '" + argv.package + "' not available on platform 'linux'"); + 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) { - die(c.red("error: ") + "package '" + argv.package + "' not available on platform 'macos'"); + 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) { @@ -85,7 +112,7 @@ module.exports = async (argv) => { console.log(" security: " + c.red.inverse("unsafe")); } - if (pkg.deprecated) { + if (typeof pkg.deprecated === "string") { if (pkg.deprecated.toString().trim() !== "") { console.log(c.yellow("warn: ") + "package '" + pkg.name + "' has been marked as deprecated: " + pkg.deprecated); } else { @@ -117,5 +144,18 @@ module.exports = async (argv) => { 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 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 ee4a0f0..6e76115 100644 --- a/commands/installable.js +++ b/commands/installable.js @@ -40,17 +40,17 @@ module.exports = async () => { } if (pack.platforms.windows === 2 && pack.platforms.linux === 2 && pack.platforms.mac === 2) { - platforms[pack.id] = c.blue("all"); + 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.blue("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.blue("linux") + "," } - if (pack.platforms.mac === 1) { platforms[pack.id] += c.yellow("darwin") + "," } - if (pack.platforms.mac === 2) { platforms[pack.id] += c.blue("darwin") + "," } + 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) } } diff --git a/commands/installed.js b/commands/installed.js index 577d071..b24d2bd 100644 --- a/commands/installed.js +++ b/commands/installed.js @@ -40,17 +40,17 @@ module.exports = async () => { } if (pack.platforms.windows === 2 && pack.platforms.linux === 2 && pack.platforms.mac === 2) { - platforms[pack.id] = c.blue("all"); + 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.blue("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.blue("linux") + "," } - if (pack.platforms.mac === 1) { platforms[pack.id] += c.yellow("darwin") + "," } - if (pack.platforms.mac === 2) { platforms[pack.id] += c.blue("darwin") + "," } + 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) } } diff --git a/commands/list.js b/commands/list.js index c40cc23..9dc0020 100644 --- a/commands/list.js +++ b/commands/list.js @@ -40,17 +40,17 @@ module.exports = async () => { } if (pack.platforms.windows === 2 && pack.platforms.linux === 2 && pack.platforms.mac === 2) { - platforms[pack.id] = c.blue("all"); + 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.blue("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.blue("linux") + "," } - if (pack.platforms.mac === 1) { platforms[pack.id] += c.yellow("darwin") + "," } - if (pack.platforms.mac === 2) { platforms[pack.id] += c.blue("darwin") + "," } + 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) } } diff --git a/commands/purge.js b/commands/purge.js new file mode 100644 index 0000000..6c15feb --- /dev/null +++ b/commands/purge.js @@ -0,0 +1,54 @@ +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/remove.js b/commands/remove.js index 5643dd3..56fc847 100644 --- a/commands/remove.js +++ b/commands/remove.js @@ -16,7 +16,7 @@ module.exports = async (argv, reinstalling) => { 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"); + 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; @@ -58,4 +58,23 @@ module.exports = async (argv, reinstalling) => { delete installed[installed.map(i => i.id).indexOf(argv.package)]; fs.writeFileSync(os.homedir() + "/.twilight/installed.json", JSON.stringify(installed)); 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/upgrade.js b/commands/upgrade.js index cf8bd0b..7dcdb4a 100644 --- a/commands/upgrade.js +++ b/commands/upgrade.js @@ -13,7 +13,6 @@ module.exports = async (argv) => { 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()); - console.log(" " + pkg.name + " (" + pkg.id + ")") if ((argv.package !== undefined && argv.package === pack.id) || argv.package === undefined) { updated = true; if (os.platform() === "win32" && pkg.platforms.windows === 0) { @@ -52,41 +51,29 @@ module.exports = async (argv) => { spinner.succeed("Checking dependencies... done") if (installable) { - spinner = ora("Fetching latest version...").start(); - let version = (await axios.get(pkg.version)).data.commit.short_id; - let publisher = (await axios.get(pkg.version)).data.commit.author_name; - let publisherMail = (await axios.get(pkg.version)).data.commit.author_email; - let date = moment((await axios.get(pkg.version)).data.commit.created_at).fromNow(); + 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; - let signRaw = { error: "404 Not Found" }; - try { - signRaw = (await axios.get(pkg.signature.replace("{version}", (await axios.get(pkg.version)).data.commit.id))).data; - } catch (e) {} - - if (signRaw.error !== "404 Not Found") { + if (pkg.sign.signed) { signed = true; - if (signRaw.verification_status === "verified") { - verified = true; - } else { - verified = false; - } - if (signRaw.gpg_key_user_name && signRaw.gpg_key_user_email && signRaw.gpg_key_primary_keyid) { - signInfo = signRaw.gpg_key_user_name + " <" + signRaw.gpg_key_user_email + "> " + c.gray("(" + signRaw.gpg_key_primary_keyid + ")"); - } else if (signRaw.gpg_key_user_name && signRaw.gpg_key_primary_keyid) { - signInfo = signRaw.gpg_key_user_name + c.gray(" (" + signRaw.gpg_key_primary_keyid + ")"); - } else if (signRaw.gpg_key_user_email && signRaw.gpg_key_primary_keyid) { - signInfo = signRaw.gpg_key_user_email + c.gray(" (" + signRaw.gpg_key_primary_keyid + ")"); - } else if (signRaw.gpg_key_primary_keyid) { - signInfo = signRaw.gpg_key_primary_keyid; + 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.succeed("Fetching latest version... done"); - if (pack.version !== version) { console.log(" Installing '" + pkg.name + "'...") console.log(" version: " + pack.version + " -> " + version); |