Created
May 17, 2016 12:42
-
-
Save a-x-/ea451100b1ac89d15ffe95c74a6973e5 to your computer and use it in GitHub Desktop.
Enumerate files which use i18n and nearest *.i18n/ dirs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use strict'; | |
| require('shelljs/global'); | |
| const fs = require('then-fs'); | |
| const _ = require('lodash'); | |
| const Path = require('path'); | |
| const dirname = path => Path.dirname(path); | |
| const basename = path => Path.basename(path); | |
| const baseBemName = path => basename(path).match(/^[^.]+/)[0]; // without tech | |
| const isexist = require('isexist'); | |
| const through = require('through'); | |
| const throughPass = fn => through(function(value) { fn(value); this.queue(value); }); | |
| const bemNaming = require('bem-naming'); | |
| const bemParse = bemNaming.parse; | |
| const bemOmit = (bemObj, omitParts) => { | |
| const omitParts_ = _.flatMap([omitParts], part => part === 'mod' ? ['modVal', 'modName'] : part); | |
| return bemNaming.stringify(_.omit(bemObj, omitParts_)); | |
| }; | |
| const getLevels = require('.').getLevels; | |
| const levels = _(getLevels()).values().flatten().map('path').uniq().value(); | |
| const techs = ['js', 'bemhtml.js']; | |
| const getI18nUsingFiles = (levels, techs) => { | |
| // let bemFind = require('bem-tools-find'); | |
| // const stringify = require('JSONStream').stringify; | |
| const walk = require('bem-walk'); | |
| const includes = require('array-includes'); | |
| // simple bem-find | |
| const bemFind = (query, levels, scheme) => { | |
| const tech = _.flatten([query.tech]); | |
| const config = { | |
| levels: _(levels).invert().mapValues(function() { | |
| return { scheme: scheme || 'nested' }; | |
| }).value() | |
| }; | |
| const filterTech = through(json => includes(tech, json.tech) && filterTech.queue(json)); | |
| const pluckPath = through(json => pluckPath.queue(json.path)); | |
| return walk(levels, config) | |
| .pipe(filterTech) | |
| .pipe(pluckPath); | |
| }; | |
| const filterI18n = through(path => { | |
| filterI18n.pause(); | |
| fs.readFile(path).then( | |
| file => { | |
| filterI18n.resume(); | |
| /BEM\.I18N/.test(file) && filterI18n.queue(path) | |
| } | |
| ); | |
| }); | |
| // debug | |
| process.argv[2] === 'techs' && console.log(tech); | |
| process.argv[2] === 'levels' && console.log(JSON.stringify(levels)); | |
| return bemFind({ tech: techs }, levels) | |
| .pipe(filterI18n); | |
| // bemFind({ | |
| // techs: ['js'], | |
| // levels: levels | |
| // }).pipe(process.stdout); | |
| }; | |
| const processDepsFile = (sourcePath) => { | |
| const bemName = baseBemName(sourcePath); | |
| const bemObj = bemParse(bemName); | |
| const nearestI18nDir = findNearestI18nDir(sourcePath); | |
| console.log('nearestI18nDir', nearestI18nDir); | |
| if (nearestI18nDir !== `${dirname(sourcePath)}/${bemName}.i18n`) { | |
| // ... | |
| } | |
| // ... | |
| }; | |
| // todo: Учитывать так же *.{js,bemhtml,priv}-i18n/ | |
| // returns {?DirPath} | |
| const findNearestI18nDir = (sourcePath) => { | |
| const bemName = baseBemName(sourcePath); | |
| const bemObj = bemParse(bemName); | |
| const dir = dirname(sourcePath); | |
| const check = (dir) => isexist(dir) && Path.resolve(dir); | |
| return check(`${dir}/${bemName}.i18n`) || (() => { | |
| switch(bemNaming.typeOf(bemObj)) { | |
| case ('blockMod'): return check(`${dir}/../${bemObj.block}.i18n`); | |
| case ('elemMod'): return check(`${dir}/../${bemOmit(bemObj, 'mod')}.i18n`) | |
| || check(`${dir}/../../${bemObj.block}.i18n`); | |
| case ('elem'): return check(`${dir}/../${bemObj.block}.i18n`); | |
| throw 'Этого никогда не должно было произойти 😱'; | |
| } | |
| })(); | |
| }; | |
| // | |
| // ==================== | |
| // | |
| // | |
| // 1 STEP | |
| // | |
| const i18nUsingFilesStream = getI18nUsingFiles(levels, techs); | |
| // | |
| // 2 STEP | |
| // | |
| i18nUsingFilesStream | |
| .on('error', function(e){console.error(e);}) | |
| .pipe(throughPass(path => processDepsFile(path)})) | |
| // .pipe(stringify()) | |
| .pipe(through(function (path) { this.queue(path+'\n'); }))//fixme:tmp | |
| .pipe(process.stdout); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment