Skip to content

Commit 3b3389d

Browse files
alan-agius4clydin
authored andcommitted
fix(@schematics/angular): add baseUrl in root tsconfig when migrating
Closes: #11258
1 parent 24e3b94 commit 3b3389d

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

packages/schematics/angular/migrations/update-6/index.ts

+40
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
addPackageJsonDependency,
3333
} from '../../utility/dependencies';
3434
import {
35+
appendPropertyInAstObject,
3536
appendValueInAstArray,
3637
findPropertyInAstObject,
3738
} from '../../utility/json-utils';
@@ -724,6 +725,44 @@ function updateTsLintConfig(): Rule {
724725
};
725726
}
726727

728+
function updateRootTsConfig(): Rule {
729+
return (host: Tree, context: SchematicContext) => {
730+
const tsConfigPath = '/tsconfig.json';
731+
const buffer = host.read(tsConfigPath);
732+
if (!buffer) {
733+
return;
734+
}
735+
736+
const tsCfgAst = parseJsonAst(buffer.toString(), JsonParseMode.Loose);
737+
if (tsCfgAst.kind != 'object') {
738+
throw new SchematicsException(
739+
'Invalid tsconfig. Was expecting an object'
740+
);
741+
}
742+
743+
const compilerOptionsAstNode = findPropertyInAstObject(tsCfgAst, 'compilerOptions');
744+
if (!compilerOptionsAstNode || compilerOptionsAstNode.kind != 'object') {
745+
throw new SchematicsException('Invalid tsconfig "compilerOptions" property; expected an object.');
746+
}
747+
748+
if (findPropertyInAstObject(compilerOptionsAstNode, 'baseUrl')) {
749+
return host;
750+
}
751+
752+
const recorder = host.beginUpdate(tsConfigPath);
753+
appendPropertyInAstObject(
754+
recorder,
755+
compilerOptionsAstNode,
756+
'baseUrl',
757+
'./',
758+
4,
759+
);
760+
761+
host.commitUpdate(recorder);
762+
return host;
763+
};
764+
}
765+
727766
export default function (): Rule {
728767
return (host: Tree, context: SchematicContext) => {
729768
if (host.exists('/.angular.json') || host.exists('/angular.json')) {
@@ -748,6 +787,7 @@ export default function (): Rule {
748787
migrateConfiguration(config, context.logger),
749788
updateSpecTsConfig(config),
750789
updatePackageJson(config),
790+
updateRootTsConfig(),
751791
updateTsLintConfig(),
752792
(host: Tree, context: SchematicContext) => {
753793
context.logger.warn(tags.oneLine`Some configuration options have been changed,

packages/schematics/angular/migrations/update-6/index_spec.ts

+21
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,27 @@ describe('Migration to v6', () => {
775775
});
776776
});
777777

778+
describe('root ts config', () => {
779+
const rootTsConfig = '/tsconfig.json';
780+
beforeEach(() => {
781+
tree.create(rootTsConfig, `
782+
{
783+
"compilerOptions": {
784+
"module": "es2015"
785+
}
786+
}
787+
`);
788+
});
789+
790+
it('should add baseUrl', () => {
791+
tree.create(oldConfigPath, JSON.stringify(baseConfig, null, 2));
792+
tree = schematicRunner.runSchematic('migration-01', defaultOptions, tree);
793+
const content = tree.readContent(rootTsConfig);
794+
const config = JSON.parse(content);
795+
expect(config.compilerOptions.baseUrl).toEqual('./');
796+
});
797+
});
798+
778799
describe('package.json', () => {
779800
it('should add a dev dependency to @angular-devkit/build-angular', () => {
780801
tree.create(oldConfigPath, JSON.stringify(baseConfig, null, 2));

0 commit comments

Comments
 (0)