UNPKG

6.82 kBJavaScriptView Raw
1const Generator = require('yeoman-generator');
2const chalk = require('chalk');
3const figlet = require('figlet');
4const path = require('path');
5const mkdirp = require('mkdirp');
6const voca = require('voca');
7
8const fileList = [
9 // ROOT FILES
10 { src: 'README.md' },
11 { src: 'package.json' },
12 // SRC ROOT FILES
13 { src: 'src/App.js' },
14 { src: 'src/App.styled.js' },
15 { src: 'src/App.test.js' },
16 { src: 'src/i18n.js' },
17 { src: 'src/index.css' },
18 { src: 'src/index.js' },
19 { src: 'src/serviceWorker.js' },
20 // CONFIG FILES
21 { src: 'config/**', dest: 'config' },
22 { src: 'scripts/**', dest: 'scripts' },
23 // PUBLIC FILES
24 { src: 'public/**', dest: 'public' },
25 // TEST FILES
26 { src: 'test/**', dest: 'test' },
27 // COMPONENTS
28 { src: 'src/components/**', dest: 'src/components' },
29 // CONSTANTS
30 { src: 'src/constants/**', dest: 'src/constants' },
31 // CONTEXTS
32 { src: 'src/contexts/**', dest: 'src/contexts' },
33 // HIGHER ORDER COMPONENTS
34 { src: 'src/hocs/**', dest: 'src/hocs' },
35 // HOOKS
36 { src: 'src/hooks/**', dest: 'src/hooks' },
37 // LAYOUTS
38 { src: 'src/layouts/**', dest: 'src/layouts' },
39 // SERVICES
40 { src: 'src/services/**', dest: 'src/services' },
41 // UTILS
42 { src: 'src/utils/**', dest: 'src/utils' },
43 // DEFAULT CONTAINERS
44 { src: 'src/containers/Login/**', dest: 'src/containers/Login' },
45 {
46 src: 'src/containers/PageNotFound/**',
47 dest: 'src/containers/PageNotFound',
48 },
49 { src: 'src/containers/Register/**', dest: 'src/containers/Register' },
50 { src: 'src/containers/Welcome/**', dest: 'src/containers/Welcome' },
51];
52
53module.exports = class extends Generator {
54 prompting() {
55 const done = this.async();
56 this.log(chalk.cyan.bold('Welcome to the \n Solid React Generator'));
57
58 return this.prompt([
59 {
60 type: 'input',
61 name: 'appName',
62 message: 'Please enter your application name :',
63 store: true,
64 validate: appName => {
65 const pass = appName.match(/^[^\d\s!@£$%^&*()+=]+$/);
66 if (pass) {
67 return true;
68 }
69 return `${chalk.red(
70 'Provide a valid "App name", digits and whitespaces not allowed'
71 )}`;
72 },
73 default: voca.kebabCase(this.appname), // Default to current folder name
74 },
75 {
76 type: 'confirm',
77 name: 'appInstalled',
78 message:
79 'Solid React Generator can install an example application illustrating how to interact with Solid, or a basic application framework. Do you want to install the example application?',
80 },
81 {
82 type: 'input',
83 name: 'appVersion',
84 message: 'Initial version:',
85 store: true,
86 validate: appVersion => {
87 const pass = appVersion.match(
88 /^\d{1,2}\.\d{1,2}\.\d{1,2}$/
89 );
90 if (pass) {
91 return true;
92 }
93 return `${chalk.red(
94 'Provide a valid version (ex: 0.1.0)'
95 )}`;
96 },
97 default: '0.1.0',
98 },
99 {
100 type: 'list',
101 name: 'isPrivate',
102 message: 'Is this application private?',
103 choices: ['false', 'true'],
104 default: 'false',
105 },
106 ]).then(answers => {
107 this.props = answers;
108 done();
109 });
110 }
111
112 writing() {
113 const { appName, appVersion, isPrivate, appInstalled } = this.props;
114 const pkgJson = {
115 name: appName,
116 version: appVersion,
117 private: isPrivate === 'true',
118 };
119
120 // FINALIZE FILES TO INSTALL
121 this.log('Processing Configuration...');
122 if (appInstalled) {
123 fileList.push(
124 { src: 'src/containers/index.js' },
125 { src: 'src/routes.js' },
126 {
127 src: 'src/constants/navigation.js',
128 dest: 'src/constants/navigation.js',
129 },
130 {
131 src: 'src/containers/Profile/**',
132 dest: 'src/containers/Profile',
133 },
134 {
135 src: 'src/containers/TicTacToe/**',
136 dest: 'src/containers/TicTacToe',
137 },
138 { src: '.env' }
139 );
140 } else {
141 fileList.push(
142 { src: 'src/_routes.lite.js', dest: 'src/routes.js' },
143 {
144 src: 'src/containers/_index.lite.js',
145 dest: 'src/containers/index.js',
146 },
147 {
148 src: 'src/constants/_navigation.lite.js',
149 dest: 'src/constants/navigation.js',
150 },
151 { src: '.env.lite', dest: '.env' },
152 {
153 src: 'src/components/AuthNavBar/_auth-nav-bar.lite.js',
154 dest: 'src/components/AuthNavBar/auth-nav-bar.component.js',
155 }
156 );
157 }
158
159 this.log(chalk.blue(this.templatePath('package.json')));
160
161 if (path.basename(this.destinationPath()) !== appName) {
162 this.log('Creating folder...');
163 mkdirp(appName);
164 this.destinationRoot(this.destinationPath(appName));
165 }
166
167 this.log('Copying app directory...');
168
169 // EXTEND PACKAGE.JSON WITH USER PROMPTS
170 this.fs.extendJSON(this.destinationPath('package.json'), pkgJson);
171
172 this.log(this.templatePath());
173
174 // WRITE NEW FILES BASED ON USER PROMPTS
175 fileList.forEach(newFile => {
176 return this.fs.copyTpl(
177 this.templatePath(newFile.src),
178 this.destinationPath(newFile.dest || newFile.src),
179 { title: voca.titleCase(appName) }
180 );
181 });
182 }
183
184 install() {
185 this.log('Installing dependencies...');
186 this.npmInstall();
187 this.completed = true;
188 }
189
190 end() {
191 if (this.completed) {
192 this.log('Installation complete. Welcome to Solid');
193 this.log(
194 chalk.bold.blue(
195 figlet.textSync('SOLID', {
196 font: '3D-ASCII',
197 horizontalLayout: 'full',
198 verticalLayout: 'full',
199 })
200 )
201 );
202 }
203 }
204};