-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathindex.ts
More file actions
124 lines (102 loc) · 3.81 KB
/
index.ts
File metadata and controls
124 lines (102 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import { copy, mkdirp, pathExists } from "fs-extra";
import { readFile } from "fs/promises";
import { join, posix } from "path";
import { lt } from "semver";
import { spawn, sync as spawnSync } from "cross-spawn";
import { FrameworkType, SupportLevel } from "../interfaces";
import { simpleProxy, warnIfCustomBuildScript, getNodeModuleBin, relativeRequire } from "../utils";
import { getNuxtVersion } from "./utils";
export const name = "Nuxt";
export const support = SupportLevel.Experimental;
export const type = FrameworkType.Toolchain;
export const supportedRange = "3";
import { nuxtConfigFilesExist } from "./utils";
import type { NuxtOptions } from "./interfaces";
import { FirebaseError } from "../../error";
import { execSync } from "child_process";
const DEFAULT_BUILD_SCRIPT = ["nuxt build", "nuxi build"];
/**
*
* @param dir current directory
* @return undefined if project is not Nuxt 2, { mayWantBackend: true, publicDirectory: string } otherwise
*/
export async function discover(dir: string) {
if (!(await pathExists(join(dir, "package.json")))) return;
const anyConfigFileExists = await nuxtConfigFilesExist(dir);
const version = getNuxtVersion(dir);
if (!anyConfigFileExists && !version) return;
if (version && lt(version, "3.0.0-0")) return;
const { ssr: mayWantBackend } = await getConfig(dir);
return { mayWantBackend, version };
}
export async function build(cwd: string) {
await warnIfCustomBuildScript(cwd, name, DEFAULT_BUILD_SCRIPT);
const cli = getNodeModuleBin("nuxt", cwd);
const {
ssr: wantsBackend,
app: { baseURL: baseUrl },
} = await getConfig(cwd);
const command = wantsBackend ? ["build"] : ["generate"];
const build = spawnSync(cli, command, {
cwd,
stdio: "inherit",
env: { ...process.env, NITRO_PRESET: "node" },
});
if (build.status !== 0) throw new FirebaseError("Was unable to build your Nuxt application.");
const rewrites = wantsBackend
? []
: [
{
source: posix.join(baseUrl, "**"),
destination: posix.join(baseUrl, "200.html"),
},
];
return { wantsBackend, rewrites, baseUrl };
}
export async function ɵcodegenPublicDirectory(root: string, dest: string) {
const {
app: { baseURL },
} = await getConfig(root);
const distPath = join(root, ".output", "public");
const fullDest = join(dest, baseURL);
await mkdirp(fullDest);
await copy(distPath, fullDest);
}
export async function ɵcodegenFunctionsDirectory(sourceDir: string) {
const serverDir = join(sourceDir, ".output", "server");
const packageJsonBuffer = await readFile(join(sourceDir, "package.json"));
const packageJson = JSON.parse(packageJsonBuffer.toString());
packageJson.dependencies ||= {};
packageJson.dependencies["nitro-output"] = `file:${serverDir}`;
return { packageJson, frameworksEntry: "nitro" };
}
export async function getDevModeHandle(cwd: string) {
const host = new Promise<string>((resolve, reject) => {
const cli = getNodeModuleBin("nuxt", cwd);
const serve = spawn(cli, ["dev"], { cwd: cwd });
serve.stdout.on("data", (data: any) => {
process.stdout.write(data);
const match = data.toString().match(/(http:\/\/.+:\d+)/);
if (match) resolve(match[1]);
});
serve.stderr.on("data", (data: any) => {
process.stderr.write(data);
});
serve.on("exit", reject);
});
return simpleProxy(await host);
}
export async function getConfig(cwd: string): Promise<NuxtOptions> {
const { loadNuxtConfig } = await relativeRequire(cwd, "@nuxt/kit");
return await loadNuxtConfig({ cwd });
}
/**
* Utility method used during project initialization.
*/
export function init(setup: any, config: any) {
execSync(`npx --yes nuxi@"${supportedRange}" init ${setup.featureInfo.hosting.source}`, {
stdio: "inherit",
cwd: config.projectDir,
});
return Promise.resolve();
}