node-module-engine-host.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. /**
  4. * @license
  5. * Copyright Google Inc. All Rights Reserved.
  6. *
  7. * Use of this source code is governed by an MIT-style license that can be
  8. * found in the LICENSE file at https://angular.io/license
  9. */
  10. const core_1 = require("@angular-devkit/core");
  11. const core = require("@angular-devkit/core/node");
  12. const path_1 = require("path");
  13. const export_ref_1 = require("./export-ref");
  14. const file_system_engine_host_base_1 = require("./file-system-engine-host-base");
  15. const file_system_utility_1 = require("./file-system-utility");
  16. class NodePackageDoesNotSupportSchematics extends core_1.BaseException {
  17. constructor(name) {
  18. super(`Package ${JSON.stringify(name)} was found but does not support schematics.`);
  19. }
  20. }
  21. exports.NodePackageDoesNotSupportSchematics = NodePackageDoesNotSupportSchematics;
  22. /**
  23. * A simple EngineHost that uses NodeModules to resolve collections.
  24. */
  25. class NodeModulesEngineHost extends file_system_engine_host_base_1.FileSystemEngineHostBase {
  26. constructor() { super(); }
  27. _resolvePackageJson(name, basedir = process.cwd()) {
  28. return core.resolve(name, {
  29. basedir,
  30. checkLocal: true,
  31. checkGlobal: true,
  32. resolvePackageJson: true,
  33. });
  34. }
  35. _resolvePath(name, basedir = process.cwd()) {
  36. // Allow relative / absolute paths.
  37. if (name.startsWith('.') || name.startsWith('/')) {
  38. return path_1.resolve(basedir, name);
  39. }
  40. else {
  41. // If it's a file inside a package, resolve the package then return the file...
  42. if (name.split('/').length > (name[0] == '@' ? 2 : 1)) {
  43. const rest = name.split('/');
  44. const packageName = rest.shift() + (name[0] == '@' ? '/' + rest.shift() : '');
  45. return path_1.resolve(core.resolve(packageName, {
  46. basedir,
  47. checkLocal: true,
  48. checkGlobal: true,
  49. resolvePackageJson: true,
  50. }), '..', ...rest);
  51. }
  52. return core.resolve(name, {
  53. basedir,
  54. checkLocal: true,
  55. checkGlobal: true,
  56. });
  57. }
  58. }
  59. _resolveCollectionPath(name) {
  60. let collectionPath = undefined;
  61. if (name.replace(/\\/g, '/').split('/').length > (name[0] == '@' ? 2 : 1)) {
  62. try {
  63. collectionPath = this._resolvePath(name, process.cwd());
  64. }
  65. catch (_a) {
  66. }
  67. }
  68. if (!collectionPath) {
  69. let packageJsonPath = this._resolvePackageJson(name, process.cwd());
  70. // If it's a file, use it as is. Otherwise append package.json to it.
  71. if (!core.fs.isFile(packageJsonPath)) {
  72. packageJsonPath = path_1.join(packageJsonPath, 'package.json');
  73. }
  74. const pkgJsonSchematics = require(packageJsonPath)['schematics'];
  75. if (!pkgJsonSchematics || typeof pkgJsonSchematics != 'string') {
  76. throw new NodePackageDoesNotSupportSchematics(name);
  77. }
  78. collectionPath = this._resolvePath(pkgJsonSchematics, path_1.dirname(packageJsonPath));
  79. }
  80. try {
  81. if (collectionPath) {
  82. file_system_utility_1.readJsonFile(collectionPath);
  83. return collectionPath;
  84. }
  85. }
  86. catch (e) {
  87. if (e instanceof core_1.InvalidJsonCharacterException || e instanceof core_1.UnexpectedEndOfInputException) {
  88. throw new file_system_engine_host_base_1.InvalidCollectionJsonException(name, collectionPath, e);
  89. }
  90. }
  91. throw new file_system_engine_host_base_1.CollectionCannotBeResolvedException(name);
  92. }
  93. _resolveReferenceString(refString, parentPath) {
  94. const ref = new export_ref_1.ExportStringRef(refString, parentPath);
  95. if (!ref.ref) {
  96. return null;
  97. }
  98. return { ref: ref.ref, path: ref.module };
  99. }
  100. _transformCollectionDescription(name, desc) {
  101. if (!desc.schematics || typeof desc.schematics != 'object') {
  102. throw new file_system_engine_host_base_1.CollectionMissingSchematicsMapException(name);
  103. }
  104. return {
  105. ...desc,
  106. name,
  107. };
  108. }
  109. _transformSchematicDescription(name, _collection, desc) {
  110. if (!desc.factoryFn || !desc.path || !desc.description) {
  111. throw new file_system_engine_host_base_1.SchematicMissingFieldsException(name);
  112. }
  113. return desc;
  114. }
  115. }
  116. exports.NodeModulesEngineHost = NodeModulesEngineHost;