-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgenerator.js
79 lines (68 loc) · 2.86 KB
/
generator.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
const chalk = require('chalk');
const inquirer = require('inquirer');
const replace = require('replace-in-file');
const fs = require('fs');
const { version } = require('./package.json');
const logger = (text, color = 'red') => console.log(chalk[color](text));
const base = './variables/.env';
fs.writeFile(base, '', (err) => err);
const generateFiles = (env) => {
fs.copyFile(`${base}.example`, `${base}.${env}`, (error) => {
if (error) logger(`Setting ${env} environment file caused an error: ${error}`);
replace({ files: [`${base}.${env}`], from: 'ENVIRONMENT =', to: `ENVIRONMENT = ${env}` })
.catch(() => logger(`Replacing value of ${env} environment variable caused an error`));
});
};
['local', 'development', 'production'].forEach(generateFiles);
const questions = [
{
type: 'input',
name: 'name',
message: 'The name of your project is...?',
default: 'Starter',
},
{
type: 'input',
name: 'description',
message: 'What\'s the proper description of this project?',
default: 'Quasar template for quickly starting projects',
},
{
type: 'input',
name: 'version',
message: 'Starting this project at version...?',
default: `${version}`,
},
];
inquirer.prompt(questions)
.then(async (answers) => {
await replace({ files: ['./package.json', './quasar.config.js', './README.md', './src/App.vue', './src-pwa/manifest.json'], from: /ExampleName/g, to: answers.name })
.catch((error) => logger(`Project name caused an error: ${error}`));
await replace({ files: ['./package.json', './quasar.config.js', './README.md', './src-pwa/manifest.json'], from: /ExampleDescription/g, to: answers.description })
.catch((error) => logger(`Setting description caused an error: ${error}`));
if (/^([0-9]+)\.([0-9]+)\.([0-9]+)/gm.test(answers.version)) {
await replace({ files: ['./package.json', './package-lock.json'], from: `${version}`, to: answers.version })
.catch((error) => logger(`Setting version caused an error: ${error}`));
} else {
logger(`Inputted version number does not comply with semantic versioning scheme; used default ${version}`, 'yellow');
}
})
.then(() => {
const helper = ['', '### Install the dependencies', '', '```bash', 'npm i', '```'];
replace({
files: ['./README.md'],
processor: (input) => {
const lines = input.split('\n');
lines.splice(3, 7, ...helper);
return lines.join('\n');
},
}).catch((error) => logger(`Setting version caused an error: ${error}`));
})
.catch((error) => {
if (error.isTtyError) logger('Prompt couldn\'t be rendered in the current environment.');
else logger(`An error occurred: ${error}`);
})
.finally(() => {
logger('\n\nCongrats! Now you can start developing your awesome application!\n\n', 'green');
});
fs.rmSync('generator.js', { recursive: true, force: true });