index.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const schematics_1 = require("@angular-devkit/schematics");
  4. const semver = require("semver");
  5. /**
  6. * Cleans up "short" version numbers so they become valid semver. For example;
  7. * 1 => 1.0.0
  8. * 1.2 => 1.2.0
  9. * 1-beta => 1.0.0-beta
  10. *
  11. * Exported for testing only.
  12. */
  13. function _coerceVersionNumber(version) {
  14. if (!version.match(/^\d{1,30}\.\d{1,30}\.\d{1,30}/)) {
  15. const match = version.match(/^\d{1,30}(\.\d{1,30})*/);
  16. if (!match) {
  17. return null;
  18. }
  19. if (!match[1]) {
  20. version = version.substr(0, match[0].length) + '.0.0' + version.substr(match[0].length);
  21. }
  22. else if (!match[2]) {
  23. version = version.substr(0, match[0].length) + '.0' + version.substr(match[0].length);
  24. }
  25. else {
  26. return null;
  27. }
  28. }
  29. return semver.valid(version);
  30. }
  31. exports._coerceVersionNumber = _coerceVersionNumber;
  32. function default_1(options) {
  33. return (tree, context) => {
  34. const schematicsToRun = [];
  35. const from = _coerceVersionNumber(options.from);
  36. if (!from) {
  37. throw new schematics_1.SchematicsException(`Invalid from option: ${JSON.stringify(options.from)}`);
  38. }
  39. const to = semver.validRange('<=' + options.to);
  40. if (!to) {
  41. throw new schematics_1.SchematicsException(`Invalid to option: ${JSON.stringify(options.to)}`);
  42. }
  43. // Create the collection for the package.
  44. const collection = context.engine.createCollection(options.collection);
  45. for (const name of collection.listSchematicNames()) {
  46. const schematic = collection.createSchematic(name, true);
  47. const description = schematic.description;
  48. let version = description['version'];
  49. if (typeof version == 'string') {
  50. version = _coerceVersionNumber(version);
  51. if (!version) {
  52. throw new schematics_1.SchematicsException(`Invalid migration version: ${JSON.stringify(description['version'])}`);
  53. }
  54. if (semver.gt(version, from) &&
  55. semver.satisfies(version, to, { includePrerelease: true })) {
  56. schematicsToRun.push({ name, version });
  57. }
  58. }
  59. }
  60. schematicsToRun.sort((a, b) => {
  61. const cmp = semver.compare(a.version, b.version);
  62. // Revert to comparing the names of the collection if the versions are equal.
  63. return cmp == 0 ? a.name.localeCompare(b.name) : cmp;
  64. });
  65. if (schematicsToRun.length > 0) {
  66. context.logger.info(`** Executing migrations for package '${options.package}' **`);
  67. const rules = schematicsToRun.map(x => schematics_1.externalSchematic(options.collection, x.name, {}));
  68. return schematics_1.chain(rules);
  69. }
  70. return tree;
  71. };
  72. }
  73. exports.default = default_1;