|
| 1 | +#! /usr/bin/env node |
| 2 | +import { spawn } from "child_process"; |
| 3 | +import { program } from "commander"; |
| 4 | +import { yellow, bgRed, bold } from "colorette"; |
| 5 | + |
| 6 | +// TODO(#382): add framework option later or incorporate micro-discovery. |
| 7 | +// TODO(#382): parse apphosting.yaml for environment variables / secrets. |
| 8 | +// TODO(#382): parse apphosting.yaml for runConfig and include in buildSchema |
| 9 | +// TODO(#382): Support custom build and run commands (parse and pass run command to build schema). |
| 10 | +program |
| 11 | + .argument("<projectRoot>", "path to the project's root directory") |
| 12 | + .action(async (projectRoot: string) => { |
| 13 | + const framework = "nextjs"; |
| 14 | + // TODO(#382): We are using the latest framework adapter versions, but in the future |
| 15 | + // we should parse the framework version and use the matching adapter version. |
| 16 | + const adapterName = `@apphosting/adapter-nextjs`; |
| 17 | + const packumentResponse = await fetch(`https://registry.npmjs.org/${adapterName}`); |
| 18 | + if (!packumentResponse.ok) throw new Error(`Something went wrong fetching ${adapterName}`); |
| 19 | + const packument = await packumentResponse.json(); |
| 20 | + const adapterVersion = packument?.["dist-tags"]?.["latest"]; |
| 21 | + if (!adapterVersion) { |
| 22 | + throw new Error(`Could not find 'latest' dist-tag for ${adapterName}`); |
| 23 | + } |
| 24 | + // TODO(#382): should check for existence of adapter in app's package.json and use that version instead. |
| 25 | + |
| 26 | + console.log(" 🔥", bgRed(` ${adapterName}@${yellow(bold(adapterVersion))} `), "\n"); |
| 27 | + |
| 28 | + const buildCommand = `apphosting-adapter-${framework}-build`; |
| 29 | + await new Promise<void>((resolve, reject) => { |
| 30 | + const child = spawn("npx", ["-y", "-p", `${adapterName}@${adapterVersion}`, buildCommand], { |
| 31 | + cwd: projectRoot, |
| 32 | + shell: true, |
| 33 | + stdio: "inherit", |
| 34 | + }); |
| 35 | + |
| 36 | + child.on("exit", (code) => { |
| 37 | + if (code !== 0) { |
| 38 | + reject(new Error(`framework adapter build failed with error code ${code}.`)); |
| 39 | + } |
| 40 | + resolve(); |
| 41 | + }); |
| 42 | + }); |
| 43 | + // TODO(#382): parse bundle.yaml and apphosting.yaml and output a buildschema somewhere. |
| 44 | + }); |
| 45 | + |
| 46 | +program.parse(); |
0 commit comments