UNPKG

4.01 kBJavaScriptView Raw
1//--------------------------------------------------------
2//-- CLI bootstrap
3//--------------------------------------------------------
4"use strict";
5
6// We disable global require rule to optimize the speed of the CLI for unrelated workflow stuff
7/* eslint-disable node/global-require */
8
9const chalk = require("chalk");
10const spawn = require("cross-spawn");
11const fs = require("fs");
12const path = require("path");
13const util = require("./helpers/util");
14
15const PACKAGE = "package.json";
16const WORKFLOW = "@absolunet/nwayo-workflow";
17
18module.exports = async () => {
19 const cliPackageConfig = util.packageConfig;
20
21 //-- Trap `-v` or `--version`
22 if (util.flag("v") || util.flag("version")) {
23 util.echo(cliPackageConfig.version);
24 util.exit();
25
26 //-- Trap `--completion`
27 } else if (util.flag("completion")) {
28 util.echo(fs.readFileSync(path.join(__dirname, "completion", "bash"), "utf8"));
29 util.exit();
30
31 //-- Trap `--pronounce`
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 //-- Trap `outdated`
40 } else if (util.cmd("outdated")) {
41 await util.obnoxiousNotificator(cliPackageConfig, true);
42
43 //-- Trap `update`
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 //-- Trap `grow`
63 } else if (util.cmd("grow")) {
64 spawn("npx @absolunet/nwayo-grow-project");
65 util.exit();
66
67 //-- Trap `grow extension`
68 } else if (util.cmd("grow extension")) {
69 spawn("npx @absolunet/nwayo-grow-extension");
70 util.exit();
71
72 //-- Trap `docs`
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 //-- Set nwayo root
82 const root = process.cwd();
83
84 //-- Search for 'package.json'
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 //-- Are dependencies installed ?
96 const nodeModules = `${root}/node_modules`;
97 if (fs.existsSync(nodeModules)) {
98 //-- If uses workflow as a package
99 if (projetPackageConfig.dependencies && projetPackageConfig.dependencies[WORKFLOW]) {
100 const workflow = `${nodeModules}/${WORKFLOW}`;
101
102 // If workflow package is present
103 if (fs.existsSync(workflow)) {
104 //-- Trap `--completion-logic`
105 const completion = util.flag("completion-logic");
106 if (completion) {
107 util.echo(require(`${workflow}/completion`)({ completion, root }));
108 util.exit();
109 }
110
111 //-- Let's do this
112 await util.obnoxiousNotificator(cliPackageConfig);
113
114 //-- Load workflow
115 require(`${workflow}/cli`)({
116 // nwayo-workflow < 3.5.0
117 cwd: root,
118 infos: {
119 version: cliPackageConfig.version,
120 path: __dirname,
121 },
122
123 // nwayo-workflow >= 3.5.0
124 cliPkg: cliPackageConfig, // eslint-disable-line unicorn/prevent-abbreviations
125 cliPath: __dirname,
126 cliUsage: util.usageData,
127 });
128
129 // Duuuuude.... Install the workflow
130 } else {
131 util.workflowNotInstalled();
132 }
133
134 //-- No workflow found
135 } else {
136 util.exit(`No ${chalk.underline(WORKFLOW)} found in ${chalk.underline(PACKAGE)}`);
137 }
138
139 // Duuuuude.... Install the workflow
140 } else {
141 util.workflowNotInstalled();
142 }
143 }
144};
145
146/* eslint-enable global-require */