Skip to content

Commit 83c07d3

Browse files
authored
Merge pull request #36 from codeBelt/IStringReplacerQuestion
Add IReplacerSlotQuestion
2 parents 156ea62 + 7df4f44 commit 83c07d3

File tree

10 files changed

+495
-483
lines changed

10 files changed

+495
-483
lines changed

README.md

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ generateTemplateFiles([
4242
entry: {
4343
folderPath: './tools/templates/react/redux-store/',
4444
},
45-
stringReplacers: ['__store__', '__model__'],
45+
stringReplacers: ['__store__', {question: 'Insert model name', slot: '__model__'}],
4646
output: {
4747
path: './src/stores/__store__(lowerCase)',
4848
pathAndFileNameDefaultCase: '(kebabCase)',
@@ -89,11 +89,12 @@ The `generateTemplateFiles` function takes an array of `IConfigItem` items.
8989
#### `IConfigItem`
9090

9191
- `option` - The name of the option to choose when asked.
92-
- `defaultCase` - The default [Case Converters](#case-converters) to use with the [Replacer Slots](#replacer-slots) in the template files. Default is `(noCase)`.
92+
- `defaultCase` - The default [Case Converters](#case-converters) to use with the [Replacer Slots](#replacer-slots-or-ireplacerslotquestion) in the template files. Default is `(noCase)`.
9393
- `entry.folderPath` - Path to a folder of files or a single template file.
94-
- `stringReplacers` - An array of [Replacer Slots](#replacer-slots) used to replace content in the designated `entry.folderPath`.
94+
95+
- `stringReplacers` - An array of [Replacer Slots](#replacer-slots-or-ireplacerslotquestion) used to replace content in the designated `entry.folderPath`.
9596
- `dynamicReplacers` - (Optional) An array of IReplacer used to replace content in the designated `entry.folderPath`.
96-
- `output.path` - The desired output path for generated files. [Case Converters](#case-converters) and [Replacer Slots](#replacer-slots) can be used to make the path somewhat dynamic.
97+
- `output.path` - The desired output path for generated files. [Case Converters](#case-converters) and [Replacer Slots](#replacer-slots-or-ireplacerslotquestion) can be used to make the path somewhat dynamic.
9798
- `output.pathAndFileNameDefaultCase` - The [Case Converters](#case-converters) to use for the file path and file name(s).
9899
- `onComplete` - (Optional) Takes a callback function that is called once the file(s) have been outputted. A [IResults](#iresults) object will be passed to the callback.
99100

@@ -106,7 +107,7 @@ The `generateTemplateFiles` function takes an array of `IConfigItem` items.
106107
entry: {
107108
folderPath: './tools/templates/react/redux-store/',
108109
},
109-
stringReplacers: ['__store__', '__model__'],
110+
stringReplacers: ['__store__', { question: 'Insert model name', slot: '__model__' }],
110111
dynamicReplacers: [
111112
{slot:'__version__', slotValue: config.version},
112113
{slot:'__description__', slotValue: config.description}
@@ -123,13 +124,13 @@ The `generateTemplateFiles` function takes an array of `IConfigItem` items.
123124

124125
#### `IResults`
125126

126-
Below is an example of what you receive from the `onComplete` callback. It has the output path, list of files created and the [Replacer Slots](#replacer-slots) with the value entered.
127+
Below is an example of what you receive from the `onComplete` callback. It has the output path, list of files created and the [Replacer Slots](#replacer-slots-or-ireplacerslotquestion) with the value entered.
127128

128129
- `output.path` - The file(s) output path
129130
- `output.files` - List of files created
130-
- `stringReplacers` - List of [Replacer Slots](#replacer-slots); name and values entered during the setup process
131+
- `stringReplacers` - List of [Replacer Slots](#replacer-slots-or-ireplacerslotquestion); name and values entered during the setup process
131132

132-
###### Example data you would get from the onComplate callback
133+
###### Example data you would get from the onComplete callback
133134

134135
```javascript
135136
{
@@ -158,16 +159,31 @@ Below is an example of what you receive from the `onComplete` callback. It has t
158159
}
159160
```
160161

161-
### Replacer Slots
162+
### Replacer Slots or IReplacerSlotQuestion
163+
164+
[Replacer Slots](#replacer-slots-or-ireplacerslotquestion) are unique string value(s) to be replaced by the generator. An array of string values and/or `IReplacerSlotQuestion` objects can be used.
162165

163-
[Replacer Slots](#replacer-slots) are unique string value(s) to be replaced by the generator.
166+
```javascript
167+
stringReplacers: ['__store__', {question: 'Insert model name', slot: '__model__'}];
168+
```
164169

165-
For example you can use something like this in your template files and/or in the file path names.
170+
Replacer slot can be any string value you want to use. You can use something like this in your template files and/or in the file path names.
166171

167172
- `~replacerSlot~`
168173
- `{{something else}}`
169174
- `__AnythingYouWant__`
170175

176+
#### `IReplacerSlotQuestion`
177+
178+
Below is an example of a `IReplacerSlotQuestion`
179+
180+
```javascript
181+
{question: 'Insert model name', slot: '__model__'}
182+
```
183+
184+
- `question` - The question to ask the use what value should be used for the replacer `slot`
185+
- `slot` - The string value for the [Replacer Slots](#replacer-slots-or-ireplacerslotquestion)
186+
171187
### Case Converters
172188

173189
[Case Converters](#case-converters) transform the string value entered upon use of the generator.
@@ -192,7 +208,7 @@ Here is the string `Lives down BY the River` with each of the converters:
192208
(snakeCase) // lives_down_by_the_river
193209
(titleCase) // Lives Down By The River
194210

195-
One Rule: no spaces between the [Replacer Slots](#replacer-slots) and [Case Converters](#case-converters). If there is a space, [Case Converters](#case-converters) will not work.
211+
One Rule: no spaces between the [Replacer Slots](#replacer-slots-or-ireplacerslotquestion) and [Case Converters](#case-converters). If there is a space, [Case Converters](#case-converters) will not work.
196212

197213
- :white_check_mark: `__name__(camelCase)`
198214
- :warning: `__name__ (camelCase)`

examples/tools/generate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ generateTemplateFiles([
1515
entry: {
1616
folderPath: './tools/templates/angular/ngrx-store/',
1717
},
18-
stringReplacers: ['__name__', '__model__'],
18+
stringReplacers: ['__name__', { question: 'Insert model name', slot: '__model__' }],
1919
dynamicReplacers: [
2020
{slot:'__version__', slotValue: config.version},
2121
{slot:'__description__', slotValue: config.description}

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
"scripts": {
2222
"---------- HELPERS -----------------------------------------": "",
2323
"clean": "rimraf dist",
24-
"type-check": "tsc --noEmit",
25-
"type-check:watch": "npm run type-check -- --watch",
24+
"ts": "tsc --noEmit",
25+
"ts:watch": "npm run ts -- --watch",
2626
"build:types": "tsc --emitDeclarationOnly",
2727
"prettier": "npx prettier --write \"./**/*.{ts,tsx,js,jsx,json,md}\"",
2828
"---------- DEVELOPMENT -------------------------------------": "",
@@ -31,7 +31,7 @@
3131
"---------- PRODUCTION --------------------------------------": "",
3232
"docs": "typedoc --out ./docs ./src --mode file --tsconfig ./tsconfig.json",
3333
"postdocs": "prettier --write './docs/**/*'",
34-
"prebuild": "npm run type-check && npm run clean && npm run build:types",
34+
"prebuild": "npm run ts && npm run clean && npm run build:types",
3535
"build": "rollup -c",
3636
"---------- TESTING -----------------------------------------": "",
3737
"test": "npm run lint && npm run unit",
@@ -73,11 +73,11 @@
7373
"@types/replace-string": "3.0.0",
7474
"@types/through2": "2.0.34",
7575
"husky": "4.2.3",
76-
"jest": "25.1.0",
77-
"prettier": "2.0.1",
76+
"jest": "25.2.3",
77+
"prettier": "2.0.2",
7878
"pretty-quick": "2.0.1",
7979
"rimraf": "3.0.2",
80-
"rollup": "2.1.0",
80+
"rollup": "2.2.0",
8181
"rollup-plugin-babel": "4.4.0",
8282
"rollup-plugin-commonjs": "10.1.0",
8383
"rollup-plugin-node-resolve": "5.2.0",

src/GenerateTemplateFiles.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import IReplacer from './models/IReplacer';
1111
import IResults from './models/IResults';
1212
import IDefaultCaseConverter from './models/IDefaultCaseConverter';
1313
import CheckUtility from './utilities/CheckUtility';
14+
import IReplacerSlotQuestion from './models/IReplacerSlotQuestion';
1415

1516
export default class GenerateTemplateFiles {
1617
/**
@@ -82,11 +83,11 @@ export default class GenerateTemplateFiles {
8283
* New question asking what should text should be used to replace the template text.
8384
*/
8485
private async _getReplacerSlotValues(selectedConfigItem: IConfigItem): Promise<IReplacer[]> {
85-
const replacerQuestions: any[] = selectedConfigItem.stringReplacers.map((str: string) => {
86+
const replacerQuestions: any[] = selectedConfigItem.stringReplacers.map((item: string | IReplacerSlotQuestion) => {
8687
return {
8788
type: 'input',
88-
name: str,
89-
message: `Replace ${str} with:`,
89+
name: StringUtility.isString(item) ? item : item.slot,
90+
message: StringUtility.isString(item) ? `Replace ${item} with` : item.question,
9091
validate: (replacerSlotValue: string) => {
9192
const isValid: boolean = Boolean(replacerSlotValue.trim());
9293

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import StringUtilityDefault from './utilities/StringUtility';
44
import CaseConverterEnumDefault from './constants/CaseConverterEnum';
55
import IResultsDefault from './models/IResults';
66
import IReplacerDefault from './models/IReplacer';
7+
import IReplacerSlotQuestionDefault from './models/IReplacerSlotQuestion';
78

89
export {StringUtilityDefault as StringUtility};
910
export {CaseConverterEnumDefault as CaseConverterEnum};
@@ -12,6 +13,7 @@ export {CaseConverterEnumDefault as CaseConverterEnum};
1213
export type IResults = IResultsDefault;
1314
export type IConfigItem = IConfigItemDefault;
1415
export type IReplacer = IReplacerDefault;
16+
export type IReplacerSlotQuestion = IReplacerSlotQuestionDefault;
1517

1618
/**
1719
* Main method to create your template files. Accepts an array of `IConfigItem` items.

src/models/IConfigItem.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import CaseConverterEnum from '../constants/CaseConverterEnum';
22
import IResults from './IResults';
33
import IReplacer from './IReplacer';
4+
import IReplacerSlotQuestion from './IReplacerSlotQuestion';
45

56
/**
67
* ```
@@ -53,7 +54,7 @@ export default interface IConfigItem {
5354
* stringReplacers: ['__store__', '__model__'],
5455
* ```
5556
*/
56-
stringReplacers: string[];
57+
stringReplacers: (string | IReplacerSlotQuestion)[];
5758
/**
5859
* ```
5960
* dynamicReplacers: [{slot:'__description__', slotValue: config.description}],
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default interface IReplacerSlotQuestion {
2+
readonly question: string;
3+
readonly slot: string;
4+
}

src/utilities/StringUtility.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,22 @@ describe('StringUtility', () => {
101101
});
102102
});
103103
});
104+
105+
describe('isString', () => {
106+
it('should return true.', () => {
107+
const actualResult = StringUtility.isString('str');
108+
const expectedResult = true;
109+
110+
expect(actualResult).toBe(expectedResult);
111+
});
112+
113+
it('should return false.', () => {
114+
const falseItems = [undefined, null, true, false, 0, 1, {}, {prop: 'data'}];
115+
const expectedResult = false;
116+
117+
falseItems.forEach((value) => {
118+
expect(StringUtility.isString(value)).toEqual(expectedResult);
119+
});
120+
});
121+
});
104122
});

src/utilities/StringUtility.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,16 @@ export default class StringUtility {
152152

153153
return sentence.charAt(0).toUpperCase() + sentence.substr(1).toLowerCase();
154154
}
155+
156+
/**
157+
* Determines if the value is a string value
158+
*
159+
* ```
160+
* StringUtility.isString('livesDown_by-the.River');
161+
* // true
162+
* ```
163+
*/
164+
public static isString(value: unknown): value is string {
165+
return typeof value === 'string';
166+
}
155167
}

0 commit comments

Comments
 (0)