1 |
|
2 |
|
3 |
|
4 | "use strict";
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | const chalk = require("chalk");
|
10 | const spawn = require("cross-spawn");
|
11 | const fs = require("fs");
|
12 | const path = require("path");
|
13 | const util = require("./helpers/util");
|
14 |
|
15 | const PACKAGE = "package.json";
|
16 | const WORKFLOW = "@absolunet/nwayo-workflow";
|
17 |
|
18 | module.exports = async () => {
|
19 | const cliPackageConfig = util.packageConfig;
|
20 |
|
21 |
|
22 | if (util.flag("v") || util.flag("version")) {
|
23 | util.echo(cliPackageConfig.version);
|
24 | util.exit();
|
25 |
|
26 |
|
27 | } else if (util.flag("completion")) {
|
28 | util.echo(fs.readFileSync(path.join(__dirname, "completion", "bash"), "utf8"));
|
29 | util.exit();
|
30 |
|
31 |
|
32 | } else if (util.flag("pronounce")) {
|
33 | util.echo("/nwajo/");
|
34 | if (process.platform === "darwin") {
|
35 | spawn("say", ["nwaw", "yo"]);
|
36 | }
|
37 | util.exit();
|
38 |
|
39 |
|
40 | } else if (util.cmd("outdated")) {
|
41 | await util.obnoxiousNotificator(cliPackageConfig, true);
|
42 |
|
43 |
|
44 | } else if (util.cmd("update")) {
|
45 | const update = await util.checkUpdate(cliPackageConfig);
|
46 |
|
47 | util.echo("");
|
48 |
|
49 | if (update.current !== update.latest) {
|
50 | util.echo(
|
51 | `Update available: ${chalk.dim(update.current)} ${chalk.reset("→")} ${chalk.green(
|
52 | update.latest
|
53 | )}\n\nUpdating...\n`
|
54 | );
|
55 |
|
56 | const { terminal } = require("@absolunet/terminal");
|
57 | terminal.process.run("npm uninstall -g @absolunet/nwayo-cli && npm install -g --no-audit @absolunet/nwayo-cli");
|
58 | } else {
|
59 | util.echo("No update available");
|
60 | }
|
61 |
|
62 |
|
63 | } else if (util.cmd("grow")) {
|
64 | spawn("npx @absolunet/nwayo-grow-project");
|
65 | util.exit();
|
66 |
|
67 |
|
68 | } else if (util.cmd("grow extension")) {
|
69 | spawn("npx @absolunet/nwayo-grow-extension");
|
70 | util.exit();
|
71 |
|
72 |
|
73 | } else if (util.cmd("docs")) {
|
74 | const open = require("open");
|
75 | const URL = "https://documentation.absolunet.com/nwayo/";
|
76 |
|
77 | util.echo(`\n${chalk.underline(URL)}`);
|
78 | open(URL);
|
79 | util.exit();
|
80 | } else {
|
81 |
|
82 | const root = process.cwd();
|
83 |
|
84 |
|
85 | const projetPackagePath = `${root}/${PACKAGE}`;
|
86 | let projetPackageConfig;
|
87 |
|
88 | if (fs.existsSync(projetPackagePath)) {
|
89 | projetPackageConfig = require(projetPackagePath);
|
90 | } else {
|
91 | util.showUsageIfNotArguments();
|
92 | util.exit(`No ${chalk.underline(PACKAGE)} found`);
|
93 | }
|
94 |
|
95 |
|
96 | const nodeModules = `${root}/node_modules`;
|
97 | if (fs.existsSync(nodeModules)) {
|
98 |
|
99 | if (projetPackageConfig.dependencies && projetPackageConfig.dependencies[WORKFLOW]) {
|
100 | const workflow = `${nodeModules}/${WORKFLOW}`;
|
101 |
|
102 |
|
103 | if (fs.existsSync(workflow)) {
|
104 |
|
105 | const completion = util.flag("completion-logic");
|
106 | if (completion) {
|
107 | util.echo(require(`${workflow}/completion`)({ completion, root }));
|
108 | util.exit();
|
109 | }
|
110 |
|
111 |
|
112 | await util.obnoxiousNotificator(cliPackageConfig);
|
113 |
|
114 |
|
115 | require(`${workflow}/cli`)({
|
116 |
|
117 | cwd: root,
|
118 | infos: {
|
119 | version: cliPackageConfig.version,
|
120 | path: __dirname,
|
121 | },
|
122 |
|
123 |
|
124 | cliPkg: cliPackageConfig,
|
125 | cliPath: __dirname,
|
126 | cliUsage: util.usageData,
|
127 | });
|
128 |
|
129 |
|
130 | } else {
|
131 | util.workflowNotInstalled();
|
132 | }
|
133 |
|
134 |
|
135 | } else {
|
136 | util.exit(`No ${chalk.underline(WORKFLOW)} found in ${chalk.underline(PACKAGE)}`);
|
137 | }
|
138 |
|
139 |
|
140 | } else {
|
141 | util.workflowNotInstalled();
|
142 | }
|
143 | }
|
144 | };
|
145 |
|
146 |
|