Skip to content

Commit 8b3c9da

Browse files
committed
Parse angular.json as fallback during builder validation.
1 parent 1dc52ce commit 8b3c9da

File tree

1 file changed

+61
-30
lines changed
  • packages/@apphosting/adapter-angular/src

1 file changed

+61
-30
lines changed

packages/@apphosting/adapter-angular/src/utils.ts

Lines changed: 61 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -50,40 +50,71 @@ export async function checkBuildConditions(opts: BuildOptions): Promise<void> {
5050
return;
5151
}
5252

53+
let angularBuilder = "";
5354
// dynamically load Angular so this can be used in an NPX context
5455
const angularCorePath = require.resolve("@angular/core", { paths: [process.cwd()] });
55-
const { NodeJsAsyncHost }: typeof import("@angular-devkit/core/node") = await import(
56-
require.resolve("@angular-devkit/core/node", {
57-
paths: [process.cwd(), angularCorePath],
58-
})
59-
);
60-
const { workspaces }: typeof import("@angular-devkit/core") = await import(
61-
require.resolve("@angular-devkit/core", {
62-
paths: [process.cwd(), angularCorePath],
63-
})
64-
);
65-
const host = workspaces.createWorkspaceHost(new NodeJsAsyncHost());
66-
const { workspace } = await workspaces.readWorkspace(opts.projectDirectory, host);
67-
68-
const apps: string[] = [];
69-
workspace.projects.forEach((value, key) => {
70-
if (value.extensions.projectType === "application") apps.push(key);
71-
});
72-
const project = apps[0];
73-
if (apps.length > 1 || !project) throw new Error("Unable to determine the application to deploy");
74-
75-
const workspaceProject = workspace.projects.get(project);
76-
if (!workspaceProject) throw new Error(`No project ${project} found.`);
77-
78-
const target = "build";
79-
if (!workspaceProject.targets.has(target)) throw new Error("Could not find build target.");
80-
81-
const { builder } = workspaceProject.targets.get(target)!;
82-
if (!ALLOWED_BUILDERS.includes(builder)) {
83-
throw new Error(
84-
`Currently, only the following builders are supported: ${ALLOWED_BUILDERS.join(",")}.`,
56+
try {
57+
// Note: we assume that the user's app has @angular-devkit/core in their node_modules/
58+
// because we expect them to have @angular-devkit/build-angular as a dependency which
59+
// pulls in @angular-devkit/core as a dependency. However this assumption may not hold
60+
// due to tree shaking.
61+
const { NodeJsAsyncHost }: typeof import("@angular-devkit/core/node") = await import(
62+
require.resolve("@angular-devkit/core/node", {
63+
paths: [process.cwd(), angularCorePath],
64+
})
8565
);
66+
const { workspaces }: typeof import("@angular-devkit/core") = await import(
67+
require.resolve("@angular-devkit/core", {
68+
paths: [process.cwd(), angularCorePath],
69+
})
70+
);
71+
const host = workspaces.createWorkspaceHost(new NodeJsAsyncHost());
72+
const { workspace } = await workspaces.readWorkspace(opts.projectDirectory, host);
73+
74+
const apps: string[] = [];
75+
workspace.projects.forEach((value, key) => {
76+
if (value.extensions.projectType === "application") apps.push(key);
77+
});
78+
const project = apps[0];
79+
if (apps.length > 1 || !project) throw new Error("Unable to determine the application to deploy");
80+
81+
const workspaceProject = workspace.projects.get(project);
82+
if (!workspaceProject) throw new Error(`No project ${project} found.`);
83+
84+
const target = "build";
85+
if (!workspaceProject.targets.has(target)) throw new Error("Could not find build target.");
86+
87+
const { builder } = workspaceProject.targets.get(target)!;
88+
angularBuilder = builder;
89+
} catch (error) {
90+
91+
logger.warn("failed to determine angular builder from the workspace api: ", error);
92+
try {
93+
const root = process.cwd();
94+
const angularJSON = JSON.parse((readFileSync(join(root, "angular.json"))).toString());
95+
const apps: string[] = [];
96+
Object.keys(angularJSON.projects).forEach(projectName => {
97+
const project = angularJSON.projects[projectName];
98+
if (project["projectType"] === "application") apps.push(projectName);
99+
});
100+
const project = apps[0];
101+
if (apps.length > 1 || !project) throw new Error("Unable to determine the application to deploy");
102+
angularBuilder = angularJSON.projects[project].architect.build.builder;
103+
} catch (error) {
104+
logger.warn("failed to determine angular builder from parsing angular.json: ", error);
105+
};
106+
};
107+
108+
if (angularBuilder !== "") {
109+
if (!ALLOWED_BUILDERS.includes(angularBuilder)) {
110+
throw new Error(
111+
`Currently, only the following builders are supported: ${ALLOWED_BUILDERS.join(",")}.`,
112+
);
113+
}
86114
}
115+
// This is just a validation step and our methods for validation are flakey. If we failed to extract
116+
// the angular builder for validation, the build will continue. It may fail further down the line
117+
// but the failure reason should be non-ambigious.
87118
}
88119

89120
// Populate file or directory paths we need for generating output directory

0 commit comments

Comments
 (0)