Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | 2x 2x 2x 2x 2x 37x 37x 14x 10x 10x 17x 17x 17x 17x 7x 10x 8x 8x 8x 8x 8x 10x 10x 6x 6x 6x 6x 5x 5x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 12x 12x 12x 12x 12x 12x 6x 6x 6x 12x 12x 11x 12x 11x 1x 5x 6x 6x 6x 6x 6x 6x 6x 2x 2x 12x 6x 6x 11x 1x 1x 2x 10x 2x | const chalk = require('chalk'); // tslint:disable-line import fs = require('fs'); import fse = require('fs-extra'); import globby = require('globby'); import pMap = require('p-map'); import path = require('path'); // Definitions export interface IOptions { source?: string | object; sourceRoot?: string; dataDir?: string; localesDir?: string; localesFile?: string; delimiter?: string; } interface IOutput { data: any; locales: any; } function isIterable(value: any): boolean { const type = Object.prototype.toString.call(value); return type === '[object Object]' || type === '[object Array]'; } export function parser(source: object, delimiter: string = '.'): IOutput { const data = { ...source }; const locales = {}; function parse(object: object, prev?: string) { Object.keys(object).forEach(key => { const value = object[key]; let newKey = prev ? prev + delimiter + key : key; if (isIterable(value) && Object.keys(value).length) { return parse(value, newKey); } if (key.match(/^\$.+/)) { const i = newKey.lastIndexOf(key); const localeStr = newKey.slice(0, i) + newKey.slice(i + 1); object[key] = localeStr; newKey = localeStr; locales[newKey] = value; } }); } parse(data); return { data, locales }; } function writeData( filepath: string, filename: string, json: string ): Promise<void> { return new Promise(resolve => { fse.outputFile(filepath, json, err => { /* istanbul ignore next */ if (err) { throw err; } console.log(`📦 ${chalk.green('Data have been saved!')} [${filename}]`); resolve(); }); }); } function writeLocales( filepath: string, filename: string, json: string ): Promise<void> { return new Promise(resolve => { fs.writeFile(filepath, json, err => { /* istanbul ignore next */ if (err) { throw err; } console.log( `💬 ${chalk.green('Locales have been saved!')} [${filename}]` ); resolve(); }); }); } function updateLocales( filepath: string, filename: string, data: any ): Promise<void> { return new Promise(resolve => { const locales = require(filepath); Object.keys(data).forEach(key => { /* istanbul ignore else */ if (locales[key] === undefined) { locales[key] = data[key]; } }); const sortedLocales = {}; Object.keys(locales) .sort() .forEach(key => { sortedLocales[key] = locales[key]; }); fs.writeFile(filepath, JSON.stringify(sortedLocales, null, 2), err => { /* istanbul ignore next */ if (err) { throw err; } console.log( `💬 ${chalk.green('Locales have been updated!')} [${filename}]` ); resolve(); }); }); } async function run({ source = 'data.js', sourceRoot = '', dataDir = 'data', localesDir = 'locales', localesFile = 'locales', delimiter = '.', }: IOptions = {}) { const dest = { data: path.resolve(process.cwd(), dataDir), locales: path.resolve(process.cwd(), localesDir), }; let locales = {}; let entries: Array<string | object>; // Check/create destination directories Object.keys(dest).forEach(key => { try { /* istanbul ignore else */ if (!fs.existsSync(dest[key])) { fs.mkdirSync(dest[key]); } console.info( `${key.toUpperCase()} directory: ${chalk.yellow(dest[key])}` ); } catch (err) { /* istanbul ignore next */ console.error(err); } }); if (isIterable(source)) { entries = [source]; } else { entries = await globby(source as string); } entries.forEach(async entry => { const { dir, base, name, ext } = isIterable(entry) ? { base: 'data.json', dir: '', ext: 'json', name: 'data', } : path.parse(entry as string); const raw = isIterable(entry) ? entry : require(path.resolve(process.cwd(), entry as string)); const { data, locales: newLocales } = parser(raw, delimiter); // Merge locales locales = { ...locales, ...newLocales, }; let filePath = `${name}.json`; if (sourceRoot !== '' && !isIterable(entry)) { // Be sure to have a trailing slash const root = `${sourceRoot.replace(/\/$/, '')}/`; filePath = (entry as string).replace(root, '').replace(/\.js$/, '.json'); } await writeData( path.join(dest.data, filePath), filePath, JSON.stringify(data, null, 2) ); }); // Write locales // Check if existing files, then update // If no locales, create one… const localesPaths = await globby(`${dest.locales}/*.json`); const localesData = JSON.stringify(locales, null, 2); if (localesPaths.length > 0) { localesPaths.forEach(async localesPath => { const { base } = path.parse(localesPath as string); await updateLocales(localesPath, base, locales); }); } else { await writeLocales( path.join(dest.locales, `${localesFile}.json`), `${localesFile}.json`, localesData ); } } export default run; |