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,76 +19,61 @@ 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 =
29- flags . functions || ( config . build && config . build . functions )
28+ const functionsDir = flags . functions || ( config . build && config . build . functions )
3029 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' )
3431 process . exit ( 1 )
3532 }
3633
3734 if ( ! fs . existsSync ( functionsDir ) ) {
3835 fs . mkdir ( functionsDir )
3936 }
4037
41- const functionPath = flags . dir ?
42- path . join ( functionsDir , name , name + '.js' ) :
43- path . join ( functionsDir , name + '.js' )
38+ const functionPath = flags . dir ? path . join ( functionsDir , name , name + '.js' ) : path . join ( functionsDir , name + '.js' )
4439 if ( fs . existsSync ( functionPath ) ) {
4540 this . log ( `Function ${ functionPath } already exists` )
4641 process . exit ( 1 )
4742 }
4843
4944 if ( flags . dir ) {
5045 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- )
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` )
5848 process . exit ( 1 )
5949 }
6050
6151 try {
62- fs . mkdirSync ( fnFolder , { recursive : true } )
52+ fs . mkdirSync ( fnFolder , { recursive : true } )
6353 } catch ( e ) {
6454 // Ignore
6555 }
6656 } 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- )
57+ this . log ( `A folder version of the function ${ name } alreadt exists at ${ functionPath . replace ( / \. j s / , '' ) } ` )
7358 process . exit ( 1 )
7459 }
7560
7661 fs . writeFileSync ( functionPath , template )
7762 }
7863}
7964
80- FunctionsCreateCommand . args = [ { name : 'name' } ]
65+ FunctionsCreateCommand . args = [ { name : 'name' } ]
8166
8267FunctionsCreateCommand . description = `create a new function locally
8368`
8469
8570FunctionsCreateCommand . examples = [ 'netlify functions:create hello-world' ]
8671
8772FunctionsCreateCommand . flags = {
88- functions : flags . string ( { char : 'f' , description : 'functions folder' } ) ,
73+ functions : flags . string ( { char : 'f' , description : 'functions folder' } ) ,
8974 dir : flags . boolean ( {
9075 char : 'd' ,
91- description : 'create function as a directory' ,
92- } ) ,
76+ description : 'create function as a directory'
77+ } )
9378}
9479module . exports = FunctionsCreateCommand
0 commit comments