|
| 1 | +import fsExtra from "fs-extra"; |
| 2 | +import { fileURLToPath } from "url"; |
| 3 | + |
| 4 | +// fs-extra is CJS, readJson can't be imported using shorthand |
| 5 | +export const { readJson } = fsExtra; |
| 6 | + |
| 7 | +export async function loadConfig(cwd: string) { |
| 8 | + // dynamically load NextJS so this can be used in an NPX context |
| 9 | + const { NodeJsAsyncHost }: typeof import("@angular-devkit/core/node") = await import( |
| 10 | + `${cwd}/node_modules/@angular-devkit/core/node/index.js` |
| 11 | + ); |
| 12 | + const { workspaces }: typeof import("@angular-devkit/core") = await import( |
| 13 | + `${cwd}/node_modules/@angular-devkit/core/src/index.js` |
| 14 | + ); |
| 15 | + const { WorkspaceNodeModulesArchitectHost }: typeof import("@angular-devkit/architect/node") = |
| 16 | + await import(`${cwd}/node_modules/@angular-devkit/architect/node/index.js`); |
| 17 | + |
| 18 | + const host = workspaces.createWorkspaceHost(new NodeJsAsyncHost()); |
| 19 | + const { workspace } = await workspaces.readWorkspace(cwd, host); |
| 20 | + const architectHost = new WorkspaceNodeModulesArchitectHost(workspace, cwd); |
| 21 | + |
| 22 | + const apps: string[] = []; |
| 23 | + workspace.projects.forEach((value, key) => { |
| 24 | + if (value.extensions.projectType === "application") apps.push(key); |
| 25 | + }); |
| 26 | + const project = apps[0]; |
| 27 | + if (apps.length > 1 || !project) throw new Error("Unable to determine the application to deploy"); |
| 28 | + |
| 29 | + const workspaceProject = workspace.projects.get(project); |
| 30 | + if (!workspaceProject) throw new Error(`No project ${project} found.`); |
| 31 | + |
| 32 | + const target = "build"; |
| 33 | + if (!workspaceProject.targets.has(target)) throw new Error("Could not find build target."); |
| 34 | + |
| 35 | + const { builder, defaultConfiguration: configuration = "production" } = |
| 36 | + workspaceProject.targets.get(target)!; |
| 37 | + if (builder !== "@angular-devkit/build-angular:application") { |
| 38 | + throw new Error("Only the Angular application builder is supported."); |
| 39 | + } |
| 40 | + |
| 41 | + const buildTarget = { |
| 42 | + project, |
| 43 | + target, |
| 44 | + configuration, |
| 45 | + }; |
| 46 | + |
| 47 | + const options = await architectHost.getOptionsForTarget(buildTarget); |
| 48 | + if (!options) throw new Error("Not able to find options for build target."); |
| 49 | + return options; |
| 50 | +} |
| 51 | + |
| 52 | +export const isMain = (meta: ImportMeta) => { |
| 53 | + if (!meta) return false; |
| 54 | + if (!process.argv[1]) return false; |
| 55 | + return process.argv[1] === fileURLToPath(meta.url); |
| 56 | +}; |
0 commit comments