-
Notifications
You must be signed in to change notification settings - Fork 210
/
Copy pathindex.ts
220 lines (179 loc) · 5.64 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
213
214
215
216
217
218
219
220
import fs from 'fs-extra';
import kleur from 'kleur';
import ora from 'ora';
import path from 'path';
import yargs from 'yargs';
import { addCodegenBuildScript } from './exampleApp/addCodegenBuildScript';
import { alignDependencyVersionsWithExampleApp } from './exampleApp/dependencies';
import generateExampleApp from './exampleApp/generateExampleApp';
import { printErrorHelp, printNextSteps, printUsedRNVersion } from './inform';
import {
acceptedArgs,
createMetadata,
createQuestions,
type Answers,
type Args,
} from './input';
import { applyTemplates, generateTemplateConfiguration } from './template';
import { assertNpxExists, assertUserInput } from './utils/assert';
import { createInitialGitCommit } from './utils/initialCommit';
import { prompt } from './utils/prompt';
import { resolveNpmPackageVersion } from './utils/resolveNpmPackageVersion';
const FALLBACK_BOB_VERSION = '0.38.3';
const FALLBACK_NITRO_MODULES_VERSION = '0.25.2';
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
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 nitroModulesVersionPromise = resolveNpmPackageVersion(
'react-native-nitro-modules',
FALLBACK_NITRO_MODULES_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 nitroModulesVersion =
answers.type === 'nitro-module' || answers.type === 'nitro-view'
? await nitroModulesVersionPromise
: undefined;
const config = generateTemplateConfiguration({
versions: {
bob: bobVersion,
nitroModules: nitroModulesVersion,
// Nitro codegen's version is always the same as nitro modules version.
nitroCodegen: nitroModulesVersion,
},
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({
destination: folder,
reactNativeVersion: answers.reactNativeVersion,
config,
});
}
spinner.text = 'Copying files';
await applyTemplates(answers, config, folder);
const rootPackageJson = await fs.readJson(path.join(folder, 'package.json'));
if (config.example !== 'none') {
await alignDependencyVersionsWithExampleApp(
rootPackageJson,
folder,
config
);
}
if (
config.example === 'vanilla' &&
(config.project.moduleConfig === 'turbo-modules' ||
config.project.viewConfig === 'fabric-view')
) {
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)) {
throw new Error(
`A folder already exists at ${kleur.blue(
folder
)}! Please specify another folder name or delete the existing one.`
);
}
return folder;
}