-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathindex.ts
More file actions
64 lines (56 loc) · 2.45 KB
/
index.ts
File metadata and controls
64 lines (56 loc) · 2.45 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
import { sync as spawnSync } from "cross-spawn";
import { copy, pathExists } from "fs-extra";
import { join } from "path";
import { BuildResult, Discovery, FrameworkType, SupportLevel } from "../interfaces";
import { FirebaseError } from "../../error";
import { assertFlutterCliExists, getPubSpec, getAdditionalBuildArgs } from "./utils";
import { DART_RESERVED_WORDS, FALLBACK_PROJECT_NAME } from "./constants";
export const name = "Flutter Web";
export const type = FrameworkType.Framework;
export const support = SupportLevel.Experimental;
export async function discover(dir: string): Promise<Discovery | undefined> {
if (!(await pathExists(join(dir, "pubspec.yaml")))) return;
if (!(await pathExists(join(dir, "web")))) return;
const pubSpec = await getPubSpec(dir);
const usingFlutter = pubSpec.dependencies?.flutter;
if (!usingFlutter) return;
return { mayWantBackend: false };
}
export function init(setup: any, config: any) {
assertFlutterCliExists();
// Convert the projectId into a valid pubspec name https://dart.dev/tools/pub/pubspec#name
// the projectId should be safe, save hyphens which we turn into underscores here
// if it's a reserved word just give it a fallback name
const projectName = DART_RESERVED_WORDS.includes(setup.projectId)
? FALLBACK_PROJECT_NAME
: setup.projectId.replaceAll("-", "_");
const result = spawnSync(
"flutter",
[
"create",
"--template=app",
`--project-name=${projectName}`,
"--overwrite",
"--platforms=web",
setup.featureInfo.hosting.source,
],
{ stdio: "inherit", cwd: config.projectDir },
);
if (result.status !== 0)
throw new FirebaseError(
"We were not able to create your flutter app, create the application yourself https://docs.flutter.dev/get-started/test-drive?tab=terminal before trying again.",
);
return Promise.resolve();
}
export async function build(cwd: string): Promise<BuildResult> {
assertFlutterCliExists();
const pubSpec = await getPubSpec(cwd);
const otherArgs = getAdditionalBuildArgs(pubSpec);
const buildArgs = ["build", "web", ...otherArgs].filter(Boolean);
const build = spawnSync("flutter", buildArgs, { cwd, stdio: "inherit" });
if (build.status !== 0) throw new FirebaseError("Unable to build your Flutter app");
return Promise.resolve({ wantsBackend: false });
}
export async function ɵcodegenPublicDirectory(sourceDir: string, destDir: string) {
await copy(join(sourceDir, "build", "web"), destDir);
}