@viewdo/dxp-story-cli
Version:
README.md
63 lines (49 loc) • 1.59 kB
JavaScript
const fs = require('fs')
const path = require('path')
function ensureDirectoryExistence(filePath) {
var dirname = path.dirname(filePath);
if (fs.existsSync(dirname)) {
return true;
}
ensureDirectoryExistence(dirname);
fs.mkdirSync(dirname);
}
const find_strip_html = (node, found = [], path, level = 0) => {
Object.keys(node).forEach((key) => {
let htmlProp = /(^.*html)$/gim
if(typeof key == 'string' && htmlProp.test(key)) {
found.push({
path: `${path}.html`,
content: node[key]
})
node[key] = `${path}.html`
return found
}
if(typeof node[key] === 'object'){
let new_key = `${path}/${node[key].id||key}`
find_strip_html(node[key], found, new_key, level++)
}
});
return found
};
module.exports = async (options, next = 0) => {
let result = next
let {
file,
output,
dir
} = options
let json_object = JSON.parse(fs.readFileSync(file))
let file_name = path.basename(file, '.json')
let results = find_strip_html(json_object, [], `${dir}/${file_name}`)
// remove empty/null properties
Object.keys(json_object).forEach(k => (!json_object[k] && json_object[k] !== undefined) && delete json_object[k])
results.forEach(file_node => {
let relative_path = path.join(path.dirname(file), file_node.path)
ensureDirectoryExistence(relative_path)
fs.writeFileSync(relative_path, file_node.content)
})
//let relative_path = path.join(path.dirname(file), 'stripped-story')
fs.writeFileSync(output, JSON.stringify(json_object, null, 2))
return result
}