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

Commit 807d989

Browse files
authored
Merge pull request #20 from netlify/fixFunctionsCreate
fix functions create
2 parents ba3ca11 + 9e679c0 commit 807d989

File tree

1 file changed

+18
-31
lines changed

1 file changed

+18
-31
lines changed

src/commands/functions/create.js

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const fs = require('fs')
22
const path = require('path')
3-
const {flags} = require('@oclif/command')
3+
const { flags } = require('@oclif/command')
44
const Command = require('@netlify/cli-utils')
55

66
const template = `async function hello() {
@@ -19,76 +19,63 @@ exports.handler = async function(event, context) {
1919

2020
class 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)) {
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`)
3938
}
4039

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')
4441
if (fs.existsSync(functionPath)) {
4542
this.log(`Function ${functionPath} already exists`)
4643
process.exit(1)
4744
}
4845

4946
if (flags.dir) {
5047
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`)
5850
process.exit(1)
5951
}
6052

6153
try {
62-
fs.mkdirSync(fnFolder, {recursive: true})
54+
fs.mkdirSync(fnFolder, { recursive: true })
6355
} catch (e) {
6456
// Ignore
6557
}
6658
} else if (fs.existsSync(functionPath.replace(/\.js/, ''))) {
67-
this.log(
68-
`A folder version of the function ${name} alreadt exists at ${functionPath.replace(
69-
/\.js/,
70-
''
71-
)}`
72-
)
59+
this.log(`A folder version of the function ${name} alreadt exists at ${functionPath.replace(/\.js/, '')}`)
7360
process.exit(1)
7461
}
7562

7663
fs.writeFileSync(functionPath, template)
7764
}
7865
}
7966

80-
FunctionsCreateCommand.args = [{name: 'name'}]
67+
FunctionsCreateCommand.args = [{ name: 'name' }]
8168

8269
FunctionsCreateCommand.description = `create a new function locally
8370
`
8471

8572
FunctionsCreateCommand.examples = ['netlify functions:create hello-world']
8673

8774
FunctionsCreateCommand.flags = {
88-
functions: flags.string({char: 'f', description: 'functions folder'}),
75+
functions: flags.string({ char: 'f', description: 'functions folder' }),
8976
dir: flags.boolean({
9077
char: 'd',
91-
description: 'create function as a directory',
92-
}),
78+
description: 'create function as a directory'
79+
})
9380
}
9481
module.exports = FunctionsCreateCommand

0 commit comments

Comments
 (0)