Skip to content

Commit e4791a7

Browse files
committed
Initial commit
0 parents  commit e4791a7

20 files changed

Lines changed: 736 additions & 0 deletions

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
.idea

.prettierrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"singleQuote": true,
3+
"arrowParens": "avoid",
4+
"printWidth": 100
5+
}

config/default.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"styles": "scss",
3+
"typescript": false,
4+
"jsxExt": true,
5+
"fileNameCase": "pascal",
6+
"path": "src/components"
7+
}

constants/index.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const fs = require('fs-extra');
2+
const path = require('path');
3+
4+
const defaultConfig = require('../config/default');
5+
6+
const ROOT = path.dirname(require.main.filename);
7+
const CONFIG_FILE_NAME = 'react-codegen.json';
8+
9+
const CONFIG = Object.assign(
10+
defaultConfig,
11+
(function () {
12+
if (fs.pathExistsSync(CONFIG_FILE_NAME)) {
13+
try {
14+
const file = fs.readFileSync(CONFIG_FILE_NAME);
15+
return JSON.parse(file);
16+
} catch (error) {
17+
console.error(error);
18+
}
19+
}
20+
return {};
21+
})()
22+
);
23+
24+
const EXT = {
25+
component: CONFIG.typescript ? 'tsx' : 'j' + (CONFIG.jsxExt ? 'sx' : 's'),
26+
script: (CONFIG.typescript ? 't' : 'j') + 's',
27+
style: CONFIG.styles,
28+
};
29+
30+
module.exports = {
31+
CONFIG,
32+
ROOT,
33+
EXT,
34+
};

core/FileService.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
const fs = require('fs-extra');
2+
const chalk = require('chalk');
3+
const path = require('path');
4+
const logSymbols = require('log-symbols');
5+
6+
const { EXT, CONFIG } = require('../constants');
7+
8+
class FileService {
9+
constructor(fileName) {
10+
this.fileName = fileName;
11+
this.dirPath = path.join(CONFIG.path, this.fileName);
12+
}
13+
14+
getFilePath(ext, type) {
15+
type = type ? `.${type}` : '';
16+
return `${this.dirPath}/${this.fileName}${type}.${ext}`;
17+
}
18+
19+
createDir() {
20+
return fs.mkdirp(this.dirPath);
21+
}
22+
23+
async genJs(template) {
24+
const filePath = this.getFilePath(EXT.component);
25+
if (!(await fs.pathExists(filePath))) {
26+
await fs.writeFile(filePath, template);
27+
console.log(
28+
logSymbols.success,
29+
chalk.green(`Successfully generated component file ${chalk.white(filePath)}`)
30+
);
31+
} else {
32+
console.log(logSymbols.warning, chalk.yellow(`File already exists ${chalk.white(filePath)}`));
33+
}
34+
}
35+
36+
async genStyle(template) {
37+
const filePath = this.getFilePath(EXT.style, 'style');
38+
if (!(await fs.pathExists(filePath))) {
39+
await fs.writeFile(filePath, template);
40+
console.log(
41+
logSymbols.success,
42+
chalk.green(`Successfully generated style file ${chalk.white(filePath)}`)
43+
);
44+
} else {
45+
console.log(logSymbols.warning, chalk.yellow(`File already exists ${chalk.white(filePath)}`));
46+
}
47+
}
48+
49+
async genTest(template) {
50+
const filePath = this.getFilePath(EXT.component, 'test');
51+
if (!(await fs.pathExists(filePath))) {
52+
await fs.writeFile(filePath, template);
53+
console.log(
54+
logSymbols.success,
55+
chalk.green(`Successfully generated test file ${chalk.white(filePath)}`)
56+
);
57+
} else {
58+
console.log(logSymbols.warning, chalk.yellow(`File already exists ${chalk.white(filePath)}`));
59+
}
60+
}
61+
}
62+
63+
module.exports = {
64+
FileService,
65+
};

core/TemplateService.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const fs = require('fs-extra');
2+
const path = require('path');
3+
const Mustache = require('mustache');
4+
5+
const { EXT, ROOT } = require('../constants');
6+
7+
class TemplateService {
8+
constructor(variables) {
9+
this.variables = variables;
10+
this.parse = this.parse.bind(this);
11+
}
12+
13+
parse(template) {
14+
return Mustache.render(template, this.variables);
15+
}
16+
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+
}
27+
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);
32+
}
33+
}
34+
35+
module.exports = {
36+
TemplateService,
37+
};

index.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env node
2+
3+
const inquirer = require('inquirer');
4+
5+
const { FileService } = require('./core/FileService');
6+
const { TemplateService } = require('./core/TemplateService');
7+
const { parseAnswers, getQuestions, getVariables } = require('./utils');
8+
9+
inquirer
10+
.prompt(getQuestions())
11+
.then(async answers => {
12+
console.log();
13+
const data = parseAnswers(answers);
14+
const variables = getVariables(data);
15+
16+
const fileService = new FileService(variables.fileName());
17+
const templateService = new TemplateService(variables);
18+
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());
24+
}
25+
console.log();
26+
})
27+
.catch(console.error);

0 commit comments

Comments
 (0)