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

Commit b3f4ff9

Browse files
author
sw-yx
committed
use metadata for templates
1 parent 0b1a156 commit b3f4ff9

File tree

4 files changed

+35
-7
lines changed

4 files changed

+35
-7
lines changed

src/commands/functions/create.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,20 @@ class FunctionsCreateCommand extends Command {
1717
name: 'templatePath',
1818
message: 'pick a template',
1919
type: 'list',
20-
choices: templates.map(t => ({ name: path.basename(t, '.js') }))
20+
choices: templates.map(t => {
21+
return require(path.resolve(__dirname, '../../functions-templates/', t)).metadata
22+
// ({ name: path.basename(t, '.js') })
23+
})
2124
}
2225
])
23-
const template =
24-
'// scaffolded from Netlify Dev \n\n' +
25-
fs.readFileSync(path.resolve(__dirname, `../../functions-templates/${templatePath}.js`)).toString()
26+
27+
let template = fs
28+
.readFileSync(path.resolve(__dirname, `../../functions-templates/${templatePath}.js`))
29+
.toString()
30+
.split('// --- Netlify Template Below -- //')
31+
if (template.length !== 2) throw new Error('template ' + templatePath + ' badly formatted')
32+
template = '// scaffolded from `netlify functions:create` \n' + template[1]
33+
2634
this.log(`Creating function ${name}`)
2735

2836
const functionsDir = flags.functions || (config.build && config.build.functions)
@@ -95,7 +103,9 @@ async function getNameFromArgs(args) {
95103
name: 'name',
96104
message: 'name your function: ',
97105
type: 'input',
98-
validate: val => !!val && /^[a-z0-9]+$/i.test(val) // make sure it is not undefined and is alphanumeric
106+
validate: val => !!val && /^[\w\-.]+$/i.test(val)
107+
// make sure it is not undefined and is a valid filename.
108+
// this has some nuance i have ignored, eg crossenv and i18n concerns
99109
}
100110
])
101111
name = responses.name

src/functions-templates/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ place new templates here and our CLI will pick it up. currently only works for s
66

77
we dont colocate this inside `src/commands/functions` because oclif will think it's a new command.
88

9+
## providing metadata (and other functionality)
10+
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.
12+
913
## future dev thoughts
1014

1115
we will want a way to scale this to TS and Go as well.

src/functions-templates/hello-world.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
// --- Netlify Template Metadata -- //
2+
exports.metadata = {
3+
name: 'Basic Hello World function: shows async/await usage, and proper formatting with statusCode and body',
4+
value: 'hello-world',
5+
short: 'hello-world'
6+
}
7+
// --- Netlify Template Below -- //
18
async function hello() {
29
return Promise.resolve('Hello, World')
310
}

src/functions-templates/node-fetch.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1-
import fetch from 'node-fetch'
2-
export async function handler(event, context) {
1+
// --- Netlify Template Metadata -- //
2+
exports.metadata = {
3+
name: 'Fetch function: uses node-fetch to hit an external API without CORS issues',
4+
value: 'node-fetch',
5+
short: 'node-fetch'
6+
}
7+
// --- Netlify Template Below -- //
8+
const fetch = require('node-fetch')
9+
exports.handler = async function(event, context) {
310
try {
411
const response = await fetch('https://api.chucknorris.io/jokes/random')
512
if (!response.ok) {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy