Skip to content

Commit 996861e

Browse files
committed
feat: new helper 'space'
1 parent bdf5129 commit 996861e

10 files changed

Lines changed: 57 additions & 85 deletions

File tree

README.md

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@
3030
- [Helper functions](#helper-functions)
3131
- [`upper`](#upper)
3232
- [`lower`](#lower)
33-
- [`camel`](#camel)
3433
- [`capital`](#capital)
34+
- [`camel`](#camel)
3535
- [`snake`](#snake)
3636
- [`kebab`](#kebab)
37+
- [`space`](#space)
3738
- [`uuid`](#uuid)
3839
- [Config](#config)
3940
- [`extra`](#extra)
@@ -94,6 +95,8 @@ Edit files inside `templates/default`. File names, directory names, and text fil
9495

9596
### Helper functions
9697

98+
In the following example, we assume that variable `name` is `create-react-app`.
99+
97100
#### `upper`
98101

99102
Output text in UPPERCASE.
@@ -106,17 +109,18 @@ Output text in lowercase.
106109

107110
`{{lower name}}` becomes `create-react-app`.
108111

109-
#### `camel`
112+
#### `capital`
110113

111-
Output text in camelCase.
114+
Output text in CapitalCase.
112115

113-
`{{camel name}}` becomes `createReactApp`.
116+
- `{{capital name}}` becomes `CreateReactApp`
117+
- `{{capital name space=true}}` becomes `Create React App`.
114118

115-
#### `capital`
119+
#### `camel`
116120

117-
Output text in CapitalCase.
121+
Output text in camelCase.
118122

119-
`{{capital name}}` becomes `CreateReactApp`, and `{{capital name space=true}}` becomes `Create React App`.
123+
`{{camel name}}` becomes `createReactApp`.
120124

121125
#### `snake`
122126

@@ -130,6 +134,12 @@ Output text in kebab-case.
130134

131135
`{{kebab name}}` becomes `create-react-app`.
132136

137+
#### `space`
138+
139+
Replace all word separators with single space.
140+
141+
`{{space name}}` becomes `create react app`
142+
133143
#### `uuid`
134144

135145
Generates unique UUID string.

src/template.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ import slash from 'slash';
77
import { v4 as uuidv4 } from 'uuid';
88
import { View } from '.';
99

10+
function split(word: string): string[] {
11+
return word.split(/[-_\s]+/);
12+
}
13+
14+
function space(word: string): string {
15+
return split(trim(word)).join(' ');
16+
}
17+
Handlebars.registerHelper('space', space);
18+
1019
function trim(text: string) {
1120
return text.replace(/[\r\n]/g, '');
1221
}
@@ -17,15 +26,15 @@ function upper(text: string) {
1726
}
1827
Handlebars.registerHelper('upper', upper);
1928

20-
function lower(text: string) {
29+
function lower(text: string, options?: any) {
30+
const space = options && options.hash && options.hash.space;
2131
return trim(text).toLowerCase();
2232
}
2333
Handlebars.registerHelper('lower', lower);
2434

2535
function capital(text: string, options?: any) {
2636
const space = options && options.hash && options.hash.space;
27-
return trim(text)
28-
.split(/[-_\s]+/)
37+
return split(trim(text))
2938
.map((s) => s.charAt(0).toUpperCase() + s.slice(1).toLowerCase())
3039
.join(space ? ' ' : '');
3140
}

templates/default/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ npx {{kebab name}} <name>
1717
- \{{name}} => {{name}}
1818
- \{{upper name}} => {{upper name}}
1919
- \{{lower name}} => {{lower name}}
20-
- \{{camel name}} => {{camel name}}
2120
- \{{capital name}} => {{capital name}}
2221
- \{{capital name space=true}} => {{capital name space=true}}
22+
- \{{camel name}} => {{camel name}}
2323
- \{{snake name}} => {{snake name}}
2424
- \{{kebab name}} => {{kebab name}}
25+
- \{{space name}} => {{space name}}
2526
- \{{uuid}} => {{uuid}}
2627
- \{{upper (uuid)}} => {{upper (uuid)}}
2728
- \{{description}} => {{description}}

templates/default/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
"name": "{{kebab name}}",
33
"description": "{{description}}",
44
"version": "0.0.0",
5+
"author": "{{contact}}",
56
"dependencies": {
6-
"create-create-app": "^5.5.0"
7+
"create-create-app": "^5.7.0"
78
},
8-
"author": "{{contact}}",
9-
"license": "{{license}}",
109
"bin": "src/cli.js",
1110
"files": [
1211
"src",
1312
"templates"
14-
]
13+
],
14+
"license": "{{license}}"
1515
}

templates/default/src/cli.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const { resolve } = require('path');
44
const { create } = require('create-create-app');
55

6-
const templateRoot = resolve(__dirname, '../templates');
6+
const templateRoot = resolve(__dirname, '..', 'templates');
77

88
const caveat = `
99
This is a caveat!

templates/typescript/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/lib
1+
/dist
22

33
# Created by https://www.gitignore.io/api/node
44
# Edit at https://www.gitignore.io/?templates=node

templates/typescript/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
## Use
88

99
```bash
10-
npx {{kebab name}} <name>
10+
npx {{lower name}} <name>
1111
```
1212

1313
## Template string reference
@@ -17,11 +17,12 @@ npx {{kebab name}} <name>
1717
- \{{name}} => {{name}}
1818
- \{{upper name}} => {{upper name}}
1919
- \{{lower name}} => {{lower name}}
20-
- \{{camel name}} => {{camel name}}
2120
- \{{capital name}} => {{capital name}}
2221
- \{{capital name space=true}} => {{capital name space=true}}
22+
- \{{camel name}} => {{camel name}}
2323
- \{{snake name}} => {{snake name}}
2424
- \{{kebab name}} => {{kebab name}}
25+
- \{{space name}} => {{space name}}
2526
- \{{uuid}} => {{uuid}}
2627
- \{{upper (uuid)}} => {{upper (uuid)}}
2728
- \{{description}} => {{description}}

templates/typescript/package.json

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,24 @@
22
"name": "{{kebab name}}",
33
"description": "{{description}}",
44
"version": "0.0.0",
5+
"author": "{{contact}}",
56
"scripts": {
6-
"build": "shx rm -rf lib && NODE_ENV=production tsc && shx chmod +x lib/cli.js",
7-
"dev": "tsc -w"
7+
"build": "tsup src/cli.ts --minify",
8+
"clean": "shx rm -rf lib",
9+
"dev": "tsc -w",
10+
"prepublishOnly": "npm run clean && npm run build"
811
},
912
"dependencies": {
10-
"create-create-app": "^5.5.0"
13+
"create-create-app": "^5.7.0"
1114
},
1215
"devDependencies": {
13-
"shx": "^0.3.2",
14-
"typescript": "^3.6.3"
16+
"tsup": "^3.1.1",
17+
"typescript": "^3.9.7"
1518
},
16-
"author": "{{contact}}",
17-
"license": "{{license}}",
18-
"bin": "lib/cli.js",
19+
"bin": "dist/cli.js",
1920
"files": [
20-
"lib",
21+
"dist",
2122
"templates"
22-
]
23+
],
24+
"license": "{{license}}"
2325
}

templates/typescript/src/cli.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { create } from 'create-create-app';
44
import { resolve } from 'path';
55

6-
const templateRoot = resolve(__dirname, '../templates');
6+
const templateRoot = resolve(__dirname, '..', 'templates');
77

88
const caveat = `
99
This is a caveat!

templates/typescript/tsconfig.json

Lines changed: 4 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,13 @@
11
{
2-
"exclude": ["lib"],
2+
"exclude": ["dist"],
33
"compilerOptions": {
4-
/* Basic Options */
5-
// "incremental": true, /* Enable incremental compilation */
6-
"target": "es5" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */,
4+
"target": "es2018" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */,
75
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
8-
// "lib": [], /* Specify library files to be included in the compilation. */
9-
// "allowJs": true, /* Allow javascript files to be compiled. */
10-
// "checkJs": true, /* Report errors in .js files. */
11-
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
126
"declaration": true /* Generates corresponding '.d.ts' file. */,
13-
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
147
// "sourceMap": true, /* Generates corresponding '.map' file. */
15-
// "outFile": "./", /* Concatenate and emit output to single file. */
16-
"outDir": "./lib" /* Redirect output structure to the directory. */,
8+
"outDir": "./dist" /* Redirect output structure to the directory. */,
179
"rootDir": "./src" /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */,
18-
// "composite": true, /* Enable project compilation */
19-
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
20-
// "removeComments": true, /* Do not emit comments to output. */
21-
// "noEmit": true, /* Do not emit outputs. */
22-
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
23-
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
24-
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
25-
26-
/* Strict Type-Checking Options */
2710
"strict": true /* Enable all strict type-checking options. */,
28-
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
29-
// "strictNullChecks": true, /* Enable strict null checks. */
30-
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
31-
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
32-
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
33-
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
34-
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
35-
36-
/* Additional Checks */
37-
// "noUnusedLocals": true, /* Report errors on unused locals. */
38-
// "noUnusedParameters": true, /* Report errors on unused parameters. */
39-
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
40-
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
41-
42-
/* Module Resolution Options */
43-
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
44-
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
45-
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
46-
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
47-
// "typeRoots": [], /* List of folders to include type definitions from. */
48-
// "types": [], /* Type declaration files to be included in compilation. */
49-
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
50-
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
51-
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
52-
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
53-
54-
/* Source Map Options */
55-
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
56-
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
57-
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
58-
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
59-
60-
/* Experimental Options */
61-
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
62-
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
11+
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies */
6312
}
6413
}

0 commit comments

Comments
 (0)