-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathrun-cli.ts
More file actions
211 lines (206 loc) · 5.43 KB
/
run-cli.ts
File metadata and controls
211 lines (206 loc) · 5.43 KB
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
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import { startServer } from './start-server';
import { portFinder } from './port-finder';
import { SupportedPHPVersion } from '@php-wasm/universal';
import getWpNowConfig, { CliOptions } from './config';
import { spawn, SpawnOptionsWithoutStdio } from 'child_process';
import { executePHP } from './execute-php';
import { executeWPCli } from './execute-wp-cli';
import { output } from './output';
import { isGitHubCodespace } from './github-codespaces';
function startSpinner(message: string) {
process.stdout.write(`${message}...\n`);
return {
succeed: (text: string) => {
output?.log(`${text}`);
},
fail: (text: string) => {
output?.error(`${text}`);
},
};
}
function commonParameters(yargs) {
return yargs
.option('path', {
describe:
'Path to the PHP or WordPress project. Defaults to the current working directory.',
type: 'string',
})
.option('php', {
describe: 'PHP version to use.',
type: 'string',
});
}
export async function runCli() {
return yargs(hideBin(process.argv))
.scriptName('wp-now')
.usage('$0 <cmd> [args]')
.check(async (argv) => {
const config: CliOptions = {
php: argv.php as SupportedPHPVersion,
path: argv.path as string,
};
if (argv._[0] !== 'php') {
config.wp = argv.wp as string;
config.port = argv.port as number;
}
try {
await getWpNowConfig(config);
} catch (error) {
return error.message;
}
return true;
})
.command(
'start',
'Start the server',
(yargs) => {
commonParameters(yargs);
yargs.option('wp', {
describe: "WordPress version to use: e.g. '--wp=6.2'",
type: 'string',
});
yargs.option('port', {
describe: 'Server port',
type: 'number',
});
yargs.option('blueprint', {
describe: 'Path to a blueprint file to be executed',
type: 'string',
});
yargs.option('reset', {
describe:
'Create a new project environment, destroying the old project environment.',
type: 'boolean',
});
yargs.option('inspect', {
describe: 'Use Node debugging client.',
type: 'number',
});
yargs.option('inspect-brk', {
describe:
'Use Node debugging client. Break immediately on script execution start.',
type: 'number',
});
yargs.option('trace-exit', {
describe:
'Prints a stack trace whenever an environment is exited proactively, i.e. invoking process.exit().',
type: 'number',
});
yargs.option('trace-uncaught', {
describe:
'Print stack traces for uncaught exceptions; usually, the stack trace associated with the creation of an Error is printed, whereas this makes Node.js also print the stack trace associated with throwing the value (which does not need to be an Error instance).',
type: 'number',
});
yargs.option('trace-warnings', {
describe:
'Print stack traces for process warnings (including deprecations).',
type: 'number',
});
},
async (argv) => {
const spinner = startSpinner('Starting the server...');
try {
const options = await getWpNowConfig({
path: argv.path as string,
php: argv.php as SupportedPHPVersion,
wp: argv.wp as string,
port: argv.port as number,
blueprint: argv.blueprint as string,
reset: argv.reset as boolean,
});
portFinder.setPort(options.port as number);
const { url } = await startServer(options);
openInDefaultBrowser(url);
} catch (error) {
output?.error(error);
spinner.fail(
`Failed to start the server: ${
(error as Error).message
}`
);
}
}
)
.command(
'php [..args]',
'Run the php command passing the arguments to php cli',
(yargs) => {
commonParameters(yargs);
yargs.strict(false);
},
async (argv) => {
try {
// 0: node, 1: wp-now, 2: php, ...args
const args = process.argv.slice(2);
const options = await getWpNowConfig({
path: argv.path as string,
php: argv.php as SupportedPHPVersion,
});
const phpArgs = args.includes('--')
? (argv._ as string[])
: args;
// 0: php, ...args
await executePHP(phpArgs, options);
process.exit(0);
} catch (error) {
console.error(error);
process.exit(error.status || -1);
}
}
)
.command(
'wp [..args]',
'run a wp-cli command',
(yargs) => {
commonParameters(yargs);
yargs.strict(false);
},
async (argv) => {
try {
// 0: node, 1: wp-now, 2: php, ...args
const args = process.argv.slice(3);
const phpArgs = args.includes('--')
? (argv._ as string[])
: args;
// 0: php, ...args
await executeWPCli(phpArgs);
process.exit(0);
} catch (error) {
console.error(error);
process.exit(error.status || -1);
}
}
)
.demandCommand(1, 'You must provide a valid command')
.help()
.alias('h', 'help')
.strict().argv;
}
function openInDefaultBrowser(url: string) {
if (isGitHubCodespace) {
return;
}
let cmd: string, args: string[] | SpawnOptionsWithoutStdio;
switch (process.platform) {
case 'darwin':
cmd = 'open';
args = [url];
break;
case 'linux':
cmd = 'xdg-open';
args = [url];
break;
case 'win32':
cmd = 'cmd';
args = ['/c', `start ${url}`];
break;
default:
output?.log(`Platform '${process.platform}' not supported`);
return;
}
spawn(cmd, args).on('error', function (err) {
console.error(err.message);
});
}