Skip to content

Commit a6c013f

Browse files
committed
Add Logger class
1 parent be324ab commit a6c013f

8 files changed

Lines changed: 61 additions & 40 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-codegen-cli",
3-
"version": "0.1.3",
3+
"version": "0.1.4",
44
"description": "Software Development Kit for React",
55
"main": "lib/index.js",
66
"bin": {

src/Logger.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/core/FileService.js

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import fs from 'fs-extra';
2-
import chalk from 'chalk';
32
import path from 'path';
4-
import logSymbols from 'log-symbols';
53

64
import { CONFIG, EXT } from '../constants';
5+
import { Logger } from './Logger';
76

87
export class FileService {
98
constructor(fileName) {
@@ -25,38 +24,29 @@ export class FileService {
2524
const filePath = this.getFilePath(EXT.component);
2625
if (!fs.pathExistsSync(filePath)) {
2726
fs.writeFileSync(filePath, template);
28-
console.log(
29-
logSymbols.success,
30-
chalk.green(`Successfully generated component file ${chalk.white(filePath)}`)
31-
);
27+
Logger.success(chalk => `Successfully generated component file ${chalk.white(filePath)}`);
3228
} else {
33-
console.log(logSymbols.warning, chalk.yellow(`File already exists ${chalk.white(filePath)}`));
29+
Logger.warn(chalk => `File already exists ${chalk.white(filePath)}`);
3430
}
3531
}
3632

3733
genStyle(template) {
3834
const filePath = this.getFilePath(EXT.style, 'style');
3935
if (!fs.pathExistsSync(filePath)) {
4036
fs.writeFileSync(filePath, template);
41-
console.log(
42-
logSymbols.success,
43-
chalk.green(`Successfully generated style file ${chalk.white(filePath)}`)
44-
);
37+
Logger.success(chalk => `Successfully generated style file ${chalk.white(filePath)}`);
4538
} else {
46-
console.log(logSymbols.warning, chalk.yellow(`File already exists ${chalk.white(filePath)}`));
39+
Logger.warn(chalk => `File already exists ${chalk.white(filePath)}`);
4740
}
4841
}
4942

5043
genTest(template) {
5144
const filePath = this.getFilePath(EXT.component, 'test');
5245
if (!fs.pathExistsSync(filePath)) {
5346
fs.writeFileSync(filePath, template);
54-
console.log(
55-
logSymbols.success,
56-
chalk.green(`Successfully generated test file ${chalk.white(filePath)}`)
57-
);
47+
Logger.success(chalk => `Successfully generated test file ${chalk.white(filePath)}`);
5848
} else {
59-
console.log(logSymbols.warning, chalk.yellow(`File already exists ${chalk.white(filePath)}`));
49+
Logger.warn(chalk => `File already exists ${chalk.white(filePath)}`);
6050
}
6151
}
6252
}

src/core/Logger.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import chalk from 'chalk';
2+
import logSymbols from 'log-symbols';
3+
4+
export class Logger {
5+
static info(value) {
6+
const log = v => this.log(v, 'blue', logSymbols.info);
7+
8+
if (typeof value === 'function') {
9+
return log(value(chalk));
10+
}
11+
return log(value);
12+
}
13+
14+
static success(value) {
15+
const log = v => this.log(v, 'green', logSymbols.success);
16+
17+
if (typeof value === 'function') {
18+
return log(value(chalk));
19+
}
20+
return log(value);
21+
}
22+
23+
static warn(value) {
24+
const log = v => this.log(v, 'yellow', logSymbols.warning);
25+
26+
if (typeof value === 'function') {
27+
return log(value(chalk));
28+
}
29+
return log(value);
30+
}
31+
32+
static error(value) {
33+
const log = v => this.log(v, 'red', logSymbols.error);
34+
35+
if (typeof value === 'function') {
36+
return log(value(chalk));
37+
}
38+
return log(value);
39+
}
40+
41+
static log(value, color = 'white', icon) {
42+
if (icon) return console.log(icon, chalk[color](value));
43+
return console.log(chalk[color](value));
44+
}
45+
}

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { getQuestions, getVariables, parseAnswers } from './utils';
99
inquirer
1010
.prompt(getQuestions())
1111
.then(async answers => {
12-
console.log();
12+
process.stdout.write('\n');
1313
const data = parseAnswers(answers);
1414
const variables = getVariables(data);
1515

@@ -22,6 +22,6 @@ inquirer
2222
if (answers.test) {
2323
await fileService.genTest(await templateService.getTestTemplate());
2424
}
25-
console.log();
25+
process.stdout.write('\n');
2626
})
2727
.catch(console.error);

src/templates/component-tsx.template

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,5 @@ export const {{ componentName.pascalCase }}: React.FC<IProps> = props => {
1414
React.useEffect(() => {}, []);
1515

1616
{{/useEffect}}
17-
return <div></div>;
18-
};
19-
};
17+
return <div></div>
18+
};

src/utils/getAppRoot.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
import path from 'path';
22
import findUp from 'find-up';
3-
import chalk from 'chalk';
4-
import logSymbols from 'log-symbols';
3+
import { Logger } from '../core/Logger';
54

65
export function getAppRoot() {
76
const file = findUp.sync('package.json');
87
if (file) return path.dirname(file);
9-
console.log(
10-
logSymbols.warning,
11-
chalk.yellow(
12-
'Could not find application root. Files will be generated relative to the current directory'
13-
)
8+
Logger.warn(
9+
'Could not find application root. Files will be generated relative to the current directory'
1410
);
1511
return process.cwd();
1612
}

src/utils/getConfig.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export function getConfig() {
88
let config = defaultConfig;
99

1010
const explorer = cosmiconfigSync('react-codegen');
11-
const { config: userConfig } = explorer.search(APP_ROOT);
11+
const { config: userConfig } = explorer.search(APP_ROOT) || {};
1212

1313
if (userConfig) {
1414
config = Object.assign(config, userConfig);

0 commit comments

Comments
 (0)