Skip to content
This repository was archived by the owner on Sep 12, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"extends": "oclif"
"extends": ["oclif", "plugin:prettier/recommended"],
"rules": {
"prettier/prettier": "error"
}
}
5 changes: 5 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"semi": false,
"singleQuote": true,
"printWidth": 120
}
58 changes: 54 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@
"chai": "^4",
"eslint": "^5.5",
"eslint-config-oclif": "^3.1",
"eslint-config-prettier": "^4.1.0",
"eslint-plugin-prettier": "^3.0.1",
"globby": "^8",
"mocha": "^5",
"nyc": "^13"
"nyc": "^13",
"prettier": "^1.16.4"
},
"engines": {
"node": ">=8.0.0"
Expand Down Expand Up @@ -55,6 +58,8 @@
"posttest": "eslint .",
"prepack": "oclif-dev manifest && oclif-dev readme",
"test": "nyc mocha --forbid-only \"test/**/*.test.js\"",
"format": "npm run format:prettier -- --write",
"format:prettier": "prettier \"{{src,test}/**/,}*.js\"",
"version": "oclif-dev readme && git add README.md"
}
}
18 changes: 9 additions & 9 deletions src/commands/functions/build.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const fs = require('fs')
const {flags} = require('@oclif/command')
const { flags } = require('@oclif/command')
const Command = require('@netlify/cli-utils')
const {zipFunctions} = require('@netlify/zip-it-and-ship-it')
const { zipFunctions } = require('@netlify/zip-it-and-ship-it')

class FunctionsBuildCommand extends Command {
async run() {
const {flags, args} = this.parse(FunctionsBuildCommand)
const {config} = this.netlify
const { flags, args } = this.parse(FunctionsBuildCommand)
const { config } = this.netlify

const src = flags.src || config.build.functionsSource
const dst = flags.functions || config.build.functions
Expand All @@ -26,10 +26,10 @@ class FunctionsBuildCommand extends Command {
process.exit(1)
}

fs.mkdirSync(dst, {recursive: true})
fs.mkdirSync(dst, { recursive: true })

this.log('Building functions')
zipFunctions(src, dst, {skipGo: true})
zipFunctions(src, dst, { skipGo: true })
this.log('Functions buildt to ', dst)
}
}
Expand All @@ -40,12 +40,12 @@ FunctionsBuildCommand.description = `build functions locally
FunctionsBuildCommand.flags = {
functions: flags.string({
char: 'f',
description: 'Specify a functions folder to build to',
description: 'Specify a functions folder to build to'
}),
src: flags.string({
char: 's',
description: 'Specify the source folder for the functions',
}),
description: 'Specify the source folder for the functions'
})
}

module.exports = FunctionsBuildCommand
45 changes: 15 additions & 30 deletions src/commands/functions/create.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const fs = require('fs')
const path = require('path')
const {flags} = require('@oclif/command')
const { flags } = require('@oclif/command')
const Command = require('@netlify/cli-utils')

const template = `async function hello() {
Expand All @@ -19,76 +19,61 @@ exports.handler = async function(event, context) {

class FunctionsCreateCommand extends Command {
async run() {
const {flags, args} = this.parse(FunctionsCreateCommand)
const {name} = args
const {config} = this.netlify
const { flags, args } = this.parse(FunctionsCreateCommand)
const { name } = args
const { config } = this.netlify

this.log(`Creating function ${name}`)

const functionsDir =
flags.functions || (config.build && config.build.functions)
const functionsDir = flags.functions || (config.build && config.build.functions)
if (!functionsDir) {
this.log(
'No functions folder specified in netlify.toml or as an argument'
)
this.log('No functions folder specified in netlify.toml or as an argument')
process.exit(1)
}

if (!fs.existsSync(functionsDir)) {
fs.mkdir(functionsDir)
}

const functionPath = flags.dir ?
path.join(functionsDir, name, name + '.js') :
path.join(functionsDir, name + '.js')
const functionPath = flags.dir ? path.join(functionsDir, name, name + '.js') : path.join(functionsDir, name + '.js')
if (fs.existsSync(functionPath)) {
this.log(`Function ${functionPath} already exists`)
process.exit(1)
}

if (flags.dir) {
const fnFolder = path.join(functionsDir, name)
if (
fs.existsSync(fnFolder + '.js') &&
fs.lstatSync(fnFolder + '.js').isFile()
) {
this.log(
`A single file version of the function ${name} already exists at ${fnFolder}.js`
)
if (fs.existsSync(fnFolder + '.js') && fs.lstatSync(fnFolder + '.js').isFile()) {
this.log(`A single file version of the function ${name} already exists at ${fnFolder}.js`)
process.exit(1)
}

try {
fs.mkdirSync(fnFolder, {recursive: true})
fs.mkdirSync(fnFolder, { recursive: true })
} catch (e) {
// Ignore
}
} else if (fs.existsSync(functionPath.replace(/\.js/, ''))) {
this.log(
`A folder version of the function ${name} alreadt exists at ${functionPath.replace(
/\.js/,
''
)}`
)
this.log(`A folder version of the function ${name} alreadt exists at ${functionPath.replace(/\.js/, '')}`)
process.exit(1)
}

fs.writeFileSync(functionPath, template)
}
}

FunctionsCreateCommand.args = [{name: 'name'}]
FunctionsCreateCommand.args = [{ name: 'name' }]

FunctionsCreateCommand.description = `create a new function locally
`

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

FunctionsCreateCommand.flags = {
functions: flags.string({char: 'f', description: 'functions folder'}),
functions: flags.string({ char: 'f', description: 'functions folder' }),
dir: flags.boolean({
char: 'd',
description: 'create function as a directory',
}),
description: 'create function as a directory'
})
}
module.exports = FunctionsCreateCommand
9 changes: 5 additions & 4 deletions src/commands/functions/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const chalk = require('chalk')
const {Command} = require('@oclif/command')
const { Command } = require('@oclif/command')
const { execSync } = require('child_process')

function showHelp(command) {
execSync(`netlify ${command} --help`, {stdio: [0, 1, 2]})
execSync(`netlify ${command} --help`, { stdio: [0, 1, 2] })
}

function isEmptyCommand(flags, args) {
Expand All @@ -22,7 +23,7 @@ function hasArgs(args) {

class FunctionsCommand extends Command {
async run() {
const {flags, args} = this.parse(FunctionsCommand)
const { flags, args } = this.parse(FunctionsCommand)
// run help command if no args passed
if (isEmptyCommand(flags, args)) {
showHelp(this.id)
Expand All @@ -38,7 +39,7 @@ The ${name} command will help you manage the functions in this site
`
FunctionsCommand.examples = [
'netlify functions:create --name function-xyz --runtime nodejs',
'netlify functions:update --name function-abc --timeout 30s',
'netlify functions:update --name function-abc --timeout 30s'
]

// TODO make visible once implementation complete
Expand Down
2 changes: 1 addition & 1 deletion src/commands/functions/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Extra documentation goes here
`

FunctionsServeCommand.flags = {
name: flags.string({char: 'n', description: 'name to print'}),
name: flags.string({ char: 'n', description: 'name to print' })
}

// TODO make visible once implementation complete
Expand Down
2 changes: 1 addition & 1 deletion src/commands/functions/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Extra documentation goes here
`

FunctionsUpdateCommand.flags = {
name: flags.string({char: 'n', description: 'name to print'}),
name: flags.string({ char: 'n', description: 'name to print' })
}

// TODO make visible once implementation complete
Expand Down