No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.ɵcodegenFunctionsDirectory = exports.ɵcodegenPublicDirectory = exports.getDevModeHandle = exports.build = exports.init = exports.discover = exports.type = exports.support = exports.name = void 0;
  4. const path_1 = require("path");
  5. const child_process_1 = require("child_process");
  6. const fs_extra_1 = require("fs-extra");
  7. const promises_1 = require("fs/promises");
  8. const __1 = require("..");
  9. const prompt_1 = require("../../prompt");
  10. const proxy_1 = require("../../hosting/proxy");
  11. const utils_1 = require("../utils");
  12. exports.name = "Angular";
  13. exports.support = "experimental";
  14. exports.type = 3;
  15. const CLI_COMMAND = (0, path_1.join)("node_modules", ".bin", process.platform === "win32" ? "ng.cmd" : "ng");
  16. const DEFAULT_BUILD_SCRIPT = ["ng build"];
  17. async function discover(dir) {
  18. if (!(await (0, fs_extra_1.pathExists)((0, path_1.join)(dir, "package.json"))))
  19. return;
  20. if (!(await (0, fs_extra_1.pathExists)((0, path_1.join)(dir, "angular.json"))))
  21. return;
  22. const { serverTarget } = await getContext(dir);
  23. return { mayWantBackend: !!serverTarget, publicDirectory: (0, path_1.join)(dir, "src", "assets") };
  24. }
  25. exports.discover = discover;
  26. async function init(setup) {
  27. (0, child_process_1.execSync)(`npx --yes -p @angular/cli@latest ng new ${setup.hosting.source} --skip-git`, {
  28. stdio: "inherit",
  29. });
  30. const useAngularUniversal = await (0, prompt_1.promptOnce)({
  31. name: "useAngularUniversal",
  32. type: "confirm",
  33. default: false,
  34. message: `Would you like to setup Angular Universal?`,
  35. });
  36. if (useAngularUniversal) {
  37. (0, child_process_1.execSync)("ng add @nguniversal/express-engine --skip-confirmation", {
  38. stdio: "inherit",
  39. cwd: setup.hosting.source,
  40. });
  41. }
  42. }
  43. exports.init = init;
  44. async function build(dir) {
  45. const { targetStringFromTarget } = (0, __1.relativeRequire)(dir, "@angular-devkit/architect");
  46. const { architect, browserTarget, prerenderTarget, serverTarget } = await getContext(dir);
  47. const scheduleTarget = async (target) => {
  48. const run = await architect.scheduleTarget(target, undefined);
  49. const { success, error } = await run.output.toPromise();
  50. if (!success)
  51. throw new Error(error);
  52. };
  53. await (0, utils_1.warnIfCustomBuildScript)(dir, exports.name, DEFAULT_BUILD_SCRIPT);
  54. if (!browserTarget)
  55. throw new Error("No build target...");
  56. if (prerenderTarget) {
  57. (0, child_process_1.execSync)(`${CLI_COMMAND} run ${targetStringFromTarget(prerenderTarget)}`, {
  58. cwd: dir,
  59. stdio: "inherit",
  60. });
  61. }
  62. else {
  63. await scheduleTarget(browserTarget);
  64. if (serverTarget)
  65. await scheduleTarget(serverTarget);
  66. }
  67. const wantsBackend = !!serverTarget;
  68. return { wantsBackend };
  69. }
  70. exports.build = build;
  71. async function getDevModeHandle(dir) {
  72. const { targetStringFromTarget } = (0, __1.relativeRequire)(dir, "@angular-devkit/architect");
  73. const { serveTarget } = await getContext(dir);
  74. if (!serveTarget)
  75. return;
  76. const host = new Promise((resolve) => {
  77. const serve = (0, child_process_1.spawn)(CLI_COMMAND, ["run", targetStringFromTarget(serveTarget), "--host", "localhost"], { cwd: dir });
  78. serve.stdout.on("data", (data) => {
  79. process.stdout.write(data);
  80. const match = data.toString().match(/(http:\/\/localhost:\d+)/);
  81. if (match)
  82. resolve(match[1]);
  83. });
  84. serve.stderr.on("data", (data) => {
  85. process.stderr.write(data);
  86. });
  87. });
  88. return (0, proxy_1.proxyRequestHandler)(await host, "Angular Live Development Server", { forceCascade: true });
  89. }
  90. exports.getDevModeHandle = getDevModeHandle;
  91. async function ɵcodegenPublicDirectory(sourceDir, destDir) {
  92. const { architectHost, browserTarget } = await getContext(sourceDir);
  93. if (!browserTarget)
  94. throw new Error("No browser target");
  95. const browserTargetOptions = await architectHost.getOptionsForTarget(browserTarget);
  96. if (typeof (browserTargetOptions === null || browserTargetOptions === void 0 ? void 0 : browserTargetOptions.outputPath) !== "string")
  97. throw new Error("browserTarget output path is not a string");
  98. const browserOutputPath = browserTargetOptions.outputPath;
  99. await (0, promises_1.mkdir)(destDir, { recursive: true });
  100. await (0, fs_extra_1.copy)((0, path_1.join)(sourceDir, browserOutputPath), destDir);
  101. }
  102. exports.ɵcodegenPublicDirectory = ɵcodegenPublicDirectory;
  103. async function ɵcodegenFunctionsDirectory(sourceDir, destDir) {
  104. var _a;
  105. const { architectHost, host, serverTarget, browserTarget } = await getContext(sourceDir);
  106. if (!serverTarget)
  107. throw new Error("No server target");
  108. if (!browserTarget)
  109. throw new Error("No browser target");
  110. const packageJson = JSON.parse(await host.readFile((0, path_1.join)(sourceDir, "package.json")));
  111. const serverTargetOptions = await architectHost.getOptionsForTarget(serverTarget);
  112. if (typeof (serverTargetOptions === null || serverTargetOptions === void 0 ? void 0 : serverTargetOptions.outputPath) !== "string")
  113. throw new Error("serverTarget output path is not a string");
  114. const browserTargetOptions = await architectHost.getOptionsForTarget(browserTarget);
  115. if (typeof (browserTargetOptions === null || browserTargetOptions === void 0 ? void 0 : browserTargetOptions.outputPath) !== "string")
  116. throw new Error("browserTarget output path is not a string");
  117. const browserOutputPath = browserTargetOptions.outputPath;
  118. const serverOutputPath = serverTargetOptions.outputPath;
  119. await (0, promises_1.mkdir)((0, path_1.join)(destDir, serverOutputPath), { recursive: true });
  120. await (0, promises_1.mkdir)((0, path_1.join)(destDir, browserOutputPath), { recursive: true });
  121. await (0, fs_extra_1.copy)((0, path_1.join)(sourceDir, serverOutputPath), (0, path_1.join)(destDir, serverOutputPath));
  122. await (0, fs_extra_1.copy)((0, path_1.join)(sourceDir, browserOutputPath), (0, path_1.join)(destDir, browserOutputPath));
  123. const bootstrapScript = `exports.handle = require('./${serverOutputPath}/main.js').app();\n`;
  124. const bundleDependencies = (_a = serverTargetOptions.bundleDependencies) !== null && _a !== void 0 ? _a : true;
  125. if (bundleDependencies) {
  126. const dependencies = {};
  127. const externalDependencies = serverTargetOptions.externalDependencies || [];
  128. externalDependencies.forEach((externalDependency) => {
  129. var _a;
  130. const packageVersion = (_a = (0, __1.findDependency)(externalDependency)) === null || _a === void 0 ? void 0 : _a.version;
  131. if (packageVersion) {
  132. dependencies[externalDependency] = packageVersion;
  133. }
  134. });
  135. packageJson.dependencies = dependencies;
  136. }
  137. return { bootstrapScript, packageJson };
  138. }
  139. exports.ɵcodegenFunctionsDirectory = ɵcodegenFunctionsDirectory;
  140. async function getContext(dir) {
  141. const { NodeJsAsyncHost } = (0, __1.relativeRequire)(dir, "@angular-devkit/core/node");
  142. const { workspaces } = (0, __1.relativeRequire)(dir, "@angular-devkit/core");
  143. const { WorkspaceNodeModulesArchitectHost } = (0, __1.relativeRequire)(dir, "@angular-devkit/architect/node");
  144. const { Architect, targetFromTargetString, targetStringFromTarget } = (0, __1.relativeRequire)(dir, "@angular-devkit/architect");
  145. const { parse } = (0, __1.relativeRequire)(dir, "jsonc-parser");
  146. const host = workspaces.createWorkspaceHost(new NodeJsAsyncHost());
  147. const { workspace } = await workspaces.readWorkspace(dir, host);
  148. const architectHost = new WorkspaceNodeModulesArchitectHost(workspace, dir);
  149. const architect = new Architect(architectHost);
  150. let project = globalThis.NG_DEPLOY_PROJECT;
  151. let browserTarget;
  152. let serverTarget;
  153. let prerenderTarget;
  154. let serveTarget;
  155. if (!project) {
  156. const angularJson = parse(await host.readFile((0, path_1.join)(dir, "angular.json")));
  157. project = angularJson.defaultProject;
  158. }
  159. if (!project) {
  160. const apps = [];
  161. workspace.projects.forEach((value, key) => {
  162. if (value.extensions.projectType === "application")
  163. apps.push(key);
  164. });
  165. if (apps.length === 1)
  166. project = apps[0];
  167. }
  168. if (!project)
  169. throw new Error("Unable to detirmine the application to deploy, you should use `ng deploy` via @angular/fire.");
  170. const workspaceProject = workspace.projects.get(project);
  171. if (!workspaceProject)
  172. throw new Error(`No project ${project} found.`);
  173. const deployTargetDefinition = workspaceProject.targets.get("deploy");
  174. if ((deployTargetDefinition === null || deployTargetDefinition === void 0 ? void 0 : deployTargetDefinition.builder) === "@angular/fire:deploy") {
  175. const options = deployTargetDefinition.options;
  176. if (typeof (options === null || options === void 0 ? void 0 : options.prerenderTarget) === "string")
  177. prerenderTarget = targetFromTargetString(options.prerenderTarget);
  178. if (typeof (options === null || options === void 0 ? void 0 : options.browserTarget) === "string")
  179. browserTarget = targetFromTargetString(options.browserTarget);
  180. if (typeof (options === null || options === void 0 ? void 0 : options.serverTarget) === "string")
  181. serverTarget = targetFromTargetString(options.serverTarget);
  182. if (!browserTarget)
  183. throw new Error("ng-deploy is missing a browser target. Plase check your angular.json.");
  184. if (prerenderTarget) {
  185. const prerenderOptions = await architectHost.getOptionsForTarget(prerenderTarget);
  186. if (targetStringFromTarget(browserTarget) !== (prerenderOptions === null || prerenderOptions === void 0 ? void 0 : prerenderOptions.browserTarget))
  187. throw new Error("ng-deploy's browserTarget and prerender's browserTarget do not match. Please check your angular.json");
  188. if (serverTarget && targetStringFromTarget(serverTarget) !== (prerenderOptions === null || prerenderOptions === void 0 ? void 0 : prerenderOptions.serverTarget))
  189. throw new Error("ng-deploy's serverTarget and prerender's serverTarget do not match. Please check your angular.json");
  190. if (!serverTarget)
  191. console.warn("Treating the application as fully rendered. Add a serverTarget to your deploy target in angular.json to utilize server-side rendering.");
  192. }
  193. }
  194. else if (workspaceProject.targets.has("prerender")) {
  195. const target = workspaceProject.targets.get("prerender");
  196. const configurations = Object.keys(target.configurations);
  197. const configuration = configurations.includes("production")
  198. ? "production"
  199. : target.defaultConfiguration;
  200. if (!configuration)
  201. throw new Error("No production or default configutation found for prerender.");
  202. if (configuration !== "production")
  203. console.warn(`Using ${configuration} configuration for the prerender, we suggest adding a production target.`);
  204. prerenderTarget = { project, target: "prerender", configuration };
  205. const production = await architectHost.getOptionsForTarget(prerenderTarget);
  206. if (typeof (production === null || production === void 0 ? void 0 : production.browserTarget) !== "string")
  207. throw new Error("Prerender browserTarget expected to be string, check your angular.json.");
  208. browserTarget = targetFromTargetString(production.browserTarget);
  209. if (typeof (production === null || production === void 0 ? void 0 : production.serverTarget) !== "string")
  210. throw new Error("Prerender serverTarget expected to be string, check your angular.json.");
  211. serverTarget = targetFromTargetString(production.serverTarget);
  212. }
  213. else {
  214. if (workspaceProject.targets.has("build")) {
  215. const target = workspaceProject.targets.get("build");
  216. const configurations = Object.keys(target.configurations);
  217. const configuration = configurations.includes("production")
  218. ? "production"
  219. : target.defaultConfiguration;
  220. if (!configuration)
  221. throw new Error("No production or default configutation found for build.");
  222. if (configuration !== "production")
  223. console.warn(`Using ${configuration} configuration for the browser deploy, we suggest adding a production target.`);
  224. browserTarget = { project, target: "build", configuration };
  225. }
  226. if (workspaceProject.targets.has("server")) {
  227. const target = workspaceProject.targets.get("server");
  228. const configurations = Object.keys(target.configurations);
  229. const configuration = configurations.includes("production")
  230. ? "production"
  231. : target.defaultConfiguration;
  232. if (!configuration)
  233. throw new Error("No production or default configutation found for server.");
  234. if (configuration !== "production")
  235. console.warn(`Using ${configuration} configuration for the server deploy, we suggest adding a production target.`);
  236. serverTarget = { project, target: "server", configuration };
  237. }
  238. }
  239. if (serverTarget && workspaceProject.targets.has("serve-ssr")) {
  240. const target = workspaceProject.targets.get("serve-ssr");
  241. const configurations = Object.keys(target.configurations);
  242. const configuration = configurations.includes("development")
  243. ? "development"
  244. : target.defaultConfiguration;
  245. if (!configuration)
  246. throw new Error("No development or default configutation found for serve-ssr.");
  247. if (configuration !== "development")
  248. console.warn(`Using ${configuration} configuration for the local server, we suggest adding a development target.`);
  249. serveTarget = { project, target: "serve-ssr", configuration };
  250. }
  251. else if (workspaceProject.targets.has("serve")) {
  252. if (serverTarget)
  253. console.warn(`No server-ssr target found.`);
  254. const target = workspaceProject.targets.get("serve");
  255. const configurations = Object.keys(target.configurations);
  256. const configuration = configurations.includes("development")
  257. ? "development"
  258. : target.defaultConfiguration;
  259. if (!configuration)
  260. throw new Error("No development or default configutation found for serve.");
  261. if (configuration !== "development")
  262. console.warn(`Using ${configuration} configuration for the local server, we suggest adding a development target.`);
  263. serveTarget = { project, target: "serve", configuration };
  264. }
  265. return {
  266. architect,
  267. architectHost,
  268. host,
  269. browserTarget,
  270. prerenderTarget,
  271. serverTarget,
  272. serveTarget,
  273. };
  274. }