Skip to content

Commit cc1b77b

Browse files
authored
Merge pull request #1 from recifejs/feature/log
Feature/log
2 parents b2b9086 + d636cb9 commit cc1b77b

10 files changed

Lines changed: 246 additions & 160 deletions

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: 28 additions & 36 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+
import Log from '../Log';
67

7-
let controllerName: string = '';
8+
const createController = (name: string) => {
9+
if (name) {
10+
Log.Instance.infoHeap(`Creating the controller`);
811

9-
commander
10-
.name(`recife-cli controller`)
11-
.arguments('<controller-name>')
12-
.action(name => (controllerName = name))
13-
.allowUnknownOption(false)
14-
.parse(process.argv);
15-
16-
if (controllerName) {
17-
controllerName = capitalize(
18-
controllerName.replace(/Controller|\.ts|\.js/g, '')
19-
);
12+
name = capitalize(name.replace(/Controller|\.ts|\.js/g, ''));
2013

21-
controllerName += 'Controller';
14+
name += 'Controller';
2215

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

30-
try {
31-
const contentFile = fs.readFileSync(source).toString();
32-
fs.writeFileSync(
33-
target,
34-
replaceMask(contentFile, { name: controllerName })
35-
);
19+
try {
20+
const contentFile = fs.readFileSync(source).toString();
21+
fs.writeFileSync(target, replaceMask(contentFile, { name }));
3622

37-
console.info(
38-
`\x1b[36mCreating the controller ${controllerName}.`,
39-
'\x1b[0m'
23+
Log.Instance.successHeap(`The ${name} controller was created.`);
24+
Log.Instance.info(`Path: ${target}\n\n`);
25+
} catch (err) {
26+
Log.Instance.exception(err);
27+
}
28+
} else {
29+
Log.Instance.errorHeap(`Specify the name controller.`);
30+
Log.Instance.info(
31+
`For example: recife-cli controller User\nRun --help for more information`
4032
);
41-
console.info(`Path: ${target}`, '\x1b[0m\n');
42-
} catch (err) {
43-
console.log(`\x1b[31m${err}\x1b[0m`);
4433
}
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-
}
34+
};
35+
36+
commander
37+
.name(`recife-cli controller`)
38+
.arguments('<controller-name>')
39+
.action(name => createController(name))
40+
.allowUnknownOption(false)
41+
.parse(process.argv);

src/generators/ModelGenerator.ts

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,38 @@ import path from 'path';
33
import commander from 'commander';
44
import capitalize from '../utils/capitalize';
55
import replaceMask from '../utils/replaceMask';
6+
import Log from '../Log';
67

7-
let modelName: string = '';
8+
const createModel = (name: string) => {
9+
if (name) {
10+
Log.Instance.infoHeap(`Creating the model`);
11+
12+
name = capitalize(name.replace(/Model|\.ts|\.js/g, ''));
13+
name += 'Model';
14+
15+
const source = path.join(__dirname, '/../../templates/model/template');
16+
const target = path.join(process.cwd(), 'src/models', `${name}.ts`);
17+
18+
try {
19+
const contentFile = fs.readFileSync(source).toString();
20+
fs.writeFileSync(target, replaceMask(contentFile, { name }));
21+
22+
Log.Instance.successHeap(`The ${name} model was created.`);
23+
Log.Instance.info(`Path: ${target}\n\n`);
24+
} catch (err) {
25+
Log.Instance.exception(err);
26+
}
27+
} else {
28+
Log.Instance.errorHeap(`Specify the name model.`);
29+
Log.Instance.info(
30+
`For example: recife-cli model User\nRun --help for more information`
31+
);
32+
}
33+
};
834

935
commander
1036
.name(`recife-cli model`)
1137
.arguments('<model-name>')
12-
.action(name => (modelName = name))
38+
.action(name => createModel(name))
1339
.allowUnknownOption(false)
1440
.parse(process.argv);
15-
16-
if (modelName) {
17-
modelName = capitalize(modelName.replace(/Model|\.ts|\.js/g, ''));
18-
19-
modelName += 'Model';
20-
21-
const source = path.join(__dirname, '/../../templates/model/template');
22-
const target = path.join(process.cwd(), 'src/models', `${modelName}.ts`);
23-
24-
try {
25-
const contentFile = fs.readFileSync(source).toString();
26-
fs.writeFileSync(target, replaceMask(contentFile, { name: modelName }));
27-
28-
console.info(`\x1b[36mCreating the model ${modelName}.`, '\x1b[0m');
29-
console.info(`Path: ${target}`, '\x1b[0m\n');
30-
} catch (err) {
31-
console.log(`\x1b[31m${err}\x1b[0m`);
32-
}
33-
} else {
34-
console.error('\x1b[31mSpecify the name model.', '\x1b[0m');
35-
console.log(` For example: recife-cli model User`);
36-
console.log(` Run --help for more information\n`);
37-
}

src/generators/ProjectGenerator.ts

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,45 @@ import copyFolder from '../utils/copyFolder';
66
import createPackageJson from '../stages/CreatePackageJson';
77
import installDependencies from '../stages/InstallDependencies';
88
import initializeGit from '../stages/InitializeGit';
9-
10-
let projectName;
9+
import Log from '../Log';
10+
11+
const createProject = (name: string) => {
12+
if (name) {
13+
Log.Instance.infoHeap(`Creating the project`);
14+
15+
const source = path.join(__dirname, '/../../templates/project');
16+
const target = path.join(process.cwd(), name);
17+
18+
try {
19+
Log.Instance.infoHeap(`Copying files`);
20+
fs.mkdirSync(name);
21+
22+
copyFolder(source, target);
23+
fs.renameSync(
24+
path.join(target, 'gitignore'),
25+
path.join(target, '.gitignore')
26+
);
27+
28+
createPackageJson(target, name);
29+
installDependencies(target);
30+
initializeGit(name);
31+
32+
Log.Instance.successHeap(`The ${name} project was created.`);
33+
Log.Instance.info(`Path: ${target}\n\n`);
34+
} catch (err) {
35+
Log.Instance.exception(err);
36+
}
37+
} else {
38+
Log.Instance.errorHeap(`Specify the name project.`);
39+
Log.Instance.info(
40+
`For example: recife-cli project my-project-name\nRun --help for more information`
41+
);
42+
}
43+
};
1144

1245
commander
1346
.name(`recife-cli project`)
1447
.arguments('<project-name>')
15-
.action(name => (projectName = name))
48+
.action(name => createProject(name))
1649
.allowUnknownOption(false)
1750
.parse(process.argv);
18-
19-
if (projectName) {
20-
const source = path.join(__dirname, '/../../templates/project');
21-
const target = path.join(process.cwd(), projectName);
22-
23-
try {
24-
fs.mkdirSync(projectName);
25-
26-
copyFolder(source, target);
27-
fs.renameSync(
28-
path.join(target, 'gitignore'),
29-
path.join(target, '.gitignore')
30-
);
31-
32-
createPackageJson(target, projectName);
33-
installDependencies(target);
34-
initializeGit(projectName);
35-
36-
console.info(`\x1b[36mCreating the project ${projectName}.`, '\x1b[0m');
37-
console.info(`Path: ${target}`, '\x1b[0m\n');
38-
} catch (err) {
39-
console.log(`\x1b[31m${err}\x1b[0m`);
40-
}
41-
} else {
42-
console.error('\x1b[31mSpecify the name project.', '\x1b[0m');
43-
console.log(` For example: recife-cli project my-project-name`);
44-
console.log(` Run --help for more information\n`);
45-
}

src/generators/ScalarGenerator.ts

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,44 @@ import path from 'path';
33
import commander from 'commander';
44
import capitalize from '../utils/capitalize';
55
import replaceMask from '../utils/replaceMask';
6+
import Log from '../Log';
67

7-
let scalarName: string = '';
8+
const createScalar = (name: string) => {
9+
if (name) {
10+
Log.Instance.infoHeap(`Creating the scalar`);
811

9-
commander
10-
.name(`recife-cli scalar`)
11-
.arguments('<scalar-name>')
12-
.action(name => (scalarName = name))
13-
.allowUnknownOption(false)
14-
.parse(process.argv);
15-
16-
if (scalarName) {
17-
scalarName = capitalize(scalarName.replace(/Scalar|\.ts|\.js/g, ''));
12+
name = capitalize(name.replace(/Scalar|\.ts|\.js/g, ''));
13+
name += 'Scalar';
1814

19-
scalarName += 'Scalar';
15+
const source = path.join(__dirname, '/../../templates/scalar/template');
16+
const target = path.join(process.cwd(), 'src/scalars', `${name}.ts`);
2017

21-
const source = path.join(__dirname, '/../../templates/scalar/template');
22-
const target = path.join(process.cwd(), 'src/scalars', `${scalarName}.ts`);
18+
try {
19+
const contentFile = fs.readFileSync(source).toString();
20+
fs.writeFileSync(
21+
target,
22+
replaceMask(contentFile, {
23+
scalarName: name,
24+
name: name.replace('Scalar', '')
25+
})
26+
);
2327

24-
try {
25-
const contentFile = fs.readFileSync(source).toString();
26-
fs.writeFileSync(
27-
target,
28-
replaceMask(contentFile, {
29-
scalarName,
30-
name: scalarName.replace('Scalar', '')
31-
})
28+
Log.Instance.successHeap(`The ${name} scalar was created.`);
29+
Log.Instance.info(`Path: ${target}\n\n`);
30+
} catch (err) {
31+
Log.Instance.exception(err);
32+
}
33+
} else {
34+
Log.Instance.errorHeap(`Specify the name scalar.`);
35+
Log.Instance.info(
36+
`For example: recife-cli scalar Email\nRun --help for more information`
3237
);
33-
34-
console.info(`\x1b[36mCreating the scalar ${scalarName}.`, '\x1b[0m');
35-
console.info(`Path: ${target}`, '\x1b[0m\n');
36-
} catch (err) {
37-
console.log(`\x1b[31m${err}\x1b[0m`);
3838
}
39-
} else {
40-
console.error('\x1b[31mSpecify the name scalar.', '\x1b[0m');
41-
console.log(` For example: recife-cli scalar Email`);
42-
console.log(` Run -help for more information\n`);
43-
}
39+
};
40+
41+
commander
42+
.name(`recife-cli scalar`)
43+
.arguments('<scalar-name>')
44+
.action(name => createScalar(name))
45+
.allowUnknownOption(false)
46+
.parse(process.argv);

0 commit comments

Comments
 (0)