-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
create-app.ts
122 lines (105 loc) · 3.21 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
/* eslint-disable import/no-extraneous-dependencies */
import chalk from "chalk";
import path from "path";
import { RepoInfo } from "./helpers/examples";
import { makeDir } from "./helpers/make-dir";
import { tryGitInit } from "./helpers/git";
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 { installTemplate, TemplateMode, TemplateType } from "./templates";
export class DownloadError extends Error {}
export async function createApp({
appPath,
packageManager,
example,
examplePath,
typescript,
eslint,
experimentalApp,
organizationName
}: {
appPath: string;
packageManager: PackageManager;
example?: string;
examplePath?: string;
typescript: boolean;
eslint: boolean;
experimentalApp: boolean;
organizationName: string;
}): Promise<void> {
let repoInfo: RepoInfo | undefined;
const mode: TemplateMode = typescript ? "ts" : "js";
const template: TemplateType = experimentalApp ? "app" : "default";
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(`Creating a new EventCatalog in ${chalk.green(root)}.`);
console.log();
process.chdir(root);
let hasPackageJson = false;
/**
* If an example repository is not provided for cloning, proceed
* by installing from a template.
*/
await installTemplate({
appName,
root,
template,
mode,
packageManager,
isOnline,
eslint,
organizationName
});
// }
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(`${chalk.green("Success!")} Created ${appName} at ${appPath}`);
if (hasPackageJson) {
console.log("Inside that directory, you can run several commands:");
console.log();
console.log(chalk.cyan(` ${packageManager} ${useYarn ? "" : "run "}dev`));
console.log(" Starts the development server.");
console.log();
console.log(
chalk.cyan(` ${packageManager} ${useYarn ? "" : "run "}build`),
);
console.log(" Builds the app for production.");
console.log();
console.log(chalk.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(chalk.cyan(" cd"), cdpath);
console.log(
` ${chalk.cyan(`${packageManager} ${useYarn ? "" : "run "}dev`)}`,
);
}
console.log();
}