file-system-engine-host-base.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 node_1 = require("@angular-devkit/core/node");
  12. const fs_1 = require("fs");
  13. const path_1 = require("path");
  14. const rxjs_1 = require("rxjs");
  15. const operators_1 = require("rxjs/operators");
  16. const src_1 = require("../src");
  17. const file_system_utility_1 = require("./file-system-utility");
  18. class CollectionCannotBeResolvedException extends core_1.BaseException {
  19. constructor(name) {
  20. super(`Collection ${JSON.stringify(name)} cannot be resolved.`);
  21. }
  22. }
  23. exports.CollectionCannotBeResolvedException = CollectionCannotBeResolvedException;
  24. class InvalidCollectionJsonException extends core_1.BaseException {
  25. constructor(_name, path, jsonException) {
  26. let msg = `Collection JSON at path ${JSON.stringify(path)} is invalid.`;
  27. if (jsonException) {
  28. msg = `${msg} ${jsonException.message}`;
  29. }
  30. super(msg);
  31. }
  32. }
  33. exports.InvalidCollectionJsonException = InvalidCollectionJsonException;
  34. class SchematicMissingFactoryException extends core_1.BaseException {
  35. constructor(name) {
  36. super(`Schematic ${JSON.stringify(name)} is missing a factory.`);
  37. }
  38. }
  39. exports.SchematicMissingFactoryException = SchematicMissingFactoryException;
  40. class FactoryCannotBeResolvedException extends core_1.BaseException {
  41. constructor(name) {
  42. super(`Schematic ${JSON.stringify(name)} cannot resolve the factory.`);
  43. }
  44. }
  45. exports.FactoryCannotBeResolvedException = FactoryCannotBeResolvedException;
  46. class CollectionMissingSchematicsMapException extends core_1.BaseException {
  47. constructor(name) { super(`Collection "${name}" does not have a schematics map.`); }
  48. }
  49. exports.CollectionMissingSchematicsMapException = CollectionMissingSchematicsMapException;
  50. class CollectionMissingFieldsException extends core_1.BaseException {
  51. constructor(name) { super(`Collection "${name}" is missing fields.`); }
  52. }
  53. exports.CollectionMissingFieldsException = CollectionMissingFieldsException;
  54. class SchematicMissingFieldsException extends core_1.BaseException {
  55. constructor(name) { super(`Schematic "${name}" is missing fields.`); }
  56. }
  57. exports.SchematicMissingFieldsException = SchematicMissingFieldsException;
  58. class SchematicMissingDescriptionException extends core_1.BaseException {
  59. constructor(name) { super(`Schematics "${name}" does not have a description.`); }
  60. }
  61. exports.SchematicMissingDescriptionException = SchematicMissingDescriptionException;
  62. class SchematicNameCollisionException extends core_1.BaseException {
  63. constructor(name) {
  64. super(`Schematics/alias ${JSON.stringify(name)} collides with another alias or schematic`
  65. + ' name.');
  66. }
  67. }
  68. exports.SchematicNameCollisionException = SchematicNameCollisionException;
  69. /**
  70. * A EngineHost base class that uses the file system to resolve collections. This is the base of
  71. * all other EngineHost provided by the tooling part of the Schematics library.
  72. */
  73. class FileSystemEngineHostBase {
  74. constructor() {
  75. this._transforms = [];
  76. this._contextTransforms = [];
  77. this._taskFactories = new Map();
  78. }
  79. /**
  80. * @deprecated Use `listSchematicNames`.
  81. */
  82. listSchematics(collection) {
  83. return this.listSchematicNames(collection.description);
  84. }
  85. listSchematicNames(collection) {
  86. const schematics = [];
  87. for (const key of Object.keys(collection.schematics)) {
  88. const schematic = collection.schematics[key];
  89. if (schematic.hidden || schematic.private) {
  90. continue;
  91. }
  92. // If extends is present without a factory it is an alias, do not return it
  93. // unless it is from another collection.
  94. if (!schematic.extends || schematic.factory) {
  95. schematics.push(key);
  96. }
  97. else if (schematic.extends && schematic.extends.indexOf(':') !== -1) {
  98. schematics.push(key);
  99. }
  100. }
  101. return schematics;
  102. }
  103. registerOptionsTransform(t) {
  104. this._transforms.push(t);
  105. }
  106. registerContextTransform(t) {
  107. this._contextTransforms.push(t);
  108. }
  109. /**
  110. *
  111. * @param name
  112. * @return {{path: string}}
  113. */
  114. createCollectionDescription(name) {
  115. const path = this._resolveCollectionPath(name);
  116. const jsonValue = file_system_utility_1.readJsonFile(path);
  117. if (!jsonValue || typeof jsonValue != 'object' || Array.isArray(jsonValue)) {
  118. throw new InvalidCollectionJsonException(name, path);
  119. }
  120. // normalize extends property to an array
  121. if (typeof jsonValue['extends'] === 'string') {
  122. jsonValue['extends'] = [jsonValue['extends']];
  123. }
  124. const description = this._transformCollectionDescription(name, {
  125. ...jsonValue,
  126. path,
  127. });
  128. if (!description || !description.name) {
  129. throw new InvalidCollectionJsonException(name, path);
  130. }
  131. // Validate aliases.
  132. const allNames = Object.keys(description.schematics);
  133. for (const schematicName of Object.keys(description.schematics)) {
  134. const aliases = description.schematics[schematicName].aliases || [];
  135. for (const alias of aliases) {
  136. if (allNames.indexOf(alias) != -1) {
  137. throw new SchematicNameCollisionException(alias);
  138. }
  139. }
  140. allNames.push(...aliases);
  141. }
  142. return description;
  143. }
  144. createSchematicDescription(name, collection) {
  145. // Resolve aliases first.
  146. for (const schematicName of Object.keys(collection.schematics)) {
  147. const schematicDescription = collection.schematics[schematicName];
  148. if (schematicDescription.aliases && schematicDescription.aliases.indexOf(name) != -1) {
  149. name = schematicName;
  150. break;
  151. }
  152. }
  153. if (!(name in collection.schematics)) {
  154. return null;
  155. }
  156. const collectionPath = path_1.dirname(collection.path);
  157. const partialDesc = collection.schematics[name];
  158. if (!partialDesc) {
  159. return null;
  160. }
  161. if (partialDesc.extends) {
  162. const index = partialDesc.extends.indexOf(':');
  163. const collectionName = index !== -1 ? partialDesc.extends.substr(0, index) : null;
  164. const schematicName = index === -1 ?
  165. partialDesc.extends : partialDesc.extends.substr(index + 1);
  166. if (collectionName !== null) {
  167. const extendCollection = this.createCollectionDescription(collectionName);
  168. return this.createSchematicDescription(schematicName, extendCollection);
  169. }
  170. else {
  171. return this.createSchematicDescription(schematicName, collection);
  172. }
  173. }
  174. // Use any on this ref as we don't have the OptionT here, but we don't need it (we only need
  175. // the path).
  176. if (!partialDesc.factory) {
  177. throw new SchematicMissingFactoryException(name);
  178. }
  179. const resolvedRef = this._resolveReferenceString(partialDesc.factory, collectionPath);
  180. if (!resolvedRef) {
  181. throw new FactoryCannotBeResolvedException(name);
  182. }
  183. let schema = partialDesc.schema;
  184. let schemaJson = undefined;
  185. if (schema) {
  186. if (!path_1.isAbsolute(schema)) {
  187. schema = path_1.join(collectionPath, schema);
  188. }
  189. schemaJson = file_system_utility_1.readJsonFile(schema);
  190. }
  191. // The schematic path is used to resolve URLs.
  192. // We should be able to just do `dirname(resolvedRef.path)` but for compatibility with
  193. // Bazel under Windows this directory needs to be resolved from the collection instead.
  194. // This is needed because on Bazel under Windows the data files (such as the collection or
  195. // url files) are not in the same place as the compiled JS.
  196. const maybePath = path_1.join(collectionPath, partialDesc.factory);
  197. const path = fs_1.existsSync(maybePath) && fs_1.statSync(maybePath).isDirectory()
  198. ? maybePath : path_1.dirname(maybePath);
  199. return this._transformSchematicDescription(name, collection, {
  200. ...partialDesc,
  201. schema,
  202. schemaJson,
  203. name,
  204. path,
  205. factoryFn: resolvedRef.ref,
  206. collection,
  207. });
  208. }
  209. createSourceFromUrl(url) {
  210. switch (url.protocol) {
  211. case null:
  212. case 'file:':
  213. return (context) => {
  214. // Resolve all file:///a/b/c/d from the schematic's own path, and not the current
  215. // path.
  216. const root = core_1.normalize(path_1.resolve(context.schematic.description.path, url.path || ''));
  217. return new src_1.HostCreateTree(new core_1.virtualFs.ScopedHost(new node_1.NodeJsSyncHost(), root));
  218. };
  219. }
  220. return null;
  221. }
  222. transformOptions(schematic, options, context) {
  223. // tslint:disable-next-line:no-any https://github.com/ReactiveX/rxjs/issues/3989
  224. return (rxjs_1.of(options)
  225. .pipe(...this._transforms.map(tFn => operators_1.mergeMap((opt) => {
  226. const newOptions = tFn(schematic, opt, context);
  227. if (rxjs_1.isObservable(newOptions)) {
  228. return newOptions;
  229. }
  230. else if (core_1.isPromise(newOptions)) {
  231. return rxjs_1.from(newOptions);
  232. }
  233. else {
  234. return rxjs_1.of(newOptions);
  235. }
  236. }))));
  237. }
  238. transformContext(context) {
  239. // tslint:disable-next-line:no-any https://github.com/ReactiveX/rxjs/issues/3989
  240. return this._contextTransforms.reduce((acc, curr) => curr(acc), context);
  241. }
  242. getSchematicRuleFactory(schematic, _collection) {
  243. return schematic.factoryFn;
  244. }
  245. registerTaskExecutor(factory, options) {
  246. this._taskFactories.set(factory.name, () => rxjs_1.from(factory.create(options)));
  247. }
  248. createTaskExecutor(name) {
  249. const factory = this._taskFactories.get(name);
  250. if (factory) {
  251. return factory();
  252. }
  253. return rxjs_1.throwError(new src_1.UnregisteredTaskException(name));
  254. }
  255. hasTaskExecutor(name) {
  256. return this._taskFactories.has(name);
  257. }
  258. }
  259. exports.FileSystemEngineHostBase = FileSystemEngineHostBase;