index.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright Google LLC All Rights Reserved.
  5. *
  6. * Use of this source code is governed by an MIT-style license that can be
  7. * found in the LICENSE file at https://angular.dev/license
  8. */
  9. Object.defineProperty(exports, "__esModule", { value: true });
  10. exports.default = default_1;
  11. const schematics_1 = require("@angular-devkit/schematics");
  12. const tasks_1 = require("@angular-devkit/schematics/tasks");
  13. const posix_1 = require("node:path/posix");
  14. const dependencies_1 = require("../utility/dependencies");
  15. const json_file_1 = require("../utility/json-file");
  16. const latest_versions_1 = require("../utility/latest-versions");
  17. const paths_1 = require("../utility/paths");
  18. const workspace_1 = require("../utility/workspace");
  19. const workspace_models_1 = require("../utility/workspace-models");
  20. function updateTsConfig(packageName, ...paths) {
  21. return (host) => {
  22. if (!host.exists('tsconfig.json')) {
  23. return host;
  24. }
  25. const file = new json_file_1.JSONFile(host, 'tsconfig.json');
  26. const jsonPath = ['compilerOptions', 'paths', packageName];
  27. const value = file.get(jsonPath);
  28. file.modify(jsonPath, Array.isArray(value) ? [...value, ...paths] : paths);
  29. };
  30. }
  31. function addTsProjectReference(...paths) {
  32. return (host) => {
  33. if (!host.exists('tsconfig.json')) {
  34. return host;
  35. }
  36. const newReferences = paths.map((path) => ({ path }));
  37. const file = new json_file_1.JSONFile(host, 'tsconfig.json');
  38. const jsonPath = ['references'];
  39. const value = file.get(jsonPath);
  40. file.modify(jsonPath, Array.isArray(value) ? [...value, ...newReferences] : newReferences);
  41. };
  42. }
  43. function addDependenciesToPackageJson() {
  44. return (host) => {
  45. [
  46. {
  47. type: dependencies_1.NodeDependencyType.Dev,
  48. name: '@angular/compiler-cli',
  49. version: latest_versions_1.latestVersions.Angular,
  50. },
  51. {
  52. type: dependencies_1.NodeDependencyType.Dev,
  53. name: '@angular/build',
  54. version: latest_versions_1.latestVersions.AngularBuild,
  55. },
  56. {
  57. type: dependencies_1.NodeDependencyType.Dev,
  58. name: 'ng-packagr',
  59. version: latest_versions_1.latestVersions.NgPackagr,
  60. },
  61. {
  62. type: dependencies_1.NodeDependencyType.Default,
  63. name: 'tslib',
  64. version: latest_versions_1.latestVersions['tslib'],
  65. },
  66. {
  67. type: dependencies_1.NodeDependencyType.Dev,
  68. name: 'typescript',
  69. version: latest_versions_1.latestVersions['typescript'],
  70. },
  71. ].forEach((dependency) => (0, dependencies_1.addPackageJsonDependency)(host, dependency));
  72. return host;
  73. };
  74. }
  75. function addLibToWorkspaceFile(options, projectRoot, projectName) {
  76. return (0, workspace_1.updateWorkspace)((workspace) => {
  77. workspace.projects.add({
  78. name: projectName,
  79. root: projectRoot,
  80. sourceRoot: `${projectRoot}/src`,
  81. projectType: workspace_models_1.ProjectType.Library,
  82. prefix: options.prefix,
  83. targets: {
  84. build: {
  85. builder: workspace_models_1.Builders.BuildNgPackagr,
  86. defaultConfiguration: 'production',
  87. configurations: {
  88. production: {
  89. tsConfig: `${projectRoot}/tsconfig.lib.prod.json`,
  90. },
  91. development: {
  92. tsConfig: `${projectRoot}/tsconfig.lib.json`,
  93. },
  94. },
  95. },
  96. test: {
  97. builder: workspace_models_1.Builders.BuildKarma,
  98. options: {
  99. tsConfig: `${projectRoot}/tsconfig.spec.json`,
  100. polyfills: ['zone.js', 'zone.js/testing'],
  101. },
  102. },
  103. },
  104. });
  105. });
  106. }
  107. function default_1(options) {
  108. return async (host) => {
  109. const prefix = options.prefix;
  110. // If scoped project (i.e. "@foo/bar"), convert projectDir to "foo/bar".
  111. const packageName = options.name;
  112. if (/^@.*\/.*/.test(options.name)) {
  113. const [, name] = options.name.split('/');
  114. options.name = name;
  115. }
  116. const workspace = await (0, workspace_1.getWorkspace)(host);
  117. const newProjectRoot = workspace.extensions.newProjectRoot || '';
  118. let folderName = packageName.startsWith('@') ? packageName.slice(1) : packageName;
  119. if (/[A-Z]/.test(folderName)) {
  120. folderName = schematics_1.strings.dasherize(folderName);
  121. }
  122. const libDir = options.projectRoot !== undefined
  123. ? (0, posix_1.join)(options.projectRoot)
  124. : (0, posix_1.join)(newProjectRoot, folderName);
  125. const distRoot = `dist/${folderName}`;
  126. const sourceDir = `${libDir}/src/lib`;
  127. const templateSource = (0, schematics_1.apply)((0, schematics_1.url)('./files'), [
  128. (0, schematics_1.applyTemplates)({
  129. ...schematics_1.strings,
  130. ...options,
  131. packageName,
  132. libDir,
  133. distRoot,
  134. relativePathToWorkspaceRoot: (0, paths_1.relativePathToWorkspaceRoot)(libDir),
  135. prefix,
  136. angularLatestVersion: latest_versions_1.latestVersions.Angular.replace(/~|\^/, ''),
  137. tsLibLatestVersion: latest_versions_1.latestVersions['tslib'].replace(/~|\^/, ''),
  138. folderName,
  139. }),
  140. (0, schematics_1.move)(libDir),
  141. ]);
  142. return (0, schematics_1.chain)([
  143. (0, schematics_1.mergeWith)(templateSource),
  144. addLibToWorkspaceFile(options, libDir, packageName),
  145. options.skipPackageJson ? (0, schematics_1.noop)() : addDependenciesToPackageJson(),
  146. options.skipTsConfig ? (0, schematics_1.noop)() : updateTsConfig(packageName, './' + distRoot),
  147. options.skipTsConfig
  148. ? (0, schematics_1.noop)()
  149. : addTsProjectReference('./' + (0, posix_1.join)(libDir, 'tsconfig.lib.json'), './' + (0, posix_1.join)(libDir, 'tsconfig.spec.json')),
  150. options.standalone
  151. ? (0, schematics_1.noop)()
  152. : (0, schematics_1.schematic)('module', {
  153. name: options.name,
  154. commonModule: false,
  155. flat: true,
  156. path: sourceDir,
  157. project: packageName,
  158. }),
  159. (0, schematics_1.schematic)('component', {
  160. name: options.name,
  161. selector: `${prefix}-${options.name}`,
  162. inlineStyle: true,
  163. inlineTemplate: true,
  164. flat: true,
  165. path: sourceDir,
  166. export: true,
  167. standalone: options.standalone,
  168. project: packageName,
  169. // Explicitly set an empty `type` since it doesn't necessarily make sense in a library.
  170. // This also ensures that the generated files are valid even if the `component` schematic
  171. // inherits its `type` from the workspace.
  172. type: '',
  173. }),
  174. (_tree, context) => {
  175. if (!options.skipPackageJson && !options.skipInstall) {
  176. context.addTask(new tasks_1.NodePackageInstallTask());
  177. }
  178. },
  179. ]);
  180. };
  181. }