-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathindex.js
executable file
·333 lines (303 loc) · 9.46 KB
/
index.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
const chalk = require("chalk");
const spawnSync = require("child_process").spawnSync;
const program = require("commander");
const prompt = require("prompt");
const ora = require("ora");
const union = require("lodash.union")
const remove = require("rimraf").sync;
const promptModule = require("./prompt/index");
const constantObjects = require("./utils/constants");
const validationObjects = require("./utils/validation");
const projectPackageJson = require("../package.json");
program.version(projectPackageJson.version, "-v, --version");
program
.command("init <projectName>")
// .command("init <projectName>", "create a Vue Native project")
.option("--no-expo", "use react-native-cli instead of expo-cli")
.option("--no-crna", "use react-native-cli instead of expo-cli")
.action(function (projectName, cmd) {
let isCrnaProject = false;
if (cmd.expo && cmd.crna) {
isCrnaProject = true;
const isCrnaInstalledPackageVersion = validationObjects.getCrnaVersionIfAvailable();
// check if Create-react-native-app dependency is present or not
if (!isCrnaInstalledPackageVersion) {
terminateTheProcess(
"Please globally install expo-cli"
);
return;
} else {
console.log(
chalk.cyan("Using globally installed expo-cli " + isCrnaInstalledPackageVersion + "\n"),
);
}
} else {
const reactNativeCLIVersion = validationObjects.getReactNativeCLIifAvailable();
if (!reactNativeCLIVersion) {
terminateTheProcess(
"Please globally install react-native-cli"
);
return;
} else {
console.log(
chalk.cyan("Using globally installed react-native-cli " + reactNativeCLIVersion + "\n"),
);
}
}
const isProjectNameValidResponse = validationObjects.isProjectNameValid(
projectName,
isCrnaProject
);
// if project Name is invalid Ask User, Do They Want to Continue
if (!isProjectNameValidResponse) {
promptModule.promptForInvalidProjectName(
prompt,
init,
terminateTheProcess,
projectName,
cmd
);
} else {
init(projectName, cmd, isCrnaProject);
}
});
program
.arguments('<command>')
.action((cmd) => {
program.outputHelp();
console.log(` ` + chalk.red(`\n Unknown command ${chalk.yellow(cmd)}.`));
console.log();
});
program.parse(process.argv);
if (!program.args.length) {
program.help();
}
function init(projectName, cmd, useExpo) {
const createProject = useExpo
? createExpoProject
: createReactNativeCLIProject;
if (fs.existsSync(projectName)) {
promptModule.createVueProjectAfterConfirmation(
prompt,
createProject,
terminateTheProcess,
projectName,
cmd
);
} else {
createProject(projectName, cmd);
}
}
async function createReactNativeCLIProject(projectName, cmd) {
const root = path.resolve(projectName);
if (fs.existsSync(projectName)) {
removeExistingDirectory(projectName);
}
console.log(chalk.green(`Creating Vue Native project ${chalk.bold(projectName)}\n`));
createRNProjectSync(projectName, cmd);
installPackages(projectName, cmd);
await setupVueNativeApp(projectName, cmd);
}
async function createExpoProject(projectName, cmd) {
const root = path.resolve(projectName);
if (fs.existsSync(projectName)) {
removeExistingDirectory(projectName);
}
console.log(chalk.green(`Creating Vue Native project ${chalk.bold(projectName)}\n`));
createCrnaProjectSync(projectName, cmd);
installPackages(projectName, cmd);
await setupVueNativeApp(projectName, cmd, true);
}
function createCrnaProjectSync(projectName, cmd) {
const spinner = ora(
chalk.cyan("Creating project with expo-cli\n"),
).start();
const crnaProjectCreationResponse = spawnSync(
constantObjects.crnaPackageName,
['init', '--template=blank', projectName],
{ stdio: "inherit", shell: true }
);
spinner.succeed(
chalk.green("Created project with expo-cli\n"),
);
}
function createRNProjectSync(projectName, cmd) {
const spinner = ora(
chalk.cyan("Creating project with react-native-cli\n"),
).start();
const rnProjectCreationResponse = spawnSync(
constantObjects.rnPackageName,
["init", projectName, "--version", constantObjects.stableRNVersion],
{ stdio: "inherit", shell: true }
);
spinner.succeed(
chalk.green("Created project with react-native-cli\n"),
);
}
function removeExistingDirectory(directoryName) {
const spinner = ora(
chalk.yellow(`Removing pre-existing directory with name ${directoryName}\n`),
).start();
remove(directoryName)
spinner.succeed(
chalk.yellow(`Removed pre-existing directory with name ${directoryName}\n`),
);
}
// Get the `sourceExts` from the default metro configuration
// Returns an array like ['js', 'json', 'ts', 'tsx']
async function getSourceFileExtensions() {
const { getDefaultConfig } = require(`${process.cwd()}/node_modules/metro-config/src/index.js`);
const {
resolver: { sourceExts: defaultSourceExts }
} = await getDefaultConfig();
const sourceExts = union(defaultSourceExts, constantObjects.vueFileExtensions);
// `sourceExts` now looks like ['js', 'json', 'ts', 'tsx', 'vue']
return sourceExts;
}
function installPackages(projectName, cmd) {
process.chdir(projectName);
installVueNativeDependency();
installVueNativeDevDependency();
process.chdir("..");
}
function installVueNativeDependency() {
const spinner = ora(
chalk.cyan("Installing Vue Native dependencies\n"),
).start();
const commandObj = getVueNativeDependencyPackageInstallationCommand();
const crnaProjectCreationResponse = spawnSync(
commandObj.commandName,
commandObj.optionsArr,
{ shell: true, stdio: "inherit" }
);
spinner.succeed(
chalk.green("Installed Vue Native dependencies\n")
);
}
function installVueNativeDevDependency() {
const spinner = ora(
chalk.cyan("Installing Vue Native devDependencies\n"),
).start();
const commandObj = getVueNativeDevDependencyPackageInstallationCommand();
const crnaProjectCreationResponse = spawnSync(
commandObj.commandName,
commandObj.optionsArr,
{ shell: true, stdio: "inherit" }
);
spinner.succeed(
chalk.green("Installed Vue Native devDependencies\n"),
);
}
function getVueNativeDependencyPackageInstallationCommand() {
const isYarnPresent = validationObjects.getYarnVersionIfAvailable();
let vueNativePkgInstallationCommand = null;
if (isYarnPresent) {
vueNativePkgInstallationCommand = {
commandName: "yarn",
optionsArr: [
"add",
`${constantObjects.vueNativePackages.vueNativeCore}`,
`${constantObjects.vueNativePackages.vueNativeHelper}`,
"--exact"
]
};
} else {
vueNativePkgInstallationCommand = {
commandName: "npm",
optionsArr: [
"install",
`${constantObjects.vueNativePackages.vueNativeCore}`,
`${constantObjects.vueNativePackages.vueNativeHelper}`,
"--save"
]
};
}
return vueNativePkgInstallationCommand;
}
function getVueNativeDevDependencyPackageInstallationCommand() {
const isYarnPresent = validationObjects.getYarnVersionIfAvailable();
let vueNativePkgInstallationCommand = null;
if (isYarnPresent) {
vueNativePkgInstallationCommand = {
commandName: "yarn",
optionsArr: [
"add",
`${constantObjects.vueNativePackages.vueNativeScripts}`,
'@babel/core@^7.0.0',
"--exact",
"--dev"
]
};
} else {
vueNativePkgInstallationCommand = {
commandName: "npm",
optionsArr: [
"install",
`${constantObjects.vueNativePackages.vueNativeScripts}`,
'@babel/core@^7.0.0',
"--save-dev"
]
};
}
return vueNativePkgInstallationCommand;
}
async function setupVueNativeApp(projectName, cmd, isCrna = false) {
// process.chdir(projectName);
const rnCliFile = fs.readFileSync(
path.resolve(__dirname, "./utils/metro.config.js")
);
fs.writeFileSync(
path.join(projectName, constantObjects.metroConfigFile),
rnCliFile
);
const transformFileContent = fs.readFileSync(
path.resolve(__dirname, "./utils/vueTransformerPlugin.js")
);
fs.writeFileSync(
path.join(projectName, constantObjects.vueTransformerFileName),
transformFileContent
);
process.chdir(projectName);
fs.renameSync("App.js", "App.vue");
remove("App.test.js");
// If created through crna
//
if (isCrna) {
const expoObj = JSON.parse(fs.readFileSync(path.join(constantObjects.appJsonPath), 'utf8'));
const sourceExts = await getSourceFileExtensions();
// Modify the app.json file to add `sourceExts`
// Adding `sourceExts` to metro.config.js stopped working for certain
// versions of Expo
// This fixes #23
expoObj.expo.packagerOpts = {
config: 'metro.config.js',
sourceExts: sourceExts,
};
fs.writeFileSync(
path.join(constantObjects.appJsonPath),
JSON.stringify(expoObj, null, 2)
);
}
process.chdir("..");
const appVueFileContent = fs.readFileSync(
path.resolve(__dirname, "./utils/app.vue")
);
fs.writeFileSync(
path.join(projectName, constantObjects.appVueFileName),
appVueFileContent
);
console.log(
chalk.green("Setup complete!")
);
}
function terminateTheProcess(msg) {
if (msg) {
console.log(chalk.red(msg));
} else {
console.log(chalk.red("Vue Native Project initialization cancelled "));
}
process.exit(0);
}