Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): supports calling without arguments for create #23

Merged
merged 2 commits into from
Apr 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
2 changes: 1 addition & 1 deletion packages/check-update/package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
"homepage": "https://github.com/vrn-deco/cli#readme",
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
"homepage": "https://github.com/vrn-deco/cli#readme",
Expand Down
2 changes: 1 addition & 1 deletion packages/command-boilerplate/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vrn-deco/cli-command-boilerplate",
"version": "1.2.1",
"version": "1.2.2",
"description": "vrn boilerplate command",
"author": "Cphayim <[email protected]>",
"homepage": "https://github.com/vrn-deco/cli#readme",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
25 changes: 22 additions & 3 deletions packages/command-boilerplate/src/create/create.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,18 @@ export class CreateAction extends Action<CreateArguments, CreateOptions> {
override async initialize(requiredBaseInfo = true): Promise<void> {
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)
Expand Down Expand Up @@ -99,6 +105,19 @@ export class CreateAction extends Action<CreateArguments, CreateOptions> {
}
}

async inquireFolderName(): Promise<string> {
// 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<ProjectBaseInfo> {
// non-interactive
if (this.options.yes) {
Expand Down
2 changes: 1 addition & 1 deletion packages/command-boilerplate/src/create/create.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default createCommand
createCommand
.name('create')
.description('create a new project with boilerplate service')
.arguments('<folder_name> [base_directory]')
.arguments('[folder_name] [base_directory]')
.addOption(
new Option('--mode <name>', 'mode of creation').choices([Mode.Package, Mode.Http, Mode.Git]).default(Mode.Package),
)
Expand Down
2 changes: 1 addition & 1 deletion packages/command-config/package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
"homepage": "https://github.com/vrn-deco/cli#readme",
Expand Down
2 changes: 1 addition & 1 deletion packages/command/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vrn-deco/cli-command",
"version": "1.2.1",
"version": "1.2.2",
"description": "base command",
"author": "Cphayim <[email protected]>",
"homepage": "https://github.com/vrn-deco/cli#readme",
Expand Down
2 changes: 1 addition & 1 deletion packages/config-helper/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vrn-deco/cli-config-helper",
"version": "1.2.1",
"version": "1.2.2",
"description": "config maintenance",
"author": "Cphayim <[email protected]>",
"homepage": "https://github.com/vrn-deco/cli#readme",
Expand Down
2 changes: 1 addition & 1 deletion packages/log/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vrn-deco/cli-log",
"version": "1.2.1",
"version": "1.2.2",
"description": "vrn logging tools",
"author": "Cphayim <[email protected]>",
"homepage": "https://github.com/vrn-deco/cli#readme",
Expand Down
2 changes: 1 addition & 1 deletion packages/npm-helper/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vrn-deco/cli-npm-helper",
"version": "1.2.1",
"version": "1.2.2",
"description": "vrn npm helper",
"author": "Cphayim <[email protected]>",
"homepage": "https://github.com/vrn-deco/cli#readme",
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vrn-deco/cli-shared",
"version": "1.2.1",
"version": "1.2.2",
"description": "vrn shared utils",
"author": "Cphayim <[email protected]>",
"homepage": "https://github.com/vrn-deco/cli#readme",
Expand Down