Last active
January 8, 2019 14:28
-
-
Save klaytonfaria/26676f47bc007159aefd9bd780538240 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env node | |
const fs = require('fs'); | |
const path = require('path'); | |
const sourcePath = process.argv[ 2 ] || './example/files.json'; | |
const fileList = require(path.resolve(__dirname, '../', sourcePath)); | |
const outputPath = process.argv[ 3 ] || './Static/'; | |
const findProcessTag = tag => process.argv.some(val => val.indexOf(tag) >= 0); | |
const mkdirSyncRecursive = directory => { | |
const currentPath = directory.replace(/\/$/, '').split('/'); | |
currentPath.forEach((item, i) => { | |
const chunk = currentPath.slice(0, i + 1).join('/'); | |
!fs.existsSync(chunk) ? fs.mkdirSync(chunk) : null; | |
}); | |
} | |
const getFilePath = ({ pageName, pageType, contentType, pageId }) => { | |
const fileName = `${pageName.replace(/:/g, '-')}-${pageId}.${contentType}`; | |
const distPath = findProcessTag("--gtm") ? `${outputPath}gtm/` : `${outputPath}almir-content/`; | |
return { | |
base: `${distPath}${pageType.replace(/\s/g, '-').toLowerCase()}/`, | |
fileName, | |
path: `${distPath}${pageType.replace(/\s/g, '-').toLowerCase()}/${fileName}` | |
}; | |
}; | |
const extractGTMContent = (data) => { | |
const { containerVersion: { tag, variable } } = data; | |
const content = [ ...tag, ...variable ]; | |
return content.filter(item => item.type.match(/html|jsm/)).map(item => ({ | |
"pageId": item.tagId, | |
"pageName": item.name, | |
"pageVersion": 1, | |
"pageType": "tag", | |
"contentId": item.tagId || item.variableId, | |
"contentType": item.type.replace('jsm', 'js'), | |
"contentValue": item.parameter.filter(item => item.key.match(/html|javascript/))[ 0 ].value | |
})).filter(item => item.contentValue.match(/ffapi/ig)); | |
} | |
const generateFile = async (data) => { | |
return new Promise((resolve, reject) => { | |
const filePath = getFilePath(data); | |
const fileContent = data.contentValue; | |
const writeOptions = { | |
encoding: 'utf8', | |
flag: 'w+', | |
}; | |
mkdirSyncRecursive(filePath.base); | |
fs.writeFile(filePath.path, fileContent, writeOptions, (err) => { | |
if (err) { | |
reject(err); | |
} | |
resolve(filePath); | |
}) | |
}); | |
}; | |
const start = () => { | |
const contentList = findProcessTag("--gtm") ? extractGTMContent(fileList) : fileList | |
contentList.forEach(async data => { | |
await generateFile(data).then(file => console.log('File created:', file.path)); | |
}); | |
} | |
start(); |
This file contains 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
{ | |
"name": "content-json2file", | |
"version": "0.0.0", | |
"description": "Generate files based in a custom json", | |
"bin": "index.js" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment