-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-app.ts
141 lines (123 loc) · 3.61 KB
/
create-app.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
import retry from "async-retry";
import { red, green, cyan } from "picocolors";
import fs from "fs";
import path from "path";
import type { RepoInfo } from "./helpers/examples";
import {
downloadAndExtractExample,
downloadAndExtractRepo,
getRepoInfo,
existsInRepo,
hasRepo,
} from "./helpers/examples";
import { makeDir } from "./helpers/make-dir";
import { tryGitInit } from "./helpers/git";
import { install } from "./helpers/install";
import { isFolderEmpty } from "./helpers/is-folder-empty";
import { getOnline } from "./helpers/is-online";
import { isWriteable } from "./helpers/is-writeable";
import type { PackageManager } from "./helpers/get-pkg-manager";
import type { TemplateMode, TemplateType } from "./templates";
import { getTemplateFile, installTemplate } from "./templates";
export class DownloadError extends Error {}
type AppProps = {
appPath: string;
packageManager: PackageManager;
example?: string;
examplePath?: string;
typescript: boolean;
eslint: boolean;
srcDir: boolean;
importAlias: string;
useNavigation: boolean;
useExpoModules: boolean;
};
export async function createApp({
appPath,
packageManager,
example,
examplePath,
typescript,
eslint,
srcDir,
importAlias,
useNavigation,
useExpoModules,
}: AppProps): Promise<void> {
let repoInfo: RepoInfo | undefined;
const mode: TemplateMode = typescript ? "ts" : "js";
const template: TemplateType = "default";
if (example) {
}
const root = path.resolve(appPath);
if (!(await isWriteable(path.dirname(root)))) {
console.error(
"The application path is not writable, please check folder permissions and try again."
);
console.error(
"It is likely you do not have write permissions for this folder."
);
process.exit(1);
}
const appName = path.basename(root);
await makeDir(root);
if (!isFolderEmpty(root, appName)) {
process.exit(1);
}
const useYarn = packageManager === "yarn";
const isOnline = !useYarn || (await getOnline());
const originalDirectory = process.cwd();
console.log();
console.log(`Creating a new React Native app in ${green(root)}.`);
console.log();
process.chdir(root);
const packageJsonPath = path.join(root, "package.json");
let hasPackageJson = false;
if (example) {
} else {
/**
* If an example repository is not provided for cloning, proceed
* by installing from a template.
*/
await installTemplate({
appName,
root,
template,
mode,
packageManager,
isOnline,
eslint,
srcDir,
importAlias,
});
}
if (tryGitInit(root)) {
console.log("Initialized a git repository.");
console.log();
}
let cdpath: string;
if (path.join(originalDirectory, appName) === appPath) {
cdpath = appName;
} else {
cdpath = appPath;
}
console.log(`${green("Success!")} Created ${appName} at ${appPath}`);
if (hasPackageJson) {
console.log("Inside that directory, you can run several commands:");
console.log();
console.log(cyan(` ${packageManager} ${useYarn ? "" : "run "}dev`));
console.log(" Starts the development server.");
console.log();
console.log(cyan(` ${packageManager} ${useYarn ? "" : "run "}build`));
console.log(" Builds the app for production.");
console.log();
console.log(cyan(` ${packageManager} start`));
console.log(" Runs the built app in production mode.");
console.log();
console.log("We suggest that you begin by typing:");
console.log();
console.log(cyan(" cd"), cdpath);
console.log(` ${cyan(`${packageManager} ${useYarn ? "" : "run "}dev`)}`);
}
console.log();
}