1
1
const fs = require ( 'fs' )
2
2
const path = require ( 'path' )
3
- const { flags} = require ( '@oclif/command' )
3
+ const { flags } = require ( '@oclif/command' )
4
4
const Command = require ( '@netlify/cli-utils' )
5
5
6
6
const template = `async function hello() {
@@ -19,76 +19,63 @@ exports.handler = async function(event, context) {
19
19
20
20
class FunctionsCreateCommand extends Command {
21
21
async run ( ) {
22
- const { flags, args} = this . parse ( FunctionsCreateCommand )
23
- const { name} = args
24
- const { config} = this . netlify
22
+ const { flags, args } = this . parse ( FunctionsCreateCommand )
23
+ const { name } = args
24
+ const { config } = this . netlify
25
25
26
26
this . log ( `Creating function ${ name } ` )
27
27
28
- const functionsDir =
29
- flags . functions || ( config . build && config . build . functions )
28
+ const functionsDir = flags . functions || ( config . build && config . build . functions )
30
29
if ( ! functionsDir ) {
31
- this . log (
32
- 'No functions folder specified in netlify.toml or as an argument'
33
- )
30
+ this . log ( 'No functions folder specified in netlify.toml or as an argument' )
34
31
process . exit ( 1 )
35
32
}
36
33
37
34
if ( ! fs . existsSync ( functionsDir ) ) {
38
- fs . mkdir ( functionsDir )
35
+ console . log ( `functions folder ${ functionsDir } specified in netlify.toml but folder not found, creating it...` )
36
+ fs . mkdirSync ( functionsDir )
37
+ console . log ( `functions folder ${ functionsDir } created` )
39
38
}
40
39
41
- const functionPath = flags . dir ?
42
- path . join ( functionsDir , name , name + '.js' ) :
43
- path . join ( functionsDir , name + '.js' )
40
+ const functionPath = flags . dir ? path . join ( functionsDir , name , name + '.js' ) : path . join ( functionsDir , name + '.js' )
44
41
if ( fs . existsSync ( functionPath ) ) {
45
42
this . log ( `Function ${ functionPath } already exists` )
46
43
process . exit ( 1 )
47
44
}
48
45
49
46
if ( flags . dir ) {
50
47
const fnFolder = path . join ( functionsDir , name )
51
- if (
52
- fs . existsSync ( fnFolder + '.js' ) &&
53
- fs . lstatSync ( fnFolder + '.js' ) . isFile ( )
54
- ) {
55
- this . log (
56
- `A single file version of the function ${ name } already exists at ${ fnFolder } .js`
57
- )
48
+ if ( fs . existsSync ( fnFolder + '.js' ) && fs . lstatSync ( fnFolder + '.js' ) . isFile ( ) ) {
49
+ this . log ( `A single file version of the function ${ name } already exists at ${ fnFolder } .js` )
58
50
process . exit ( 1 )
59
51
}
60
52
61
53
try {
62
- fs . mkdirSync ( fnFolder , { recursive : true } )
54
+ fs . mkdirSync ( fnFolder , { recursive : true } )
63
55
} catch ( e ) {
64
56
// Ignore
65
57
}
66
58
} else if ( fs . existsSync ( functionPath . replace ( / \. j s / , '' ) ) ) {
67
- this . log (
68
- `A folder version of the function ${ name } alreadt exists at ${ functionPath . replace (
69
- / \. j s / ,
70
- ''
71
- ) } `
72
- )
59
+ this . log ( `A folder version of the function ${ name } alreadt exists at ${ functionPath . replace ( / \. j s / , '' ) } ` )
73
60
process . exit ( 1 )
74
61
}
75
62
76
63
fs . writeFileSync ( functionPath , template )
77
64
}
78
65
}
79
66
80
- FunctionsCreateCommand . args = [ { name : 'name' } ]
67
+ FunctionsCreateCommand . args = [ { name : 'name' } ]
81
68
82
69
FunctionsCreateCommand . description = `create a new function locally
83
70
`
84
71
85
72
FunctionsCreateCommand . examples = [ 'netlify functions:create hello-world' ]
86
73
87
74
FunctionsCreateCommand . flags = {
88
- functions : flags . string ( { char : 'f' , description : 'functions folder' } ) ,
75
+ functions : flags . string ( { char : 'f' , description : 'functions folder' } ) ,
89
76
dir : flags . boolean ( {
90
77
char : 'd' ,
91
- description : 'create function as a directory' ,
92
- } ) ,
78
+ description : 'create function as a directory'
79
+ } )
93
80
}
94
81
module . exports = FunctionsCreateCommand
0 commit comments