|
| 1 | +import { PathFragment } from '@angular-devkit/core'; |
| 2 | +import { Rule, Tree, SchematicContext, DirEntry, SchematicsException, EmptyTree } from '@angular-devkit/schematics'; |
| 3 | +import { from } from 'rxjs'; |
| 4 | +import { getProject } from '../utils/projects'; |
| 5 | +import { SchemaOptions } from './schema'; |
| 6 | +const glob = require('glob'); |
| 7 | + |
| 8 | +const p = require('path'); |
| 9 | + |
| 10 | +function getFileContent(fileName: PathFragment, dir: DirEntry) { |
| 11 | + return JSON.parse(dir.file(fileName).content.toString('utf-8')); |
| 12 | +} |
| 13 | + |
| 14 | +function hasSubdirs(dir: DirEntry) { |
| 15 | + return dir.subdirs && dir.subdirs.length; |
| 16 | +} |
| 17 | + |
| 18 | +function getTranslationKey(prefix = '', key) { |
| 19 | + return prefix ? `${prefix}.${key}` : key; |
| 20 | +} |
| 21 | + |
| 22 | +function reduceTranslations(host: Tree, dir: DirEntry, translationJson, lang: string, prefix = '') { |
| 23 | + if (!hasSubdirs(dir)) return translationJson; |
| 24 | + dir.subdirs.forEach(subDirName => { |
| 25 | + const subDir = dir.dir(subDirName); |
| 26 | + const key = getTranslationKey(prefix, subDirName); |
| 27 | + subDir.subfiles |
| 28 | + .filter(fileName => fileName.includes(`${lang}.json`)) |
| 29 | + .forEach(fileName => { |
| 30 | + if (translationJson[key]) { |
| 31 | + throw new SchematicsException( |
| 32 | + `key: ${key} is already exist in translation file, please rename it and rerun the command.` |
| 33 | + ); |
| 34 | + } |
| 35 | + translationJson[key] = getFileContent(fileName, subDir); |
| 36 | + }); |
| 37 | + if (hasSubdirs(subDir)) { |
| 38 | + reduceTranslations(host, subDir, translationJson, lang, key); |
| 39 | + } |
| 40 | + }); |
| 41 | + |
| 42 | + return translationJson; |
| 43 | +} |
| 44 | + |
| 45 | +function reduceTranslations2(path: string, translationJson: object, lang: string, prefix = '') { |
| 46 | + glob(path, {}, function(er, files) { |
| 47 | + console.log(files); |
| 48 | + }); |
| 49 | +} |
| 50 | + |
| 51 | +function getTranslationsRoot(host: Tree, options: SchemaOptions): string { |
| 52 | + const project = getProject(host, options.project); |
| 53 | + const rootPath = (project && project.sourceRoot) || 'src'; |
| 54 | + |
| 55 | + return options.rootTranslationPath ? options.rootTranslationPath : p.join(rootPath, 'assets', 'i18n'); |
| 56 | +} |
| 57 | + |
| 58 | +function getRootTranslationFiles(host: Tree, root: string): { lang: string; translation: Object }[] { |
| 59 | + const rootDir = host.getDir(root); |
| 60 | + return rootDir.subfiles.map(fileName => ({ |
| 61 | + lang: fileName.split('.')[0], |
| 62 | + translation: getFileContent(fileName, rootDir) |
| 63 | + })); |
| 64 | +} |
| 65 | + |
| 66 | +function getTranslationEntryPaths(options: SchemaOptions, rootDir: string): string[] { |
| 67 | + if (Array.isArray(options.translationPaths)) { |
| 68 | + return options.translationPaths; |
| 69 | + } else if (typeof options.translationPaths === 'string') { |
| 70 | + return options.translationPaths.split(','); |
| 71 | + } |
| 72 | + |
| 73 | + return [rootDir]; |
| 74 | +} |
| 75 | + |
| 76 | +export default function(options: SchemaOptions): Rule { |
| 77 | + return (host: Tree, context: SchematicContext) => { |
| 78 | + const path = process.cwd(); |
| 79 | + // console.log(p.join(path, 'src/assets/**/*.json')); |
| 80 | + // console.log(glob); |
| 81 | + // const promise = new Promise((res, rej) => { |
| 82 | + // |
| 83 | + // glob(`**/*.ts`, {}, function(er, files) { |
| 84 | + // console.log(files); |
| 85 | + // console.log(er); |
| 86 | + // res(); |
| 87 | + // }); |
| 88 | + // }); |
| 89 | + //console.log(a); |
| 90 | + const root = getTranslationsRoot(host, options); |
| 91 | + const rootTranslations = getRootTranslationFiles(host, root); |
| 92 | + const translationEntryPaths = getTranslationEntryPaths(options, root); |
| 93 | + |
| 94 | + const output = rootTranslations.map(t => ({ |
| 95 | + lang: t.lang, |
| 96 | + translation: translationEntryPaths.reduce((acc, path) => { |
| 97 | + return reduceTranslations(host, host.getDir(path), t.translation, t.lang); |
| 98 | + }, t.translation) |
| 99 | + })); |
| 100 | + |
| 101 | + const treeSource = new EmptyTree(); |
| 102 | + output.forEach(o => { |
| 103 | + treeSource.create(`${options.outDir}/${o.lang}.json`, JSON.stringify(o.translation, null, 2)); |
| 104 | + }); |
| 105 | + |
| 106 | + // return from(promise) as any; |
| 107 | + return treeSource; |
| 108 | + }; |
| 109 | +} |
0 commit comments