|
| 1 | +import * as path from 'path'; |
| 2 | +import * as fs from 'fs'; |
| 3 | +import * as ts from 'typescript'; |
| 4 | + |
| 5 | +import { findAstNodes, getFirstNode } from './ast_helpers'; |
| 6 | +import { AddNodeOperation, TransformOperation } from './make_transform'; |
| 7 | + |
| 8 | + |
| 9 | +export function registerLocaleData( |
| 10 | + sourceFile: ts.SourceFile, |
| 11 | + entryModule: { path: string, className: string }, |
| 12 | + locale: string |
| 13 | +): TransformOperation[] { |
| 14 | + const ops: TransformOperation[] = []; |
| 15 | + |
| 16 | + // Find all identifiers using the entry module class name. |
| 17 | + const entryModuleIdentifiers = findAstNodes<ts.Identifier>(null, sourceFile, |
| 18 | + ts.SyntaxKind.Identifier, true) |
| 19 | + .filter(identifier => identifier.getText() === entryModule.className); |
| 20 | + |
| 21 | + if (entryModuleIdentifiers.length === 0) { |
| 22 | + return []; |
| 23 | + } |
| 24 | + |
| 25 | + // Find the bootstrap call |
| 26 | + entryModuleIdentifiers.forEach(entryModuleIdentifier => { |
| 27 | + // Figure out if it's a `platformBrowserDynamic().bootstrapModule(AppModule)` call. |
| 28 | + if (!( |
| 29 | + entryModuleIdentifier.parent |
| 30 | + && entryModuleIdentifier.parent.kind === ts.SyntaxKind.CallExpression |
| 31 | + )) { |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + const callExpr = entryModuleIdentifier.parent as ts.CallExpression; |
| 36 | + |
| 37 | + if (callExpr.expression.kind !== ts.SyntaxKind.PropertyAccessExpression) { |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + const propAccessExpr = callExpr.expression as ts.PropertyAccessExpression; |
| 42 | + |
| 43 | + if (propAccessExpr.name.text !== 'bootstrapModule' |
| 44 | + || propAccessExpr.expression.kind !== ts.SyntaxKind.CallExpression) { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + // get the path of the common module |
| 49 | + const commonPath = path.dirname(require.resolve('@angular/common/package.json')); |
| 50 | + // check if the locale file exists |
| 51 | + if (!fs.existsSync(path.resolve(commonPath, 'locales', `${locale}.js`))) { |
| 52 | + // check for an alternative locale (if the locale id was badly formatted) |
| 53 | + const locales = fs.readdirSync(path.resolve(commonPath, 'locales')) |
| 54 | + .filter(file => file.endsWith('.js')) |
| 55 | + .map(file => file.replace('.js', '')); |
| 56 | + |
| 57 | + let newLocale; |
| 58 | + const normalizedLocale = locale.toLowerCase().replace(/_/g, '-'); |
| 59 | + for (const l of locales) { |
| 60 | + if (l.toLowerCase() === normalizedLocale) { |
| 61 | + newLocale = l; |
| 62 | + break; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + if (newLocale) { |
| 67 | + locale = newLocale; |
| 68 | + } else { |
| 69 | + // check for a parent locale |
| 70 | + const parentLocale = normalizedLocale.split('-')[0]; |
| 71 | + if (locales.indexOf(parentLocale) !== -1) { |
| 72 | + locale = parentLocale; |
| 73 | + } else { |
| 74 | + throw new Error( |
| 75 | + `Unable to load the locale data file "@angular/common/locales/${locale}", ` + |
| 76 | + `please check that "${locale}" is a valid locale id.`); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + // Create the import node for the locale. |
| 82 | + const localeIdentifier = ts.createIdentifier(`__locale_${locale.replace(/-/g, '')}__`); |
| 83 | + const localeImportClause = ts.createImportClause(localeIdentifier, undefined); |
| 84 | + const localeNewImport = ts.createImportDeclaration(undefined, undefined, localeImportClause, |
| 85 | + ts.createLiteral(`@angular/common/locales/${locale}`)); |
| 86 | + |
| 87 | + ops.push(new AddNodeOperation( |
| 88 | + sourceFile, |
| 89 | + getFirstNode(sourceFile), |
| 90 | + localeNewImport |
| 91 | + )); |
| 92 | + |
| 93 | + // Create the import node for the registerLocaleData function. |
| 94 | + const regIdentifier = ts.createIdentifier(`registerLocaleData`); |
| 95 | + const regImportSpecifier = ts.createImportSpecifier(regIdentifier, regIdentifier); |
| 96 | + const regNamedImport = ts.createNamedImports([regImportSpecifier]); |
| 97 | + const regImportClause = ts.createImportClause(undefined, regNamedImport); |
| 98 | + const regNewImport = ts.createImportDeclaration(undefined, undefined, regImportClause, |
| 99 | + ts.createLiteral('@angular/common')); |
| 100 | + |
| 101 | + ops.push(new AddNodeOperation( |
| 102 | + sourceFile, |
| 103 | + getFirstNode(sourceFile), |
| 104 | + regNewImport |
| 105 | + )); |
| 106 | + |
| 107 | + // Create the register function call |
| 108 | + const registerFunctionCall = ts.createCall(regIdentifier, undefined, [localeIdentifier]); |
| 109 | + const registerFunctionStatement = ts.createStatement(registerFunctionCall); |
| 110 | + |
| 111 | + ops.push(new AddNodeOperation( |
| 112 | + sourceFile, |
| 113 | + getFirstNode(sourceFile), |
| 114 | + registerFunctionStatement |
| 115 | + )); |
| 116 | + }); |
| 117 | + |
| 118 | + return ops; |
| 119 | +} |
0 commit comments