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

Commit 657e442

Browse files
authored
Merge pull request #24 from netlify/netlify-functions/addOnComplete
add onComplete lifecycle to netlify functions template
2 parents 1a9660d + 25741c2 commit 657e442

File tree

4 files changed

+17
-5
lines changed

4 files changed

+17
-5
lines changed

src/commands/functions/create.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,29 @@ const { flags } = require('@oclif/command')
44
const Command = require('@netlify/cli-utils')
55
const inquirer = require('inquirer')
66

7+
const templatesDir = path.resolve(__dirname, '../../functions-templates')
78
class FunctionsCreateCommand extends Command {
89
async run() {
910
const { flags, args } = this.parse(FunctionsCreateCommand)
1011
const name = await getNameFromArgs(args)
1112
const { config } = this.netlify
1213
const templates = fs
13-
.readdirSync(path.resolve(__dirname, '../../functions-templates'))
14+
.readdirSync(templatesDir)
1415
.filter(x => path.extname(x) === '.js') // only js templates for now
1516
const { templatePath } = await inquirer.prompt([
1617
{
1718
name: 'templatePath',
1819
message: 'pick a template',
1920
type: 'list',
2021
choices: templates.map(t => {
21-
return require(path.resolve(__dirname, '../../functions-templates/', t)).metadata
22+
return require(path.join(templatesDir, t)).metadata
2223
// ({ name: path.basename(t, '.js') })
2324
})
2425
}
2526
])
2627

2728
let template = fs
28-
.readFileSync(path.resolve(__dirname, `../../functions-templates/${templatePath}.js`))
29+
.readFileSync(path.join(templatesDir, `${templatePath}.js`))
2930
.toString()
3031
.split('// --- Netlify Template Below -- //')
3132
if (template.length !== 2) throw new Error('template ' + templatePath + ' badly formatted')
@@ -69,6 +70,9 @@ class FunctionsCreateCommand extends Command {
6970
}
7071

7172
fs.writeFileSync(functionPath, template)
73+
74+
const onComplete = require(path.join(templatesDir, templatePath)).onComplete
75+
if (onComplete) onComplete() // do whatever the template wants to do after it is scaffolded
7276
}
7377
}
7478

@@ -94,7 +98,6 @@ FunctionsCreateCommand.flags = {
9498
module.exports = FunctionsCreateCommand
9599

96100
// prompt for a name if name not supplied
97-
// we tried using required:true in oclif args (see below) but the error msg was very ugly
98101
async function getNameFromArgs(args) {
99102
let { name } = args
100103
if (!name) {

src/functions-templates/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ we dont colocate this inside `src/commands/functions` because oclif will think i
88

99
## providing metadata (and other functionality)
1010

11-
we split the file based on the `// --- Netlify Template Below -- //` string. everything below it is cloned as the template. everything above it can be required and run as a module for configuring the template. for now we simply export a `metadata` object that fits [`inquirer's choices spec`](https://www.npmjs.com/package/inquirer#question). in future we can think about other options we may want to offer.
11+
we split the file based on the `// --- Netlify Template Below -- //` string. everything below it is cloned as the template. everything above it can be required and run as a module for configuring the template. for now we simply export a `metadata` object that fits [`inquirer's choices spec`](https://www.npmjs.com/package/inquirer#question).
12+
13+
once the templating is done we can also call an `onComplete` hook to print a reminder or execute other logic - see `node-fetch.js` for an example.
14+
15+
in future we can think about other options we may want to offer.
1216

1317
## future dev thoughts
1418

src/functions-templates/hello-world.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ exports.metadata = {
44
value: 'hello-world',
55
short: 'hello-world'
66
}
7+
// exports.onComplete = () => {} // optional
78
// --- Netlify Template Below -- //
89
async function hello() {
910
return Promise.resolve('Hello, World')

src/functions-templates/node-fetch.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ exports.metadata = {
44
value: 'node-fetch',
55
short: 'node-fetch'
66
}
7+
exports.onComplete = () => {
8+
console.log(`node-fetch function created from template!`)
9+
console.log('REMINDER: make sure to install `node-fetch` if you dont have it.')
10+
}
711
// --- Netlify Template Below -- //
812
const fetch = require('node-fetch')
913
exports.handler = async function(event, context) {

0 commit comments

Comments
 (0)