|
1 |
| -const { Command, flags } = require("@oclif/command"); |
| 1 | +const chalk = require("chalk"); |
| 2 | +const Command = require("@netlify/cli-utils"); |
| 3 | +const { flags } = require("@oclif/command"); |
2 | 4 | const AsciiTable = require("ascii-table");
|
3 |
| - |
| 5 | +const { getFunctions } = require("../../utils/get-functions"); |
4 | 6 | class FunctionsListCommand extends Command {
|
5 | 7 | async run() {
|
6 |
| - var table = new AsciiTable("Netlify Functions"); |
7 |
| - table |
8 |
| - .setHeading("Name", "Url", "Type", "id") |
9 |
| - .addRow( |
10 |
| - "function-abc", |
11 |
| - "site.com/.netlify/function-abc", |
12 |
| - "http GET", |
13 |
| - "124123-ddhshs1212-1211" |
14 |
| - ) |
15 |
| - .addRow( |
16 |
| - "send-email-function", |
17 |
| - "site.com/.netlify/send-email-function", |
18 |
| - "http POST", |
19 |
| - "x3123-22345-1211" |
20 |
| - ) |
21 |
| - .addRow( |
22 |
| - "lol-function-cool", |
23 |
| - "site.com/.netlify/lol-function-cool", |
24 |
| - "scheduled", |
25 |
| - "weyhfd-hjjk-67533" |
| 8 | + let { flags } = this.parse(FunctionsListCommand); |
| 9 | + const { api, site, config } = this.netlify; |
| 10 | + |
| 11 | + // get deployed site details |
| 12 | + // copied from `netlify status` |
| 13 | + const siteId = site.id; |
| 14 | + if (!siteId) { |
| 15 | + this.warn("Did you run `netlify link` yet?"); |
| 16 | + this.error(`You don't appear to be in a folder that is linked to a site`); |
| 17 | + } |
| 18 | + let siteData; |
| 19 | + try { |
| 20 | + siteData = await api.getSite({ siteId }); |
| 21 | + } catch (e) { |
| 22 | + if (e.status === 401 /* unauthorized*/) { |
| 23 | + this.warn( |
| 24 | + `Log in with a different account or re-link to a site you have permission for` |
| 25 | + ); |
| 26 | + this.error( |
| 27 | + `Not authorized to view the currently linked site (${siteId})` |
| 28 | + ); |
| 29 | + } |
| 30 | + if (e.status === 404 /* missing */) { |
| 31 | + this.error(`The site this folder is linked to can't be found`); |
| 32 | + } |
| 33 | + this.error(e); |
| 34 | + } |
| 35 | + const deploy = siteData.published_deploy || {}; |
| 36 | + const deployed_functions = deploy.available_functions || []; |
| 37 | + |
| 38 | + const functionsDir = |
| 39 | + flags.functions || |
| 40 | + (config.dev && config.dev.functions) || |
| 41 | + (config.build && config.build.functions); |
| 42 | + if (typeof functionsDir === "undefined") { |
| 43 | + this.error( |
| 44 | + "functions directory is undefined, did you forget to set it in netlify.toml?" |
| 45 | + ); |
| 46 | + process.exit(1); |
| 47 | + } |
| 48 | + var table = new AsciiTable( |
| 49 | + `Netlify Functions (based on local functions folder "${functionsDir}")` |
| 50 | + ); |
| 51 | + const functions = getFunctions(functionsDir); |
| 52 | + |
| 53 | + table.setHeading("Name", "Url", "moduleDir", "deployed"); |
| 54 | + Object.entries(functions).forEach(([functionName, { moduleDir }]) => { |
| 55 | + const isDeployed = deployed_functions |
| 56 | + .map(({ n }) => n) |
| 57 | + .includes(functionName); |
| 58 | + |
| 59 | + // this.log(`${chalk.yellow("function name")}: ${functionName}`); |
| 60 | + // this.log( |
| 61 | + // ` ${chalk.yellow( |
| 62 | + // "url" |
| 63 | + // )}: ${`/.netlify/functions/${functionName}`}` |
| 64 | + // ); |
| 65 | + // this.log(` ${chalk.yellow("moduleDir")}: ${moduleDir}`); |
| 66 | + // this.log( |
| 67 | + // ` ${chalk.yellow("deployed")}: ${ |
| 68 | + // isDeployed ? chalk.green("yes") : chalk.yellow("no") |
| 69 | + // }` |
| 70 | + // ); |
| 71 | + // this.log("----------"); |
| 72 | + table.addRow( |
| 73 | + functionName, |
| 74 | + `/.netlify/functions/${functionName}`, |
| 75 | + moduleDir, |
| 76 | + isDeployed ? "yes" : "no" |
26 | 77 | );
|
27 |
| - this.log(`netlify functions:list NOT IMPLEMENTED YET`); |
| 78 | + }); |
28 | 79 | this.log(table.toString());
|
29 | 80 | }
|
30 | 81 | }
|
31 | 82 |
|
32 |
| -FunctionsListCommand.description = `list sites |
33 |
| -... |
34 |
| -Extra documentation goes here |
| 83 | +FunctionsListCommand.description = `list functions that exist locally |
| 84 | +
|
| 85 | +Helpful for making sure that you have formatted your functions correctly |
| 86 | +
|
| 87 | +NOT the same as listing the functions that have been deployed. For that info you need to go to your Netlify deploy log. |
35 | 88 | `;
|
36 | 89 | FunctionsListCommand.aliases = ["function:list"];
|
37 | 90 | FunctionsListCommand.flags = {
|
38 |
| - name: flags.string({ char: "n", description: "name to print" }) |
| 91 | + name: flags.string({ char: "n", description: "name to print" }), |
| 92 | + functions: flags.string({ |
| 93 | + char: "f", |
| 94 | + description: "Specify a functions folder to serve" |
| 95 | + }) |
39 | 96 | };
|
40 | 97 |
|
41 | 98 | // TODO make visible once implementation complete
|
|
0 commit comments