@ionic/angular 7.5.5-dev.11699974376.13a15397 → 7.5.5-dev.11699997410.16566960

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ionic/angular",
3
- "version": "7.5.5-dev.11699974376.13a15397",
3
+ "version": "7.5.5-dev.11699997410.16566960",
4
4
  "description": "Angular specific wrappers for @ionic/core",
5
5
  "keywords": [
6
6
  "ionic",
@@ -56,7 +56,7 @@
56
56
  }
57
57
  },
58
58
  "dependencies": {
59
- "@ionic/core": "7.5.5-dev.11699974376.13a15397",
59
+ "@ionic/core": "7.5.5-dev.11699997410.16566960",
60
60
  "ionicons": "^7.0.0",
61
61
  "jsonc-parser": "^3.0.0",
62
62
  "tslib": "^2.3.0"
@@ -4,3 +4,244 @@
4
4
 
5
5
  /* To quickly generate your own theme, check out the color generator */
6
6
  /* https://ionicframework.com/docs/theming/color-generator */
7
+
8
+ /** Ionic CSS Variables **/
9
+ :root {
10
+ /** primary **/
11
+ --ion-color-primary: #3880ff;
12
+ --ion-color-primary-rgb: 56, 128, 255;
13
+ --ion-color-primary-contrast: #ffffff;
14
+ --ion-color-primary-contrast-rgb: 255, 255, 255;
15
+ --ion-color-primary-shade: #3171e0;
16
+ --ion-color-primary-tint: #4c8dff;
17
+
18
+ /** secondary **/
19
+ --ion-color-secondary: #3dc2ff;
20
+ --ion-color-secondary-rgb: 61, 194, 255;
21
+ --ion-color-secondary-contrast: #ffffff;
22
+ --ion-color-secondary-contrast-rgb: 255, 255, 255;
23
+ --ion-color-secondary-shade: #36abe0;
24
+ --ion-color-secondary-tint: #50c8ff;
25
+
26
+ /** tertiary **/
27
+ --ion-color-tertiary: #5260ff;
28
+ --ion-color-tertiary-rgb: 82, 96, 255;
29
+ --ion-color-tertiary-contrast: #ffffff;
30
+ --ion-color-tertiary-contrast-rgb: 255, 255, 255;
31
+ --ion-color-tertiary-shade: #4854e0;
32
+ --ion-color-tertiary-tint: #6370ff;
33
+
34
+ /** success **/
35
+ --ion-color-success: #2dd36f;
36
+ --ion-color-success-rgb: 45, 211, 111;
37
+ --ion-color-success-contrast: #ffffff;
38
+ --ion-color-success-contrast-rgb: 255, 255, 255;
39
+ --ion-color-success-shade: #28ba62;
40
+ --ion-color-success-tint: #42d77d;
41
+
42
+ /** warning **/
43
+ --ion-color-warning: #ffc409;
44
+ --ion-color-warning-rgb: 255, 196, 9;
45
+ --ion-color-warning-contrast: #000000;
46
+ --ion-color-warning-contrast-rgb: 0, 0, 0;
47
+ --ion-color-warning-shade: #e0ac08;
48
+ --ion-color-warning-tint: #ffca22;
49
+
50
+ /** danger **/
51
+ --ion-color-danger: #eb445a;
52
+ --ion-color-danger-rgb: 235, 68, 90;
53
+ --ion-color-danger-contrast: #ffffff;
54
+ --ion-color-danger-contrast-rgb: 255, 255, 255;
55
+ --ion-color-danger-shade: #cf3c4f;
56
+ --ion-color-danger-tint: #ed576b;
57
+
58
+ /** dark **/
59
+ --ion-color-dark: #222428;
60
+ --ion-color-dark-rgb: 34, 36, 40;
61
+ --ion-color-dark-contrast: #ffffff;
62
+ --ion-color-dark-contrast-rgb: 255, 255, 255;
63
+ --ion-color-dark-shade: #1e2023;
64
+ --ion-color-dark-tint: #383a3e;
65
+
66
+ /** medium **/
67
+ --ion-color-medium: #92949c;
68
+ --ion-color-medium-rgb: 146, 148, 156;
69
+ --ion-color-medium-contrast: #ffffff;
70
+ --ion-color-medium-contrast-rgb: 255, 255, 255;
71
+ --ion-color-medium-shade: #808289;
72
+ --ion-color-medium-tint: #9d9fa6;
73
+
74
+ /** light **/
75
+ --ion-color-light: #f4f5f8;
76
+ --ion-color-light-rgb: 244, 245, 248;
77
+ --ion-color-light-contrast: #000000;
78
+ --ion-color-light-contrast-rgb: 0, 0, 0;
79
+ --ion-color-light-shade: #d7d8da;
80
+ --ion-color-light-tint: #f5f6f9;
81
+ }
82
+
83
+ @media (prefers-color-scheme: dark) {
84
+ /*
85
+ * Dark Colors
86
+ * -------------------------------------------
87
+ */
88
+
89
+ body {
90
+ --ion-color-primary: #428cff;
91
+ --ion-color-primary-rgb: 66, 140, 255;
92
+ --ion-color-primary-contrast: #ffffff;
93
+ --ion-color-primary-contrast-rgb: 255, 255, 255;
94
+ --ion-color-primary-shade: #3a7be0;
95
+ --ion-color-primary-tint: #5598ff;
96
+
97
+ --ion-color-secondary: #50c8ff;
98
+ --ion-color-secondary-rgb: 80, 200, 255;
99
+ --ion-color-secondary-contrast: #ffffff;
100
+ --ion-color-secondary-contrast-rgb: 255, 255, 255;
101
+ --ion-color-secondary-shade: #46b0e0;
102
+ --ion-color-secondary-tint: #62ceff;
103
+
104
+ --ion-color-tertiary: #6a64ff;
105
+ --ion-color-tertiary-rgb: 106, 100, 255;
106
+ --ion-color-tertiary-contrast: #ffffff;
107
+ --ion-color-tertiary-contrast-rgb: 255, 255, 255;
108
+ --ion-color-tertiary-shade: #5d58e0;
109
+ --ion-color-tertiary-tint: #7974ff;
110
+
111
+ --ion-color-success: #2fdf75;
112
+ --ion-color-success-rgb: 47, 223, 117;
113
+ --ion-color-success-contrast: #000000;
114
+ --ion-color-success-contrast-rgb: 0, 0, 0;
115
+ --ion-color-success-shade: #29c467;
116
+ --ion-color-success-tint: #44e283;
117
+
118
+ --ion-color-warning: #ffd534;
119
+ --ion-color-warning-rgb: 255, 213, 52;
120
+ --ion-color-warning-contrast: #000000;
121
+ --ion-color-warning-contrast-rgb: 0, 0, 0;
122
+ --ion-color-warning-shade: #e0bb2e;
123
+ --ion-color-warning-tint: #ffd948;
124
+
125
+ --ion-color-danger: #ff4961;
126
+ --ion-color-danger-rgb: 255, 73, 97;
127
+ --ion-color-danger-contrast: #ffffff;
128
+ --ion-color-danger-contrast-rgb: 255, 255, 255;
129
+ --ion-color-danger-shade: #e04055;
130
+ --ion-color-danger-tint: #ff5b71;
131
+
132
+ --ion-color-dark: #f4f5f8;
133
+ --ion-color-dark-rgb: 244, 245, 248;
134
+ --ion-color-dark-contrast: #000000;
135
+ --ion-color-dark-contrast-rgb: 0, 0, 0;
136
+ --ion-color-dark-shade: #d7d8da;
137
+ --ion-color-dark-tint: #f5f6f9;
138
+
139
+ --ion-color-medium: #989aa2;
140
+ --ion-color-medium-rgb: 152, 154, 162;
141
+ --ion-color-medium-contrast: #000000;
142
+ --ion-color-medium-contrast-rgb: 0, 0, 0;
143
+ --ion-color-medium-shade: #86888f;
144
+ --ion-color-medium-tint: #a2a4ab;
145
+
146
+ --ion-color-light: #222428;
147
+ --ion-color-light-rgb: 34, 36, 40;
148
+ --ion-color-light-contrast: #ffffff;
149
+ --ion-color-light-contrast-rgb: 255, 255, 255;
150
+ --ion-color-light-shade: #1e2023;
151
+ --ion-color-light-tint: #383a3e;
152
+ }
153
+
154
+ /*
155
+ * iOS Dark Theme
156
+ * -------------------------------------------
157
+ */
158
+
159
+ .ios body {
160
+ --ion-background-color: #000000;
161
+ --ion-background-color-rgb: 0, 0, 0;
162
+
163
+ --ion-text-color: #ffffff;
164
+ --ion-text-color-rgb: 255, 255, 255;
165
+
166
+ --ion-color-step-50: #0d0d0d;
167
+ --ion-color-step-100: #1a1a1a;
168
+ --ion-color-step-150: #262626;
169
+ --ion-color-step-200: #333333;
170
+ --ion-color-step-250: #404040;
171
+ --ion-color-step-300: #4d4d4d;
172
+ --ion-color-step-350: #595959;
173
+ --ion-color-step-400: #666666;
174
+ --ion-color-step-450: #737373;
175
+ --ion-color-step-500: #808080;
176
+ --ion-color-step-550: #8c8c8c;
177
+ --ion-color-step-600: #999999;
178
+ --ion-color-step-650: #a6a6a6;
179
+ --ion-color-step-700: #b3b3b3;
180
+ --ion-color-step-750: #bfbfbf;
181
+ --ion-color-step-800: #cccccc;
182
+ --ion-color-step-850: #d9d9d9;
183
+ --ion-color-step-900: #e6e6e6;
184
+ --ion-color-step-950: #f2f2f2;
185
+
186
+ --ion-item-background: #000000;
187
+
188
+ --ion-card-background: #1c1c1d;
189
+ }
190
+
191
+ .ios ion-modal {
192
+ --ion-background-color: var(--ion-color-step-100);
193
+ --ion-toolbar-background: var(--ion-color-step-150);
194
+ --ion-toolbar-border-color: var(--ion-color-step-250);
195
+ }
196
+
197
+ /*
198
+ * Material Design Dark Theme
199
+ * -------------------------------------------
200
+ */
201
+
202
+ .md body {
203
+ --ion-background-color: #121212;
204
+ --ion-background-color-rgb: 18, 18, 18;
205
+
206
+ --ion-text-color: #ffffff;
207
+ --ion-text-color-rgb: 255, 255, 255;
208
+
209
+ --ion-border-color: #222222;
210
+
211
+ --ion-color-step-50: #1e1e1e;
212
+ --ion-color-step-100: #2a2a2a;
213
+ --ion-color-step-150: #363636;
214
+ --ion-color-step-200: #414141;
215
+ --ion-color-step-250: #4d4d4d;
216
+ --ion-color-step-300: #595959;
217
+ --ion-color-step-350: #656565;
218
+ --ion-color-step-400: #717171;
219
+ --ion-color-step-450: #7d7d7d;
220
+ --ion-color-step-500: #898989;
221
+ --ion-color-step-550: #949494;
222
+ --ion-color-step-600: #a0a0a0;
223
+ --ion-color-step-650: #acacac;
224
+ --ion-color-step-700: #b8b8b8;
225
+ --ion-color-step-750: #c4c4c4;
226
+ --ion-color-step-800: #d0d0d0;
227
+ --ion-color-step-850: #dbdbdb;
228
+ --ion-color-step-900: #e7e7e7;
229
+ --ion-color-step-950: #f3f3f3;
230
+
231
+ --ion-item-background: #1e1e1e;
232
+
233
+ --ion-toolbar-background: #1f1f1f;
234
+
235
+ --ion-tab-bar-background: #1f1f1f;
236
+
237
+ --ion-card-background: #1e1e1e;
238
+ }
239
+ }
240
+
241
+ html {
242
+ /*
243
+ * For more information on dynamic font scaling, visit the documentation:
244
+ * https://ionicframework.com/docs/layout/dynamic-font-scaling
245
+ */
246
+ --ion-dynamic-font: var(--ion-default-dynamic-font);
247
+ }
@@ -3,8 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const core_1 = require("@angular-devkit/core");
4
4
  const schematics_1 = require("@angular-devkit/schematics");
5
5
  const tasks_1 = require("@angular-devkit/schematics/tasks");
6
+ const utility_1 = require("@schematics/angular/utility");
6
7
  const workspace_1 = require("@schematics/angular/utility/workspace");
7
- const ast_1 = require("./../utils/ast");
8
+ const ast_1 = require("../utils/ast");
8
9
  const config_1 = require("./../utils/config");
9
10
  const package_1 = require("./../utils/package");
10
11
  function addIonicAngularToPackageJson() {
@@ -19,9 +20,48 @@ function addIonicAngularToolkitToPackageJson() {
19
20
  return host;
20
21
  };
21
22
  }
23
+ /**
24
+ * Adds the @ionic/angular-toolkit schematics and cli configuration to the project's `angular.json` file.
25
+ * @param projectName The name of the project.
26
+ */
27
+ function addIonicAngularToolkitToAngularJson() {
28
+ return (host) => {
29
+ (0, config_1.addCli)(host, '@ionic/angular-toolkit');
30
+ (0, config_1.addSchematics)(host, '@ionic/angular-toolkit:component', {
31
+ styleext: 'scss',
32
+ });
33
+ (0, config_1.addSchematics)(host, '@ionic/angular-toolkit:page', {
34
+ styleext: 'scss',
35
+ });
36
+ return host;
37
+ };
38
+ }
39
+ /**
40
+ * Adds the `IonicModule.forRoot()` usage to the project's `AppModule`.
41
+ * If the project does not use modules this will operate as a noop.
42
+ * @param projectSourceRoot The source root path of the project.
43
+ */
22
44
  function addIonicAngularModuleToAppModule(projectSourceRoot) {
23
45
  return (host) => {
24
- (0, ast_1.addModuleImportToRootModule)(host, projectSourceRoot, 'IonicModule.forRoot()', '@ionic/angular');
46
+ const appModulePath = `${projectSourceRoot}/app/app.module.ts`;
47
+ if (host.exists(appModulePath)) {
48
+ (0, ast_1.addIonicModuleImportToNgModule)(host, appModulePath);
49
+ }
50
+ return host;
51
+ };
52
+ }
53
+ /**
54
+ * Adds the `provideIonicAngular` usage to the project's app config.
55
+ * If the project does not use an app config this will operate as a noop.
56
+ * @param projectName The name of the project.
57
+ * @param projectSourceRoot The source root path of the project.
58
+ */
59
+ function addProvideIonicAngular(projectName, projectSourceRoot) {
60
+ return (host) => {
61
+ const appConfig = `${projectSourceRoot}/app/app.config.ts`;
62
+ if (host.exists(appConfig)) {
63
+ return (0, utility_1.addRootProvider)(projectName, ({ code, external }) => code `${external('provideIonicAngular', '@ionic/angular/standalone')}({})`);
64
+ }
25
65
  return host;
26
66
  };
27
67
  }
@@ -46,15 +86,36 @@ function addIonicStyles(projectName, projectSourceRoot) {
46
86
  return host;
47
87
  };
48
88
  }
49
- function addIonicons(projectName) {
89
+ function addIonicons(projectName, projectSourceRoot) {
50
90
  return (host) => {
51
- const ioniconsGlob = {
52
- glob: '**/*.svg',
53
- input: 'node_modules/ionicons/dist/ionicons/svg',
54
- output: './svg',
55
- };
56
- (0, config_1.addAsset)(host, projectName, 'build', ioniconsGlob);
57
- (0, config_1.addAsset)(host, projectName, 'test', ioniconsGlob);
91
+ const hasAppModule = host.exists(`${projectSourceRoot}/app/app.module.ts`);
92
+ if (hasAppModule) {
93
+ /**
94
+ * Add Ionicons to the `angular.json` file only if the project
95
+ * is using the lazy build of `@ionic/angular` with modules.
96
+ */
97
+ const ioniconsGlob = {
98
+ glob: '**/*.svg',
99
+ input: 'node_modules/ionicons/dist/ionicons/svg',
100
+ output: './svg',
101
+ };
102
+ (0, config_1.addAsset)(host, projectName, 'build', ioniconsGlob);
103
+ (0, config_1.addAsset)(host, projectName, 'test', ioniconsGlob);
104
+ }
105
+ return host;
106
+ };
107
+ }
108
+ function addIonicConfig() {
109
+ return (host) => {
110
+ const ionicConfig = `ionic.config.json`;
111
+ if (!host.exists(ionicConfig)) {
112
+ host.create(ionicConfig, JSON.stringify({
113
+ name: 'ionic-app',
114
+ app_id: '',
115
+ type: 'angular',
116
+ integrations: {},
117
+ }, null, 2));
118
+ }
58
119
  return host;
59
120
  };
60
121
  }
@@ -108,10 +169,13 @@ function ngAdd(options) {
108
169
  // @ionic/angular
109
170
  addIonicAngularToPackageJson(),
110
171
  addIonicAngularToolkitToPackageJson(),
172
+ addIonicAngularToolkitToAngularJson(),
111
173
  addIonicAngularModuleToAppModule(sourcePath),
174
+ addProvideIonicAngular(options.project, sourcePath),
112
175
  addIonicBuilder(options.project),
113
176
  addIonicStyles(options.project, sourcePath),
114
- addIonicons(options.project),
177
+ addIonicons(options.project, sourcePath),
178
+ addIonicConfig(),
115
179
  (0, schematics_1.mergeWith)(rootTemplateSource),
116
180
  // install freshly added dependencies
117
181
  installNodeDeps(),
@@ -1,11 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.addModuleImportToModule = exports.addModuleImportToRootModule = exports.getSourceFile = void 0;
4
- const core_1 = require("@angular-devkit/core");
3
+ exports.addIonicModuleImportToNgModule = void 0;
5
4
  const schematics_1 = require("@angular-devkit/schematics");
5
+ const ast_utils_1 = require("@schematics/angular/utility/ast-utils");
6
+ const change_1 = require("@schematics/angular/utility/change");
6
7
  const ts = require("typescript");
7
- const ast_utils_1 = require("./devkit-utils/ast-utils");
8
- const change_1 = require("./devkit-utils/change");
9
8
  /**
10
9
  * Reads file given path and returns TypeScript source file.
11
10
  */
@@ -18,30 +17,16 @@ function getSourceFile(host, path) {
18
17
  const source = ts.createSourceFile(path, content, ts.ScriptTarget.Latest, true);
19
18
  return source;
20
19
  }
21
- exports.getSourceFile = getSourceFile;
22
20
  /**
23
21
  * Import and add module to root app module.
24
22
  */
25
- function addModuleImportToRootModule(host, projectSourceRoot, moduleName, importSrc) {
26
- addModuleImportToModule(host, (0, core_1.normalize)(`${projectSourceRoot}/app/app.module.ts`), moduleName, importSrc);
27
- }
28
- exports.addModuleImportToRootModule = addModuleImportToRootModule;
29
- /**
30
- * Import and add module to specific module path.
31
- * @param host the tree we are updating
32
- * @param modulePath src location of the module to import
33
- * @param moduleName name of module to import
34
- * @param src src location to import
35
- */
36
- function addModuleImportToModule(host, modulePath, moduleName, src) {
37
- const moduleSource = getSourceFile(host, modulePath);
38
- const changes = (0, ast_utils_1.addImportToModule)(moduleSource, modulePath, moduleName, src);
23
+ function addIonicModuleImportToNgModule(host, modulePath) {
39
24
  const recorder = host.beginUpdate(modulePath);
40
- changes.forEach((change) => {
41
- if (change instanceof change_1.InsertChange) {
42
- recorder.insertLeft(change.pos, change.toAdd);
43
- }
44
- });
25
+ const moduleSource = getSourceFile(host, modulePath);
26
+ const ionicModuleChange = (0, ast_utils_1.insertImport)(moduleSource, modulePath, 'IonicModule', '@ionic/angular');
27
+ (0, change_1.applyToUpdateRecorder)(recorder, [ionicModuleChange]);
28
+ const metadataChange = (0, ast_utils_1.addSymbolToNgModuleMetadata)(moduleSource, modulePath, 'imports', 'IonicModule.forRoot({})');
29
+ (0, change_1.applyToUpdateRecorder)(recorder, metadataChange);
45
30
  host.commitUpdate(recorder);
46
31
  }
47
- exports.addModuleImportToModule = addModuleImportToModule;
32
+ exports.addIonicModuleImportToNgModule = addIonicModuleImportToNgModule;
@@ -1,24 +1,25 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getWorkspace = exports.getWorkspacePath = exports.addArchitectBuilder = exports.addAsset = exports.addStyle = exports.getAngularAppConfig = exports.getDefaultAngularAppName = exports.writeConfig = exports.readConfig = void 0;
3
+ exports.getWorkspace = exports.getWorkspacePath = exports.addSchematics = exports.addCli = exports.addArchitectBuilder = exports.addAsset = exports.addStyle = exports.getDefaultAngularAppName = exports.writeConfig = exports.readConfig = void 0;
4
4
  const schematics_1 = require("@angular-devkit/schematics");
5
5
  const jsonc_parser_1 = require("jsonc-parser");
6
- const CONFIG_PATH = 'angular.json';
7
- // TODO(FW-2827): types
6
+ const ANGULAR_JSON_PATH = 'angular.json';
8
7
  function readConfig(host) {
9
- var _a;
10
- const sourceText = (_a = host.read(CONFIG_PATH)) === null || _a === void 0 ? void 0 : _a.toString('utf-8');
11
- return JSON.parse(sourceText);
8
+ return host.readJson(ANGULAR_JSON_PATH);
12
9
  }
13
10
  exports.readConfig = readConfig;
14
11
  function writeConfig(host, config) {
15
- host.overwrite(CONFIG_PATH, JSON.stringify(config, null, 2));
12
+ host.overwrite(ANGULAR_JSON_PATH, JSON.stringify(config, null, 2));
16
13
  }
17
14
  exports.writeConfig = writeConfig;
18
15
  function isAngularBrowserProject(projectConfig) {
19
16
  if (projectConfig.projectType === 'application') {
20
17
  const buildConfig = projectConfig.architect.build;
21
- return buildConfig.builder === '@angular-devkit/build-angular:browser';
18
+ // Angular 16 and lower
19
+ const legacyAngularBuilder = buildConfig.builder === '@angular-devkit/build-angular:browser';
20
+ // Angular 17+
21
+ const modernAngularBuilder = buildConfig.builder === '@angular-devkit/build-angular:application';
22
+ return legacyAngularBuilder || modernAngularBuilder;
22
23
  }
23
24
  return false;
24
25
  }
@@ -34,7 +35,7 @@ function getDefaultAngularAppName(config) {
34
35
  return projectNames[0];
35
36
  }
36
37
  exports.getDefaultAngularAppName = getDefaultAngularAppName;
37
- function getAngularAppConfig(config, projectName) {
38
+ function getAngularJson(config, projectName) {
38
39
  // eslint-disable-next-line no-prototype-builtins
39
40
  if (!config.projects.hasOwnProperty(projectName)) {
40
41
  throw new schematics_1.SchematicsException(`Could not find project: ${projectName}`);
@@ -51,11 +52,10 @@ function getAngularAppConfig(config, projectName) {
51
52
  throw new schematics_1.SchematicsException(`Invalid builder for ${projectName}: ${buildConfig.builder}`);
52
53
  }
53
54
  }
54
- exports.getAngularAppConfig = getAngularAppConfig;
55
55
  function addStyle(host, projectName, stylePath) {
56
56
  const config = readConfig(host);
57
- const appConfig = getAngularAppConfig(config, projectName);
58
- appConfig.architect.build.options.styles.push({
57
+ const angularJson = getAngularJson(config, projectName);
58
+ angularJson.architect.build.options.styles.push({
59
59
  input: stylePath,
60
60
  });
61
61
  writeConfig(host, config);
@@ -63,8 +63,8 @@ function addStyle(host, projectName, stylePath) {
63
63
  exports.addStyle = addStyle;
64
64
  function addAsset(host, projectName, architect, asset) {
65
65
  const config = readConfig(host);
66
- const appConfig = getAngularAppConfig(config, projectName);
67
- const target = appConfig.architect[architect];
66
+ const angularJson = getAngularJson(config, projectName);
67
+ const target = angularJson.architect[architect];
68
68
  if (target) {
69
69
  target.options.assets.push(asset);
70
70
  writeConfig(host, config);
@@ -73,11 +73,32 @@ function addAsset(host, projectName, architect, asset) {
73
73
  exports.addAsset = addAsset;
74
74
  function addArchitectBuilder(host, projectName, builderName, builderOpts) {
75
75
  const config = readConfig(host);
76
- const appConfig = getAngularAppConfig(config, projectName);
77
- appConfig.architect[builderName] = builderOpts;
76
+ const angularJson = getAngularJson(config, projectName);
77
+ angularJson.architect[builderName] = builderOpts;
78
78
  writeConfig(host, config);
79
79
  }
80
80
  exports.addArchitectBuilder = addArchitectBuilder;
81
+ function addCli(host, collectionName) {
82
+ const angularJson = readConfig(host);
83
+ if (angularJson.cli === undefined) {
84
+ angularJson.cli = {};
85
+ }
86
+ if (angularJson.cli.schematicCollections === undefined) {
87
+ angularJson.cli.schematicCollections = [];
88
+ }
89
+ angularJson.cli.schematicCollections.push(collectionName);
90
+ writeConfig(host, angularJson);
91
+ }
92
+ exports.addCli = addCli;
93
+ function addSchematics(host, schematicName, schematicOpts) {
94
+ const angularJson = readConfig(host);
95
+ if (angularJson.schematics === undefined) {
96
+ angularJson.schematics = {};
97
+ }
98
+ angularJson.schematics[schematicName] = schematicOpts;
99
+ writeConfig(host, angularJson);
100
+ }
101
+ exports.addSchematics = addSchematics;
81
102
  function getWorkspacePath(host) {
82
103
  const possibleFiles = ['/angular.json', '/.angular.json'];
83
104
  const path = possibleFiles.filter((path) => host.exists(path))[0];
@@ -1,488 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isImported = exports.addEntryComponentToModule = exports.addBootstrapToModule = exports.addExportToModule = exports.addProviderToModule = exports.addImportToModule = exports.addDeclarationToModule = exports.addSymbolToNgModuleMetadata = exports.getFirstNgModuleName = exports.getDecoratorMetadata = exports.getContentOfKeyLiteral = exports.insertAfterLastOccurrence = exports.findNode = exports.getSourceNodes = exports.findNodes = exports.insertImport = void 0;
4
- /**
5
- * @license
6
- * Copyright Google Inc. All Rights Reserved.
7
- *
8
- * Use of this source code is governed by an MIT-style license that can be
9
- * found in the LICENSE file at https://angular.io/license
10
- */
11
- const ts = require("typescript");
12
- const change_1 = require("./change");
13
- /**
14
- * Add Import `import { symbolName } from fileName` if the import doesn't exit
15
- * already. Assumes fileToEdit can be resolved and accessed.
16
- * @param fileToEdit (file we want to add import to)
17
- * @param symbolName (item to import)
18
- * @param fileName (path to the file)
19
- * @param isDefault (if true, import follows style for importing default exports)
20
- * @return Change
21
- */
22
- function insertImport(source, fileToEdit, symbolName, fileName, isDefault = false) {
23
- const rootNode = source;
24
- const allImports = findNodes(rootNode, ts.SyntaxKind.ImportDeclaration);
25
- // get nodes that map to import statements from the file fileName
26
- const relevantImports = allImports.filter((node) => {
27
- // StringLiteral of the ImportDeclaration is the import file (fileName in this case).
28
- const importFiles = node
29
- .getChildren()
30
- .filter((child) => child.kind === ts.SyntaxKind.StringLiteral)
31
- .map((n) => n.text);
32
- return importFiles.filter((file) => file === fileName).length === 1;
33
- });
34
- if (relevantImports.length > 0) {
35
- let importsAsterisk = false;
36
- // imports from import file
37
- const imports = [];
38
- relevantImports.forEach((n) => {
39
- Array.prototype.push.apply(imports, findNodes(n, ts.SyntaxKind.Identifier));
40
- if (findNodes(n, ts.SyntaxKind.AsteriskToken).length > 0) {
41
- importsAsterisk = true;
42
- }
43
- });
44
- // if imports * from fileName, don't add symbolName
45
- if (importsAsterisk) {
46
- return new change_1.NoopChange();
47
- }
48
- const importTextNodes = imports.filter((n) => n.text === symbolName);
49
- // insert import if it's not there
50
- if (importTextNodes.length === 0) {
51
- const fallbackPos = findNodes(relevantImports[0], ts.SyntaxKind.CloseBraceToken)[0].getStart() ||
52
- findNodes(relevantImports[0], ts.SyntaxKind.FromKeyword)[0].getStart();
53
- return insertAfterLastOccurrence(imports, `, ${symbolName}`, fileToEdit, fallbackPos);
54
- }
55
- return new change_1.NoopChange();
56
- }
57
- // no such import declaration exists
58
- const useStrict = findNodes(rootNode, ts.SyntaxKind.StringLiteral).filter((n) => n.text === 'use strict');
59
- let fallbackPos = 0;
60
- if (useStrict.length > 0) {
61
- fallbackPos = useStrict[0].end;
62
- }
63
- const open = isDefault ? '' : '{ ';
64
- const close = isDefault ? '' : ' }';
65
- // if there are no imports or 'use strict' statement, insert import at beginning of file
66
- const insertAtBeginning = allImports.length === 0 && useStrict.length === 0;
67
- const separator = insertAtBeginning ? '' : ';\n';
68
- const toInsert = `${separator}import ${open}${symbolName}${close}` + ` from '${fileName}'${insertAtBeginning ? ';\n' : ''}`;
69
- return insertAfterLastOccurrence(allImports, toInsert, fileToEdit, fallbackPos, ts.SyntaxKind.StringLiteral);
70
- }
71
- exports.insertImport = insertImport;
72
- /**
73
- * Find all nodes from the AST in the subtree of node of SyntaxKind kind.
74
- * @param node
75
- * @param kind
76
- * @param max The maximum number of items to return.
77
- * @return all nodes of kind, or [] if none is found
78
- */
79
- function findNodes(node, kind, max = Infinity) {
80
- if (!node || max == 0) {
81
- return [];
82
- }
83
- const arr = [];
84
- if (node.kind === kind) {
85
- arr.push(node);
86
- max--;
87
- }
88
- if (max > 0) {
89
- for (const child of node.getChildren()) {
90
- findNodes(child, kind, max).forEach((node) => {
91
- if (max > 0) {
92
- arr.push(node);
93
- }
94
- max--;
95
- });
96
- if (max <= 0) {
97
- break;
98
- }
99
- }
100
- }
101
- return arr;
102
- }
103
- exports.findNodes = findNodes;
104
- /**
105
- * Get all the nodes from a source.
106
- * @param sourceFile The source file object.
107
- * @returns {Observable<ts.Node>} An observable of all the nodes in the source.
108
- */
109
- function getSourceNodes(sourceFile) {
110
- const nodes = [sourceFile];
111
- const result = [];
112
- while (nodes.length > 0) {
113
- const node = nodes.shift();
114
- if (node) {
115
- result.push(node);
116
- if (node.getChildCount(sourceFile) >= 0) {
117
- nodes.unshift(...node.getChildren());
118
- }
119
- }
120
- }
121
- return result;
122
- }
123
- exports.getSourceNodes = getSourceNodes;
124
- function findNode(node, kind, text) {
125
- if (node.kind === kind && node.getText() === text) {
126
- // throw new Error(node.getText());
127
- return node;
128
- }
129
- let foundNode = null;
130
- ts.forEachChild(node, (childNode) => {
131
- foundNode = foundNode || findNode(childNode, kind, text);
132
- });
133
- return foundNode;
134
- }
135
- exports.findNode = findNode;
136
- /**
137
- * Helper for sorting nodes.
138
- * @return function to sort nodes in increasing order of position in sourceFile
139
- */
140
- function nodesByPosition(first, second) {
141
- return first.getStart() - second.getStart();
142
- }
143
- /**
144
- * Insert `toInsert` after the last occurence of `ts.SyntaxKind[nodes[i].kind]`
145
- * or after the last of occurence of `syntaxKind` if the last occurence is a sub child
146
- * of ts.SyntaxKind[nodes[i].kind] and save the changes in file.
147
- *
148
- * @param nodes insert after the last occurence of nodes
149
- * @param toInsert string to insert
150
- * @param file file to insert changes into
151
- * @param fallbackPos position to insert if toInsert happens to be the first occurence
152
- * @param syntaxKind the ts.SyntaxKind of the subchildren to insert after
153
- * @return Change instance
154
- * @throw Error if toInsert is first occurence but fall back is not set
155
- */
156
- function insertAfterLastOccurrence(nodes, toInsert, file, fallbackPos, syntaxKind) {
157
- // sort() has a side effect, so make a copy so that we won't overwrite the parent's object.
158
- let lastItem = [...nodes].sort(nodesByPosition).pop();
159
- if (!lastItem) {
160
- throw new Error();
161
- }
162
- if (syntaxKind) {
163
- lastItem = findNodes(lastItem, syntaxKind).sort(nodesByPosition).pop();
164
- }
165
- if (!lastItem && fallbackPos == undefined) {
166
- throw new Error(`tried to insert ${toInsert} as first occurence with no fallback position`);
167
- }
168
- const lastItemPosition = lastItem ? lastItem.getEnd() : fallbackPos;
169
- return new change_1.InsertChange(file, lastItemPosition, toInsert);
170
- }
171
- exports.insertAfterLastOccurrence = insertAfterLastOccurrence;
172
- function getContentOfKeyLiteral(_source, node) {
173
- if (node.kind == ts.SyntaxKind.Identifier) {
174
- return node.text;
175
- }
176
- else if (node.kind == ts.SyntaxKind.StringLiteral) {
177
- return node.text;
178
- }
179
- else {
180
- return null;
181
- }
182
- }
183
- exports.getContentOfKeyLiteral = getContentOfKeyLiteral;
184
- function _angularImportsFromNode(node,
185
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
186
- _sourceFile) {
187
- const ms = node.moduleSpecifier;
188
- let modulePath;
189
- switch (ms.kind) {
190
- case ts.SyntaxKind.StringLiteral:
191
- modulePath = ms.text;
192
- break;
193
- default:
194
- return {};
195
- }
196
- if (!modulePath.startsWith('@angular/')) {
197
- return {};
198
- }
199
- if (node.importClause) {
200
- if (node.importClause.name) {
201
- // This is of the form `import Name from 'path'`. Ignore.
202
- return {};
203
- }
204
- else if (node.importClause.namedBindings) {
205
- const nb = node.importClause.namedBindings;
206
- if (nb.kind == ts.SyntaxKind.NamespaceImport) {
207
- // This is of the form `import * as name from 'path'`. Return `name.`.
208
- return {
209
- [nb.name.text + '.']: modulePath,
210
- };
211
- }
212
- else {
213
- // This is of the form `import {a,b,c} from 'path'`
214
- const namedImports = nb;
215
- return namedImports.elements
216
- .map((is) => (is.propertyName ? is.propertyName.text : is.name.text))
217
- .reduce((acc, curr) => {
218
- acc[curr] = modulePath;
219
- return acc;
220
- }, {});
221
- }
222
- }
223
- return {};
224
- }
225
- else {
226
- // This is of the form `import 'path';`. Nothing to do.
227
- return {};
228
- }
229
- }
230
- function getDecoratorMetadata(source, identifier, module) {
231
- const angularImports = findNodes(source, ts.SyntaxKind.ImportDeclaration)
232
- .map((node) => _angularImportsFromNode(node, source))
233
- .reduce((acc, current) => {
234
- for (const key of Object.keys(current)) {
235
- acc[key] = current[key];
236
- }
237
- return acc;
238
- }, {});
239
- return getSourceNodes(source)
240
- .filter((node) => {
241
- return (node.kind == ts.SyntaxKind.Decorator && node.expression.kind == ts.SyntaxKind.CallExpression);
242
- })
243
- .map((node) => node.expression)
244
- .filter((expr) => {
245
- if (expr.expression.kind == ts.SyntaxKind.Identifier) {
246
- const id = expr.expression;
247
- return id.getFullText(source) == identifier && angularImports[id.getFullText(source)] === module;
248
- }
249
- else if (expr.expression.kind == ts.SyntaxKind.PropertyAccessExpression) {
250
- // This covers foo.NgModule when importing * as foo.
251
- const paExpr = expr.expression;
252
- // If the left expression is not an identifier, just give up at that point.
253
- if (paExpr.expression.kind !== ts.SyntaxKind.Identifier) {
254
- return false;
255
- }
256
- const id = paExpr.name.text;
257
- const moduleId = paExpr.expression.getText(source);
258
- return id === identifier && angularImports[moduleId + '.'] === module;
259
- }
260
- return false;
261
- })
262
- .filter((expr) => expr.arguments[0] && expr.arguments[0].kind == ts.SyntaxKind.ObjectLiteralExpression)
263
- .map((expr) => expr.arguments[0]);
264
- }
265
- exports.getDecoratorMetadata = getDecoratorMetadata;
266
- function findClassDeclarationParent(node) {
267
- if (ts.isClassDeclaration(node)) {
268
- return node;
269
- }
270
- return node.parent && findClassDeclarationParent(node.parent);
271
- }
272
- /**
273
- * Given a source file with @NgModule class(es), find the name of the first @NgModule class.
274
- *
275
- * @param source source file containing one or more @NgModule
276
- * @returns the name of the first @NgModule, or `undefined` if none is found
277
- */
278
- function getFirstNgModuleName(source) {
279
- // First, find the @NgModule decorators.
280
- const ngModulesMetadata = getDecoratorMetadata(source, 'NgModule', '@angular/core');
281
- if (ngModulesMetadata.length === 0) {
282
- return undefined;
283
- }
284
- // Then walk parent pointers up the AST, looking for the ClassDeclaration parent of the NgModule
285
- // metadata.
286
- const moduleClass = findClassDeclarationParent(ngModulesMetadata[0]);
287
- if (!(moduleClass === null || moduleClass === void 0 ? void 0 : moduleClass.name)) {
288
- return undefined;
289
- }
290
- // Get the class name of the module ClassDeclaration.
291
- return moduleClass.name.text;
292
- }
293
- exports.getFirstNgModuleName = getFirstNgModuleName;
294
- function addSymbolToNgModuleMetadata(source, ngModulePath, metadataField, symbolName, importPath = null) {
295
- const nodes = getDecoratorMetadata(source, 'NgModule', '@angular/core');
296
- let node = nodes[0]; // tslint:disable-line:no-any
297
- // Find the decorator declaration.
298
- if (!node) {
299
- return [];
300
- }
301
- // Get all the children property assignment of object literals.
302
- const matchingProperties = node.properties
303
- .filter((prop) => prop.kind == ts.SyntaxKind.PropertyAssignment)
304
- // Filter out every fields that's not "metadataField". Also handles string literals
305
- // (but not expressions).
306
- .filter((prop) => {
307
- const name = prop.name;
308
- switch (name.kind) {
309
- case ts.SyntaxKind.Identifier:
310
- return name.getText(source) == metadataField;
311
- case ts.SyntaxKind.StringLiteral:
312
- return name.text == metadataField;
313
- }
314
- return false;
315
- });
316
- // Get the last node of the array literal.
317
- if (!matchingProperties) {
318
- return [];
319
- }
320
- if (matchingProperties.length == 0) {
321
- // We haven't found the field in the metadata declaration. Insert a new field.
322
- const expr = node;
323
- let position;
324
- let toInsert;
325
- if (expr.properties.length == 0) {
326
- position = expr.getEnd() - 1;
327
- toInsert = ` ${metadataField}: [${symbolName}]\n`;
328
- }
329
- else {
330
- node = expr.properties[expr.properties.length - 1];
331
- position = node.getEnd();
332
- // Get the indentation of the last element, if any.
333
- const text = node.getFullText(source);
334
- const matches = text.match(/^\r?\n\s*/);
335
- if (matches.length > 0) {
336
- toInsert = `,${matches[0]}${metadataField}: [${symbolName}]`;
337
- }
338
- else {
339
- toInsert = `, ${metadataField}: [${symbolName}]`;
340
- }
341
- }
342
- if (importPath !== null) {
343
- return [
344
- new change_1.InsertChange(ngModulePath, position, toInsert),
345
- insertImport(source, ngModulePath, symbolName.replace(/\..*$/, ''), importPath),
346
- ];
347
- }
348
- else {
349
- return [new change_1.InsertChange(ngModulePath, position, toInsert)];
350
- }
351
- }
352
- const assignment = matchingProperties[0];
353
- // If it's not an array, nothing we can do really.
354
- if (assignment.initializer.kind !== ts.SyntaxKind.ArrayLiteralExpression) {
355
- return [];
356
- }
357
- const arrLiteral = assignment.initializer;
358
- if (arrLiteral.elements.length == 0) {
359
- // Forward the property.
360
- node = arrLiteral;
361
- }
362
- else {
363
- node = arrLiteral.elements;
364
- }
365
- if (!node) {
366
- console.log('No app module found. Please add your new class to your component.');
367
- return [];
368
- }
369
- if (Array.isArray(node)) {
370
- // eslint-disable-next-line @typescript-eslint/ban-types
371
- const nodeArray = node;
372
- const symbolsArray = nodeArray.map((node) => node.getText());
373
- if (symbolsArray.includes(symbolName)) {
374
- return [];
375
- }
376
- node = node[node.length - 1];
377
- }
378
- let toInsert;
379
- let position = node.getEnd();
380
- if (node.kind == ts.SyntaxKind.ObjectLiteralExpression) {
381
- // We haven't found the field in the metadata declaration. Insert a new
382
- // field.
383
- const expr = node;
384
- if (expr.properties.length == 0) {
385
- position = expr.getEnd() - 1;
386
- toInsert = ` ${metadataField}: [${symbolName}]\n`;
387
- }
388
- else {
389
- node = expr.properties[expr.properties.length - 1];
390
- position = node.getEnd();
391
- // Get the indentation of the last element, if any.
392
- const text = node.getFullText(source);
393
- if (text.match('^\r?\r?\n')) {
394
- toInsert = `,${text.match(/^\r?\n\s+/)[0]}${metadataField}: [${symbolName}]`;
395
- }
396
- else {
397
- toInsert = `, ${metadataField}: [${symbolName}]`;
398
- }
399
- }
400
- }
401
- else if (node.kind == ts.SyntaxKind.ArrayLiteralExpression) {
402
- // We found the field but it's empty. Insert it just before the `]`.
403
- position--;
404
- toInsert = `${symbolName}`;
405
- }
406
- else {
407
- // Get the indentation of the last element, if any.
408
- const text = node.getFullText(source);
409
- if (text.match(/^\r?\n/)) {
410
- toInsert = `,${text.match(/^\r?\n(\r?)\s+/)[0]}${symbolName}`;
411
- }
412
- else {
413
- toInsert = `, ${symbolName}`;
414
- }
415
- }
416
- if (importPath !== null) {
417
- return [
418
- new change_1.InsertChange(ngModulePath, position, toInsert),
419
- insertImport(source, ngModulePath, symbolName.replace(/\..*$/, ''), importPath),
420
- ];
421
- }
422
- return [new change_1.InsertChange(ngModulePath, position, toInsert)];
423
- }
424
- exports.addSymbolToNgModuleMetadata = addSymbolToNgModuleMetadata;
425
- /**
426
- * Custom function to insert a declaration (component, pipe, directive)
427
- * into NgModule declarations. It also imports the component.
428
- */
429
- function addDeclarationToModule(source, modulePath, classifiedName, importPath) {
430
- return addSymbolToNgModuleMetadata(source, modulePath, 'declarations', classifiedName, importPath);
431
- }
432
- exports.addDeclarationToModule = addDeclarationToModule;
433
- /**
434
- * Custom function to insert an NgModule into NgModule imports. It also imports the module.
435
- */
436
- function addImportToModule(source, modulePath, classifiedName, importPath) {
437
- return addSymbolToNgModuleMetadata(source, modulePath, 'imports', classifiedName, importPath);
438
- }
439
- exports.addImportToModule = addImportToModule;
440
- /**
441
- * Custom function to insert a provider into NgModule. It also imports it.
442
- */
443
- function addProviderToModule(source, modulePath, classifiedName, importPath) {
444
- return addSymbolToNgModuleMetadata(source, modulePath, 'providers', classifiedName, importPath);
445
- }
446
- exports.addProviderToModule = addProviderToModule;
447
- /**
448
- * Custom function to insert an export into NgModule. It also imports it.
449
- */
450
- function addExportToModule(source, modulePath, classifiedName, importPath) {
451
- return addSymbolToNgModuleMetadata(source, modulePath, 'exports', classifiedName, importPath);
452
- }
453
- exports.addExportToModule = addExportToModule;
454
- /**
455
- * Custom function to insert an export into NgModule. It also imports it.
456
- */
457
- function addBootstrapToModule(source, modulePath, classifiedName, importPath) {
458
- return addSymbolToNgModuleMetadata(source, modulePath, 'bootstrap', classifiedName, importPath);
459
- }
460
- exports.addBootstrapToModule = addBootstrapToModule;
461
- /**
462
- * Custom function to insert an entryComponent into NgModule. It also imports it.
463
- */
464
- function addEntryComponentToModule(source, modulePath, classifiedName, importPath) {
465
- return addSymbolToNgModuleMetadata(source, modulePath, 'entryComponents', classifiedName, importPath);
466
- }
467
- exports.addEntryComponentToModule = addEntryComponentToModule;
468
- /**
469
- * Determine if an import already exists.
470
- */
471
- function isImported(source, classifiedName, importPath) {
472
- const allNodes = getSourceNodes(source);
473
- const matchingNodes = allNodes
474
- .filter((node) => node.kind === ts.SyntaxKind.ImportDeclaration)
475
- .filter((imp) => imp.moduleSpecifier.kind === ts.SyntaxKind.StringLiteral)
476
- .filter((imp) => {
477
- return imp.moduleSpecifier.text === importPath;
478
- })
479
- .filter((imp) => {
480
- if (!imp.importClause) {
481
- return false;
482
- }
483
- const nodes = findNodes(imp.importClause, ts.SyntaxKind.ImportSpecifier).filter((n) => n.getText() === classifiedName);
484
- return nodes.length > 0;
485
- });
486
- return matchingNodes.length > 0;
487
- }
488
- exports.isImported = isImported;
@@ -1,96 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ReplaceChange = exports.RemoveChange = exports.InsertChange = exports.NoopChange = void 0;
4
- /**
5
- * An operation that does nothing.
6
- */
7
- class NoopChange {
8
- constructor() {
9
- this.description = 'No operation.';
10
- this.order = Infinity;
11
- this.path = null;
12
- }
13
- apply() {
14
- return Promise.resolve();
15
- }
16
- }
17
- exports.NoopChange = NoopChange;
18
- /**
19
- * Will add text to the source code.
20
- */
21
- class InsertChange {
22
- constructor(path, pos, toAdd) {
23
- this.path = path;
24
- this.pos = pos;
25
- this.toAdd = toAdd;
26
- if (pos < 0) {
27
- throw new Error('Negative positions are invalid');
28
- }
29
- this.description = `Inserted ${toAdd} into position ${pos} of ${path}`;
30
- this.order = pos;
31
- }
32
- /**
33
- * This method does not insert spaces if there is none in the original string.
34
- */
35
- apply(host) {
36
- return host.read(this.path).then((content) => {
37
- const prefix = content.substring(0, this.pos);
38
- const suffix = content.substring(this.pos);
39
- return host.write(this.path, `${prefix}${this.toAdd}${suffix}`);
40
- });
41
- }
42
- }
43
- exports.InsertChange = InsertChange;
44
- /**
45
- * Will remove text from the source code.
46
- */
47
- class RemoveChange {
48
- constructor(path, pos, toRemove) {
49
- this.path = path;
50
- this.pos = pos;
51
- this.toRemove = toRemove;
52
- if (pos < 0) {
53
- throw new Error('Negative positions are invalid');
54
- }
55
- this.description = `Removed ${toRemove} into position ${pos} of ${path}`;
56
- this.order = pos;
57
- }
58
- apply(host) {
59
- return host.read(this.path).then((content) => {
60
- const prefix = content.substring(0, this.pos);
61
- const suffix = content.substring(this.pos + this.toRemove.length);
62
- // TODO: throw error if toRemove doesn't match removed string.
63
- return host.write(this.path, `${prefix}${suffix}`);
64
- });
65
- }
66
- }
67
- exports.RemoveChange = RemoveChange;
68
- /**
69
- * Will replace text from the source code.
70
- */
71
- class ReplaceChange {
72
- constructor(path, pos, oldText, newText) {
73
- this.path = path;
74
- this.pos = pos;
75
- this.oldText = oldText;
76
- this.newText = newText;
77
- if (pos < 0) {
78
- throw new Error('Negative positions are invalid');
79
- }
80
- this.description = `Replaced ${oldText} into position ${pos} of ${path} with ${newText}`;
81
- this.order = pos;
82
- }
83
- apply(host) {
84
- return host.read(this.path).then((content) => {
85
- const prefix = content.substring(0, this.pos);
86
- const suffix = content.substring(this.pos + this.oldText.length);
87
- const text = content.substring(this.pos, this.pos + this.oldText.length);
88
- if (text !== this.oldText) {
89
- return Promise.reject(new Error(`Invalid replace: "${text}" != "${this.oldText}".`));
90
- }
91
- // TODO: throw error if oldText doesn't match removed string.
92
- return host.write(this.path, `${prefix}${this.newText}${suffix}`);
93
- });
94
- }
95
- }
96
- exports.ReplaceChange = ReplaceChange;
@@ -1,72 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.insertImport = void 0;
4
- /**
5
- * @license
6
- * Copyright Google Inc. All Rights Reserved.
7
- *
8
- * Use of this source code is governed by an MIT-style license that can be
9
- * found in the LICENSE file at https://angular.io/license
10
- */
11
- const ts = require("typescript");
12
- const ast_utils_1 = require("./ast-utils");
13
- const change_1 = require("./change");
14
- /**
15
- * Add Import `import { symbolName } from fileName` if the import doesn't exit
16
- * already. Assumes fileToEdit can be resolved and accessed.
17
- * @param fileToEdit (file we want to add import to)
18
- * @param symbolName (item to import)
19
- * @param fileName (path to the file)
20
- * @param isDefault (if true, import follows style for importing default exports)
21
- * @return Change
22
- */
23
- function insertImport(source, fileToEdit, symbolName, fileName, isDefault = false) {
24
- const rootNode = source;
25
- const allImports = (0, ast_utils_1.findNodes)(rootNode, ts.SyntaxKind.ImportDeclaration);
26
- // get nodes that map to import statements from the file fileName
27
- const relevantImports = allImports.filter((node) => {
28
- // StringLiteral of the ImportDeclaration is the import file (fileName in this case).
29
- const importFiles = node
30
- .getChildren()
31
- .filter((child) => child.kind === ts.SyntaxKind.StringLiteral)
32
- .map((n) => n.text);
33
- return importFiles.filter((file) => file === fileName).length === 1;
34
- });
35
- if (relevantImports.length > 0) {
36
- let importsAsterisk = false;
37
- // imports from import file
38
- const imports = [];
39
- relevantImports.forEach((n) => {
40
- Array.prototype.push.apply(imports, (0, ast_utils_1.findNodes)(n, ts.SyntaxKind.Identifier));
41
- if ((0, ast_utils_1.findNodes)(n, ts.SyntaxKind.AsteriskToken).length > 0) {
42
- importsAsterisk = true;
43
- }
44
- });
45
- // if imports * from fileName, don't add symbolName
46
- if (importsAsterisk) {
47
- return new change_1.NoopChange();
48
- }
49
- const importTextNodes = imports.filter((n) => n.text === symbolName);
50
- // insert import if it's not there
51
- if (importTextNodes.length === 0) {
52
- const fallbackPos = (0, ast_utils_1.findNodes)(relevantImports[0], ts.SyntaxKind.CloseBraceToken)[0].getStart() ||
53
- (0, ast_utils_1.findNodes)(relevantImports[0], ts.SyntaxKind.FromKeyword)[0].getStart();
54
- return (0, ast_utils_1.insertAfterLastOccurrence)(imports, `, ${symbolName}`, fileToEdit, fallbackPos);
55
- }
56
- return new change_1.NoopChange();
57
- }
58
- // no such import declaration exists
59
- const useStrict = (0, ast_utils_1.findNodes)(rootNode, ts.SyntaxKind.StringLiteral).filter((n) => n.text === 'use strict');
60
- let fallbackPos = 0;
61
- if (useStrict.length > 0) {
62
- fallbackPos = useStrict[0].end;
63
- }
64
- const open = isDefault ? '' : '{ ';
65
- const close = isDefault ? '' : ' }';
66
- // if there are no imports or 'use strict' statement, insert import at beginning of file
67
- const insertAtBeginning = allImports.length === 0 && useStrict.length === 0;
68
- const separator = insertAtBeginning ? '' : ';\n';
69
- const toInsert = `${separator}import ${open}${symbolName}${close}` + ` from '${fileName}'${insertAtBeginning ? ';\n' : ''}`;
70
- return (0, ast_utils_1.insertAfterLastOccurrence)(allImports, toInsert, fileToEdit, fallbackPos, ts.SyntaxKind.StringLiteral);
71
- }
72
- exports.insertImport = insertImport;