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

Commit 8f6c6d4

Browse files
authored
Merge pull request #4 from netlify/functions-build-cmd
Add functions:build command
2 parents 111c83c + e5f8e82 commit 8f6c6d4

File tree

2 files changed

+72
-28
lines changed

2 files changed

+72
-28
lines changed

src/commands/functions/build.js

+38-6
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,51 @@
1-
const { Command, flags } = require('@oclif/command')
1+
const fs = require('fs')
2+
const {flags} = require('@oclif/command')
3+
const Command = require('@netlify/cli-utils')
4+
const {zipFunctions} = require('@netlify/zip-it-and-ship-it')
25

36
class FunctionsBuildCommand extends Command {
47
async run() {
5-
this.log(`build a function locally`)
8+
const {flags, args} = this.parse(FunctionsBuildCommand)
9+
const {config} = this.netlify
10+
11+
const src = flags.src || config.build.functionsSource
12+
const dst = flags.functions || config.build.functions
13+
14+
if (src === dst) {
15+
this.log("Source and destination for function build can't be the same")
16+
process.exit(1)
17+
}
18+
19+
if (!src) {
20+
this.log('You must specify a source folder')
21+
process.exit(1)
22+
}
23+
24+
if (!dst) {
25+
this.log('You must specify a functions folder')
26+
process.exit(1)
27+
}
28+
29+
fs.mkdirSync(dst, {recursive: true})
30+
31+
this.log('Building functions')
32+
zipFunctions(src, dst, {skipGo: true})
33+
this.log('Functions buildt to ', dst)
634
}
735
}
836

937
FunctionsBuildCommand.description = `build functions locally
1038
`
1139

1240
FunctionsBuildCommand.flags = {
13-
name: flags.string({char: 'n', description: 'name to print'}),
41+
functions: flags.string({
42+
char: 'f',
43+
description: 'Specify a functions folder to build to',
44+
}),
45+
src: flags.string({
46+
char: 's',
47+
description: 'Specify the source folder for the functions',
48+
}),
1449
}
1550

16-
// TODO make visible once implementation complete
17-
FunctionsBuildCommand.hidden = true
18-
1951
module.exports = FunctionsBuildCommand

src/commands/functions/create.js

+34-22
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,64 +19,76 @@ 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 = 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(/\.js/, ''))) {
58-
this.log(`A folder version of the function ${name} alreadt exists at ${functionPath.replace(/\.js/, '')}`)
59-
process.exit(1)
60-
}
66+
} 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+
)
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

6982
FunctionsCreateCommand.description = `create a new function locally
7083
`
7184

7285
FunctionsCreateCommand.examples = ['netlify functions:create hello-world']
7386

7487
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+
}),
7793
}
78-
79-
// TODO make visible once implementation complete
80-
FunctionsCreateCommand.hidden = true
81-
8294
module.exports = FunctionsCreateCommand

0 commit comments

Comments
 (0)