Skip to content

Commit 6fba9b9

Browse files
committed
fix: rename to modifyName
BREAKING CHANGE: handleName => modifyName
1 parent 996861e commit 6fba9b9

3 files changed

Lines changed: 48 additions & 34 deletions

File tree

README.md

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@
3737
- [`space`](#space)
3838
- [`uuid`](#uuid)
3939
- [Config](#config)
40-
- [`extra`](#extra)
41-
- [`caveat`](#caveat)
42-
- [AfterHookOptions](#afterhookoptions)
43-
- [`after`](#after)
44-
- [`handleName`](#handlename)
40+
- [templateRoot (required)](#templateroot-required)
41+
- [promptForTemplate (default: `false`)](#promptfortemplate-default-false)
42+
- [extra (default: `undefined`)](#extra-default-undefined)
43+
- [modifyName (default: `undefined`)](#modifyname-default-undefined)
44+
- [after (default: `undefined`)](#after-default-undefined)
45+
- [caveat (default: `undefined`)](#caveat-default-undefined)
46+
- [AfterHookOptions](#afterhookoptions)
4547
- [Contribution](#contribution)
4648
- [Contributors ✨](#contributors-)
4749

@@ -151,14 +153,14 @@ Generates unique UUID string.
151153

152154
## Config
153155

154-
You can find the app config in `src/cli.ts` (or `src/cli.js` if you chose `default` template).
156+
You can find the app config in `src/cli.js` (or `src/cli.ts` if you chose `typescript` template).
155157

156158
```ts
157159
import { resolve } from 'path';
158160
import { create } from 'create-create-app';
159161

160162
create('create-greet', {
161-
templateRoot: resolve(__dirname, '../templates'),
163+
templateRoot: resolve(__dirname, '..', 'templates'),
162164
extra: {
163165
language: {
164166
type: 'input',
@@ -167,23 +169,47 @@ create('create-greet', {
167169
prompt: 'if-no-arg',
168170
},
169171
},
172+
modifyName: (name) => `package-prefix-${name}`,
170173
after: ({ installNpmPackage }) => installNpmPackage('chalk'),
171-
caveat: `Your app has been created successfuly!`,
172-
handleName: (name) => `package-prefix-${name}`,
174+
caveat: `Your app has been created successfully!`,
173175
});
174176
```
175177

178+
### templateRoot (required)
179+
176180
`templateRoot` set to `path.resolve(__dirname, '../templates')`. You can change it to whereever you want.
177181

178-
### `extra`
182+
### promptForTemplate (default: `false`)
183+
184+
Ask users to choose a template to be used for initialization only if `promptForTemplate` is set `true` AND there's multiple templates found in `templates/`.
185+
186+
With `promptForTemplate` set `false`, users still can specify template via command-line flag `--template`:
187+
188+
```
189+
create-something <name> --template <template>
190+
```
191+
192+
### extra (default: `undefined`)
179193

180194
`object | undefined`
181195

182196
Extra options passed to the app. These options will be accessible as a cli option, interactive question, and template string. In this case, `--language` cli option and `{{language}}` template string will be available.
183197

184198
You can find all possible options in [yargs-interactive documentation](https://github.com/nanovazquez/yargs-interactive#options).
185199

186-
### `caveat`
200+
### modifyName (default: `undefined`)
201+
202+
`(name: string) => string | Promise<string>`
203+
204+
Modify `name` property.
205+
206+
### after (default: `undefined`)
207+
208+
`(options: AfterHookOptions) => void`
209+
210+
After hook script that runs after the initialization.
211+
212+
### caveat (default: `undefined`)
187213

188214
`string | ((options: AfterHookOptions) => string | void) | undefined`
189215

@@ -211,7 +237,7 @@ create('create-greet', {
211237
});
212238
```
213239

214-
#### AfterHookOptions
240+
### AfterHookOptions
215241

216242
```typescript
217243
{
@@ -234,18 +260,6 @@ create('create-greet', {
234260
}
235261
```
236262

237-
### `after`
238-
239-
`(options: AfterHookOptions) => void`
240-
241-
After hook script that runs after the initialization.
242-
243-
### `handleName`
244-
245-
`(name: string) => string | Promise<string>`
246-
247-
Modify `name` property.
248-
249263
## Contribution
250264

251265
PRs are always welcome!

src/cli.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ ${chalk.yellow(
4444

4545
create('create-create-app', {
4646
templateRoot,
47-
alwaysAskForTemplate: true,
47+
promptForTemplate: true,
48+
modifyName: (name) => (name.startsWith('create-') ? name : `create-${name}`),
4849
caveat,
49-
handleName: (name) => (name.startsWith('create-') ? name : `create-${name}`),
5050
});

src/index.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ export interface Option {
3333

3434
export interface Options {
3535
templateRoot: string;
36-
alwaysAskForTemplate?: boolean;
37-
handleName?: (name: string) => string | Promise<string>;
36+
promptForTemplate?: boolean;
37+
modifyName?: (name: string) => string | Promise<string>;
3838
extra?: Option;
3939
caveat?:
4040
| string
@@ -144,13 +144,13 @@ function isOccupied(dirname: string) {
144144

145145
async function getYargsOptions(
146146
templateRoot: string,
147-
alwaysAskForTemplate: boolean,
147+
promptForTemplate: boolean,
148148
extraOptions: Option = {},
149149
) {
150150
const gitUser = await getGitUser();
151151
const availableTemplates = getAvailableTemplates(templateRoot);
152152
const isMultipleTemplates = availableTemplates.length > 1;
153-
const askForTemplate = isMultipleTemplates && alwaysAskForTemplate;
153+
const askForTemplate = isMultipleTemplates && promptForTemplate;
154154
const yargOption: Option = {
155155
interactive: { default: true },
156156
description: {
@@ -203,19 +203,19 @@ export async function create(appName: string, options: Options) {
203203
const useCurrentDir = firstArg === '.';
204204
const name: string = useCurrentDir
205205
? path.basename(process.cwd())
206-
: options.handleName
207-
? await Promise.resolve(options.handleName(firstArg))
206+
: options.modifyName
207+
? await Promise.resolve(options.modifyName(firstArg))
208208
: firstArg;
209209
const packageDir = useCurrentDir ? process.cwd() : path.resolve(name);
210-
const { templateRoot, alwaysAskForTemplate = false } = options;
210+
const { templateRoot, promptForTemplate = false } = options;
211211

212212
if (isOccupied(packageDir)) {
213213
error(`${packageDir} is not empty directory.`);
214214
}
215215

216216
const yargsOption = await getYargsOptions(
217217
templateRoot,
218-
alwaysAskForTemplate,
218+
promptForTemplate,
219219
options.extra,
220220
);
221221
const args = await yargsInteractive()

0 commit comments

Comments
 (0)