index.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 addDependenciesToPackageJson() {
  32. return (host) => {
  33. [
  34. {
  35. type: dependencies_1.NodeDependencyType.Dev,
  36. name: '@angular/compiler-cli',
  37. version: latest_versions_1.latestVersions.Angular,
  38. },
  39. {
  40. type: dependencies_1.NodeDependencyType.Dev,
  41. name: '@angular-devkit/build-angular',
  42. version: latest_versions_1.latestVersions.DevkitBuildAngular,
  43. },
  44. {
  45. type: dependencies_1.NodeDependencyType.Dev,
  46. name: 'ng-packagr',
  47. version: latest_versions_1.latestVersions.NgPackagr,
  48. },
  49. {
  50. type: dependencies_1.NodeDependencyType.Default,
  51. name: 'tslib',
  52. version: latest_versions_1.latestVersions['tslib'],
  53. },
  54. {
  55. type: dependencies_1.NodeDependencyType.Dev,
  56. name: 'typescript',
  57. version: latest_versions_1.latestVersions['typescript'],
  58. },
  59. ].forEach((dependency) => (0, dependencies_1.addPackageJsonDependency)(host, dependency));
  60. return host;
  61. };
  62. }
  63. function addLibToWorkspaceFile(options, projectRoot, projectName) {
  64. return (0, workspace_1.updateWorkspace)((workspace) => {
  65. workspace.projects.add({
  66. name: projectName,
  67. root: projectRoot,
  68. sourceRoot: `${projectRoot}/src`,
  69. projectType: workspace_models_1.ProjectType.Library,
  70. prefix: options.prefix,
  71. targets: {
  72. build: {
  73. builder: workspace_models_1.Builders.NgPackagr,
  74. defaultConfiguration: 'production',
  75. options: {
  76. project: `${projectRoot}/ng-package.json`,
  77. },
  78. configurations: {
  79. production: {
  80. tsConfig: `${projectRoot}/tsconfig.lib.prod.json`,
  81. },
  82. development: {
  83. tsConfig: `${projectRoot}/tsconfig.lib.json`,
  84. },
  85. },
  86. },
  87. test: {
  88. builder: workspace_models_1.Builders.Karma,
  89. options: {
  90. tsConfig: `${projectRoot}/tsconfig.spec.json`,
  91. polyfills: ['zone.js', 'zone.js/testing'],
  92. },
  93. },
  94. },
  95. });
  96. });
  97. }
  98. function default_1(options) {
  99. return async (host) => {
  100. const prefix = options.prefix;
  101. // If scoped project (i.e. "@foo/bar"), convert projectDir to "foo/bar".
  102. const packageName = options.name;
  103. if (/^@.*\/.*/.test(options.name)) {
  104. const [, name] = options.name.split('/');
  105. options.name = name;
  106. }
  107. const workspace = await (0, workspace_1.getWorkspace)(host);
  108. const newProjectRoot = workspace.extensions.newProjectRoot || '';
  109. let folderName = packageName.startsWith('@') ? packageName.slice(1) : packageName;
  110. if (/[A-Z]/.test(folderName)) {
  111. folderName = schematics_1.strings.dasherize(folderName);
  112. }
  113. const libDir = options.projectRoot !== undefined
  114. ? (0, posix_1.join)(options.projectRoot)
  115. : (0, posix_1.join)(newProjectRoot, folderName);
  116. const distRoot = `dist/${folderName}`;
  117. const sourceDir = `${libDir}/src/lib`;
  118. const templateSource = (0, schematics_1.apply)((0, schematics_1.url)('./files'), [
  119. (0, schematics_1.applyTemplates)({
  120. ...schematics_1.strings,
  121. ...options,
  122. packageName,
  123. libDir,
  124. distRoot,
  125. relativePathToWorkspaceRoot: (0, paths_1.relativePathToWorkspaceRoot)(libDir),
  126. prefix,
  127. angularLatestVersion: latest_versions_1.latestVersions.Angular.replace(/~|\^/, ''),
  128. tsLibLatestVersion: latest_versions_1.latestVersions['tslib'].replace(/~|\^/, ''),
  129. folderName,
  130. }),
  131. (0, schematics_1.move)(libDir),
  132. ]);
  133. return (0, schematics_1.chain)([
  134. (0, schematics_1.mergeWith)(templateSource),
  135. addLibToWorkspaceFile(options, libDir, packageName),
  136. options.skipPackageJson ? (0, schematics_1.noop)() : addDependenciesToPackageJson(),
  137. options.skipTsConfig ? (0, schematics_1.noop)() : updateTsConfig(packageName, './' + distRoot),
  138. options.standalone
  139. ? (0, schematics_1.noop)()
  140. : (0, schematics_1.schematic)('module', {
  141. name: options.name,
  142. commonModule: false,
  143. flat: true,
  144. path: sourceDir,
  145. project: packageName,
  146. }),
  147. (0, schematics_1.schematic)('component', {
  148. name: options.name,
  149. selector: `${prefix}-${options.name}`,
  150. inlineStyle: true,
  151. inlineTemplate: true,
  152. flat: true,
  153. path: sourceDir,
  154. export: true,
  155. standalone: options.standalone,
  156. project: packageName,
  157. }),
  158. (0, schematics_1.schematic)('service', {
  159. name: options.name,
  160. flat: true,
  161. path: sourceDir,
  162. project: packageName,
  163. }),
  164. (_tree, context) => {
  165. if (!options.skipPackageJson && !options.skipInstall) {
  166. context.addTask(new tasks_1.NodePackageInstallTask());
  167. }
  168. },
  169. ]);
  170. };
  171. }