deno.land / x / trex@v1.13.1 / cli.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343/** * Copyright (c) Crew Dev. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */
import { customPackage, existImports, installPackages,} from "./handlers/handle_packages.ts";import { CommandNotFound, HelpCommand, LogHelp, LogPackages, updateTrex, Version,} from "./utils/logs.ts";import { createPackage, getImportMap } from "./handlers/handle_files.ts";import { flags, helpsInfo, keyWords, VERSION } from "./utils/info.ts";import { checkDepsUpdates } from "./handlers/handler_check.ts";import { deletepackage } from "./handlers/delete_package.ts";import { execution } from "./handlers/handle_execution.ts";import { Config } from "./handlers/global_configs.ts";import { purge } from "./handlers/purge_package.ts";import { packageTreeInfo } from "./tools/logs.ts";import type { importMap } from "./utils/types.ts";import { LoadingSpinner } from "./tools/logs.ts";import { Run, Scripts } from "./commands/run.ts";import * as colors from "fmt/colors.ts";import { exists } from "tools-fs";import { Spinner } from "wait";
const { bold, green, yellow, red } = colors;
async function Main() { try { const Args = Deno.args; // * install some packages if (keyWords.install.includes(Args[0])) { // * prevent error in trex install if (Args[1]) { CommandNotFound({ commands: keyWords.install, flags: [...flags.map, ...flags.nest, ...flags.pkg, ...flags.help], }); } if (flags.help.includes(Args[1])) { return HelpCommand({ command: { alias: keyWords.install, description: "install a package", }, flags: [ { alias: flags.map, description: "install package from deno.land" }, { alias: flags.nest, description: "install package from nest.land" }, { alias: flags.pkg, description: "install package from some repository", }, { alias: flags.help, description: "show command help" }, ], }); } if (await exists(`./${Config.getConfig("importMap")}`)) { try { const data = (await getImportMap()) as any; const oldPackage = existImports(data); const newPackage = await installPackages(Args); await createPackage({ ...oldPackage, ...newPackage }, true); } catch (error) { throw new Error(error).message; } } else { await createPackage(await installPackages(Args), true); } const runJson = await Scripts(); // post install hook if (runJson?.scripts?.postinstall) await Run("postinstall"); } // * display trex version else if (flags.version.includes(Args[0])) { Version(VERSION.VERSION); } // * show help info else if (flags.help.includes(Args[0])) { LogHelp(helpsInfo); } // * install a custom package else if (flags.custom.includes(Args[0])) { if (flags.help.includes(Args[1])) { return HelpCommand({ command: { alias: flags.custom, description: "install custom package", }, flags: [{ alias: flags.help, description: "show command help" }], }); } customPackage(Args); } // * uninstall some package else if (Args[0] === keyWords.uninstall) { if (flags.help.includes(Args[1])) { return HelpCommand({ command: { alias: [keyWords.uninstall], description: "delete a package", }, flags: [ { alias: flags.help, description: "show command help", }, ], }); } let loading: Spinner; const [, ...pkgs] = Args; for (const pkg of pkgs) { // @ts-ignore loading = LoadingSpinner( green( `Removing ${bold(yellow(pkg))} from ${Config.getConfig("importMap")}`, ), )!; await deletepackage(pkg); loading?.stop(); } } // * update to lastest version of trex else if (Args[0] === keyWords.upgrade) { if (Args[1]) { CommandNotFound({ commands: [keyWords.upgrade], flags: [...flags.help, "--canary"], }); } if (flags.help.includes(Args[1])) { return HelpCommand({ command: { alias: [keyWords.upgrade], description: "update trex", }, flags: [ { alias: flags.help, description: "show command help" }, { alias: ["--canary"], description: "install from dev branch" }, ], }); } await updateTrex(); } // * shows the dependency tree of a package else if (Args[0] === keyWords.tree) { if (flags.help.includes(Args[1])) { return HelpCommand({ command: { alias: [keyWords.tree], description: "view dependency tree", }, flags: [{ alias: flags.help, description: "show command help" }], }); } if (!Args[1]) { throw new Error(red("you need to pass a package name")).message; } await packageTreeInfo(...Args); } // * run script aliases else if (Args[0] === keyWords.run) { if (flags.help.includes(Args[1])) { return HelpCommand({ command: { alias: [keyWords.run], description: "run a script alias in a file run.json", }, flags: [ { alias: flags.help, description: "show command help" }, { alias: ["--watch", "-w"], description: "use reboot script alias protocol (rsap)", }, { alias: ["-wv"], description: "verbose output in --watch mode (rsap)", }, ], }); } // ignore '--watch' and '-w' in injected args const runArgs = Args[2]?.match(/^(--watch|-w|-wv)$/) ? Args.slice(3) : Args.slice(2); await Run(Args[1], runArgs); } // * purge command else if (Args[0] === keyWords.purge) { if (flags.help.includes(Args[1])) { HelpCommand({ command: { alias: [keyWords.purge], description: "remove a package or url from cache", }, flags: [ { alias: flags.help, description: "show command help", }, ], }); } await purge(); } // * ls command else if (Args[0] === keyWords.ls) { if (flags.help.includes(Args[1])) { HelpCommand({ command: { alias: [keyWords.ls], description: "shows the list of installed packages", }, flags: [ { alias: flags.help, description: "show command help", }, ], }); } else { const map = (await getImportMap<importMap>())!; LogPackages(map?.imports, false); } } // * execute a cli with out install else if (Args[0] === keyWords.exec) { if (flags.help.includes(Args[1])) { HelpCommand({ command: { alias: [keyWords.exec], description: "execute a cli tool with out install then", }, flags: [ { alias: flags.help, description: "show command help", }, { alias: ["--perms"], description: "specify cli permisions", }, ], }); } else { await execution(); } } // * check deno.land updates else if (Args[0] === keyWords.check) { if (flags.help.includes(Args[1])) { HelpCommand({ command: { alias: [keyWords.check], description: "check deno.land [std/x] dependencies updates", }, flags: [ { alias: flags.help, description: "show command help", }, { alias: flags.fix, description: "update outdate dependencies", }, ], }); } else { await checkDepsUpdates(); } } // * set globals config else if (Args[0] === keyWords.globalConfig) { if (flags.help.includes(Args[1])) { HelpCommand({ command: { alias: [keyWords.globalConfig], description: "set and get global configurations", }, flags: [ { alias: ["--importMap="], description: "set import map default name", }, { alias: ["--getImportMap"], description: "get current import map name configuration", }, ], }); } else { const flag = Args[1].trim(); if (/(--importMap=\w*).(\w*)/gim.test(flag)) { const [, value] = flag.split("="); Config.setConfig("importMap", value.trim()); } else if (/--getImportMap/) { console.log(Config.getConfig("importMap")); } } } // * displays help information else { return CommandNotFound({ commands: [ keyWords.purge, keyWords.run, keyWords.tree, ...keyWords.install, keyWords.uninstall, keyWords.upgrade, ...flags.help, ...flags.version, keyWords.setup, keyWords.ls, keyWords.exec, keyWords.check, keyWords.globalConfig, ], flags: [], }); } } catch (error) { console.error(error) }}
if (import.meta.main) { await Main();}
Version Info