Skip to content

Commit 4a3eb1e

Browse files
committed
Refactor config & add css modules support
1 parent 39cdb3c commit 4a3eb1e

29 files changed

Lines changed: 250 additions & 85 deletions

.babelrc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
"@babel/preset-typescript"
1010
],
1111
"plugins": [
12-
"@babel/plugin-proposal-class-properties"
12+
"@babel/plugin-proposal-class-properties",
13+
["module-resolver", {
14+
"root": ".",
15+
"alias": {
16+
"@": "./src"
17+
}
18+
}]
1319
]
1420
}

package-lock.json

Lines changed: 88 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"@types/yargs": "^15.0.5",
5656
"@typescript-eslint/eslint-plugin": "^3.9.0",
5757
"@typescript-eslint/parser": "^3.9.0",
58+
"babel-plugin-module-resolver": "^4.0.0",
5859
"eslint": "^6.8.0",
5960
"eslint-config-airbnb-base": "^14.2.0",
6061
"eslint-config-airbnb-typescript": "^8.0.2",

src/config/Config.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { FileNameCase, IConfig, StyleFormats } from '@/types';
2+
import defaultConfig from './default.json';
3+
4+
export type ConfigPrefixes = {
5+
style: 'module' | 'styles';
6+
test: 'test';
7+
};
8+
9+
export type ConfigExt = {
10+
component: string;
11+
script: string;
12+
style: string;
13+
};
14+
15+
export class Config implements IConfig {
16+
config: IConfig;
17+
18+
fileNameCase: FileNameCase;
19+
20+
styles: StyleFormats;
21+
22+
path: string;
23+
24+
jsxExt: boolean;
25+
26+
typescript: boolean;
27+
28+
wrapFolder: boolean;
29+
30+
cssModules: boolean;
31+
32+
prefixes: ConfigPrefixes;
33+
34+
ext: ConfigExt;
35+
36+
constructor(config: IConfig) {
37+
this.config = Object.assign(defaultConfig, config);
38+
39+
this.setInitialVariables();
40+
this.setFilesExtension();
41+
this.setPrefixes();
42+
}
43+
44+
private setInitialVariables() {
45+
this.fileNameCase = this.config.fileNameCase;
46+
this.styles = this.config.styles;
47+
this.path = this.config.path;
48+
this.jsxExt = this.config.jsxExt;
49+
this.typescript = this.config.typescript;
50+
this.wrapFolder = this.config.wrapFolder;
51+
this.cssModules = this.config.cssModules;
52+
}
53+
54+
private setFilesExtension() {
55+
let styleExt = this.styles.toLowerCase();
56+
if (styleExt === 'stylus') {
57+
styleExt = 'styl';
58+
}
59+
60+
this.ext = {
61+
component: this.typescript ? 'tsx' : `j${this.jsxExt ? 'sx' : 's'}`,
62+
script: `${this.typescript ? 't' : 'j'}s`,
63+
style: styleExt,
64+
};
65+
}
66+
67+
private setPrefixes() {
68+
this.prefixes = {
69+
style: this.cssModules ? 'module' : 'styles',
70+
test: 'test',
71+
};
72+
}
73+
}

src/config/default.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
"jsxExt": true,
55
"wrapFolder": true,
66
"fileNameCase": "pascal",
7-
"path": "src/components"
7+
"path": "src/components",
8+
"cssModules": true
89
}

src/config/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { Config } from '@/config/Config';
2+
import { getUserConfig } from '@/utils';
3+
4+
export { Config } from './Config';
5+
const userConfig = getUserConfig();
6+
export const config = new Config(userConfig);

src/constants/index.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
1-
import path from 'path';
2-
3-
import { getAppRoot, getConfig, getStylesExtension } from '../utils';
4-
import { StyleFormats } from '../types';
1+
import { StyleFormats } from '@/types';
2+
import * as utils from '@/utils';
53

4+
// export const ROOT = path.dirname(require.main.filename);
65
export const STYLE_FORMATS: StyleFormats[] = ['CSS', 'SCSS', 'SASS', 'Less', 'Stylus'];
7-
export const APP_ROOT = getAppRoot();
8-
export const ROOT = path.dirname(require.main.filename);
9-
export const CONFIG = getConfig();
10-
export const EXT = {
11-
component: CONFIG.typescript ? 'tsx' : `j${CONFIG.jsxExt ? 'sx' : 's'}`,
12-
script: `${CONFIG.typescript ? 't' : 'j'}s`,
13-
style: getStylesExtension(CONFIG.styles),
14-
};
6+
export const APP_ROOT = utils.getAppRoot();

src/core/FileGenerateManager.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getComponentVariables, getHOCVariables, getHookVariables } from '../utils';
1+
import * as utils from '../utils';
22
import { FileService } from './FileService';
33
import { TemplateGenerator } from './TemplateGenerator';
44
import { HOCTemplate } from './temlpates/components/HOCTemplate';
@@ -8,7 +8,7 @@ import { HookTemplate } from './temlpates/components/HookTemplate';
88

99
export class FileGenerateManager {
1010
static generateHook(answers) {
11-
const variables = getHookVariables(answers);
11+
const variables = utils.getHookVariables(answers);
1212

1313
const fileService = new FileService(variables.fileName);
1414
const templateGenerator = new TemplateGenerator(variables);
@@ -20,7 +20,7 @@ export class FileGenerateManager {
2020
}
2121

2222
static generateHOC(answers) {
23-
const variables = getHOCVariables(answers);
23+
const variables = utils.getHOCVariables(answers);
2424

2525
const fileService = new FileService(variables.fileName);
2626
const templateGenerator = new TemplateGenerator(variables);
@@ -32,7 +32,7 @@ export class FileGenerateManager {
3232
}
3333

3434
static generateComponent(answers) {
35-
const variables = getComponentVariables(answers);
35+
const variables = utils.getComponentVariables(answers);
3636

3737
const fileService = new FileService(variables.fileName);
3838
const templateGenerator = new TemplateGenerator(variables);

src/core/FileService.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import fs from 'fs-extra';
22
import path from 'path';
33

4-
import { CONFIG, EXT, APP_ROOT } from '../constants';
4+
import { APP_ROOT } from '@/constants';
5+
import { config } from '@/config';
56
import { Logger } from './Logger';
67

78
export class FileService {
8-
private dirPath = path.resolve(CONFIG.path, CONFIG.wrapFolder ? this.fileName : '');
9+
private dirPath = path.resolve(config.path, config.wrapFolder ? this.fileName : '');
910

1011
constructor(private fileName: string) {}
1112

@@ -19,7 +20,7 @@ export class FileService {
1920
}
2021

2122
genJs(template: string) {
22-
const filePath = this.getFilePath(EXT.component);
23+
const filePath = this.getFilePath(config.ext.component);
2324
if (!fs.pathExistsSync(filePath)) {
2425
fs.writeFileSync(filePath, template);
2526
Logger.success(
@@ -32,7 +33,7 @@ export class FileService {
3233
}
3334

3435
genStyle(template: string) {
35-
const filePath = this.getFilePath(EXT.style, 'styles');
36+
const filePath = this.getFilePath(config.ext.style, config.prefixes.style);
3637
if (!fs.pathExistsSync(filePath)) {
3738
fs.writeFileSync(filePath, template);
3839
Logger.success(
@@ -45,7 +46,7 @@ export class FileService {
4546
}
4647

4748
genTest(template: string) {
48-
const filePath = this.getFilePath(EXT.component, 'test');
49+
const filePath = this.getFilePath(config.ext.component, config.prefixes.test);
4950
if (!fs.pathExistsSync(filePath)) {
5051
fs.writeFileSync(filePath, template);
5152
Logger.success(

src/core/TemplateBase.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as c from './temlpates/shared/utils';
1+
import * as c from './temlpates/shared';
22

33
export class TemplateBase {
44
constructor(protected vars) {}

0 commit comments

Comments
 (0)