Skip to content

Commit d96792b

Browse files
authored
Merge pull request #56 from Prioe/improved-config-loading
Improved loading of configuration files
2 parents 7d17f9f + da8e521 commit d96792b

File tree

4 files changed

+59
-76
lines changed

4 files changed

+59
-76
lines changed

.ccarc.example

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"templatesDirPath": "./templates",
3+
"path": "./src/components",
4+
"jsExtension": "js",
5+
"cssExtension": "css",
6+
"includeTests": true,
7+
"includeStories": true,
8+
"indexFile": false,
9+
"connected": false,
10+
"componentMethods": [],
11+
"fileNames": {
12+
"testFileMatch": "spec",
13+
"testFileName": "myTest",
14+
"componentFileName": "template",
15+
"styleFileName": "style"
16+
}
17+
}

README.md

Lines changed: 22 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -81,71 +81,32 @@ and searching up the file tree until a config file is (or isn't) found.
8181

8282
### Basic Configuration
8383

84-
JSON:
85-
86-
```json
87-
// .ccarc
88-
{
89-
"type": "class",
90-
"path": "./src/components"
91-
}
92-
```
93-
94-
YAML:
95-
96-
```yaml
97-
# .ccarc
98-
type: class
99-
path: ./src/components
100-
```
84+
An example configuration file can be found here: [.ccarc.example](.ccarc.example), you can use this
85+
file by copying it to the root of your project.
86+
87+
Currently supported options are:
88+
89+
Option | Description
90+
--- | ---
91+
`type` | Default type of the component `["stateless", "class", "pure"]`
92+
`templatesDirPath` | Default path to get the templates from the custom templates folder
93+
`path` | Default path to create component file and folder
94+
`jsExtension` | Default extension for your javascript file `["js", "jsx"]`
95+
`cssExtension` | Default extension for your css file `["css", "scss", "sass", "less", false]`. Set to false if you don't want a style file
96+
`includeTests` | Default flag to include a test file in the folder `[true, false]`
97+
`includeStories` | Default flag to include a storybook file in the folder `[true, false]`
98+
`indexFile` | Default flag to create an index file in the folder `[false, true]`
99+
`connected` | Default flag to integrate connect redux in the index file `[false, true]`
100+
`componentMethods` | Only for "class" and "pure", insert method inside the component (i.e. `["componentDidMount", "shouldComponentUpdate", "onClick"]`)
101+
`fileNames` | Choose the specific filename for your component's file.
102+
`fileNames.testFileMatch` | specify the match part of test file
103+
`fileNames.testFileName` | specify the file name of your test file
104+
`fileNames.componentFileName` | specify the component file name
105+
`fileNames.styleFileName` | specify the style file name !!IMPORTANT: Include cssExtension.
101106

102107
### You can also pass a config file
103108

104109
1) Create a JSON file `config.json`:
105-
106-
```javascript
107-
{
108-
// Default type of component ["stateless", "class", "pure"]
109-
"type": "stateless",
110-
111-
// Default path to get the templates from the custom templates folder
112-
"templatesDirPath": "./templates",
113-
114-
// Default path to create component file and folder
115-
"path": "./src/components",
116-
117-
// Default extension for your javascript file ["js", "jsx"]
118-
"jsExtension": "js",
119-
120-
// Default extension for your css file ["css", "scss", "sass", "less", false]
121-
// Set to false if you don't want a style file
122-
"cssExtension": "css",
123-
124-
// Default flag to include a test file in the folder [true, false]
125-
"includeTests": true,
126-
127-
// Default flag to include a storybook file in the folder [true, false]
128-
"includeStories": true,
129-
130-
// Default flag to create an index file in the folder [false, true]
131-
"indexFile": false,
132-
133-
// Default flag to integrate connect redux in the index file [false, true]
134-
"connected": false,
135-
136-
// Only for "class" and "pure", insert method inside the component
137-
"componentMethods": ["componentDidMount", "shouldComponentUpdate", "onClick"],
138-
139-
// Choose the specific filename for your component's file.
140-
"fileNames": {
141-
"testFileMatch": "spec", // specify the match part of test file
142-
"testFileName": "myTest", // specify the file name of your test file
143-
"componentFileName": "template", // specify the component file name
144-
"styleFileName": "style" // specify the style file name !!IMPORTANT: Include cssExtension.
145-
}
146-
}
147-
```
148-
149110
2) and pass the path to config param
150111

151112
```sh

src/logger.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1+
/* eslint-disable no-console */
12
import chalk from 'chalk'
23

34
const Logger = {
4-
log(msg) {
5-
/* eslint-disable no-console */
6-
return console.log(chalk.green(JSON.stringify(msg)))
7-
/* eslint-enable no-console */
5+
log(...msg) {
6+
return console.log(chalk.green('[Info]'), ...msg)
87
},
9-
error(msg) {
10-
/* eslint-disable no-console */
11-
return console.error(chalk.bold.red(JSON.stringify(msg)))
12-
/* eslint-enable no-console */
8+
error(...msg) {
9+
return console.error(chalk.bold.red('[Error]'), ...msg)
1310
},
14-
warn(msg) {
15-
/* eslint-disable no-console */
16-
return console.warn(chalk.keyword('orange')(JSON.stringify(msg)))
17-
/* eslint-enable no-console */
11+
warn(...msg) {
12+
return console.warn(chalk.keyword('orange')('[Warn]'), ...msg)
13+
},
14+
debug(...msg) {
15+
return console.warn(chalk.blue('[Debug]'), ...msg)
1816
},
1917
}
2018

src/utils.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,17 @@ function getConfig(configPath, searchPath = process.cwd(), stopDir = homedir())
124124
const configPathAbsolute = useCustomPath && path.join(process.cwd(), configPath)
125125
// search from the root of the process if the user didnt specify a config file,
126126
// or use the custom path if a file is passed.
127-
const { config, filepath } = explorer.load(searchPathAbsolute, configPathAbsolute)
128-
return { ...(config || {}), filepath }
127+
const result = explorer.load(searchPathAbsolute, configPathAbsolute)
128+
129+
// dont throw if the explorer didnt find a configfile,
130+
// instead use default config
131+
const config = result ? result.config : {}
132+
const filepath = result ? result.filepath : {}
133+
if (!result) Logger.log('No config file detected, using defaults.')
134+
135+
return { ...config, filepath }
129136
} catch (error) {
130-
Logger.error('Bad config file, Please check config file syntax')
137+
Logger.error('An error occured while parsing your config file. Using defaults...\n\n', error.message)
131138
}
132139
return {}
133140
}

0 commit comments

Comments
 (0)