-
Notifications
You must be signed in to change notification settings - Fork 815
/
Copy pathcli.js
224 lines (215 loc) · 5.3 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import path from 'path';
import { fileURLToPath } from 'url';
import chalk from 'chalk';
import { execaCommand, execaCommandSync } from 'execa';
import Listr from 'listr';
import UpdateRenderer from 'listr-update-renderer';
import VerboseRenderer from 'listr-verbose-renderer';
import PATH from 'path-name';
import { setAnalyticsEnabled } from '../helpers/analytics.js';
import { chalkJetpackGreen } from '../helpers/styling.js';
/**
* Show us the status of the cli, such as the currenet linked directory.
*/
function cliStatus() {
if ( process.env.JETPACK_CLI_DID_REEXEC ) {
console.log(
chalkJetpackGreen(
'Jetpack CLI is apparently linked to ' + process.env.JETPACK_CLI_DID_REEXEC
)
);
} else {
console.log(
chalkJetpackGreen(
'Jetpack CLI is currently linked to ' +
fileURLToPath( new URL( `../../../`, import.meta.url ) )
)
);
}
console.log( 'To change the linked directory of the CLI, run `pnpm jetpack cli link` ' );
}
/**
* CLI link.
*
* @param {object} options - The argv options.
*/
function cliLink( options ) {
const opts = {
renderer: options.v ? VerboseRenderer : UpdateRenderer,
};
const linker = new Listr(
[
{
title: `Linking the CLI`,
task: () => {
return new Listr(
[
{
title: chalkJetpackGreen( `Enabling global access to the CLI` ),
task: () => command( 'pnpm link', options.v, path.resolve( 'tools/cli' ) ),
},
],
opts
);
},
},
],
opts
);
linker.run().catch( err => {
console.error( err );
if ( ! options.v ) {
console.error(
chalk.yellow( 'You might try running with `-v` to get more information on the failure' )
);
}
process.exit( err.exitCode || 1 );
} );
}
/**
* CLI unlink.
*
* @param {object} options - The argv options.
*/
function cliUnlink( options ) {
const opts = {
renderer: options.v ? VerboseRenderer : UpdateRenderer,
};
const unlinker = new Listr(
[
{
title: `Unlinking the CLI`,
task: () => {
return new Listr(
[
{
title: chalkJetpackGreen( `Removing global access to the CLI` ),
task: () => command( 'pnpm unlink', options.v, path.resolve( 'tools/cli' ) ),
},
],
opts
);
},
},
],
opts
);
unlinker.run().catch( err => {
console.error( err );
if ( ! options.v ) {
console.error(
chalk.yellow( 'You might try running with `-v` to get more information on the failure' )
);
}
process.exit( err.exitCode || 1 );
} );
}
/**
* Sets the analytics tracking preference for the CLI.
*
* @param {string} preference - The state to set the analytics tracking to, 'on' or 'off'.
*/
function cliAnalytics( preference ) {
setAnalyticsEnabled( preference === 'on' );
}
/**
* Command definition for the cli subcommand.
*
* @param {object} yargs - The Yargs dependency.
* @return {object} Yargs with the CLI commands defined.
*/
export function cliDefine( yargs ) {
yargs.command( 'cli <cmd>', 'Tools for the CLI tool. Meta, eh?', yarg => {
yarg
.command(
'link',
'Symlink the CLI for global use or development.',
() => {},
argv => {
if ( process.env.JETPACK_MONOREPO_ENV ) {
console.log(
chalk.yellow( 'CLI linking is not needed within the monorepo container.' )
);
return;
}
cliLink( argv );
if ( argv.v ) {
console.log( argv );
}
}
)
.command(
'unlink',
'Unlink the CLI.',
() => {},
argv => {
if ( process.env.JETPACK_MONOREPO_ENV ) {
console.log(
chalk.yellow( 'CLI unlinking is not needed within the monorepo container.' )
);
return;
}
cliUnlink( argv );
if ( argv.v ) {
console.log( argv );
}
}
)
.command(
'status',
'Get the status of the CLI',
() => {},
argv => {
cliStatus( argv );
if ( argv.v ) {
console.log( argv );
}
}
)
.command(
'analytics <preference>',
'Set analytics tracking preference',
() => {
return yargs.positional( 'preference', {
describe: 'Turn on or off analytics tracking',
type: 'string',
choices: [ 'on', 'off' ],
} );
},
argv => {
cliAnalytics( argv.preference );
if ( argv.v ) {
console.log( argv );
}
}
);
} );
return yargs;
}
/**
* Returns the command, normalized for verbosity.
*
* @param {string} cmd - The command to normalize.
* @param {boolean} verbose - If verbose is enabled or not.
* @param {string} cwd - Current working directory.
* @return {object} - The execa command to run.
*/
function command( cmd, verbose, cwd ) {
// If this is being run via the cli-link script from the monorepo root package.json, pnpm may
// have prepended node-gyp-bin and node_modules/.bin directories. Remove them so pnpm doesn't
// try to link the CLI into one of those.
const env = { ...process.env };
if ( env[ PATH ] ) {
const d = path.delimiter.replace( /[-[\]{}()*+?.\\^$|]/g, '\\$&' );
const s = path.sep.replace( /[-[\]{}()*+?.\\^$|]/g, '\\$&' );
env[ PATH ] = env[ PATH ].replace(
new RegExp(
`^(?:[^${ d }]*${ s }node-gyp-bin${ d })?(?:[^${ d }]*${ s }node_modules${ s }\\.bin${ d })+`
),
''
);
}
return verbose
? execaCommandSync( `${ cmd }`, { cwd: cwd, env: env, stdio: 'inherit' } )
: execaCommand( `${ cmd }`, { cwd: cwd, env: env } );
}