Skip to content

Commit 7e24326

Browse files
committed
Inserting log in controller
1 parent b2b9086 commit 7e24326

3 files changed

Lines changed: 100 additions & 33 deletions

File tree

src/Log.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
class Log {
2+
private static _instance: Log;
3+
private infoOld: string;
4+
private colors = {
5+
reset: '\x1b[0m',
6+
fgBlack: '\x1b[30m',
7+
fgBlue: '\x1b[34m',
8+
fgGreen: '\x1b[32m',
9+
fgCyan: '\x1b[36m',
10+
fgYellow: '\x1b[33m',
11+
fgRed: '\x1b[31m',
12+
bgBlue: '\x1b[44m',
13+
bgRed: '\x1b[41m',
14+
bgYellow: '\x1b[43m'
15+
};
16+
17+
private constructor() {
18+
this.infoOld = '';
19+
}
20+
21+
public static get Instance() {
22+
return this._instance || (this._instance = new this());
23+
}
24+
25+
infoHeap(text: string) {
26+
if (this.infoOld !== '') {
27+
process.stdout.clearLine(0);
28+
process.stdout.cursorTo(0);
29+
}
30+
31+
process.stdout.write(`${this.colors.fgBlue}${this.colors.reset} ${text}`);
32+
this.infoOld = text;
33+
}
34+
35+
successHeap(text: string) {
36+
if (this.infoOld !== '') {
37+
process.stdout.clearLine(0);
38+
process.stdout.cursorTo(0);
39+
}
40+
41+
process.stdout.write(
42+
`${this.colors.fgGreen}${this.colors.reset} ${text}\n`
43+
);
44+
this.infoOld = text;
45+
}
46+
47+
errorHeap(text: string) {
48+
process.stdout.clearLine(0);
49+
process.stdout.cursorTo(0);
50+
process.stdout.write(`${this.colors.fgRed}${this.colors.reset} ${text}\n`);
51+
}
52+
53+
jump() {
54+
process.stdout.write(`\n`);
55+
}
56+
57+
title(text: string) {
58+
process.stdout.write(
59+
`\n${this.colors.bgBlue}${text.toUpperCase()}${this.colors.reset}\n\n`
60+
);
61+
}
62+
63+
info(text: string) {
64+
process.stdout.write(
65+
`\n${this.colors.fgCyan}${this.colors.reset} ${text}`
66+
);
67+
}
68+
69+
exception(text: string) {
70+
process.stdout.write(`\n${this.colors.fgRed}${this.colors.reset} ${text}`);
71+
process.exit(1);
72+
}
73+
}
74+
75+
export default Log;

src/generators/ControllerGenerator.ts

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,39 @@ import path from 'path';
33
import commander from 'commander';
44
import capitalize from '../utils/capitalize';
55
import replaceMask from '../utils/replaceMask';
6-
7-
let controllerName: string = '';
6+
import Log from '../Log';
87

98
commander
109
.name(`recife-cli controller`)
1110
.arguments('<controller-name>')
12-
.action(name => (controllerName = name))
11+
.action(name => createController(name))
1312
.allowUnknownOption(false)
1413
.parse(process.argv);
1514

16-
if (controllerName) {
17-
controllerName = capitalize(
18-
controllerName.replace(/Controller|\.ts|\.js/g, '')
19-
);
15+
const createController = (name: string) => {
16+
if (name) {
17+
Log.Instance.infoHeap(`Creating the controller`);
2018

21-
controllerName += 'Controller';
19+
name = capitalize(name.replace(/Controller|\.ts|\.js/g, ''));
2220

23-
const source = path.join(__dirname, '/../../templates/controller/template');
24-
const target = path.join(
25-
process.cwd(),
26-
'src/controllers',
27-
`${controllerName}.ts`
28-
);
21+
name += 'Controller';
2922

30-
try {
31-
const contentFile = fs.readFileSync(source).toString();
32-
fs.writeFileSync(
33-
target,
34-
replaceMask(contentFile, { name: controllerName })
35-
);
23+
const source = path.join(__dirname, '/../../templates/controller/template');
24+
const target = path.join(process.cwd(), 'src/controllers', `${name}.ts`);
25+
26+
try {
27+
const contentFile = fs.readFileSync(source).toString();
28+
fs.writeFileSync(target, replaceMask(contentFile, { name }));
3629

37-
console.info(
38-
`\x1b[36mCreating the controller ${controllerName}.`,
39-
'\x1b[0m'
30+
Log.Instance.successHeap(`The controller ${name} created.`);
31+
Log.Instance.info(`Path: ${target}`);
32+
} catch (err) {
33+
Log.Instance.exception(err);
34+
}
35+
} else {
36+
Log.Instance.errorHeap(`Specify the name controller.`);
37+
Log.Instance.info(
38+
`For example: recife-cli controller User\nRun --help for more information`
4039
);
41-
console.info(`Path: ${target}`, '\x1b[0m\n');
42-
} catch (err) {
43-
console.log(`\x1b[31m${err}\x1b[0m`);
4440
}
45-
} else {
46-
console.error('\x1b[31mSpecify the name controller.', '\x1b[0m');
47-
console.log(` For example: recife-cli controller User`);
48-
console.log(` Run --help for more information\n`);
49-
}
41+
};

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
import commander from 'commander';
66
import path from 'path';
7+
import Log from './Log';
78

89
const packageJson = require(path.join(__dirname, '../package.json'));
910

10-
console.log('\x1b[36mRecife CLI', '\x1b[0m');
11-
console.log(`🚀 Version: ${packageJson.version}\n`);
11+
Log.Instance.title(`RecifeJs CLI`);
1212

1313
commander
1414
.version(packageJson.version, '-v --version', 'Version number')

0 commit comments

Comments
 (0)