-
Notifications
You must be signed in to change notification settings - Fork 6
/
createApp.ts
145 lines (128 loc) · 4.29 KB
/
createApp.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
// https://github.com/vercel/next.js/blob/canary/packages/create-next-app/create-app.ts
import retry from 'async-retry';
import chalk from 'chalk';
import cpy from 'cpy';
import fs from 'fs-extra';
import path from 'path';
import { isWriteable, makeDir, isFolderEmpty } from './utils';
import packageJson from './template/package.json';
import { install, installFromCache } from './helpers/install';
import { tryGitInit } from './helpers/git';
export async function createApp({
appPath,
packageManager,
fastMode,
}: {
appPath: string;
packageManager: string;
fastMode: boolean;
}): Promise<void> {
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 originalDirectory = process.cwd();
console.log();
console.log(`Creating a new KubeSphere extension project in ${chalk.green(root)}.`);
console.log();
process.chdir(root);
await cpy('**', root, {
parents: true,
cwd: path.join(__dirname, 'template'),
});
const renameFiles = [
'editorconfig',
'eslintignore',
'eslintrc.js',
'gitignore',
'prettierignore',
];
renameFiles.forEach(file => {
fs.renameSync(path.join(root, file), path.join(root, `.${file}`));
});
if (fastMode) {
try {
console.log(chalk.yellow('Downloading dependencies. This might take a moment.'));
console.log();
await retry(() => installFromCache(root), {
retries: 3,
});
console.log();
} catch (e) {
console.error(e);
}
} else {
const installFlags = { packageManager, isOnline: true };
const dependencies: string[] = Object.keys(packageJson.dependencies).map(key => {
// @ts-ignore
return `${key}@${packageJson.dependencies[key]}`;
});
if (dependencies.length) {
console.log('Installing dependencies:');
for (const dependency of dependencies) {
console.log(`- ${chalk.cyan(dependency)}`);
}
console.log();
await install(root, dependencies, installFlags);
}
const devDependencies: string[] = Object.keys(packageJson.devDependencies).map(key => {
// @ts-ignore
return `${key}@${packageJson.devDependencies[key]}`;
});
if (devDependencies.length) {
console.log();
console.log('Installing devDependencies:');
for (const devDependency of devDependencies) {
console.log(`- ${chalk.cyan(devDependency)}`);
}
console.log();
const devInstallFlags = { devDependencies: true, ...installFlags };
await install(root, devDependencies, devInstallFlags);
}
}
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();
console.log(`${chalk.green('Success!')} The project ${appName} is created at ${appPath}`);
console.log('Inside the directory, you can run the following commands:');
console.log();
console.log(chalk.cyan(` ${packageManager} ${useYarn ? '' : 'run '}create:ext`));
console.log(' Creates a new extension.');
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:prod`));
console.log(' Builds the app for production to use.');
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 '}create:ext`)}`);
console.log();
console.log('And');
console.log();
console.log(chalk.cyan(` ${packageManager} ${useYarn ? '' : 'run '}dev`));
console.log();
}