Skip to content

Commit cbb6222

Browse files
Config re-work and template publishing (#38)
* - Moved templates into mustache - Generation logic now resides in the templates - Added flag 'namedexports' to remove default exports * - Updated templates * - Added template reading mechanism to add to the promise chain - Templates refactored as pure handlebars files - Stories and Tests added to templates * - Added template publishing - Split application into two sub commands with the default set to create for backward compatibility. - Added argument to logComponentTree to allow control over appending the component name to the folderPath * - Updated readme with extra detail on usage for templates and namedexports * - Added error handling for directory reading in the publish scripts. * Fixed bug in classComponents template for proptypes * Fixed functional template proptypes * Added capability to change output by config * Added a few comments * Delete .crcfrc Removed .crcfrc - Mistakenly added * Refactored config * Added freeze to config api * Updated gitignore to remove intelij config * Bump js-yaml from 3.12.0 to 3.13.1 Bumps [js-yaml](https://github.com/nodeca/js-yaml) from 3.12.0 to 3.13.1. - [Release notes](https://github.com/nodeca/js-yaml/releases) - [Changelog](https://github.com/nodeca/js-yaml/blob/master/CHANGELOG.md) - [Commits](nodeca/js-yaml@3.12.0...3.13.1) Signed-off-by: dependabot[bot] <[email protected]> * Bump eslint-utils from 1.3.1 to 1.4.3 Bumps [eslint-utils](https://github.com/mysticatea/eslint-utils) from 1.3.1 to 1.4.3. - [Release notes](https://github.com/mysticatea/eslint-utils/releases) - [Commits](mysticatea/eslint-utils@v1.3.1...v1.4.3) Signed-off-by: dependabot[bot] <[email protected]> * Prettier now used by default * Bump lodash.merge from 4.6.1 to 4.6.2 Bumps [lodash.merge](https://github.com/lodash/lodash) from 4.6.1 to 4.6.2. - [Release notes](https://github.com/lodash/lodash/releases) - [Commits](https://github.com/lodash/lodash/commits) Signed-off-by: dependabot[bot] <[email protected]> * Bump lodash from 4.17.11 to 4.17.15 Bumps [lodash](https://github.com/lodash/lodash) from 4.17.11 to 4.17.15. - [Release notes](https://github.com/lodash/lodash/releases) - [Commits](lodash/lodash@4.17.11...4.17.15) Signed-off-by: dependabot[bot] <[email protected]> * - Updated CRCF runtime error catching - Fixed proptype errors in templates * Updated lock file after merge with upstream * Bumped version to 0.3.0 * Fixed bugs introduced during merge * - Fixed bug with undefined showing at start - Added more information to the readme Co-authored-by: dependabot[bot] <dependabot[bot]@users.noreply.github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
1 parent 5c14304 commit cbb6222

20 files changed

+1359
-1278
lines changed

.eslintrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"extends": "airbnb"
2+
"extends": "airbnb",
3+
"env": {"node": true}
34
}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ node_modules
33
package-lock.json
44
.prettierrc
55
.DS_Store
6+
.idea

README.md

Lines changed: 58 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ _([npx](https://medium.com/@maybekatz/introducing-npx-an-npm-package-runner-55f7
3232
$ npm install --save-dev create-react-component-folder
3333
```
3434

35-
## Creating single component
35+
## Creating a single component
3636

3737
```sh
3838
$ npx crcf myComponent
@@ -64,21 +64,21 @@ myComponent
6464
├── index.js
6565
├── myComponent.js
6666
├── myComponent.css
67-
├── myComponent.test.js
67+
├── myComponent.test.handlebars
6868
```
6969

70-
### if story book is enable
70+
### With storybook enabled
7171

7272
```sh
7373
myComponent
7474
├── index.js
7575
├── myComponent.js
7676
├── myComponent.css
77-
├── myComponent.test.js
77+
├── myComponent.test.handlebars
7878
├── myComponent.stories.js
7979
```
8080

81-
## Set default config
81+
## Setting default config
8282

8383
There is support for setting default config options, so you only have to set you desired config once. This makes creating your components even easier. All you have to do is follow one of these three options.
8484

@@ -133,7 +133,11 @@ So now all you have to do is type **npx crcf componentName** and you will get al
133133
"proptypes",
134134
"stories",
135135
"nosemi",
136-
"cssmodules",
136+
"cssmodules"
137+
"namedexports",
138+
{
139+
"output": "base/directory/to/place/created/components"
140+
}
137141
]
138142
```
139143

@@ -149,6 +153,37 @@ $ npx crcf --createindex
149153
<img src='https://github.com/snaerth/create-react-component-folder/blob/master/docs/index2.png?raw=true' width='600'/>
150154
</p>
151155

156+
## Publishing templates
157+
158+
If the project you are working on always needs components structured differently, we've got you covered.
159+
160+
Publishing the templates allows you to have finer control over the generated components and content. Published templates use <handlebars>[https://handlebarsjs.com/] to generate the different components.
161+
162+
```sh
163+
$ npx crcf publish-templates
164+
```
165+
166+
The templates will be copied to a directory `.crcf/templates` relative to the directory you ran the script from.
167+
168+
### Modifying templates
169+
170+
Templates are always passed a number of variables to help you generate templates when certain flags / config options have been enabled.
171+
172+
| Variable | Type | Description |
173+
|----------|------|-------------|
174+
| name | string | The generated name of the component |
175+
| typescript | boolean | `true` when `typescript` is enabled |
176+
| native | boolean | `true` when building react native components |
177+
| proptypes | boolean | `true` when `proptypes` is enabled |
178+
| export | boolean | `true` when `namedexports` is enabled |
179+
180+
**Tests and stories** have extra variables (**NOT** available in functional or class component templates)
181+
182+
| Variable | Type | Description |
183+
|----------|------|-------------|
184+
| nameLowercase | string | The generated name of the component in lowercase so it can be interpolated in sentences. |
185+
| uppercase | boolean | `true` when `uppercase` is enabled
186+
152187
## Options
153188

154189
```sh
@@ -158,22 +193,23 @@ $ npx crcf --help
158193

159194
Options:
160195

161-
-V, --version output the version number
162-
--typescript Creates Typescript component and files
163-
--nocss No css file
164-
--notest No test file
165-
--cssmodules Creates css/less/scss file with .module extensions. Example
166-
--reactnative Creates React Native components
167-
--createindex Creates index.js file for multple component imports
168-
-f, --functional Creates React stateless functional component
169-
-j, --jsx Creates the component file with .jsx extension
170-
-l, --less Adds .less file to component
171-
-s, --scss Adds .scss file to component
172-
-p, --proptypes Adds prop-types to component
173-
-u, --uppercase Component files start on uppercase letter
174-
-h, --help output usage information
175-
-sb, --stories Add Storie file to component
176-
-ns, --nosemi No semicolons
196+
-V, --version output the version number
197+
--typescript Creates Typescript component and files
198+
--nocss No css file
199+
--notest No test file
200+
--cssmodules Creates css/less/scss file with .module extensions. Example
201+
--reactnative Creates React Native components
202+
--createindex Creates index.js file for multple component imports
203+
-f, --functional Creates React stateless functional component
204+
-j, --jsx Creates the component file with .jsx extension
205+
-l, --less Adds .less file to component
206+
-s, --scss Adds .scss file to component
207+
-p, --proptypes Adds prop-types to component
208+
-u, --uppercase Component files start on uppercase letter
209+
-h, --help output usage information
210+
-sb, --stories Add Storie file to component
211+
-ns, --nosemi No semicolons
212+
-x, --namedexports Creates files using named exports
177213
```
178214

179215
## Author

bin/crcf-create.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env node
2+
require('../lib/index');

bin/crcf-publish-templates.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env node
2+
require('../lib/publish');

bin/crcf.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
11
#!/usr/bin/env node
22

3-
require('../lib/index');
3+
const program = require('commander');
4+
5+
try {
6+
program.version('0.2.0')
7+
.command('publish-templates', 'Publish template files to a project')
8+
.command('create', 'Create a react component folder', { isDefault: true })
9+
.parse(process.argv);
10+
} catch(error) {
11+
logger.error('Invalid command', error);
12+
}

lib/config.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const CONFIG_KEY = Symbol.for('crcf.config');
2+
const globalSymbols = Object.getOwnPropertySymbols(global);
3+
4+
if (!globalSymbols.indexOf(CONFIG_KEY) > -1) {
5+
global[CONFIG_KEY] = {
6+
flags: [],
7+
values: {},
8+
};
9+
}
10+
11+
const store = {};
12+
Object.defineProperty(store, 'flags', {
13+
get: () => global[CONFIG_KEY].flags,
14+
set: (flags) => { global[CONFIG_KEY].flags = flags; },
15+
});
16+
17+
Object.defineProperty(store, 'values', {
18+
get: () => global[CONFIG_KEY].values,
19+
set: (values) => { global[CONFIG_KEY].values = values; },
20+
});
21+
22+
Object.freeze(store);
23+
24+
const config = {
25+
mergeFlags: (flags = []) => { store.flags = [...store.flags, ...flags]; },
26+
hasFlag: flag => store.flags.includes(flag),
27+
getValue: (prop, def = null) => store.values[prop] || def,
28+
setValue: (prop, value) => { config.mergeValues({ [prop]: value }); },
29+
mergeValues: (values) => { store.values = { ...store.values, ...values }; },
30+
mergeAll: (toMerge) => {
31+
const mergeObject = { ...toMerge };
32+
if (mergeObject.flags) {
33+
config.mergeFlags(mergeObject.flags);
34+
delete mergeObject.flags;
35+
}
36+
37+
config.mergeValues(mergeObject);
38+
},
39+
};
40+
41+
Object.freeze(config);
42+
43+
module.exports = config;

0 commit comments

Comments
 (0)