schematic-options.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const config_1 = require("../../utility/config");
  4. const json_utils_1 = require("../../utility/json-utils");
  5. const utils_1 = require("./utils");
  6. function default_1() {
  7. return tree => {
  8. const workspacePath = config_1.getWorkspacePath(tree);
  9. const workspace = utils_1.getWorkspace(tree);
  10. const recorder = tree.beginUpdate(workspacePath);
  11. const rootSchematics = findSchematicsField(workspace);
  12. if (rootSchematics) {
  13. updateSchematicsField(rootSchematics, recorder);
  14. }
  15. const projects = json_utils_1.findPropertyInAstObject(workspace, 'projects');
  16. if (!projects || projects.kind !== 'object' || !projects.properties) {
  17. return;
  18. }
  19. for (const { value } of projects.properties) {
  20. if (value.kind !== 'object') {
  21. continue;
  22. }
  23. const projectSchematics = findSchematicsField(value);
  24. if (!projectSchematics) {
  25. continue;
  26. }
  27. updateSchematicsField(projectSchematics, recorder);
  28. }
  29. tree.commitUpdate(recorder);
  30. return tree;
  31. };
  32. }
  33. exports.default = default_1;
  34. function findSchematicsField(value) {
  35. const schematics = json_utils_1.findPropertyInAstObject(value, 'schematics');
  36. if (schematics && schematics.kind == 'object') {
  37. return schematics;
  38. }
  39. return null;
  40. }
  41. function updateSchematicsField(schematics, recorder) {
  42. for (const { key: { value: schematicName }, value: schematicValue, } of schematics.properties) {
  43. if (schematicValue.kind !== 'object') {
  44. continue;
  45. }
  46. if (!schematicName.startsWith('@schematics/angular:')) {
  47. continue;
  48. }
  49. for (const { key: optionKey, value: optionValue } of schematicValue.properties) {
  50. if (optionKey.value === 'styleext') {
  51. // Rename `styleext` to `style
  52. const offset = optionKey.start.offset + 1;
  53. recorder.remove(offset, optionKey.value.length);
  54. recorder.insertLeft(offset, 'style');
  55. }
  56. else if (optionKey.value === 'spec') {
  57. // Rename `spec` to `skipTests`
  58. const offset = optionKey.start.offset + 1;
  59. recorder.remove(offset, optionKey.value.length);
  60. recorder.insertLeft(offset, 'skipTests');
  61. // invert value
  62. const { start, end } = optionValue;
  63. recorder.remove(start.offset, end.offset - start.offset);
  64. recorder.insertLeft(start.offset, `${!optionValue.value}`);
  65. }
  66. }
  67. }
  68. }