|
| 1 | +import * as fs from "fs"; |
| 2 | +import * as spawn from "cross-spawn"; |
| 3 | +import * as path from "path"; |
| 4 | + |
| 5 | +import { Config } from "../../../config"; |
| 6 | +import { getPythonBinary, LATEST_VERSION } from "../../../deploy/functions/runtimes/python"; |
| 7 | +import { runWithVirtualEnv } from "../../../functions/python"; |
| 8 | +import { promptOnce } from "../../../prompt"; |
| 9 | + |
| 10 | +const TEMPLATE_ROOT = path.resolve(__dirname, "../../../../templates/init/functions/python"); |
| 11 | +const MAIN_TEMPLATE = fs.readFileSync(path.join(TEMPLATE_ROOT, "main.py"), "utf8"); |
| 12 | +const REQUIREMENTS_TEMPLATE = fs.readFileSync(path.join(TEMPLATE_ROOT, "requirements.txt"), "utf8"); |
| 13 | +const GITIGNORE_TEMPLATE = fs.readFileSync(path.join(TEMPLATE_ROOT, "_gitignore"), "utf8"); |
| 14 | + |
| 15 | +/** |
| 16 | + * Create a Python Firebase Functions project. |
| 17 | + */ |
| 18 | +export async function setup(setup: any, config: Config): Promise<void> { |
| 19 | + await config.askWriteProjectFile( |
| 20 | + `${setup.functions.source}/requirements.txt`, |
| 21 | + REQUIREMENTS_TEMPLATE |
| 22 | + ); |
| 23 | + await config.askWriteProjectFile(`${setup.functions.source}/.gitignore`, GITIGNORE_TEMPLATE); |
| 24 | + await config.askWriteProjectFile(`${setup.functions.source}/main.py`, MAIN_TEMPLATE); |
| 25 | + |
| 26 | + // Write the latest supported runtime version to the config. |
| 27 | + config.set("functions.runtime", LATEST_VERSION); |
| 28 | + // Add python specific ignores to config. |
| 29 | + config.set("functions.ignore", ["venv", "__pycache__"]); |
| 30 | + |
| 31 | + // Setup VENV. |
| 32 | + const venvProcess = spawn(getPythonBinary(LATEST_VERSION), ["-m", "venv", "venv"], { |
| 33 | + shell: true, |
| 34 | + cwd: config.path(setup.functions.source), |
| 35 | + stdio: [/* stdin= */ "pipe", /* stdout= */ "pipe", /* stderr= */ "pipe", "pipe"], |
| 36 | + }); |
| 37 | + await new Promise((resolve, reject) => { |
| 38 | + venvProcess.on("exit", resolve); |
| 39 | + venvProcess.on("error", reject); |
| 40 | + }); |
| 41 | + |
| 42 | + const install = await promptOnce({ |
| 43 | + name: "install", |
| 44 | + type: "confirm", |
| 45 | + message: "Do you want to install dependencies now?", |
| 46 | + default: true, |
| 47 | + }); |
| 48 | + if (install) { |
| 49 | + // Update pip to support dependencies like pyyaml. |
| 50 | + const upgradeProcess = runWithVirtualEnv( |
| 51 | + ["pip3", "install", "--upgrade", "pip"], |
| 52 | + config.path(setup.functions.source), |
| 53 | + {}, |
| 54 | + { stdio: ["inherit", "inherit", "inherit"] } |
| 55 | + ); |
| 56 | + await new Promise((resolve, reject) => { |
| 57 | + upgradeProcess.on("exit", resolve); |
| 58 | + upgradeProcess.on("error", reject); |
| 59 | + }); |
| 60 | + const installProcess = runWithVirtualEnv( |
| 61 | + [getPythonBinary(LATEST_VERSION), "-m", "pip", "install", "-r", "requirements.txt"], |
| 62 | + config.path(setup.functions.source), |
| 63 | + {}, |
| 64 | + { stdio: ["inherit", "inherit", "inherit"] } |
| 65 | + ); |
| 66 | + await new Promise((resolve, reject) => { |
| 67 | + installProcess.on("exit", resolve); |
| 68 | + installProcess.on("error", reject); |
| 69 | + }); |
| 70 | + } |
| 71 | +} |
0 commit comments