Skip to content
This repository was archived by the owner on Sep 12, 2019. It is now read-only.

Commit ecb0208

Browse files
authored
Merge pull request #17 from netlify/add-name-flag-to-functions-create
Add name flag to functions create
2 parents b650db7 + 4a7ed2a commit ecb0208

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

README.md

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,23 @@ netlify plugins:link .
2929

3030
Now you're both ready to start testing netlify dev and to contribute to the project.
3131

32-
## Functionality
32+
## Functionality Notes
3333

34-
- `netlify dev` now supports both `_redirects` and `netlify.toml` for redirects and has the same logic around loading order as our system (_redirects, toml in public folder, toml in base)
35-
- `netlify dev` can be configured for projects we don’t detect out of the box with a `[dev]` block in the toml file
34+
- `netlify dev` now supports both `_redirects` and `netlify.toml` for redirects and has the same logic around loading order as our system (\_redirects, toml in public folder, toml in base)
35+
- `netlify dev` can be configured for projects we don’t detect out of the box with a `[dev]` block in the toml file
36+
37+
## `netlify functions:create`
38+
39+
Create a new function from a given template.
40+
41+
Examples:
42+
43+
```
44+
netlify functions:create
45+
netlify functions:create hello-world
46+
netlify functions:create --name hello-world
47+
netlify functions:create hello-world --dir
48+
49+
```
50+
51+
You can just call `netlify functions:create` and the prompts will guide you all the way, however you can also supply a first argument to name the function. By default it creates a single file, however you can also use a `--dir` flag to create a function as a directory.

src/commands/functions/create.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ class FunctionsCreateCommand extends Command {
3131
console.error('an error occurred retrieving template code, please check ' + templatePath, err)
3232
process.exit(0)
3333
}
34-
const name = await getNameFromArgs(args, path.basename(templatePath, '.js'))
34+
35+
const name = await getNameFromArgs(args, flags, path.basename(templatePath, '.js'))
3536

3637
this.log(`Creating function ${name}`)
3738

@@ -93,9 +94,15 @@ FunctionsCreateCommand.args = [
9394

9495
FunctionsCreateCommand.description = `create a new function locally`
9596

96-
FunctionsCreateCommand.examples = ['netlify functions:create hello-world']
97+
FunctionsCreateCommand.examples = [
98+
'netlify functions:create',
99+
'netlify functions:create hello-world',
100+
'netlify functions:create --name hello-world',
101+
'netlify functions:create hello-world --dir'
102+
]
97103

98104
FunctionsCreateCommand.flags = {
105+
name: flags.string({ char: 'n', description: 'function name' }),
99106
functions: flags.string({ char: 'f', description: 'functions folder' }),
100107
dir: flags.boolean({
101108
char: 'd',
@@ -105,8 +112,14 @@ FunctionsCreateCommand.flags = {
105112
module.exports = FunctionsCreateCommand
106113

107114
// prompt for a name if name not supplied
108-
async function getNameFromArgs(args, defaultName) {
109-
let { name } = args
115+
async function getNameFromArgs(args, flags, defaultName) {
116+
if (flags.name && args.name) throw new Error('function name specified in both flag and arg format, pick one')
117+
let name
118+
if (flags.name && !args.name) name = flags.name
119+
// use flag if exists
120+
else if (!flags.name && args.name) name = args.name
121+
122+
// if neither are specified, prompt for it
110123
if (!name) {
111124
let responses = await inquirer.prompt([
112125
{

0 commit comments

Comments
 (0)