Skip to content

Commit 6218ba2

Browse files
committed
Clean up the upgrade schematic a bit
1 parent ec33419 commit 6218ba2

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

src/schematics/update/v7/index.ts

+20-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import { Rule, SchematicContext, SchematicsException, Tree } from '@angular-devk
22
import { overwriteIfExists, safeReadJSON, stringifyFormatted } from '../../ng-add-common';
33
import { default as defaultDependencies, firebaseFunctions } from '../../versions.json';
44

5+
const FIREBASE_IMPORT_REGEX = /(?<key>import|export)\s+(?:(?<alias>[\w,{}\s\*]+)\s+from)?\s*(?:(["'])?firebase\/(?<ref>[@\w\s\\\/.-]+)\3?)\s*;/g;
6+
const AT_FIREBASE_IMPORT_REGEX = /(?<key>import|export)\s+(?:(?<alias>[\w,{}\s\*]+)\s+from)?\s*(?:(["'])?@firebase\/(?<ref>[@\w\s\\\/.-]+)\3?)\s*;/g;
7+
const ANGULAR_FIRE_IMPORT_REGEX = /(?<key>import|export)\s+(?:(?<alias>[\w,{}\s\*]+)\s+from)?\s*(?:(["'])?@angular\/fire\/(?<ref>[@\w\s\\\/.-]+)\3?)\s*;/g;
8+
59
export const ngUpdate = (): Rule => (
610
host: Tree,
711
context: SchematicContext
@@ -45,14 +49,23 @@ export const ngUpdate = (): Rule => (
4549
if (!content) {
4650
return;
4751
}
48-
// TODO clean this up, skip overwrite if uneeded
49-
content.replace(/(?<key>import|export)\s+(?:(?<alias>[\w,{}\s\*]+)\s+from)?\s*(?:(["'])?firebase\/(?<ref>[@\w\s\\\/.-]+)\3?)\s*;/, '$1 $2 from $3firebase/compat/$4$3;');
50-
content.replace(/(?<key>import|export)\s+(?:(?<alias>[\w,{}\s\*]+)\s+from)?\s*(?:(["'])?@firebase\/(?<ref>[@\w\s\\\/.-]+)\3?)\s*;/, '$1 $2 from $3@firebase/compat/$4$3;');
51-
content.replace(/(?<key>import|export)\s+(?:(?<alias>[\w,{}\s\*]+)\s+from)?\s*(?:(["'])?@angular\/fire\/(?<ref>[@\w\s\\\/.-]+)\3?)\s*;/, '$1 $2 from $3@angular/fire/compat/$4$3;');
52-
host.overwrite(filePath, content);
53-
console.log(filePath);
52+
let didChangeContent = false;
53+
if (content.match(FIREBASE_IMPORT_REGEX)) {
54+
content.replace(FIREBASE_IMPORT_REGEX, '$1 $2 from $3firebase/compat/$4$3;');
55+
didChangeContent = true;
56+
}
57+
if (content.match(AT_FIREBASE_IMPORT_REGEX)) {
58+
content.replace(AT_FIREBASE_IMPORT_REGEX, '$1 $2 from $3@firebase/compat/$4$3;');
59+
didChangeContent = true;
60+
}
61+
if (content.match(ANGULAR_FIRE_IMPORT_REGEX)) {
62+
content.replace(ANGULAR_FIRE_IMPORT_REGEX, '$1 $2 from $3@angular/fire/compat/$4$3;');
63+
didChangeContent = true;
64+
}
65+
if (didChangeContent) {
66+
host.overwrite(filePath, content);
67+
}
5468
});
5569

56-
console.log('Called ng-update');
5770
return host;
5871
};

0 commit comments

Comments
 (0)