From db4e8563ca652f5ebd3471c54885e62e4cfa9f92 Mon Sep 17 00:00:00 2001 From: sw-yx Date: Fri, 5 Jul 2019 13:13:38 -0400 Subject: [PATCH 1/5] implement basic netlify functions:list command --- src/commands/functions/list.js | 56 ++++++++++++++++++---------------- src/utils/get-functions.js | 33 ++++++++++++++++++++ src/utils/serve-functions.js | 29 ++---------------- 3 files changed, 66 insertions(+), 52 deletions(-) create mode 100644 src/utils/get-functions.js diff --git a/src/commands/functions/list.js b/src/commands/functions/list.js index 1f5c763..6d314d6 100644 --- a/src/commands/functions/list.js +++ b/src/commands/functions/list.js @@ -1,41 +1,45 @@ -const { Command, flags } = require("@oclif/command"); +const Command = require("@netlify/cli-utils"); +const { flags } = require("@oclif/command"); const AsciiTable = require("ascii-table"); - +const { getFunctions } = require("../../utils/get-functions"); class FunctionsListCommand extends Command { async run() { + let { flags } = this.parse(FunctionsListCommand); + const { api, site, config } = this.netlify; + const functionsDir = + flags.functions || + (config.dev && config.dev.functions) || + (config.build && config.build.functions); var table = new AsciiTable("Netlify Functions"); - table - .setHeading("Name", "Url", "Type", "id") - .addRow( - "function-abc", - "site.com/.netlify/function-abc", - "http GET", - "124123-ddhshs1212-1211" - ) - .addRow( - "send-email-function", - "site.com/.netlify/send-email-function", - "http POST", - "x3123-22345-1211" - ) - .addRow( - "lol-function-cool", - "site.com/.netlify/lol-function-cool", - "scheduled", - "weyhfd-hjjk-67533" + const functions = getFunctions(functionsDir); + + table.setHeading("Name", "Url", "moduleDir"); + + Object.entries(functions).forEach(([functionName, { moduleDir }]) => { + table.addRow( + functionName, + `/.netlify/functions/${functionName}`, + moduleDir ); - this.log(`netlify functions:list NOT IMPLEMENTED YET`); + }); + this.log(`netlify functions:list NOT NOT IMPLEMENTED YET`); this.log(table.toString()); } } -FunctionsListCommand.description = `list sites -... -Extra documentation goes here +FunctionsListCommand.description = `list functions that exist locally + +Helpful for making sure that you have formatted your functions correctly + +NOT the same as listing the functions that have been deployed. For that info you need to go to your Netlify deploy log. `; FunctionsListCommand.aliases = ["function:list"]; FunctionsListCommand.flags = { - name: flags.string({ char: "n", description: "name to print" }) + name: flags.string({ char: "n", description: "name to print" }), + functions: flags.string({ + char: "f", + description: "Specify a functions folder to serve" + }) }; // TODO make visible once implementation complete diff --git a/src/utils/get-functions.js b/src/utils/get-functions.js new file mode 100644 index 0000000..0c1ae7c --- /dev/null +++ b/src/utils/get-functions.js @@ -0,0 +1,33 @@ +const fs = require("fs"); +const path = require("path"); +const { findModuleDir, findHandler } = require("./finders"); + +module.exports = { + getFunctions(dir) { + const functions = {}; + if (fs.existsSync(dir)) { + fs.readdirSync(dir).forEach(file => { + if (dir === "node_modules") { + return; + } + const functionPath = path.resolve(path.join(dir, file)); + const handlerPath = findHandler(functionPath); + if (!handlerPath) { + return; + } + if (path.extname(functionPath) === ".js") { + functions[file.replace(/\.js$/, "")] = { + functionPath, + moduleDir: findModuleDir(functionPath) + }; + } else if (fs.lstatSync(functionPath).isDirectory()) { + functions[file] = { + functionPath: handlerPath, + moduleDir: findModuleDir(functionPath) + }; + } + }); + } + return functions; + } +}; diff --git a/src/utils/serve-functions.js b/src/utils/serve-functions.js index e321d10..c7b13e6 100644 --- a/src/utils/serve-functions.js +++ b/src/utils/serve-functions.js @@ -13,8 +13,7 @@ const { // NETLIFYDEVWARN, NETLIFYDEVERR } = require("netlify-cli-logo"); - -const { findModuleDir, findHandler } = require("./finders"); +const { getFunctions } = require("./get-functions"); const defaultPort = 34567; @@ -55,30 +54,7 @@ function buildClientContext(headers) { } function createHandler(dir) { - const functions = {}; - if (fs.existsSync(dir)) { - fs.readdirSync(dir).forEach(file => { - if (dir === "node_modules") { - return; - } - const functionPath = path.resolve(path.join(dir, file)); - const handlerPath = findHandler(functionPath); - if (!handlerPath) { - return; - } - if (path.extname(functionPath) === ".js") { - functions[file.replace(/\.js$/, "")] = { - functionPath, - moduleDir: findModuleDir(functionPath) - }; - } else if (fs.lstatSync(functionPath).isDirectory()) { - functions[file] = { - functionPath: handlerPath, - moduleDir: findModuleDir(functionPath) - }; - } - }); - } + const functions = getFunctions(dir); const clearCache = action => path => { console.log(`${NETLIFYDEVLOG} ${path} ${action}, reloading...`); // eslint-disable-line no-console @@ -144,6 +120,7 @@ function createHandler(dir) { let callbackWasCalled = false; const callback = createCallback(response); + // we already checked that it exports a function named handler above const promise = handler.handler( lambdaRequest, { clientContext: buildClientContext(request.headers) || {} }, From cc22042ef81af299288c4b1c15bc2db5a387c5a1 Mon Sep 17 00:00:00 2001 From: sw-yx Date: Sat, 6 Jul 2019 19:49:09 -0400 Subject: [PATCH 2/5] add check against deployment --- src/commands/functions/list.js | 54 ++++++++++++++++++++++++++++------ 1 file changed, 45 insertions(+), 9 deletions(-) diff --git a/src/commands/functions/list.js b/src/commands/functions/list.js index 6d314d6..0976432 100644 --- a/src/commands/functions/list.js +++ b/src/commands/functions/list.js @@ -1,3 +1,4 @@ +const chalk = require("chalk"); const Command = require("@netlify/cli-utils"); const { flags } = require("@oclif/command"); const AsciiTable = require("ascii-table"); @@ -6,24 +7,59 @@ class FunctionsListCommand extends Command { async run() { let { flags } = this.parse(FunctionsListCommand); const { api, site, config } = this.netlify; + + // get deployed site details + // copied from `netlify status` + const siteId = site.id; + if (!siteId) { + this.warn("Did you run `netlify link` yet?"); + this.error(`You don't appear to be in a folder that is linked to a site`); + } + let siteData; + try { + siteData = await api.getSite({ siteId }); + } catch (e) { + if (e.status === 401 /* unauthorized*/) { + this.warn( + `Log in with a different account or re-link to a site you have permission for` + ); + this.error( + `Not authorized to view the currently linked site (${siteId})` + ); + } + if (e.status === 404 /* missing */) { + this.error(`The site this folder is linked to can't be found`); + } + this.error(e); + } + const deploy = siteData.published_deploy || {}; + const deployed_functions = deploy.available_functions || []; + const functionsDir = flags.functions || (config.dev && config.dev.functions) || (config.build && config.build.functions); - var table = new AsciiTable("Netlify Functions"); + // var table = new AsciiTable("Netlify Functions"); const functions = getFunctions(functionsDir); - table.setHeading("Name", "Url", "moduleDir"); - + // table.setHeading("Name", "Url", "moduleDir", "deployed"); Object.entries(functions).forEach(([functionName, { moduleDir }]) => { - table.addRow( - functionName, - `/.netlify/functions/${functionName}`, - moduleDir + const isDeployed = deployed_functions + .map(({ n }) => n) + .includes(functionName); + + this.log(`function name: ${chalk.yellow(functionName)}`); + this.log( + ` url: ${chalk.yellow(`/.netlify/functions/${functionName}`)}` + ); + this.log(` moduleDir: ${chalk.yellow(moduleDir)}`); + this.log( + ` deployed: ${isDeployed ? chalk.green("yes") : chalk.yellow("no")}` ); + this.log("----------"); }); - this.log(`netlify functions:list NOT NOT IMPLEMENTED YET`); - this.log(table.toString()); + // this.log(`netlify functions:list NOT NOT IMPLEMENTED YET`); + // this.log(table.toString()); } } From 3323b3635def404662e66cafb525e2323fbe6a68 Mon Sep 17 00:00:00 2001 From: sw-yx Date: Sat, 6 Jul 2019 19:52:34 -0400 Subject: [PATCH 3/5] list --- src/commands/functions/list.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/commands/functions/list.js b/src/commands/functions/list.js index 0976432..11160b9 100644 --- a/src/commands/functions/list.js +++ b/src/commands/functions/list.js @@ -48,13 +48,17 @@ class FunctionsListCommand extends Command { .map(({ n }) => n) .includes(functionName); - this.log(`function name: ${chalk.yellow(functionName)}`); + this.log(`${chalk.yellow("function name")}: ${functionName}`); this.log( - ` url: ${chalk.yellow(`/.netlify/functions/${functionName}`)}` + ` ${chalk.yellow( + "url" + )}: ${`/.netlify/functions/${functionName}`}` ); - this.log(` moduleDir: ${chalk.yellow(moduleDir)}`); + this.log(` ${chalk.yellow("moduleDir")}: ${moduleDir}`); this.log( - ` deployed: ${isDeployed ? chalk.green("yes") : chalk.yellow("no")}` + ` ${chalk.yellow("deployed")}: ${ + isDeployed ? chalk.green("yes") : chalk.yellow("no") + }` ); this.log("----------"); }); From 6042b906e872390792d9aa9f4f65f866abed7c42 Mon Sep 17 00:00:00 2001 From: sw-yx Date: Sat, 6 Jul 2019 19:57:34 -0400 Subject: [PATCH 4/5] revert to use table format --- src/commands/functions/list.js | 37 +++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/src/commands/functions/list.js b/src/commands/functions/list.js index 11160b9..a30e9aa 100644 --- a/src/commands/functions/list.js +++ b/src/commands/functions/list.js @@ -39,31 +39,36 @@ class FunctionsListCommand extends Command { flags.functions || (config.dev && config.dev.functions) || (config.build && config.build.functions); - // var table = new AsciiTable("Netlify Functions"); + var table = new AsciiTable("Netlify Functions"); const functions = getFunctions(functionsDir); - // table.setHeading("Name", "Url", "moduleDir", "deployed"); + table.setHeading("Name", "Url", "moduleDir", "deployed"); Object.entries(functions).forEach(([functionName, { moduleDir }]) => { const isDeployed = deployed_functions .map(({ n }) => n) .includes(functionName); - this.log(`${chalk.yellow("function name")}: ${functionName}`); - this.log( - ` ${chalk.yellow( - "url" - )}: ${`/.netlify/functions/${functionName}`}` + // this.log(`${chalk.yellow("function name")}: ${functionName}`); + // this.log( + // ` ${chalk.yellow( + // "url" + // )}: ${`/.netlify/functions/${functionName}`}` + // ); + // this.log(` ${chalk.yellow("moduleDir")}: ${moduleDir}`); + // this.log( + // ` ${chalk.yellow("deployed")}: ${ + // isDeployed ? chalk.green("yes") : chalk.yellow("no") + // }` + // ); + // this.log("----------"); + table.addRow( + functionName, + `/.netlify/functions/${functionName}`, + moduleDir, + isDeployed ? "yes" : "no" ); - this.log(` ${chalk.yellow("moduleDir")}: ${moduleDir}`); - this.log( - ` ${chalk.yellow("deployed")}: ${ - isDeployed ? chalk.green("yes") : chalk.yellow("no") - }` - ); - this.log("----------"); }); - // this.log(`netlify functions:list NOT NOT IMPLEMENTED YET`); - // this.log(table.toString()); + this.log(table.toString()); } } From 80a24d1f8cd845bbaf260dc5ef8db13b922f5290 Mon Sep 17 00:00:00 2001 From: sw-yx Date: Sat, 6 Jul 2019 20:01:47 -0400 Subject: [PATCH 5/5] note and check functions folder source --- src/commands/functions/list.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/commands/functions/list.js b/src/commands/functions/list.js index a30e9aa..09050a1 100644 --- a/src/commands/functions/list.js +++ b/src/commands/functions/list.js @@ -39,7 +39,15 @@ class FunctionsListCommand extends Command { flags.functions || (config.dev && config.dev.functions) || (config.build && config.build.functions); - var table = new AsciiTable("Netlify Functions"); + if (typeof functionsDir === "undefined") { + this.error( + "functions directory is undefined, did you forget to set it in netlify.toml?" + ); + process.exit(1); + } + var table = new AsciiTable( + `Netlify Functions (based on local functions folder "${functionsDir}")` + ); const functions = getFunctions(functionsDir); table.setHeading("Name", "Url", "moduleDir", "deployed"); 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