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,64 +19,76 @@ 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 = flags . functions || ( config . build && config . build . functions )
28
+ const functionsDir =
29
+ flags . functions || ( config . build && config . build . functions )
29
30
if ( ! functionsDir ) {
30
- this . log ( `No functions folder specified in netlify.toml or as an argument` )
31
+ this . log (
32
+ 'No functions folder specified in netlify.toml or as an argument'
33
+ )
31
34
process . exit ( 1 )
32
35
}
33
36
34
37
if ( ! fs . existsSync ( functionsDir ) ) {
35
38
fs . mkdir ( functionsDir )
36
39
}
37
40
38
- const functionPath = flags . dir ? path . join ( functionsDir , name , name + '.js' ) : path . join ( functionsDir , name + '.js' )
41
+ const functionPath = flags . dir ?
42
+ path . join ( functionsDir , name , name + '.js' ) :
43
+ path . join ( functionsDir , name + '.js' )
39
44
if ( fs . existsSync ( functionPath ) ) {
40
45
this . log ( `Function ${ functionPath } already exists` )
41
46
process . exit ( 1 )
42
47
}
43
48
44
49
if ( flags . dir ) {
45
50
const fnFolder = path . join ( functionsDir , name )
46
- if ( fs . existsSync ( fnFolder + '.js' ) && fs . lstatSync ( fnFolder + '.js' ) . isFile ( ) ) {
47
- this . log ( `A single file version of the function ${ name } already exists at ${ fnFolder } .js` )
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
58
process . exit ( 1 )
49
59
}
50
60
51
61
try {
52
- fs . mkdirSync ( fnFolder , { recursive : true } )
62
+ fs . mkdirSync ( fnFolder , { recursive : true } )
53
63
} catch ( e ) {
54
64
// Ignore
55
65
}
56
- } else {
57
- if ( fs . existsSync ( functionPath . replace ( / \. j s / , '' ) ) ) {
58
- this . log ( `A folder version of the function ${ name } alreadt exists at ${ functionPath . replace ( / \. j s / , '' ) } ` )
59
- process . exit ( 1 )
60
- }
66
+ } 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
+ )
73
+ process . exit ( 1 )
61
74
}
62
75
63
76
fs . writeFileSync ( functionPath , template )
64
77
}
65
78
}
66
79
67
- FunctionsCreateCommand . args = [ { name : 'name' } ]
80
+ FunctionsCreateCommand . args = [ { name : 'name' } ]
68
81
69
82
FunctionsCreateCommand . description = `create a new function locally
70
83
`
71
84
72
85
FunctionsCreateCommand . examples = [ 'netlify functions:create hello-world' ]
73
86
74
87
FunctionsCreateCommand . flags = {
75
- functions : flags . string ( { char : 'f' , description : 'functions folder' } ) ,
76
- dir : flags . boolean ( { char : 'd' , description : 'create function as a directory' } )
88
+ functions : flags . string ( { char : 'f' , description : 'functions folder' } ) ,
89
+ dir : flags . boolean ( {
90
+ char : 'd' ,
91
+ description : 'create function as a directory' ,
92
+ } ) ,
77
93
}
78
-
79
- // TODO make visible once implementation complete
80
- FunctionsCreateCommand . hidden = true
81
-
82
94
module . exports = FunctionsCreateCommand
0 commit comments