This repository has been archived by the owner on Feb 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 214
/
index.js
217 lines (182 loc) · 7.08 KB
/
index.js
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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
const { ensureDirSync, readJsonSync, writeJsonSync } = require('fs-extra');
const { basename, join, relative } = require('path');
const chalk = require('chalk');
const stringify = require('javascript-stringify');
const merge = require('deepmerge');
const { contains, partition } = require('ramda');
const Generator = require('yeoman-generator');
const questions = require('./questions');
const { projects, packages, isYarn } = require('./utils');
/* eslint-disable no-underscore-dangle */
module.exports = class Project extends Generator {
_logo() {
return ` _ _
_ __ ___ _ _ | |_ _ __ (_) _ __ ___
| '_ \\ / _ \\| | | || __|| '__|| || '_ \\ / _ \\
| | | || __/| |_| || |_ | | | || | | || (_) |
|_| |_| \\___| \\__,_| \\__||_| |_||_| |_| \\___/
`;
}
_getProjectMiddleware() {
const { projectType, project } = this.data;
if (projectType === 'application' && project !== packages.NODE) {
return [project, {
html: {
title: this.options.name
}
}];
} else if (projectType === 'library') {
return [project, {
name: this.options.name
}];
}
return project;
}
_getNeutrinorcContent() {
const rc = {
use: [
this.data.linter,
this._getProjectMiddleware(),
this.data.testRunner
].filter(Boolean)
};
return `module.exports = ${stringify(rc, null, 2)};\n`;
}
_getDependencies() {
const deps = [this.data.project, this.data.testRunner, this.data.linter]
.reduce(
(deps, project) => merge(deps, projects[project] || {}),
{ dependencies: [], devDependencies: [] }
);
if (deps.dependencies.length && deps.devDependencies.length) {
return deps;
} else if (deps.dependencies.length) {
return { dependencies: deps.dependencies };
} else if (deps.devDependencies.length) {
return { devDependencies: deps.devDependencies };
}
return {};
}
_initialPackageJson() {
const installer = isYarn ? 'yarn' : 'npm';
const scripts = { build: `${packages.NEUTRINO} build` };
if (this.data.projectType !== 'library') {
scripts.start = `${packages.NEUTRINO} start`;
}
if (this.data.linter) {
scripts.lint = `${packages.NEUTRINO} lint`;
}
if (this.data.testRunner) {
scripts.test = `${packages.NEUTRINO} test`;
}
ensureDirSync(this.options.directory);
this.spawnCommandSync(installer, ['init', '--yes'], {
cwd: this.options.directory,
stdio: this.options.stdio
});
const jsonPath = join(this.options.directory, 'package.json');
const json = readJsonSync(jsonPath);
const packageJson = Object.assign(json, { scripts });
writeJsonSync(jsonPath, packageJson, { spaces: 2 });
this.log(` ${chalk.green('create')} ${join(basename(this.options.directory), 'package.json')}`);
}
prompting() {
const done = this.async();
this.log(chalk.cyan.bold(this._logo()));
this.log(chalk.white.bold('Welcome to Neutrino! 👋'));
this.log(chalk.cyan('To help you create your new project, I am going to ask you a few questions.\n'));
this
.prompt(questions())
.then(answers => this.data = answers)
.then(() => {
this.log(`\n👌 ${chalk.white.bold('Looks like I have all the info I need. Give me a moment while I create your project!')}\n`);
done();
});
}
writing() {
const templates = [this.data.project, this.data.testRunner, this.data.linter].filter(Boolean);
this._initialPackageJson();
this.fs.write(
join(this.options.directory, '.neutrinorc.js'),
this._getNeutrinorcContent()
);
templates.forEach(template => {
const templateDir = template.replace('@neutrinojs/', '');
this.fs.copyTpl(
this.templatePath(`${templateDir}/**`),
this.options.directory,
{ data: this.options },
{},
{ globOptions: { dot: true } }
);
});
}
install() {
const packageManager = isYarn ? 'yarn' : 'npm';
const install = isYarn ? 'add' : 'install';
const devFlag = isYarn ? '--dev' : '--save-dev';
const { dependencies, devDependencies } = this._getDependencies();
this.log('');
if (dependencies) {
this.log(`${chalk.green('⏳ Installing dependencies:')} ${chalk.yellow(dependencies.join(', '))}`);
this.spawnCommandSync(packageManager, [install, ...dependencies], {
cwd: this.options.directory,
stdio: this.options.stdio,
env: process.env
});
}
if (devDependencies) {
if (process.env.NODE_ENV === 'test') {
const [local, remote] = partition(contains(packages.NEUTRINO), devDependencies);
if (remote.length) {
this.log(`${chalk.green('⏳ Installing remote devDependencies:')} ${chalk.yellow(remote.join(', '))}`);
this.spawnCommandSync(packageManager, [install, devFlag, ...remote], {
stdio: this.options.stdio,
env: process.env,
cwd: this.options.directory
});
}
if (local.length) {
this.log(`${chalk.green('⏳ Linking local devDependencies:')} ${chalk.yellow(local.join(', '))}`);
this.spawnCommandSync('yarn', ['link', ...local], {
stdio: this.options.stdio,
env: process.env,
cwd: this.options.directory
});
}
} else {
this.log(`${chalk.green('⏳ Installing devDependencies:')} ${chalk.yellow(devDependencies.join(', '))}`);
this.spawnCommandSync(packageManager, [install, devFlag, ...devDependencies], {
stdio: this.options.stdio,
env: process.env,
cwd: this.options.directory
});
}
}
if (this.data.linter) {
this.log(`${chalk.green('⏳ Performing one-time lint')}`);
this.spawnCommandSync(packages.NEUTRINO, ['lint', '--fix'], {
stdio: this.options.stdio === 'inherit' ? 'ignore' : this.options.stdio,
cwd: this.options.directory
});
}
}
end() {
this.log(`\n${chalk.green('Hooray, I successfully created your project!')}`);
this.log(`\nI have added a few ${isYarn ? 'yarn' : 'npm'} scripts to help you get started:`);
this.log(` • To build your project run: ${chalk.cyan.bold(`${isYarn ? 'yarn' : 'npm run'} build`)}`);
if (this.data.projectType !== 'library') {
this.log(` • To start your project locally run: ${chalk.cyan.bold(`${isYarn ? 'yarn' : 'npm'} start`)}`);
}
if (this.data.testRunner) {
this.log(` • To execute tests run: ${chalk.cyan.bold(`${isYarn ? 'yarn' : 'npm'} test`)}`);
}
if (this.data.linter) {
this.log(` • To lint your project manually run: ${chalk.cyan.bold(`${isYarn ? 'yarn' : 'npm run'} lint`)}`);
this.log(` You can also fix some linting problems with: ${chalk.cyan.bold(`${isYarn ? 'yarn' : 'npm run'} lint --fix`)}`);
}
this.log('\nNow change your directory to the following to get started:');
this.log(` ${chalk.cyan(relative(process.cwd(), this.options.directory))}`);
this.log(`\n❤️ ${chalk.white.bold('Neutrino')}`);
}
};