Skip to content

Commit 8b1b433

Browse files
committed
Add new component hooks
1 parent a6c013f commit 8b1b433

20 files changed

Lines changed: 262 additions & 110 deletions

.eslintrc.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ env:
44
extends:
55
- airbnb-base
66
- prettier
7+
#parser: babel
78
globals:
89
Atomics: readonly
910
SharedArrayBuffer: readonly

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/config/default.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
"jsxExt": true,
55
"fileNameCase": "pascal",
66
"path": "src/components"
7-
}
7+
}

src/core/FileService.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ export class FileService {
88
constructor(fileName) {
99
this.fileName = fileName;
1010
this.dirPath = path.resolve(CONFIG.path, this.fileName);
11-
console.log(fileName, CONFIG.path);
1211
}
1312

1413
getFilePath(ext, type) {
@@ -17,7 +16,7 @@ export class FileService {
1716
}
1817

1918
createDir() {
20-
return fs.mkdirp(this.dirPath);
19+
fs.mkdirpSync(this.dirPath);
2120
}
2221

2322
genJs(template) {

src/core/Logger.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import logSymbols from 'log-symbols';
33

44
export class Logger {
55
static info(value) {
6-
const log = v => this.log(v, 'blue', logSymbols.info);
6+
const log = v => Logger.log(v, 'blue', logSymbols.info);
77

88
if (typeof value === 'function') {
99
return log(value(chalk));
@@ -12,7 +12,7 @@ export class Logger {
1212
}
1313

1414
static success(value) {
15-
const log = v => this.log(v, 'green', logSymbols.success);
15+
const log = v => Logger.log(v, 'green', logSymbols.success);
1616

1717
if (typeof value === 'function') {
1818
return log(value(chalk));
@@ -21,7 +21,7 @@ export class Logger {
2121
}
2222

2323
static warn(value) {
24-
const log = v => this.log(v, 'yellow', logSymbols.warning);
24+
const log = v => Logger.log(v, 'yellow', logSymbols.warning);
2525

2626
if (typeof value === 'function') {
2727
return log(value(chalk));
@@ -30,7 +30,7 @@ export class Logger {
3030
}
3131

3232
static error(value) {
33-
const log = v => this.log(v, 'red', logSymbols.error);
33+
const log = v => Logger.log(v, 'red', logSymbols.error);
3434

3535
if (typeof value === 'function') {
3636
return log(value(chalk));

src/core/TemplateService.js

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,36 @@ import fs from 'fs-extra';
33
import Mustache from 'mustache';
44

55
import { EXT, ROOT } from '../constants';
6+
import { Logger } from './Logger';
67

78
export class TemplateService {
89
constructor(variables) {
910
this.variables = variables;
10-
this.parse = this.parse.bind(this);
1111
}
1212

13-
parse(template) {
14-
return Mustache.render(template, this.variables);
13+
static getExt(format) {
14+
switch (format) {
15+
case 'script':
16+
case 'test':
17+
return EXT.component === 'js' ? 'jsx' : EXT.component;
18+
case 'style':
19+
return 'css';
20+
default:
21+
Logger.error(`Unhandled format ${format}`);
22+
return process.exit(1);
23+
}
1524
}
1625

17-
getScriptTemplate() {
18-
const ext = EXT.component === 'js' ? 'jsx' : EXT.component;
19-
const filePath = path.resolve(ROOT, `templates/component-${ext}.template`);
20-
return fs.readFile(filePath, 'utf8').then(this.parse);
21-
}
22-
23-
getStyleTemplate() {
24-
const filePath = path.resolve(ROOT, `templates/style-css.template`);
25-
return fs.readFile(filePath, 'utf8').then(this.parse);
26-
}
26+
getTemplate(format = 'script') {
27+
const ext = TemplateService.getExt(format);
28+
const { type } = this.variables;
2729

28-
getTestTemplate() {
29-
const ext = EXT.component === 'js' ? 'jsx' : EXT.component;
30-
const filePath = path.resolve(ROOT, `templates/test-${ext}.template`);
31-
return fs.readFile(filePath, 'utf8').then(this.parse);
30+
const filePath = path.resolve(ROOT, `templates/${type}/${type}-${format}-${ext}.template`);
31+
if (fs.pathExistsSync(filePath)) {
32+
const template = fs.readFileSync(filePath, 'utf8');
33+
return Mustache.render(template, this.variables);
34+
}
35+
Logger.warn(chalk => `Couldn't find "${format}" template at ${chalk.white(filePath)}`);
36+
return '// Template not found';
3237
}
3338
}

src/index.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,34 @@ import inquirer from 'inquirer';
44

55
import { FileService } from './core/FileService';
66
import { TemplateService } from './core/TemplateService';
7-
import { getQuestions, getVariables, parseAnswers } from './utils';
7+
import { Logger } from './core/Logger';
8+
import { getVariables, parseAnswers } from './utils';
9+
import { getQuestions } from './questions/getQuestions';
10+
// import { type as typeQuestion } from './questions/questions';
811

12+
const type = 'component';
913
inquirer
10-
.prompt(getQuestions())
14+
.prompt(getQuestions(type))
1115
.then(async answers => {
16+
// const answers = await inquirer.prompt(getQuestions(type));
17+
1218
process.stdout.write('\n');
13-
const data = parseAnswers(answers);
19+
20+
const data = parseAnswers({ ...answers, type });
1421
const variables = getVariables(data);
1522

16-
const fileService = new FileService(variables.fileName());
23+
const fileService = new FileService(variables.fileName(type));
1724
const templateService = new TemplateService(variables);
1825

19-
await fileService.createDir();
20-
await fileService.genJs(await templateService.getScriptTemplate());
21-
await fileService.genStyle(await templateService.getStyleTemplate());
22-
if (answers.test) {
23-
await fileService.genTest(await templateService.getTestTemplate());
26+
fileService.createDir();
27+
fileService.genJs(templateService.getTemplate());
28+
if (data.type !== 'hoc') {
29+
fileService.genStyle(templateService.getTemplate('style'));
2430
}
31+
if (data.test) {
32+
fileService.genTest(templateService.getTemplate('test'));
33+
}
34+
2535
process.stdout.write('\n');
2636
})
27-
.catch(console.error);
37+
.catch(Logger.error);

src/questions/getQuestions.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { CONFIG } from '../constants';
2+
import * as q from './questions';
3+
import { Logger } from '../core/Logger';
4+
5+
const hooks = [
6+
'useState',
7+
'useEffect',
8+
'useContext',
9+
'useReducer',
10+
'useRef',
11+
'useMemo',
12+
'useCallBack',
13+
];
14+
const mods = [];
15+
16+
export function getQuestions(type) {
17+
switch (type) {
18+
case 'component': {
19+
if (!CONFIG.typescript) mods.push('propTypes');
20+
21+
const val = [q.name('Component'), q.test, q.hooks(hooks)];
22+
if (mods.length) val.push(q.mods(mods));
23+
24+
return val;
25+
}
26+
case 'hoc': {
27+
const val = [q.name('HOC'), q.hooks(hooks)];
28+
if (mods.length) val.push(q.mods(mods));
29+
30+
return val;
31+
}
32+
default:
33+
Logger.error(`Unhandled generation type ${type}`);
34+
return process.exit(1);
35+
}
36+
}

src/questions/questions.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
export const type = {
2+
type: 'list',
3+
name: 'type',
4+
message: 'Select type:',
5+
choices: [
6+
{
7+
name: 'Component',
8+
value: 'component',
9+
},
10+
{
11+
name: 'High Order Component',
12+
value: 'hoc',
13+
},
14+
],
15+
};
16+
17+
export const name = genType => ({
18+
type: 'input',
19+
name: 'name',
20+
message: `${genType} name:`,
21+
});
22+
23+
export const test = {
24+
type: 'confirm',
25+
name: 'test',
26+
message: 'Create a test file?',
27+
};
28+
29+
export const hooks = args => ({
30+
type: 'checkbox',
31+
name: 'hooks',
32+
message: 'Add hooks:',
33+
choices: args,
34+
});
35+
36+
export const mods = args => ({
37+
type: 'checkbox',
38+
name: 'mods',
39+
message: 'Add modifications:',
40+
choices: args,
41+
});

src/templates/component-jsx.template

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

0 commit comments

Comments
 (0)