UNPKG

2.65 kBJavaScriptView Raw
1#!/usr/bin/env node
2"use strict";
3// The MIT License (MIT)
4//
5// Copyright (c) 2022 Firebase
6//
7// Permission is hereby granted, free of charge, to any person obtaining a copy
8// of this software and associated documentation files (the "Software"), to deal
9// in the Software without restriction, including without limitation the rights
10// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11// copies of the Software, and to permit persons to whom the Software is
12// furnished to do so, subject to the following conditions:
13//
14// The above copyright notice and this permission notice shall be included in all
15// copies or substantial portions of the Software.
16//
17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23// SOFTWARE.
24Object.defineProperty(exports, "__esModule", { value: true });
25const express = require("express");
26const loader_1 = require("../runtime/loader");
27const manifest_1 = require("../runtime/manifest");
28function printUsageAndExit() {
29 console.error(`
30Usage: firebase-functions [functionsDir]
31
32Arguments:
33 - functionsDir: Directory containing source code for Firebase Functions.
34`);
35 process.exit(1);
36}
37let functionsDir = ".";
38const args = process.argv.slice(2);
39if (args.length > 1) {
40 if (args[0] === "-h" || args[0] === "--help") {
41 printUsageAndExit();
42 }
43 functionsDir = args[0];
44}
45let server = undefined;
46const app = express();
47function handleQuitquitquit(req, res) {
48 res.send("ok");
49 server.close();
50}
51app.get("/__/quitquitquit", handleQuitquitquit);
52app.post("/__/quitquitquit", handleQuitquitquit);
53if (process.env.FUNCTIONS_CONTROL_API === "true") {
54 app.get("/__/functions.yaml", async (req, res) => {
55 try {
56 const stack = await (0, loader_1.loadStack)(functionsDir);
57 res.setHeader("content-type", "text/yaml");
58 res.send(JSON.stringify((0, manifest_1.stackToWire)(stack)));
59 }
60 catch (e) {
61 console.error(e);
62 res.status(400).send(`Failed to generate manifest from function source: ${e}`);
63 }
64 });
65}
66let port = 8080;
67if (process.env.PORT) {
68 port = Number.parseInt(process.env.PORT);
69}
70console.log("Serving at port", port);
71server = app.listen(port);