-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
context.ts
124 lines (114 loc) · 2.75 KB
/
context.ts
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
import os from 'node:os';
import { type Task, prompt } from '@astrojs/cli-kit';
import { random } from '@astrojs/cli-kit/utils';
import arg from 'arg';
import getSeasonalData from '../data/seasonal.js';
import { getName, getVersion } from '../messages.js';
export interface Context {
help: boolean;
prompt: typeof prompt;
cwd: string;
packageManager: string;
username: Promise<string>;
version: Promise<string>;
skipHouston: boolean;
fancy?: boolean;
add?: string[];
dryRun?: boolean;
yes?: boolean;
projectName?: string;
template?: string;
ref: string;
install?: boolean;
git?: boolean;
typescript?: string;
stdin?: typeof process.stdin;
stdout?: typeof process.stdout;
exit(code: number): never;
welcome?: string;
hat?: string;
tie?: string;
tasks: Task[];
}
export async function getContext(argv: string[]): Promise<Context> {
const flags = arg(
{
'--template': String,
'--ref': String,
'--yes': Boolean,
'--no': Boolean,
'--install': Boolean,
'--no-install': Boolean,
'--git': Boolean,
'--no-git': Boolean,
'--skip-houston': Boolean,
'--dry-run': Boolean,
'--help': Boolean,
'--fancy': Boolean,
'--add': [String],
'-y': '--yes',
'-n': '--no',
'-h': '--help',
},
{ argv, permissive: true },
);
const packageManager = detectPackageManager() ?? 'npm';
let cwd = flags['_'][0];
let {
'--help': help = false,
'--template': template,
'--no': no,
'--yes': yes,
'--install': install,
'--no-install': noInstall,
'--git': git,
'--no-git': noGit,
'--fancy': fancy,
'--skip-houston': skipHouston,
'--dry-run': dryRun,
'--ref': ref,
'--add': add,
} = flags;
let projectName = cwd;
if (no) {
yes = false;
if (install == undefined) install = false;
if (git == undefined) git = false;
}
skipHouston =
((os.platform() === 'win32' && !fancy) || skipHouston) ??
[yes, no, install, git].some((v) => v !== undefined);
const { messages, hats, ties } = getSeasonalData({ fancy });
const context: Context = {
help,
prompt,
packageManager,
username: getName(),
version: getVersion(packageManager, 'astro', process.env.ASTRO_VERSION),
skipHouston,
fancy,
add,
dryRun,
projectName,
template,
ref: ref ?? 'latest',
welcome: random(messages),
hat: hats ? random(hats) : undefined,
tie: ties ? random(ties) : undefined,
yes,
install: install ?? (noInstall ? false : undefined),
git: git ?? (noGit ? false : undefined),
cwd,
exit(code) {
process.exit(code);
},
tasks: [],
};
return context;
}
function detectPackageManager() {
if (!process.env.npm_config_user_agent) return;
const specifier = process.env.npm_config_user_agent.split(' ')[0];
const name = specifier.substring(0, specifier.lastIndexOf('/'));
return name === 'npminstall' ? 'cnpm' : name;
}