index.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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.io/license
  8. */
  9. Object.defineProperty(exports, "__esModule", { value: true });
  10. const ts = require("typescript");
  11. const ast_utils_1 = require("@schematics/angular/utility/ast-utils");
  12. const change_1 = require("@schematics/angular/utility/change");
  13. const ng_ast_utils_1 = require("@schematics/angular/utility/ng-ast-utils");
  14. const index_1 = require("@schematics/angular/utility/test/index");
  15. const project_main_file_1 = require("./project-main-file");
  16. const tasks_1 = require("@angular-devkit/schematics/tasks");
  17. const schematics_1 = require("@angular-devkit/schematics");
  18. function installPackageJsonDependencies() {
  19. return (host, context) => {
  20. context.addTask(new tasks_1.NodePackageInstallTask());
  21. context.logger.log('info', `🔍 Installing packages...`);
  22. return host;
  23. };
  24. }
  25. exports.installPackageJsonDependencies = installPackageJsonDependencies;
  26. function addStyleToTarget(project, targetName, host, assetPath, workspace) {
  27. const targetOptions = getProjectTargetOptions(project, targetName);
  28. if (!targetOptions.styles) {
  29. targetOptions.styles = [assetPath];
  30. }
  31. else {
  32. const existingStyles = targetOptions.styles
  33. .map((style) => {
  34. return typeof style === 'string' ? style : style.input;
  35. });
  36. const hasBootstrapStyle = existingStyles.find((style) => {
  37. return style.includes(assetPath);
  38. });
  39. if (!hasBootstrapStyle) {
  40. targetOptions.styles.unshift(assetPath);
  41. }
  42. }
  43. host.overwrite('angular.json', JSON.stringify(workspace, null, 2));
  44. }
  45. exports.addStyleToTarget = addStyleToTarget;
  46. function getProjectFromWorkspace(workspace, projectName) {
  47. /* tslint:disable-next-line: no-non-null-assertion */
  48. const project = workspace.projects[projectName || workspace.defaultProject];
  49. if (!project) {
  50. throw new Error(`Could not find project in workspace: ${projectName}`);
  51. }
  52. return project;
  53. }
  54. exports.getProjectFromWorkspace = getProjectFromWorkspace;
  55. function expectProjectStyleFile(project, filePath) {
  56. expect(getProjectTargetOptions(project, 'build').styles).toContain(filePath, `Expected "${filePath}" to be added to the project styles in the workspace.`);
  57. }
  58. exports.expectProjectStyleFile = expectProjectStyleFile;
  59. function getProjectTargetOptions(project, buildTarget) {
  60. const targetConfig = project.architect && project.architect[buildTarget] ||
  61. project.targets && project.targets[buildTarget];
  62. if (targetConfig && targetConfig.options) {
  63. return targetConfig.options;
  64. }
  65. throw new Error(`Cannot determine project target configuration for: ${buildTarget}.`);
  66. }
  67. exports.getProjectTargetOptions = getProjectTargetOptions;
  68. function sortObjectByKeys(obj) {
  69. return Object
  70. .keys(obj)
  71. .sort()
  72. /* tslint:disable-next-line: no-any */
  73. .reduce((result, key) => (result[key] = obj[key]) && result, {});
  74. }
  75. function addPackageToPackageJson(host, pkg, version) {
  76. if (host.exists('package.json')) {
  77. /* tslint:disable-next-line: no-non-null-assertion */
  78. const sourceText = host.read('package.json').toString('utf-8');
  79. const json = JSON.parse(sourceText);
  80. if (!json.dependencies) {
  81. json.dependencies = {};
  82. }
  83. if (!json.dependencies[pkg]) {
  84. json.dependencies[pkg] = version;
  85. json.dependencies = sortObjectByKeys(json.dependencies);
  86. }
  87. host.overwrite('package.json', JSON.stringify(json, null, 2));
  88. }
  89. return host;
  90. }
  91. exports.addPackageToPackageJson = addPackageToPackageJson;
  92. function createTestApp(runner, appOptions = {}) {
  93. const workspaceTree = runner.runExternalSchematic('@schematics/angular', 'workspace', {
  94. name: 'workspace',
  95. version: '8.2.0',
  96. newProjectRoot: 'projects'
  97. });
  98. return runner.runExternalSchematicAsync('@schematics/angular', 'application', Object.assign({ name: 'ngx-bootstrap' }, appOptions), workspaceTree);
  99. }
  100. exports.createTestApp = createTestApp;
  101. function removePackageJsonDependency(tree, dependencyName) {
  102. const packageContent = JSON.parse(index_1.getFileContent(tree, '/package.json'));
  103. delete packageContent.dependencies[dependencyName];
  104. tree.overwrite('/package.json', JSON.stringify(packageContent, null, 2));
  105. }
  106. exports.removePackageJsonDependency = removePackageJsonDependency;
  107. function addModuleImportToRootModule(host, moduleName, src, project) {
  108. const modulePath = ng_ast_utils_1.getAppModulePath(host, project_main_file_1.getProjectMainFile(project));
  109. addModuleImportToModule(host, modulePath, moduleName, src);
  110. }
  111. exports.addModuleImportToRootModule = addModuleImportToRootModule;
  112. function addModuleImportToModule(host, modulePath, moduleName, src) {
  113. const moduleSource = getSourceFile(host, modulePath);
  114. if (!moduleSource) {
  115. throw new schematics_1.SchematicsException(`Module not found: ${modulePath}`);
  116. }
  117. const changes = ast_utils_1.addImportToModule(moduleSource, modulePath, moduleName, src);
  118. const recorder = host.beginUpdate(modulePath);
  119. changes.forEach((change) => {
  120. if (change instanceof change_1.InsertChange) {
  121. recorder.insertLeft(change.pos, change.toAdd);
  122. }
  123. });
  124. host.commitUpdate(recorder);
  125. }
  126. exports.addModuleImportToModule = addModuleImportToModule;
  127. function getSourceFile(host, path) {
  128. const buffer = host.read(path);
  129. if (!buffer) {
  130. throw new schematics_1.SchematicsException(`Could not find file for path: ${path}`);
  131. }
  132. const content = buffer.toString();
  133. return ts.createSourceFile(path, content, ts.ScriptTarget.Latest, true);
  134. }
  135. exports.getSourceFile = getSourceFile;
  136. //# sourceMappingURL=index.js.map