Skip to content

Commit

Permalink
fix(@schematics/angular): workaround schematic/json AST issues in 9.0…
Browse files Browse the repository at this point in the history
… i18n migration
  • Loading branch information
clydin authored and vikerman committed Dec 13, 2019
1 parent c257515 commit dc2d460
Showing 1 changed file with 83 additions and 32 deletions.
115 changes: 83 additions & 32 deletions packages/schematics/angular/migrations/update-9/update-i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,63 @@ import { getAllOptions, getProjectTarget, getTargets, getWorkspace } from './uti

export function updateI18nConfig(): Rule {
return (tree, context) => {
// this is whole process of partial change writing and repeat loading/looping is only necessary
// to workaround underlying issues with the recorder and ast helper functions

const workspacePath = getWorkspacePath(tree);
const workspace = getWorkspace(tree);
const recorder = tree.beginUpdate(workspacePath);
let workspaceAst = getWorkspace(tree);

for (const { target } of getTargets(workspace, 'build', Builders.Browser)) {
addBuilderI18NOptions(recorder, target, context.logger);
// Update extract targets
const extractTargets = getTargets(workspaceAst, 'extract-i18n', Builders.ExtractI18n);
if (extractTargets.length > 0) {
const recorder = tree.beginUpdate(workspacePath);

for (const { target, project } of extractTargets) {
addProjectI18NOptions(recorder, tree, target, project);
removeExtracti18nDeprecatedOptions(recorder, target);
}

tree.commitUpdate(recorder);

// workspace was changed so need to reload
workspaceAst = getWorkspace(tree);
}

for (const { target } of getTargets(workspace, 'test', Builders.Karma)) {
addBuilderI18NOptions(recorder, target, context.logger);
// Update base HREF values for existing configurations
let recorder = tree.beginUpdate(workspacePath);
for (const { target } of getTargets(workspaceAst, 'build', Builders.Browser)) {
updateBaseHrefs(recorder, target);
}
for (const { target } of getTargets(workspaceAst, 'test', Builders.Karma)) {
updateBaseHrefs(recorder, target);
}
tree.commitUpdate(recorder);

for (const { target, project } of getTargets(workspace, 'extract-i18n', Builders.ExtractI18n)) {
addProjectI18NOptions(recorder, tree, target, project);
removeExtracti18nDeprecatedOptions(recorder, target);
// Remove i18n format option
workspaceAst = getWorkspace(tree);
recorder = tree.beginUpdate(workspacePath);
for (const { target } of getTargets(workspaceAst, 'build', Builders.Browser)) {
removeFormatOption(recorder, target);
}
for (const { target } of getTargets(workspaceAst, 'test', Builders.Karma)) {
removeFormatOption(recorder, target);
}
tree.commitUpdate(recorder);

// Add new i18n options to build target configurations
workspaceAst = getWorkspace(tree);
recorder = tree.beginUpdate(workspacePath);
for (const { target } of getTargets(workspaceAst, 'build', Builders.Browser)) {
addBuilderI18NOptions(recorder, target, context.logger);
}
tree.commitUpdate(recorder);

// Add new i18n options to test target configurations
workspaceAst = getWorkspace(tree);
recorder = tree.beginUpdate(workspacePath);
for (const { target } of getTargets(workspaceAst, 'test', Builders.Karma)) {
addBuilderI18NOptions(recorder, target, context.logger);
}
tree.commitUpdate(recorder);

return tree;
Expand Down Expand Up @@ -139,24 +179,11 @@ function addBuilderI18NOptions(
logger: logging.LoggerApi,
) {
const options = getAllOptions(builderConfig);
const mainOptions = findPropertyInAstObject(builderConfig, 'options');
const mainBaseHref =
mainOptions &&
mainOptions.kind === 'object' &&
findPropertyInAstObject(mainOptions, 'baseHref');
const hasMainBaseHref =
!!mainBaseHref && mainBaseHref.kind === 'string' && mainBaseHref.value !== '/';

for (const option of options) {
const localeId = findPropertyInAstObject(option, 'i18nLocale');
const i18nFile = findPropertyInAstObject(option, 'i18nFile');

// The format is always auto-detected now
const i18nFormat = findPropertyInAstObject(option, 'i18nFormat');
if (i18nFormat) {
removePropertyInAstObject(recorder, option, 'i18nFormat');
}

const outputPath = findPropertyInAstObject(option, 'outputPath');
if (
localeId &&
Expand All @@ -165,16 +192,6 @@ function addBuilderI18NOptions(
outputPath &&
outputPath.kind === 'string'
) {
// This first block was intended to remove the redundant output path field
// but due to defects in the recorder, removing the option will cause malformed json
// if (
// mainOutputPathValue &&
// outputPath.value.match(
// new RegExp(`[/\\\\]?${mainOutputPathValue}[/\\\\]${localeId.value}[/\\\\]?$`),
// )
// ) {
// removePropertyInAstObject(recorder, option, 'outputPath');
// } else
if (outputPath.value.match(new RegExp(`[/\\\\]${localeId.value}[/\\\\]?$`))) {
const newOutputPath = outputPath.value.replace(
new RegExp(`[/\\\\]${localeId.value}[/\\\\]?$`),
Expand Down Expand Up @@ -206,6 +223,40 @@ function addBuilderI18NOptions(
if (i18nFile) {
removePropertyInAstObject(recorder, option, 'i18nFile');
}
}
}

function removeFormatOption(
recorder: UpdateRecorder,
builderConfig: JsonAstObject,
) {
const options = getAllOptions(builderConfig);

for (const option of options) {
// The format is always auto-detected now
const i18nFormat = findPropertyInAstObject(option, 'i18nFormat');
if (i18nFormat) {
removePropertyInAstObject(recorder, option, 'i18nFormat');
}
}
}

function updateBaseHrefs(
recorder: UpdateRecorder,
builderConfig: JsonAstObject,
) {
const options = getAllOptions(builderConfig);
const mainOptions = findPropertyInAstObject(builderConfig, 'options');
const mainBaseHref =
mainOptions &&
mainOptions.kind === 'object' &&
findPropertyInAstObject(mainOptions, 'baseHref');
const hasMainBaseHref =
!!mainBaseHref && mainBaseHref.kind === 'string' && mainBaseHref.value !== '/';

for (const option of options) {
const localeId = findPropertyInAstObject(option, 'i18nLocale');
const i18nFile = findPropertyInAstObject(option, 'i18nFile');

// localize base HREF values are controlled by the i18n configuration
const baseHref = findPropertyInAstObject(option, 'baseHref');
Expand Down

0 comments on commit dc2d460

Please sign in to comment.