Usage examples: data transformation
const mockfs = require('mock-fs');
const fs = require('fs');
const walk = require('@bem/sdk.walk');
const react = require('@bem/sdk.naming.presets/react');
const { promisify } = require('util');
const readFileAsync = promisify(fs.readFile);
const toArray = require('stream-to-array');
mockfs({
'src/components': {
'btn': {
'btn.js': 'alert(1)',
'btn.css': '.btn { color: red }',
'btn.examples': { 'all.bemjson.js': '({ block: "btn" })' },
'btn@desktop.js': 'alert(2)',
'btn@touch.js': 'alert(3)',
'text/btn-text.js': '// oh really\n',
},
},
'node_modules/react-components': {
'input': {
'input.ts': 'alert(4)',
'input.styl': '.btn { color: red }',
'input@desktop.ts': 'alert(5)',
'input@touch.ts': 'alert(6)',
}
}
});
await toArray(walk(
['src/components', 'node_modules/react-components'],
{ defaults: { naming: react } }
)).then(async (files) => {
const res = [];
for (const file of files) {
let source = null;
try {
source = await readFileAsync(file.path, 'utf-8');
} catch (e) {
if (e.code !== 'EBADF' && e.code !== 'ENOENT') {
throw e;
}
}
res.push({ file, source });
}
return res;
});
no comments