-
Notifications
You must be signed in to change notification settings - Fork 205
/
Copy pathindex.ts
212 lines (169 loc) · 5.26 KB
/
index.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
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
import path from 'path';
import fs from 'fs-extra';
import kleur from 'kleur';
import yargs from 'yargs';
import ora from 'ora';
import { prompt } from './utils/prompt';
import generateExampleApp from './exampleApp/generateExampleApp';
import { addCodegenBuildScript } from './exampleApp/addCodegenBuildScript';
import { createInitialGitCommit } from './utils/initialCommit';
import { assertUserInput, assertNpxExists } from './utils/assert';
import { resolveNpmPackageVersion } from './utils/resolveNpmPackageVersion';
import { applyTemplates, generateTemplateConfiguration } from './template';
import {
createQuestions,
createMetadata,
type Answers,
acceptedArgs,
type Args,
} from './input';
import { getDependencyVersionsFromExampleApp } from './exampleApp/dependencies';
import { printErrorHelp, printNextSteps, printUsedRNVersion } from './inform';
const FALLBACK_BOB_VERSION = '0.32.0';
yargs
.command(
'$0 [name]',
'create a react native library',
acceptedArgs,
// @ts-expect-error Some types are still incompatible
create
)
.demandCommand()
.recommendCommands()
.fail(printErrorHelp)
.parserConfiguration({
// don't pass kebab-case args to handler.
'strip-dashed': true,
})
.strict().argv;
async function create(_argv: yargs.Arguments<Args>) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { _, $0, ...argv } = _argv;
// Prefetch bob version in background while asking questions
const bobVersionPromise = resolveNpmPackageVersion(
'react-native-builder-bob',
FALLBACK_BOB_VERSION
);
const local = await promptLocalLibrary(argv);
const folder = await promptPath(argv, local);
await assertNpxExists();
const basename = path.basename(folder);
const questions = await createQuestions({ basename, local });
assertUserInput(questions, argv);
const promptAnswers = await prompt(questions, argv);
const answers: Answers = {
...promptAnswers,
local,
};
assertUserInput(questions, answers);
const bobVersion = await bobVersionPromise;
const config = generateTemplateConfiguration({
bobVersion,
basename,
answers,
});
await fs.mkdirp(folder);
if (answers.reactNativeVersion != null) {
printUsedRNVersion(answers.reactNativeVersion, config);
}
const spinner = ora().start();
if (config.example !== 'none') {
spinner.text = 'Generating example app';
await generateExampleApp({
type: config.example,
dest: folder,
arch: config.project.arch,
project: config.project,
bobVersion,
reactNativeVersion: answers.reactNativeVersion,
});
}
spinner.text = 'Copying files';
await applyTemplates(answers, config, folder);
const rootPackageJson = await fs.readJson(path.join(folder, 'package.json'));
if (config.example !== 'none') {
const { devDependencies } = await getDependencyVersionsFromExampleApp(
folder,
config.example
);
rootPackageJson.devDependencies = rootPackageJson.devDependencies
? {
...rootPackageJson.devDependencies,
...devDependencies,
}
: devDependencies;
}
if (config.example === 'vanilla' && config.project.arch === 'new') {
addCodegenBuildScript(folder);
}
const libraryMetadata = createMetadata(answers);
rootPackageJson['create-react-native-library'] = libraryMetadata;
await fs.writeJson(path.join(folder, 'package.json'), rootPackageJson, {
spaces: 2,
});
if (!local) {
await createInitialGitCommit(folder);
}
spinner.succeed(
`Project created successfully at ${kleur.yellow(
path.relative(process.cwd(), folder)
)}!\n`
);
await printNextSteps(local, folder, config);
}
async function promptLocalLibrary(argv: Args) {
let local = false;
if (typeof argv.local === 'boolean') {
local = argv.local;
} else {
const hasPackageJson = await fs.pathExists(
path.join(process.cwd(), 'package.json')
);
if (hasPackageJson) {
// If we're under a project with package.json, ask the user if they want to create a local library
const answers = await prompt({
type: 'confirm',
name: 'local',
message: `Looks like you're under a project folder. Do you want to create a local library?`,
initial: true,
});
local = answers.local;
}
}
return local;
}
async function promptPath(argv: Args, local: boolean) {
let folder: string;
if (argv.name && !local) {
folder = path.join(process.cwd(), argv.name);
} else {
const answers = await prompt({
type: 'text',
name: 'folder',
message: `Where do you want to create the library?`,
initial:
local && argv.name && !argv.name.includes('/')
? `modules/${argv.name}`
: argv.name,
validate: (input) => {
if (!input) {
return 'Cannot be empty';
}
if (fs.pathExistsSync(path.join(process.cwd(), input))) {
return 'Folder already exists';
}
return true;
},
});
folder = path.join(process.cwd(), answers.folder);
}
if (await fs.pathExists(folder)) {
console.log(
`A folder already exists at ${kleur.blue(
folder
)}! Please specify another folder name or delete the existing one.`
);
process.exit(1);
}
return folder;
}