11const fs = require ( 'fs' )
22const path = require ( 'path' )
3- const { flags } = require ( '@oclif/command' )
3+ const { flags} = require ( '@oclif/command' )
44const Command = require ( '@netlify/cli-utils' )
55
66const template = `async function hello() {
@@ -19,64 +19,76 @@ exports.handler = async function(event, context) {
1919
2020class FunctionsCreateCommand extends Command {
2121 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
2525
2626 this . log ( `Creating function ${ name } ` )
2727
28- const functionsDir = flags . functions || ( config . build && config . build . functions )
28+ const functionsDir =
29+ flags . functions || ( config . build && config . build . functions )
2930 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+ )
3134 process . exit ( 1 )
3235 }
3336
3437 if ( ! fs . existsSync ( functionsDir ) ) {
3538 fs . mkdir ( functionsDir )
3639 }
3740
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' )
3944 if ( fs . existsSync ( functionPath ) ) {
4045 this . log ( `Function ${ functionPath } already exists` )
4146 process . exit ( 1 )
4247 }
4348
4449 if ( flags . dir ) {
4550 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+ )
4858 process . exit ( 1 )
4959 }
5060
5161 try {
52- fs . mkdirSync ( fnFolder , { recursive : true } )
62+ fs . mkdirSync ( fnFolder , { recursive : true } )
5363 } catch ( e ) {
5464 // Ignore
5565 }
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 )
6174 }
6275
6376 fs . writeFileSync ( functionPath , template )
6477 }
6578}
6679
67- FunctionsCreateCommand . args = [ { name : 'name' } ]
80+ FunctionsCreateCommand . args = [ { name : 'name' } ]
6881
6982FunctionsCreateCommand . description = `create a new function locally
7083`
7184
7285FunctionsCreateCommand . examples = [ 'netlify functions:create hello-world' ]
7386
7487FunctionsCreateCommand . 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+ } ) ,
7793}
78-
79- // TODO make visible once implementation complete
80- FunctionsCreateCommand . hidden = true
81-
8294module . exports = FunctionsCreateCommand
0 commit comments