Skip to content

Commit

Permalink
Clean up the upgrade schematic a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesdaniels committed Aug 17, 2021
1 parent ec33419 commit 6218ba2
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/schematics/update/v7/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { Rule, SchematicContext, SchematicsException, Tree } from '@angular-devk
import { overwriteIfExists, safeReadJSON, stringifyFormatted } from '../../ng-add-common';
import { default as defaultDependencies, firebaseFunctions } from '../../versions.json';

const FIREBASE_IMPORT_REGEX = /(?<key>import|export)\s+(?:(?<alias>[\w,{}\s\*]+)\s+from)?\s*(?:(["'])?firebase\/(?<ref>[@\w\s\\\/.-]+)\3?)\s*;/g;
const AT_FIREBASE_IMPORT_REGEX = /(?<key>import|export)\s+(?:(?<alias>[\w,{}\s\*]+)\s+from)?\s*(?:(["'])?@firebase\/(?<ref>[@\w\s\\\/.-]+)\3?)\s*;/g;
const ANGULAR_FIRE_IMPORT_REGEX = /(?<key>import|export)\s+(?:(?<alias>[\w,{}\s\*]+)\s+from)?\s*(?:(["'])?@angular\/fire\/(?<ref>[@\w\s\\\/.-]+)\3?)\s*;/g;

export const ngUpdate = (): Rule => (
host: Tree,
context: SchematicContext
Expand Down Expand Up @@ -45,14 +49,23 @@ export const ngUpdate = (): Rule => (
if (!content) {
return;
}
// TODO clean this up, skip overwrite if uneeded
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;');
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;');
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;');
host.overwrite(filePath, content);
console.log(filePath);
let didChangeContent = false;
if (content.match(FIREBASE_IMPORT_REGEX)) {
content.replace(FIREBASE_IMPORT_REGEX, '$1 $2 from $3firebase/compat/$4$3;');
didChangeContent = true;
}
if (content.match(AT_FIREBASE_IMPORT_REGEX)) {
content.replace(AT_FIREBASE_IMPORT_REGEX, '$1 $2 from $3@firebase/compat/$4$3;');
didChangeContent = true;
}
if (content.match(ANGULAR_FIRE_IMPORT_REGEX)) {
content.replace(ANGULAR_FIRE_IMPORT_REGEX, '$1 $2 from $3@angular/fire/compat/$4$3;');
didChangeContent = true;
}
if (didChangeContent) {
host.overwrite(filePath, content);
}
});

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

0 comments on commit 6218ba2

Please sign in to comment.