Skip to content

Commit

Permalink
fix: ng updates for v10
Browse files Browse the repository at this point in the history
  • Loading branch information
fpaul-1A committed Feb 16, 2024
1 parent be4bfe4 commit e54d273
Show file tree
Hide file tree
Showing 28 changed files with 161 additions and 39 deletions.
7 changes: 7 additions & 0 deletions .verdaccio/conf/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,21 @@ packages:
access: $all
publish: $authenticated
proxy: no-proxy
# proxy: npmjs
'@ama-sdk/*':
access: $all
publish: $authenticated
proxy: no-proxy
# proxy: npmjs
'@ama-terasu/*':
access: $all
publish: $authenticated
proxy: no-proxy
# proxy: npmjs

#uplinks:
# npmjs:
# url: https://registry.npmjs.org/

no_proxy: localhost,127.0.0.1

Expand Down
2 changes: 1 addition & 1 deletion packages/@ama-sdk/schematics/migration.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"schematics": {
"migration-v10_0": {
"version": "10.0.0-alpha.0",
"description": "Updates of the Otter Library to v10.0.*",
"description": "Updates of @ama-sdk/schematics to v10.0.*",
"factory": "./schematics/ng-update/index#updateV10_0"
}
}
Expand Down
6 changes: 3 additions & 3 deletions packages/@o3r/application/migration.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"$schema": "https://raw.githubusercontent.com/angular/angular-cli/master/packages/angular_devkit/schematics/collection-schema.json",
"schematics": {
"ng-update": {
"migration-v10_0": {
"version": "10.0.0-alpha.0",
"description": "Updates of the Otter Application module to v10.0.x",
"factory": "./schematics/ng-update/index#ngUpdate"
"description": "Updates of @o3r/application to v10.0.*",
"factory": "./schematics/ng-update/index#updateV10_0"
}
}
}
2 changes: 1 addition & 1 deletion packages/@o3r/components/migration.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"schematics": {
"migration-v10_0": {
"version": "10.0.0-alpha.0",
"description": "Updates of the Otter Library to v10.0.*",
"description": "Updates of @o3r/components to v10.0.*",
"factory": "./schematics/ng-update/v10-0/index#updateV10_0"
}
}
Expand Down
5 changes: 4 additions & 1 deletion packages/@o3r/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -173,5 +173,8 @@
"node": ">=18.0.0"
},
"schematics": "./collection.json",
"builders": "./builders.json"
"builders": "./builders.json",
"ng-update": {
"migrations": "./migration.json"
}
}
2 changes: 1 addition & 1 deletion packages/@o3r/core/migration.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"migration-v10_0": {
"version": "10.0.0-alpha.0",
"description": "Updates of the Otter Library to v10.0.*",
"description": "Updates of @o3r/core to v10.0.*",
"factory": "./schematics/ng-update/index#updateV10_0"
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/@o3r/core/schematics/ng-update/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ function updateV10_0Fn(): Rule {
return (tree: Tree, context: SchematicContext) => {

const updateRules: Rule[] = [
// Some of these imports were missed in the generators of v9, so it's easier to just run the update again
updateImports(mapImportAsyncStore),
updateConfiguration
];

Expand Down
2 changes: 1 addition & 1 deletion packages/@o3r/dev-tools/migration.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"schematics": {
"ng-update": {
"version": "10.0.0-alpha.0",
"description": "Updates of the Otter DevTools module to v10.0.x",
"description": "Deprecation warning of @o3r/dev-tools for v10.0.*",
"factory": "./schematics/ng-update/index#ngUpdate"
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/@o3r/dev-tools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"build": "yarn nx build dev-tools",
"postbuild": "patch-package-json-main",
"prepare:publish": "prepare-publish ./dist",
"prepare:build:builders": "yarn cpy 'schematics/**/*.json' dist/schematics && yarn cpy 'collection.json' dist",
"prepare:build:builders": "yarn cpy 'schematics/**/*.json' dist/schematics && yarn cpy '{collection,migration}.json' dist",
"build:builders": "tsc -b tsconfig.builders.json --pretty && yarn generate-cjs-manifest"
},
"bin": {
Expand Down
2 changes: 1 addition & 1 deletion packages/@o3r/dynamic-content/migration.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"schematics": {
"migration-v10_0": {
"version": "10.0.0-alpha.0",
"description": "Updates of the Otter Library to v10.0.*",
"description": "Updates of @o3r/dynamic-content to v10.0.*",
"factory": "./schematics/ng-update/v10-0/index#updateV10_0"
}
}
Expand Down
5 changes: 4 additions & 1 deletion packages/@o3r/dynamic-content/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,8 @@
"engines": {
"node": ">=18.0.0"
},
"schematics": "./collection.json"
"schematics": "./collection.json",
"ng-update": {
"migrations": "./migration.json"
}
}
2 changes: 1 addition & 1 deletion packages/@o3r/eslint-config-otter/migration.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"schematics": {
"migration-v10_0": {
"version": "10.0.0-alpha.0",
"description": "Updates of the Otter Library to v10.0.*",
"description": "Updates of @o3r/eslint-config-otter to v10.0.*",
"factory": "./schematics/ng-update/index#updateV100"
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/@o3r/extractors/migration.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"schematics": {
"migration-v10_0": {
"version": "10.0.0-alpha.0",
"description": "Updates of the Otter Extractors module to v10.0.x",
"description": "Updates of @o3r/extractors to v10.0.*",
"factory": "./schematics/ng-update/v10-0/index#updateV10_0"
}
}
Expand Down
8 changes: 4 additions & 4 deletions packages/@o3r/extractors/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
"extractors",
"otter"
],
"ng-update": {
"migrations": "./migration.json"
},
"scripts": {
"nx": "nx",
"ng": "yarn nx",
Expand Down Expand Up @@ -104,5 +101,8 @@
"engines": {
"node": ">=18.0.0"
},
"schematics": "./collection.json"
"schematics": "./collection.json",
"ng-update": {
"migrations": "./migration.json"
}
}
2 changes: 1 addition & 1 deletion packages/@o3r/localization/migration.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"schematics": {
"migration-v10_0": {
"version": "10.0.0-alpha.0",
"description": "Updates of the Otter Library to v10.0.*",
"description": "Updates of @o3r/localization to v10.0.*",
"factory": "./schematics/ng-update/v10-0/index#updateV10_0"
}
}
Expand Down
5 changes: 4 additions & 1 deletion packages/@o3r/localization/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -149,5 +149,8 @@
"node": ">=18.0.0"
},
"builders": "./builders.json",
"schematics": "./collection.json"
"schematics": "./collection.json",
"ng-update": {
"migrations": "./migration.json"
}
}
2 changes: 1 addition & 1 deletion packages/@o3r/rules-engine/migration.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"schematics": {
"migration-v10_0": {
"version": "10.0.0-alpha.0",
"description": "Updates of the Otter Library to v10.0.*",
"description": "Updates of @o3r/rules-engine to v10.0.*",
"factory": "./schematics/ng-update/index#updateV100"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ export function updateRuleEngineService(): Rule {
newValue: 'RulesEngineRunnerModule'
}
}
});
}, undefined, true);
/* eslint-enable @typescript-eslint/naming-convention */
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@ export function updateImports(mapImports: ImportsMapping = {}, renamedPackages:
const files = fromRoot ? getFilesWithExtensionFromTree(tree, 'ts') : getSourceFilesFromWorkspaceProjects(tree);

// exact match on import path
const importsRegexp = new RegExp(`^(${[...Object.keys(mapImports)].join('|')})$`);
const escapeRegExp = (str: string) => str.replace(/[-[\]/{}()*+?.\\^$|]/g, '\\$&');
const importsRegexp = new RegExp(`^(${Object.keys(mapImports).map(escapeRegExp).join('|')})$`);
// match the import path starting with the package to be renamed
const renamePackagesRegexp = new RegExp(`^(${[...Object.keys(renamedPackages)].join('|')})`);
const renamePackagesRegexp = new RegExp(`^(${Object.keys(renamedPackages).map(escapeRegExp).join('|')})`);
let nbOfUnResolvedImports = 0;

files.forEach((file) => {
const sourceFile = ts.createSourceFile(
file,
tree.read(file)!.toString(),
tree.readText(file),
ts.ScriptTarget.ES2015,
true
);
Expand Down
84 changes: 84 additions & 0 deletions packages/@o3r/schematics/src/utility/update-imports.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import {logging} from '@angular-devkit/core';
import {Tree} from '@angular-devkit/schematics';
import {updateImportsInFile} from '@o3r/schematics';
import * as ts from 'typescript';

describe('updateImportsInFile', () => {
const logger = {
debug: jest.fn(),
info: jest.fn(),
log: jest.fn(),
warn: jest.fn(),
error: jest.fn()
} as any as logging.Logger;

it('should replace imports in a file', () => {
const fileName = 'test.ts';
const originalFileContent = `
import {DontTouchThis} from 'leave-me-alone';
import {MyAwesomeService, MyVeryOldService} from 'my-decent-package';
const myVeryOldService = new MyVeryOldService();
`;
const tree = Tree.empty();
tree.create(fileName, originalFileContent);
const sourceFile = ts.createSourceFile(fileName, originalFileContent, ts.ScriptTarget.ES2015, true);
const importsRegexp = /^MyVeryOldService$/;
const renamePackagesRegexp = /^my-decent-package$/;
/* eslint-disable @typescript-eslint/naming-convention */
const mapImports = {
'my-decent-package': {
MyVeryOldService: {
newPackage: 'my-brand-new-package',
newValue: 'MyVeryNewService'
}
}
};
const renamedPackages = {};
/* eslint-enable @typescript-eslint/naming-convention */

const unresolvedImports = updateImportsInFile(logger, tree, sourceFile, importsRegexp, renamePackagesRegexp, mapImports, renamedPackages);
const finalFileContent = tree.readText(fileName);

expect(unresolvedImports).toBe(1);
expect(finalFileContent).toContain('import {DontTouchThis} from \'leave-me-alone\';');
expect(finalFileContent).toContain('import {MyAwesomeService} from \'my-decent-package\';');
expect(finalFileContent).toContain('import {MyVeryNewService} from \'my-brand-new-package\';');
expect(finalFileContent).toContain('const myVeryOldService = new MyVeryNewService();');
});

it('should rename packages', () => {
const fileName = 'test.ts';
const originalFileContent = `
import {DontTouchThis} from 'leave-me-alone';
import {MyAwesomeService, MyVeryOldService} from 'my-decent-package';
const myVeryOldService = new MyVeryOldService();
`;
const tree = Tree.empty();
tree.create(fileName, originalFileContent);
const sourceFile = ts.createSourceFile(fileName, originalFileContent, ts.ScriptTarget.ES2015, true);
const importsRegexp = /^MyVeryOldService$/;
const renamePackagesRegexp = /^my-decent-package$/;
/* eslint-disable @typescript-eslint/naming-convention */
const mapImports = {
'my-decent-package': {
MyVeryOldService: {
newPackage: 'my-brand-new-package',
newValue: 'MyVeryNewService'
}
}
};
const renamedPackages = {
'my-decent-package': 'my-slightly-better-package'
};
/* eslint-enable @typescript-eslint/naming-convention */

const unresolvedImports = updateImportsInFile(logger, tree, sourceFile, importsRegexp, renamePackagesRegexp, mapImports, renamedPackages);
const finalFileContent = tree.readText(fileName);

expect(unresolvedImports).toBe(0);
expect(finalFileContent).toContain('import {DontTouchThis} from \'leave-me-alone\';');
expect(finalFileContent).toContain('import {MyAwesomeService, MyVeryNewService} from \'my-slightly-better-package\';');
expect(finalFileContent).toContain('const myVeryOldService = new MyVeryNewService();');
expect(finalFileContent).not.toContain('my-decent-package');
});
});
30 changes: 23 additions & 7 deletions packages/@o3r/schematics/src/utility/update-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export function updateImportsInFile(

const renamePackageMatch = importFrom.match(renamePackagesRegexp);

const otterPackage = renamePackageMatch ? renamePackageMatch[0] : importFrom.match(importsRegexp)?.[0];
const otterPackage = renamePackageMatch?.[0] || importFrom.match(importsRegexp)?.[0];

// If the import matched an Otter package
if (otterPackage) {
Expand Down Expand Up @@ -84,7 +84,7 @@ export function updateImportsInFile(
importsFromOldPackage.forEach((importSymbol) => {

let newPackageNameImport;
if (Object.keys(renamedPackages).indexOf(oldPackageName) > 0) {
if (renamedPackages[oldPackageName]) {
newPackageNameImport = importSymbol.location.replace(oldPackageName, renamedPackages[oldPackageName]);
} else {
newPackageNameImport = mapImports[oldPackageName]?.[importSymbol.symbol]?.newPackage;
Expand All @@ -103,14 +103,30 @@ export function updateImportsInFile(
return acc;
}, {} as Record<string, string[]>);

let fileContent = tree.readText(sourceFile.fileName);
const escapeRegExp = (str: string) => str.replace(/[-[\]/{}()*+?.\\^$|]/g, '\\$&');

// Remove captured imports
const fileWithoutImports = tree.read(sourceFile.fileName)!.toString()
.replace(new RegExp(`(${importNodes.map((node) => node.getText()).join('|')})[\\n\\r]*`, 'g'), '');
fileContent = fileContent.replace(new RegExp(`(${importNodes.map((node) => escapeRegExp(node.getText())).join('|')})[\\n\\r]*`, 'g'), '');

// Replace imported values
const valuesToReplace = Object.fromEntries(Object.values(mapImports).flatMap((mapImport) =>
Object.entries(mapImport)
.filter(([_, value]) => value.newValue)
.map(([key, value]) => [key, value.newValue!])
));
if (Object.keys(valuesToReplace).length) {
const matcher = new RegExp(Object.keys(valuesToReplace).map((oldValue) => `\\b${escapeRegExp(oldValue)}\\b`).join('|'), 'g');
const replacer = (match: string) => valuesToReplace[match];
fileContent = fileContent.replace(matcher, replacer);
}

// Add the computed imports at the top of the file
const fileWithNewImports = Object.entries(resolvedImports)
fileContent = Object.entries(resolvedImports)
.map(([importFrom, imports]) => `import {${imports.join(', ')}} from '${importFrom}';`)
.join('\n') + '\n' + fileWithoutImports;
tree.overwrite(sourceFile.fileName, fileWithNewImports);
.join('\n') + '\n' + fileContent;

tree.overwrite(sourceFile.fileName, fileContent);

// Log details about imports for which we could not find an associated sub-entry
if (unResolvedImports.length > 0) {
Expand Down
2 changes: 1 addition & 1 deletion packages/@o3r/schematics/src/utility/update-pipes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export const updatePipes = (pipeReplacementInfo: PipeReplacementInfo): Rule => (
).filter((matcher) => !!pipeReplacementInfo[matcher[1]]);
if (matchers.length) {
const directory = dirname(file.path);
const baseFileName = basename(file.path, '.template.html');
const baseFileName = basename(basename(file.path, '.html'), '.template');
const componentFile = join(directory, `${baseFileName}.component.ts`);
const moduleFile = join(directory, `${baseFileName}.module.ts`);
let standalone = false;
Expand Down
2 changes: 1 addition & 1 deletion packages/@o3r/styling/migration.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"schematics": {
"migration-v10_0": {
"version": "10.0.0-alpha.0",
"description": "Updates of the Otter Styling package to v10.0.*",
"description": "Updates of @o3r/styling to v10.0.*",
"factory": "./schematics/ng-update/index#updateV10_0"
}
}
Expand Down
6 changes: 3 additions & 3 deletions packages/@o3r/testing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
"main": "public_api.js",
"typings": "public_api.d.ts",
"types": "public_api.d.ts",
"ng-update": {
"migrations": "./migration.json"
},
"sideEffects": false,
"scripts": {
"nx": "nx",
Expand Down Expand Up @@ -226,6 +223,9 @@
"zone.js": "~0.14.2"
},
"schematics": "./collection.json",
"ng-update": {
"migrations": "./migration.json"
},
"engines": {
"node": ">=18.0.0"
}
Expand Down
Loading

0 comments on commit e54d273

Please sign in to comment.