1 | 'use strict';
|
2 |
|
3 | process.on('unhandledRejection', err => {
|
4 | throw err;
|
5 | });
|
6 |
|
7 | const fs = require('fs-extra');
|
8 | const path = require('path');
|
9 | const chalk = require('chalk');
|
10 | const execSync = require('child_process').execSync;
|
11 | const spawn = require('react-dev-utils/crossSpawn');
|
12 | const os = require('os');
|
13 | const scriptVersion = require("../package").version;
|
14 | function isInGitRepository() {
|
15 | try {
|
16 | execSync('git rev-parse --is-inside-work-tree', { stdio: 'ignore' });
|
17 | return true;
|
18 | } catch (e) {
|
19 | return false;
|
20 | }
|
21 | }
|
22 |
|
23 | function isInMercurialRepository() {
|
24 | try {
|
25 | execSync('hg --cwd . root', { stdio: 'ignore' });
|
26 | return true;
|
27 | } catch (e) {
|
28 | return false;
|
29 | }
|
30 | }
|
31 |
|
32 | function tryGitInit(appPath) {
|
33 | let didInit = false;
|
34 | try {
|
35 | execSync('git --version', { stdio: 'ignore' });
|
36 | if (isInGitRepository() || isInMercurialRepository()) {
|
37 | return false;
|
38 | }
|
39 |
|
40 | execSync('git init', { stdio: 'ignore' });
|
41 | didInit = true;
|
42 |
|
43 | execSync('git add -A', { stdio: 'ignore' });
|
44 | execSync('git commit -m "Initial commit from @mogul/create-mogul-app"', {
|
45 | stdio: 'ignore',
|
46 | });
|
47 | return true;
|
48 | } catch (e) {
|
49 | if (didInit) {
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | try {
|
56 |
|
57 | fs.removeSync(path.join(appPath, '.git'));
|
58 | } catch (removeErr) {
|
59 |
|
60 | }
|
61 | }
|
62 | return false;
|
63 | }
|
64 | }
|
65 |
|
66 | module.exports = function(
|
67 | appPath,
|
68 | appName,
|
69 | verbose,
|
70 | originalDirectory,
|
71 | template
|
72 | ) {
|
73 | const ownPackageName = require(path.join(__dirname, '..', 'package.json'))
|
74 | .name;
|
75 | const ownPath = path.join(appPath, 'node_modules', ownPackageName);
|
76 | const appPackage = require(path.join(appPath, 'package.json'));
|
77 | const useYarn = fs.existsSync(path.join(appPath, 'yarn.lock'));
|
78 |
|
79 |
|
80 | appPackage.dependencies = appPackage.dependencies || {};
|
81 |
|
82 |
|
83 | appPackage.scripts = Object.assign({}, appPackage.scripts,{
|
84 | start: 'create-mogul-app start',
|
85 | build: 'create-mogul-app build',
|
86 | "server:mock": "json-server --watch mock/index.js --port 3004"
|
87 | });
|
88 |
|
89 | appPackage.proxy = {
|
90 | "/example": {
|
91 | "target": "http://localhost:3004",
|
92 | "secure": false,
|
93 | "changeOrigin": true,
|
94 | "pathRewrite": {
|
95 | "^/example": ""
|
96 | }
|
97 | }
|
98 | };
|
99 |
|
100 | fs.writeFileSync(
|
101 | path.join(appPath, 'package.json'),
|
102 | JSON.stringify(appPackage, null, 2) + os.EOL
|
103 | );
|
104 |
|
105 | const readmeExists = fs.existsSync(path.join(appPath, 'README.md'));
|
106 | if (readmeExists) {
|
107 | fs.renameSync(
|
108 | path.join(appPath, 'README.md'),
|
109 | path.join(appPath, 'README.old.md')
|
110 | );
|
111 | }
|
112 |
|
113 |
|
114 | const templatePath = template
|
115 | ? path.resolve(originalDirectory, template)
|
116 | : path.join(ownPath, 'template');
|
117 | if (fs.existsSync(templatePath)) {
|
118 | fs.copySync(templatePath, appPath);
|
119 | } else {
|
120 | console.error(
|
121 | `Could not locate supplied template: ${chalk.green(templatePath)}`
|
122 | );
|
123 | return;
|
124 | }
|
125 |
|
126 |
|
127 |
|
128 | try {
|
129 | fs.moveSync(
|
130 | path.join(appPath, 'gitignore'),
|
131 | path.join(appPath, '.gitignore'),
|
132 | []
|
133 | );
|
134 | } catch (err) {
|
135 |
|
136 | if (err.code === 'EEXIST') {
|
137 | const data = fs.readFileSync(path.join(appPath, 'gitignore'));
|
138 | fs.appendFileSync(path.join(appPath, '.gitignore'), data);
|
139 | fs.unlinkSync(path.join(appPath, 'gitignore'));
|
140 | } else {
|
141 | throw err;
|
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 | if (tryGitInit(appPath)) {
|
182 | console.log();
|
183 | console.log('Initialized a git repository.');
|
184 | }
|
185 |
|
186 |
|
187 |
|
188 |
|
189 | let cdpath;
|
190 | if (originalDirectory && path.join(originalDirectory, appName) === appPath) {
|
191 | cdpath = appName;
|
192 | } else {
|
193 | cdpath = appPath;
|
194 | }
|
195 |
|
196 |
|
197 | const displayedCommand = useYarn ? 'yarn' : 'npm';
|
198 |
|
199 | console.log();
|
200 | console.log(`Success! Created ${appName} at ${appPath}`);
|
201 | console.log('Inside that directory, you can run several commands:');
|
202 | console.log();
|
203 | console.log(chalk.cyan(` ${displayedCommand} start`));
|
204 | console.log(' Starts the development server.');
|
205 | console.log();
|
206 | console.log(
|
207 | chalk.cyan(` ${displayedCommand} ${useYarn ? '' : 'run '}build`)
|
208 | );
|
209 | console.log(' Bundles the app into static files for production.');
|
210 | console.log();
|
211 | console.log('We suggest that you begin by typing:');
|
212 | console.log();
|
213 | console.log(chalk.cyan(' cd'), cdpath);
|
214 | console.log(` ${chalk.cyan(`${displayedCommand} start`)}`);
|
215 | if (readmeExists) {
|
216 | console.log();
|
217 | console.log(
|
218 | chalk.yellow(
|
219 | 'You had a `README.md` file, we renamed it to `README.old.md`'
|
220 | )
|
221 | );
|
222 | }
|
223 | console.log();
|
224 | console.log(chalk.cyan(`食行生鲜运营平台开发部出品 version:${ scriptVersion }`));
|
225 | };
|
226 |
|
227 | function isReactInstalled(appPackage) {
|
228 | const dependencies = appPackage.dependencies || {};
|
229 |
|
230 | return (
|
231 | typeof dependencies.react !== 'undefined' &&
|
232 | typeof dependencies['react-dom'] !== 'undefined'
|
233 | );
|
234 | }
|