1 |
|
2 |
|
3 | const fs = require('fs-extra');
|
4 | const path = require('path');
|
5 | const chalk = require('chalk');
|
6 | const spawn = require('cross-spawn');
|
7 | const os = require('os');
|
8 | const Logger = require('@availity/workflow-logger');
|
9 |
|
10 | module.exports = function init(
|
11 | appPath,
|
12 | appName,
|
13 | originalDirectory,
|
14 | templateName
|
15 | ) {
|
16 | const ownPackageName = require(path.join(__dirname, '..', 'package.json'))
|
17 | .name;
|
18 | const ownPath = path.join(appPath, 'node_modules', ownPackageName);
|
19 |
|
20 | const readmeExists = fs.existsSync(path.join(appPath, 'README.md'));
|
21 | if (readmeExists) {
|
22 | fs.renameSync(
|
23 | path.join(appPath, 'README.md'),
|
24 | path.join(appPath, 'README.old.md')
|
25 | );
|
26 | }
|
27 |
|
28 |
|
29 | const templatePath = path.join(ownPath, `templates/react-template-${templateName}`);
|
30 | if (fs.existsSync(templatePath)) {
|
31 | fs.copySync(templatePath, appPath);
|
32 | } else {
|
33 | Logger.failed(
|
34 | `Could not locate supplied template: ${chalk.green(templatePath)}`
|
35 | );
|
36 | return;
|
37 | }
|
38 |
|
39 | fs.renameSync(
|
40 | path.join(appPath, 'gitignore'),
|
41 | path.join(appPath, '.gitignore')
|
42 | );
|
43 |
|
44 | const appPackage = require(path.join(appPath, 'package.json'));
|
45 |
|
46 | appPackage.name = appName;
|
47 | appPackage.version = "0.1.0";
|
48 | appPackage.private = true;
|
49 |
|
50 | fs.writeFileSync(
|
51 | path.join(appPath, 'package.json'),
|
52 | JSON.stringify(appPackage, null, 2) + os.EOL
|
53 | );
|
54 |
|
55 | Logger.info('Installing dependencies using npm...');
|
56 | Logger.empty()
|
57 |
|
58 | const proc = spawn.sync('npm', ['install', '--loglevel', 'error'], { stdio: 'inherit' });
|
59 | if (proc.status !== 0) {
|
60 | Logger.failed('`npm install` failed');
|
61 | return;
|
62 | }
|
63 |
|
64 |
|
65 |
|
66 |
|
67 | let cdpath;
|
68 | if (originalDirectory && path.join(originalDirectory, appName) === appPath) {
|
69 | cdpath = appName;
|
70 | } else {
|
71 | cdpath = appPath;
|
72 | }
|
73 |
|
74 | Logger.empty()
|
75 | Logger.success(`Success! Created ${appName} at ${appPath}`);
|
76 | Logger.info('Inside that directory, you can run several commands:');
|
77 | Logger.info()
|
78 | Logger.info(chalk.cyan(' npm start'));
|
79 | Logger.info(' Starts the development server.');
|
80 | Logger.info()
|
81 | Logger.info(chalk.cyan(' npm run build'));
|
82 | Logger.info(' Bundles the app into static files for production.');
|
83 | Logger.info()
|
84 | Logger.info(chalk.cyan(' npm test'));
|
85 | Logger.info(' Starts the test runner.');
|
86 | Logger.info()
|
87 | Logger.info('We suggest that you begin by typing:');
|
88 | if(originalDirectory !== appPath) {
|
89 | Logger.info(chalk.cyan(` cd ${cdpath}`));
|
90 | }
|
91 | Logger.info(` ${chalk.cyan('npm start')}`);
|
92 | };
|