mirror of
https://github.com/angular/angular-cli.git
synced 2025-05-17 11:03:53 +08:00
fix(@schematics/angular): add baseUrl
in root tsconfig when migrating
Closes: #11258
This commit is contained in:
parent
76f46bc00a
commit
e6b59e44ef
@ -32,6 +32,7 @@ import {
|
|||||||
addPackageJsonDependency,
|
addPackageJsonDependency,
|
||||||
} from '../../utility/dependencies';
|
} from '../../utility/dependencies';
|
||||||
import {
|
import {
|
||||||
|
appendPropertyInAstObject,
|
||||||
appendValueInAstArray,
|
appendValueInAstArray,
|
||||||
findPropertyInAstObject,
|
findPropertyInAstObject,
|
||||||
} from '../../utility/json-utils';
|
} from '../../utility/json-utils';
|
||||||
@ -724,6 +725,44 @@ function updateTsLintConfig(): Rule {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function updateRootTsConfig(): Rule {
|
||||||
|
return (host: Tree, context: SchematicContext) => {
|
||||||
|
const tsConfigPath = '/tsconfig.json';
|
||||||
|
const buffer = host.read(tsConfigPath);
|
||||||
|
if (!buffer) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const tsCfgAst = parseJsonAst(buffer.toString(), JsonParseMode.Loose);
|
||||||
|
if (tsCfgAst.kind != 'object') {
|
||||||
|
throw new SchematicsException(
|
||||||
|
'Invalid tsconfig. Was expecting an object'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const compilerOptionsAstNode = findPropertyInAstObject(tsCfgAst, 'compilerOptions');
|
||||||
|
if (!compilerOptionsAstNode || compilerOptionsAstNode.kind != 'object') {
|
||||||
|
throw new SchematicsException('Invalid tsconfig "compilerOptions" property; expected an object.');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (findPropertyInAstObject(compilerOptionsAstNode, 'baseUrl')) {
|
||||||
|
return host;
|
||||||
|
}
|
||||||
|
|
||||||
|
const recorder = host.beginUpdate(tsConfigPath);
|
||||||
|
appendPropertyInAstObject(
|
||||||
|
recorder,
|
||||||
|
compilerOptionsAstNode,
|
||||||
|
'baseUrl',
|
||||||
|
'./',
|
||||||
|
4,
|
||||||
|
);
|
||||||
|
|
||||||
|
host.commitUpdate(recorder);
|
||||||
|
return host;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export default function (): Rule {
|
export default function (): Rule {
|
||||||
return (host: Tree, context: SchematicContext) => {
|
return (host: Tree, context: SchematicContext) => {
|
||||||
if (host.exists('/.angular.json') || host.exists('/angular.json')) {
|
if (host.exists('/.angular.json') || host.exists('/angular.json')) {
|
||||||
@ -748,6 +787,7 @@ export default function (): Rule {
|
|||||||
migrateConfiguration(config, context.logger),
|
migrateConfiguration(config, context.logger),
|
||||||
updateSpecTsConfig(config),
|
updateSpecTsConfig(config),
|
||||||
updatePackageJson(config),
|
updatePackageJson(config),
|
||||||
|
updateRootTsConfig(),
|
||||||
updateTsLintConfig(),
|
updateTsLintConfig(),
|
||||||
(host: Tree, context: SchematicContext) => {
|
(host: Tree, context: SchematicContext) => {
|
||||||
context.logger.warn(tags.oneLine`Some configuration options have been changed,
|
context.logger.warn(tags.oneLine`Some configuration options have been changed,
|
||||||
|
@ -775,6 +775,27 @@ describe('Migration to v6', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('root ts config', () => {
|
||||||
|
const rootTsConfig = '/tsconfig.json';
|
||||||
|
beforeEach(() => {
|
||||||
|
tree.create(rootTsConfig, `
|
||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"module": "es2015"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should add baseUrl', () => {
|
||||||
|
tree.create(oldConfigPath, JSON.stringify(baseConfig, null, 2));
|
||||||
|
tree = schematicRunner.runSchematic('migration-01', defaultOptions, tree);
|
||||||
|
const content = tree.readContent(rootTsConfig);
|
||||||
|
const config = JSON.parse(content);
|
||||||
|
expect(config.compilerOptions.baseUrl).toEqual('./');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('package.json', () => {
|
describe('package.json', () => {
|
||||||
it('should add a dev dependency to @angular-devkit/build-angular', () => {
|
it('should add a dev dependency to @angular-devkit/build-angular', () => {
|
||||||
tree.create(oldConfigPath, JSON.stringify(baseConfig, null, 2));
|
tree.create(oldConfigPath, JSON.stringify(baseConfig, null, 2));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user