Skip to content

Commit f89178f

Browse files
committed
Add testing directory, refactor config, minor fixes
1 parent 6e214f6 commit f89178f

19 files changed

Lines changed: 3796 additions & 5424 deletions

.prettierrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"singleQuote": true,
33
"arrowParens": "avoid",
4-
"printWidth": 100
4+
"printWidth": 80
55
}

package-lock.json

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

package.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@
1010
"lib"
1111
],
1212
"scripts": {
13-
"prepublish": "npm run build",
13+
"prepublish": "yarn build && yarn clean",
14+
"postpublish": "yarn build",
1415
"build": "babel src --out-dir lib --copy-files --extensions \".ts\"",
15-
"watch": "npm run build -- --watch --source-maps inline",
16-
"lint": "eslint src --ext .ts"
16+
"watch": "yarn build --watch --source-maps inline",
17+
"lint": "eslint src --ext .ts",
18+
"start": "node ./lib/bin.js",
19+
"gen": "node ./lib/__tests__/script.js",
20+
"clean": "rm -rf ./src/__tests__/generated/*; rm -rf ./lib/__tests__"
1721
},
1822
"husky": {
1923
"hooks": {
@@ -36,7 +40,6 @@
3640
"inquirer": "^7.3.3",
3741
"lodash": "^4.17.19",
3842
"log-symbols": "^4.0.0",
39-
"mustache": "^4.0.1",
4043
"prettier": "^2.0.5",
4144
"yargs": "^15.4.1"
4245
},
@@ -61,7 +64,6 @@
6164
"eslint-config-airbnb-typescript": "^8.0.2",
6265
"eslint-config-prettier": "^6.11.0",
6366
"eslint-plugin-import": "^2.22.0",
64-
"husky": "^4.2.5",
6567
"typescript": "^3.9.7"
6668
},
6769
"keywords": [

src/__tests__/answers.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "button",
3+
"test": true,
4+
"hooks": ["useState"],
5+
"mods": ["propTypes"]
6+
}

src/__tests__/config.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"styles": "sccss",
3+
"typescript": false,
4+
"jsxExt": false,
5+
"wrapFolder": true,
6+
"fileNameCase": "pascalx",
7+
"path": "src/__tests__/generated",
8+
"cssModules": false,
9+
"arrowFunction": true,
10+
"exportType": "modules"
11+
}

src/__tests__/script.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env node
2+
3+
import fs from 'fs-extra';
4+
import path from 'path';
5+
6+
import { config } from '@/config';
7+
import { IConfig } from '@/types';
8+
import { FileGenerateManager } from '@/core/FileGenerateManager';
9+
import testAnswers from './answers.json';
10+
import testConfig from './config.json';
11+
12+
fs.emptyDirSync(path.resolve(__dirname, '../../src/__tests__/generated'));
13+
14+
config.update(testConfig as IConfig);
15+
16+
FileGenerateManager.generateComponent(testAnswers);
17+
FileGenerateManager.generateHook(testAnswers);
18+
FileGenerateManager.generateHOC(testAnswers);

src/config/Config.ts

Lines changed: 66 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import { ExportType, FileNameCase, IConfig, StyleFormats } from '@/types';
2-
import defaultConfig from './default.json';
2+
import {
3+
APP_ROOT,
4+
DEFAULT_CONFIG,
5+
EXPORT_TYPES,
6+
STYLE_FORMATS,
7+
} from '@/constants';
8+
import { Logger } from '@/core/Logger';
9+
import path from 'path';
310

411
export type ConfigPrefixes = {
512
style: 'module' | 'styles';
@@ -13,8 +20,6 @@ export type ConfigExt = {
1320
};
1421

1522
export class Config implements IConfig {
16-
config: IConfig;
17-
1823
fileNameCase: FileNameCase;
1924

2025
styles: StyleFormats;
@@ -38,23 +43,63 @@ export class Config implements IConfig {
3843
exportType: ExportType;
3944

4045
constructor(config: IConfig) {
41-
this.config = Object.assign(defaultConfig, config);
46+
const cfg = Object.assign(DEFAULT_CONFIG, config);
4247

43-
this.setInitialVariables();
48+
this.setVariables(cfg);
4449
this.setFilesExtension();
4550
this.setPrefixes();
4651
}
4752

48-
private setInitialVariables() {
49-
this.fileNameCase = this.config.fileNameCase;
50-
this.styles = this.config.styles;
51-
this.path = this.config.path;
52-
this.jsxExt = this.config.jsxExt;
53-
this.typescript = this.config.typescript;
54-
this.wrapFolder = this.config.wrapFolder;
55-
this.cssModules = this.config.cssModules;
56-
this.exportType = this.config.exportType;
57-
this.arrowFunction = this.config.arrowFunction;
53+
private setVariables(config) {
54+
// Boolean
55+
56+
this.jsxExt = config.jsxExt ?? this.jsxExt;
57+
this.typescript = config.typescript ?? this.typescript;
58+
this.wrapFolder = config.wrapFolder ?? this.wrapFolder;
59+
this.cssModules = config.cssModules ?? this.cssModules;
60+
this.arrowFunction = config.arrowFunction ?? this.arrowFunction;
61+
62+
// Enum
63+
64+
this.fileNameCase = config.fileNameCase ?? this.fileNameCase;
65+
66+
if (config.styles) {
67+
const styles = config.styles.toLowerCase();
68+
const formats = STYLE_FORMATS.map(v => v.toLowerCase());
69+
const isSupported = formats.includes(styles);
70+
71+
if (isSupported) {
72+
this.styles = config.styles;
73+
} else {
74+
Logger.warn(
75+
chalk =>
76+
`Unsupported "styles" value - ${styles} ${chalk.white(
77+
`(using default value - ${DEFAULT_CONFIG.styles})`
78+
)}`
79+
);
80+
this.styles = DEFAULT_CONFIG.styles;
81+
}
82+
}
83+
84+
if (config.exportType) {
85+
if (EXPORT_TYPES.includes(config.exportType)) {
86+
this.exportType = config.exportType;
87+
} else {
88+
Logger.warn(
89+
chalk =>
90+
`Unsupported "exportType" value - ${
91+
config.exportType
92+
} ${chalk.white(
93+
`(using default value - ${DEFAULT_CONFIG.exportType})`
94+
)}`
95+
);
96+
this.exportType = DEFAULT_CONFIG.exportType;
97+
}
98+
}
99+
100+
// Other
101+
102+
this.path = path.resolve(APP_ROOT, config.path ?? DEFAULT_CONFIG.path);
58103
}
59104

60105
private setFilesExtension() {
@@ -76,4 +121,10 @@ export class Config implements IConfig {
76121
test: 'test',
77122
};
78123
}
124+
125+
public update(config: IConfig) {
126+
this.setVariables(config);
127+
this.setFilesExtension();
128+
this.setPrefixes();
129+
}
79130
}

src/config/default.json

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

src/config/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { Config } from '@/config/Config';
21
import { getUserConfig } from '@/utils';
2+
import { Config } from './Config';
33

44
export { Config } from './Config';
5-
export { default as defaultConfig } from './default.json';
65

76
const userConfig = getUserConfig();
87
export const config = new Config(userConfig);

src/constants/index.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
1-
import { ExportType, StyleFormats } from '@/types';
1+
import { ExportType, IConfig, StyleFormats } from '@/types';
22
import * as utils from '@/utils';
33

4-
// export const ROOT = path.dirname(require.main.filename);
5-
export const STYLE_FORMATS: StyleFormats[] = ['CSS', 'SCSS', 'SASS', 'Less', 'Stylus'];
4+
export const DEFAULT_CONFIG: IConfig = {
5+
styles: 'scss',
6+
typescript: false,
7+
jsxExt: true,
8+
wrapFolder: true,
9+
fileNameCase: 'pascal',
10+
path: 'src/components',
11+
cssModules: false,
12+
exportType: 'default',
13+
arrowFunction: true,
14+
};
15+
export const STYLE_FORMATS: StyleFormats[] = [
16+
'css',
17+
'scss',
18+
'sass',
19+
'less',
20+
'stylus',
21+
];
622
export const EXPORT_TYPES: ExportType[] = ['named', 'default'];
723
export const APP_ROOT = utils.getAppRoot();

0 commit comments

Comments
 (0)