Skip to content

Commit ac7e5ab

Browse files
committed
refactor: 💡 Prompts and cFonts added for styling
1 parent 7ebd9be commit ac7e5ab

File tree

4 files changed

+1020
-224
lines changed

4 files changed

+1020
-224
lines changed

cli.js

Lines changed: 102 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -2,91 +2,122 @@
22

33
const path = require('path');
44
const fastifyGenTs = require('./fastify-generator');
5-
const { start, get } = require('prompt');
5+
const { prompt } = require('prompts');
6+
const { say } = require('cfonts');
67

78
const options = [
8-
{
9-
description: "Fastify Plugin Structure with Mongoose and Swagger",
10-
defaultName: "fastify-plugin-mongoose-swagger",
11-
templateName: "plugin-structure-mongoose",
12-
dependencies: "dotenv fastify fastify-cors fastify-oas mongoose",
13-
devDependencies: "@types/cors @types/jest @types/node jest nodemon ts-jest ts-node typescript @typescript-eslint/eslint-plugin @typescript-eslint/parser eslint"
14-
}, {
15-
description: "Fastify Plugin Structure with TypeORM and Swagger",
16-
defaultName: "fastify-plugin-typeorm-swagger",
17-
templateName: "plugin-structure-typeorm",
18-
dependencies: "dotenv fastify fastify-cors fastify-oas fastify-plugin mongodb reflect-metadata typeorm",
19-
devDependencies: "@types/cors @types/jest @types/node jest nodemon ts-jest ts-node typescript @typescript-eslint/eslint-plugin @typescript-eslint/parser eslint"
20-
}, {
21-
description: "Fastify with Mongoose and Express Directory Structure",
22-
defaultName: "fastify-express-mongoose",
23-
templateName: "express-structure-mongoose",
24-
dependencies: "dotenv fastify fastify-cors mongoose",
25-
devDependencies: "@types/cors @types/jest @types/node jest nodemon ts-jest ts-node typescript @typescript-eslint/eslint-plugin @typescript-eslint/parser eslint"
26-
}, {
27-
description: "Fastify with TypeORM and Express Directory Structure",
28-
defaultName: "fastify-express-typeorm",
29-
templateName: "express-structure-typeorm",
30-
dependencies: "dotenv fastify fastify-cors fastify-plugin mongodb reflect-metadata typeorm",
31-
devDependencies: "@types/cors @types/jest @types/node jest nodemon ts-jest ts-node typescript @typescript-eslint/eslint-plugin @typescript-eslint/parser eslint"
32-
},
9+
{
10+
description: 'Fastify Plugin Structure with Mongoose and Swagger',
11+
defaultName: 'fastify-plugin-mongoose-swagger',
12+
templateName: 'plugin-structure-mongoose',
13+
dependencies: 'dotenv fastify fastify-cors fastify-oas mongoose',
14+
devDependencies: '@types/cors @types/jest @types/node jest nodemon ts-jest ts-node typescript @typescript-eslint/eslint-plugin @typescript-eslint/parser eslint'
15+
}, {
16+
description: 'Fastify Plugin Structure with TypeORM and Swagger',
17+
defaultName: 'fastify-plugin-typeorm-swagger',
18+
templateName: 'plugin-structure-typeorm',
19+
dependencies: 'dotenv fastify fastify-cors fastify-oas fastify-plugin mongodb reflect-metadata typeorm',
20+
devDependencies: '@types/cors @types/jest @types/node jest nodemon ts-jest ts-node typescript @typescript-eslint/eslint-plugin @typescript-eslint/parser eslint'
21+
}, {
22+
description: 'Fastify with Mongoose and Express Directory Structure',
23+
defaultName: 'fastify-express-mongoose',
24+
templateName: 'express-structure-mongoose',
25+
dependencies: 'dotenv fastify fastify-cors mongoose',
26+
devDependencies: '@types/cors @types/jest @types/node jest nodemon ts-jest ts-node typescript @typescript-eslint/eslint-plugin @typescript-eslint/parser eslint'
27+
}, {
28+
description: 'Fastify with TypeORM and Express Directory Structure',
29+
defaultName: 'fastify-express-typeorm',
30+
templateName: 'express-structure-typeorm',
31+
dependencies: 'dotenv fastify fastify-cors fastify-plugin mongodb reflect-metadata typeorm',
32+
devDependencies: '@types/cors @types/jest @types/node jest nodemon ts-jest ts-node typescript @typescript-eslint/eslint-plugin @typescript-eslint/parser eslint'
33+
},
3334
];
34-
const schema = {
35-
properties: {
36-
option: {
37-
description: 'Select a options for the type of project',
38-
pattern: /^[1-4]{0,1}$/,
39-
message: 'Specify a number in range of 4 corresponding options',
40-
required: true
41-
},
42-
path: {
43-
description: '[OPTIONAL] Specify directory where create the files, if no path is provided, current directory will be used',
44-
message: '[OPTIONAL] Specify directory where create the files, if no path is provided, current directory will be used',
45-
default: './',
46-
required: false
47-
},
48-
projectName: {
49-
description: '[OPTIONAL] Specify project name',
50-
pattern: /^(@[a-z0-9-~][a-z0-9-._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/,
51-
message: '[OPTIONAL] Specify project name',
52-
required: false
53-
}
54-
}
55-
};
5635

5736

5837
const getDest = (option, destFolder, projectName) => {
59-
return `${path.join(process.cwd(), destFolder)}/${projectName || option.defaultName}`;
60-
}
38+
return `${path.join(process.cwd(), destFolder)}/${projectName || option.defaultName}`;
39+
};
6140

6241

6342
const initiate = async (option, path, projectName) => {
64-
try {
65-
const destination = getDest(option, path, projectName);
66-
console.log(`Choose the number for corresponding option.\n${destination}`);
67-
await fastifyGenTs(option, destination);
68-
console.log('Project setup complete!');
69-
} catch (error) {
70-
console.error(error);
71-
}
43+
try {
44+
const destination = getDest(option, path, projectName);
45+
await fastifyGenTs(option, destination);
46+
console.log('\x1b[32m\x1b[40m%s\x1b[0m', `Project setup complete! at ${destination}`);
47+
} catch (error) {
48+
console.error(error);
49+
}
7250
};
7351

7452

53+
say('fastify typescript generator', {
54+
font: 'chrome',
55+
align: 'center',
56+
colors: ['system'],
57+
background: 'transparent',
58+
letterSpacing: 1,
59+
lineHeight: 1,
60+
space: true,
61+
});
62+
63+
64+
say('open-devs', {
65+
font: 'chrome',
66+
align: 'right',
67+
colors: ['system'],
68+
background: 'transparent',
69+
letterSpacing: 1,
70+
lineHeight: 1,
71+
space: true,
72+
gradient: ['#fff', '#89d8d3'],
73+
});
74+
75+
7576
console.log('\x1b[33m\x1b[40m%s\x1b[0m', 'Setting up new Fastify TypeScript project...\n');
76-
console.log('\x1b[33m\x1b[40m%s\x1b[0m', `Available options:\n`);
77-
options.map((el, index) => console.log(`${index + 1}. ${el.description} -> ${el.templateName}\n`));
7877

7978

80-
start();
79+
const onCancel = () => {
80+
console.log('\x1b[31m%s\x1b[0m', 'Project creation failed!');
81+
};
82+
83+
84+
(async () => {
85+
const questions = [
86+
{
87+
type: 'select',
88+
name: 'option',
89+
message: 'Select a option for the type of project',
90+
validate: value => value >= 1 && value <=4 ? true : 'Specify number in the range of 1 - 4',
91+
suggest: (input, choices) => choices.filter(i => i.value),
92+
choices: options.map((el, index) => ({ value: index, title: el.templateName, description: el.description })),
93+
fallback: {
94+
title: 'Using default',
95+
value: 1
96+
}
97+
},
98+
{
99+
type: 'text',
100+
name: 'projectName',
101+
message: 'Specify project name',
102+
validate: projectName => /^(@[a-z0-9-~][a-z0-9-._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/.test(projectName) ? true : 'Invalid project name, please follow npm guidelines'
103+
},
104+
{
105+
type: 'text',
106+
name: 'path',
107+
message: '[OPTIONAL] Specify directory where you want to create the project, if no path is provided current directory will be used.',
108+
fallback: {
109+
title: 'Using default',
110+
value: './'
111+
}
112+
}
113+
];
114+
115+
const answers = await prompt(questions, { onCancel });
116+
117+
if (answers.option, answers.path, answers.projectName) {
118+
console.log('\x1b[33m\x1b[40m%s\x1b[0m', 'Starting Project creation...');
119+
initiate(options[answers.option], answers.path, answers.projectName);
120+
}
81121

82122

83-
get(schema, (error, argv) => {
84-
if (error) {
85-
console.error(`Prompt error: ${error}`);
86-
throw error;
87-
} else {
88-
console.log(`Option Chosen:\n${options[argv.option].templateName}\nDirectory Chosen/Default:\n${argv.path}\nProject Name:\n${argv.projectName || options[argv.option].defaultName}`);
89-
console.log(`Starting Project creation...`);
90-
initiate(options[argv.option - 1], argv.path, argv.projectName);
91-
}
92-
});
123+
})();

fastify-generator.js

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,61 @@
11
const path = require('path');
22
const { execSync } = require('child_process');
33
const ncp = require('ncp').ncp;
4-
const { writeFile, mkdir } = require('fs/promises');
4+
const { writeFile, mkdir } = require('fs').promises;
55
const { existsSync } = require('fs');
66

77

88
const copyProjectFiles = (config, destination) => {
9-
const prjFolder = `./templates/${config.templateName}`;
10-
const source = path.join(__dirname, prjFolder);
11-
return new Promise((resolve, reject) => {
12-
ncp.limit = 16;
13-
ncp(source, destination, { filter: /^((?!node_modules|coverage|package-lock.json).)*$/ }, (err) => {
14-
if (err) {
15-
reject(err);
16-
}
17-
resolve();
18-
});
19-
})
20-
}
9+
const prjFolder = `./templates/${config.templateName}`;
10+
const source = path.join(__dirname, prjFolder);
11+
return new Promise((resolve, reject) => {
12+
ncp.limit = 16;
13+
ncp(source, destination, { filter: /^((?!node_modules|coverage|package-lock.json).)*$/ }, (err) => {
14+
if (err) {
15+
reject(err);
16+
}
17+
resolve();
18+
});
19+
});
20+
};
2121

2222

2323
const updatePackageJson = async (destination) => {
24-
try {
25-
const pathName = `${destination}/package.json`
26-
let data = require(pathName);
27-
data.name = path.basename(destination);
28-
data = JSON.stringify(data, null, 2);
29-
await writeFile(pathName, data);
30-
} catch (err) {
31-
throw err;
32-
}
33-
}
24+
try {
25+
const pathName = `${destination}/package.json`;
26+
let data = require(pathName);
27+
data.name = path.basename(destination);
28+
data = JSON.stringify(data, null, 2);
29+
await writeFile(pathName, data);
30+
} catch (err) {
31+
throw err;
32+
}
33+
};
3434

3535

3636
const downloadNodeModules = (config, destination) => {
37-
const options = {cwd: destination};
38-
console.log('\x1b[31m\x1b[40m%s\x1b[0m', 'Installing dependencies...\n');
39-
execSync('npm i -s ' + config.dependencies, options);
40-
execSync('npm i -D ' + config.devDependencies, options);
41-
console.log('\x1b[32m\x1b[40m%s\x1b[0m', 'Dependencies installation done...\n');
42-
}
37+
const options = {cwd: destination};
38+
console.log('\x1b[31m\x1b[40m%s\x1b[0m', 'Installing dependencies...\n');
39+
execSync('npm i -s ' + config.dependencies, options);
40+
console.log('\x1b[32m\x1b[40m%s\x1b[0m', 'Dependencies installation done...\n');
41+
console.log('\x1b[31m\x1b[40m%s\x1b[0m', 'Installing dev dependencies...\n');
42+
execSync('npm i -D ' + config.devDependencies, options);
43+
console.log('\x1b[32m\x1b[40m%s\x1b[0m', 'Dependencies installation complete...\n');
44+
};
4345

4446

4547
const fastifyGenTs = async (config, destination) => {
46-
try {
47-
if (!existsSync(destination)) {
48-
await mkdir(destination, { recursive: true });
49-
}
50-
await copyProjectFiles(config, destination);
51-
await updatePackageJson(destination);
52-
downloadNodeModules(config, destination);
53-
} catch (err) {
54-
console.error(err);
55-
}
56-
}
48+
try {
49+
if (!existsSync(destination)) {
50+
await mkdir(destination, { recursive: true });
51+
}
52+
await copyProjectFiles(config, destination);
53+
await updatePackageJson(destination);
54+
downloadNodeModules(config, destination);
55+
} catch (err) {
56+
console.error(err);
57+
}
58+
};
5759

5860

5961
module.exports = fastifyGenTs;

0 commit comments

Comments
 (0)