From eed1779cc3a8b5b28cc7fc0c75b1edcb7ba894fd Mon Sep 17 00:00:00 2001 From: Cphayim Date: Sat, 23 Apr 2022 18:08:59 +0800 Subject: [PATCH 1/2] feat(cli): `create` command supports calling without arguments --- .../__tests__/create/create.action.spec.ts | 32 +++++++++++++++++++ .../src/create/create.action.ts | 25 +++++++++++++-- .../src/create/create.command.ts | 2 +- 3 files changed, 55 insertions(+), 4 deletions(-) diff --git a/packages/command-boilerplate/src/__tests__/create/create.action.spec.ts b/packages/command-boilerplate/src/__tests__/create/create.action.spec.ts index 74aa4bed..fb0ec83c 100644 --- a/packages/command-boilerplate/src/__tests__/create/create.action.spec.ts +++ b/packages/command-boilerplate/src/__tests__/create/create.action.spec.ts @@ -151,6 +151,22 @@ describe('@vrn-deco/cli-command-boilerplate -> create -> create.action.ts', () = expect(createAction.baseInfo).toEqual({ name: 'my-project', version: '1.0.0', author: 'Cphayim' }) }) + // v1.2.2 + // Calls without arguments do not support non-interactive + it('When the --yes options is passed and called without arguments, will throw a error', async () => { + expect.assertions(1) + try { + await runAction(CreateAction)( + undefined, + undefined, + { yes: true, name: 'my-project', version: '1.0.0', author: 'Cphayim' }, + new Command(), + ) + } catch (error) { + expect(error.message).toBe('missing arguments: folderName') + } + }) + // interactive it('When user has answered the projectName, version, and author, will get baseInfo', async () => { // call pathExistsSync twice @@ -164,6 +180,22 @@ describe('@vrn-deco/cli-command-boilerplate -> create -> create.action.ts', () = expect(createAction.baseInfo).toEqual({ name: 'my-project', version: '1.0.0', author: 'Cphayim' }) }) + // v1.2.2 + // Calling with no arguments will ask the user for the folder name + it('Calling with no arguments will ask the user for the folder name', async () => { + // call pathExistsSync twice + // the first time to check baseDirectory + // the second time to check targetDirectory + prompt.mockReturnValueOnce(Promise.resolve({ folderName: 'my-project' })) + pathExistsSyncSpy.mockImplementationOnce(() => false) // baseDirectory exist, not mkdir + pathExistsSyncSpy.mockImplementationOnce(() => false) // targetDirectory not exist, good! + prompt.mockReturnValueOnce(Promise.resolve({ name: 'my-project', version: '1.0.0', author: 'Cphayim' })) + const createAction = await runAction(CreateAction)(undefined, undefined, {}, new Command()) + expect(createAction.folderName).toBe('my-project') + expect(pathExistsSyncSpy).toBeCalledTimes(2) + expect(createAction.baseInfo).toEqual({ name: 'my-project', version: '1.0.0', author: 'Cphayim' }) + }) + // getBoilerplateChoiceName it('Can get correct boilerplate choice name', () => { // eslint-disable-next-line @typescript-eslint/no-explicit-any diff --git a/packages/command-boilerplate/src/create/create.action.ts b/packages/command-boilerplate/src/create/create.action.ts index b5c1c33c..13997f06 100644 --- a/packages/command-boilerplate/src/create/create.action.ts +++ b/packages/command-boilerplate/src/create/create.action.ts @@ -60,12 +60,18 @@ export class CreateAction extends Action { override async initialize(requiredBaseInfo = true): Promise { let [folderName, baseDirectory] = this.arguments - this.folderName = folderName + if (folderName) { + this.folderName = folderName + } else { + // - called via vrn create with no arguments + // - called via create-vrn package + this.folderName = await this.inquireFolderName() + } + this.baseDirectory = path.resolve(process.cwd(), baseDirectory ?? this.DEFAULT_BASE_DIRECTORY) - if (!isValidProjectName(this.folderName)) { + if (!isValidProjectName(this.folderName)) throw new Error(`the 'folderName' must conform to the npm package name specification: ${this.folderName}`) - } await this.verifyDirectory() this.targetDirectory = path.join(this.baseDirectory, this.folderName) @@ -99,6 +105,19 @@ export class CreateAction extends Action { } } + async inquireFolderName(): Promise { + // non-interactive should be passed via arguments + if (this.options.yes) throw new Error('missing arguments: folderName') + + const { folderName } = await prompt<{ folderName: string }>([ + { + name: 'folderName', + message: 'Folder name: ', + }, + ]) + return folderName + } + async inquireBaseInfo(): Promise { // non-interactive if (this.options.yes) { diff --git a/packages/command-boilerplate/src/create/create.command.ts b/packages/command-boilerplate/src/create/create.command.ts index c07a583d..0f399b0c 100644 --- a/packages/command-boilerplate/src/create/create.command.ts +++ b/packages/command-boilerplate/src/create/create.command.ts @@ -34,7 +34,7 @@ export default createCommand createCommand .name('create') .description('create a new project with boilerplate service') - .arguments(' [base_directory]') + .arguments('[folder_name] [base_directory]') .addOption( new Option('--mode ', 'mode of creation').choices([Mode.Package, Mode.Http, Mode.Git]).default(Mode.Package), ) From 9a3f909b16dd39c6f31cb871a0701e751d143eb8 Mon Sep 17 00:00:00 2001 From: Cphayim Date: Sat, 23 Apr 2022 21:09:01 +0800 Subject: [PATCH 2/2] chore(cli): release 1.2.2 --- CHANGELOG.md | 15 +++++++++++++++ packages/check-update/package.json | 2 +- packages/cli/package.json | 2 +- packages/command-boilerplate/package.json | 2 +- packages/command-config/package.json | 2 +- packages/command/package.json | 2 +- packages/config-helper/package.json | 2 +- packages/log/package.json | 2 +- packages/npm-helper/package.json | 2 +- packages/shared/package.json | 2 +- 10 files changed, 24 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 56d04e5d..cd22cafa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,20 @@ # @vrn-deco/cli +## 1.2.2 + +### Refactor: + +- Supports calling without arguments for `create` + +```sh +vrn create app + +# Will ask for `folderName` +vrn create +``` + +Note that if you call with no arguments and pass the --yes option, you will get an error + ## 1.2.1 ### Refactor: diff --git a/packages/check-update/package.json b/packages/check-update/package.json index afd76d42..d0d0837b 100644 --- a/packages/check-update/package.json +++ b/packages/check-update/package.json @@ -1,6 +1,6 @@ { "name": "@vrn-deco/cli-check-update", - "version": "1.2.1", + "version": "1.2.2", "description": "@vrn-deco/cli-check-update", "author": "Cphayim ", "homepage": "https://github.com/vrn-deco/cli#readme", diff --git a/packages/cli/package.json b/packages/cli/package.json index 5c415b10..61b0406b 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -1,6 +1,6 @@ { "name": "@vrn-deco/cli", - "version": "1.2.1", + "version": "1.2.2", "description": "⚙️ Project scaffolding with command line tools. 🛠", "author": "Cphayim ", "homepage": "https://github.com/vrn-deco/cli#readme", diff --git a/packages/command-boilerplate/package.json b/packages/command-boilerplate/package.json index 120127a2..1b45ea86 100644 --- a/packages/command-boilerplate/package.json +++ b/packages/command-boilerplate/package.json @@ -1,6 +1,6 @@ { "name": "@vrn-deco/cli-command-boilerplate", - "version": "1.2.1", + "version": "1.2.2", "description": "vrn boilerplate command", "author": "Cphayim ", "homepage": "https://github.com/vrn-deco/cli#readme", diff --git a/packages/command-config/package.json b/packages/command-config/package.json index 6249b8ea..32e15445 100644 --- a/packages/command-config/package.json +++ b/packages/command-config/package.json @@ -1,6 +1,6 @@ { "name": "@vrn-deco/cli-command-config", - "version": "1.2.1", + "version": "1.2.2", "description": "vrn create project boilerplate command", "author": "Cphayim ", "homepage": "https://github.com/vrn-deco/cli#readme", diff --git a/packages/command/package.json b/packages/command/package.json index bdbfd641..ca238b86 100644 --- a/packages/command/package.json +++ b/packages/command/package.json @@ -1,6 +1,6 @@ { "name": "@vrn-deco/cli-command", - "version": "1.2.1", + "version": "1.2.2", "description": "base command", "author": "Cphayim ", "homepage": "https://github.com/vrn-deco/cli#readme", diff --git a/packages/config-helper/package.json b/packages/config-helper/package.json index 29473ab2..19b14fdc 100644 --- a/packages/config-helper/package.json +++ b/packages/config-helper/package.json @@ -1,6 +1,6 @@ { "name": "@vrn-deco/cli-config-helper", - "version": "1.2.1", + "version": "1.2.2", "description": "config maintenance", "author": "Cphayim ", "homepage": "https://github.com/vrn-deco/cli#readme", diff --git a/packages/log/package.json b/packages/log/package.json index 0b76894d..bfc16cbd 100644 --- a/packages/log/package.json +++ b/packages/log/package.json @@ -1,6 +1,6 @@ { "name": "@vrn-deco/cli-log", - "version": "1.2.1", + "version": "1.2.2", "description": "vrn logging tools", "author": "Cphayim ", "homepage": "https://github.com/vrn-deco/cli#readme", diff --git a/packages/npm-helper/package.json b/packages/npm-helper/package.json index 966c24a3..15708c14 100644 --- a/packages/npm-helper/package.json +++ b/packages/npm-helper/package.json @@ -1,6 +1,6 @@ { "name": "@vrn-deco/cli-npm-helper", - "version": "1.2.1", + "version": "1.2.2", "description": "vrn npm helper", "author": "Cphayim ", "homepage": "https://github.com/vrn-deco/cli#readme", diff --git a/packages/shared/package.json b/packages/shared/package.json index 9cc07960..6585cdce 100644 --- a/packages/shared/package.json +++ b/packages/shared/package.json @@ -1,6 +1,6 @@ { "name": "@vrn-deco/cli-shared", - "version": "1.2.1", + "version": "1.2.2", "description": "vrn shared utils", "author": "Cphayim ", "homepage": "https://github.com/vrn-deco/cli#readme",