Last active
March 3, 2017 05:02
-
-
Save abruzzihraig/a03a765efb62e772b36f1d6950cc6aa9 to your computer and use it in GitHub Desktop.
verifying script for checking templates from KDP
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
const path = require('path'); | |
const fs = require('fs'); | |
const Table = require('cli-table'); | |
const mainDir = path.join(__dirname, 'templates'); | |
function findPath(filePath) { | |
return new Promise(function(res, rej) { | |
fs.readdir(filePath, (err, filenames) => { | |
const filePaths = filenames | |
.filter(filename => filename !== '.DS_Store') | |
.map(filename => path.join(filePath, filename)); | |
res(filePaths); | |
}); | |
}); | |
} | |
findPath(mainDir) | |
.then(filePaths => { | |
fliePaths = filePaths.map(filePath => findPath(filePath)); | |
return Promise.all(fliePaths); | |
}).then(arr => { | |
const filePaths = arr.reduce((prev, cur) => { | |
cur = cur.map(filePath => findPath(filePath)); | |
return prev.concat(cur); | |
}, []); | |
return Promise.all(filePaths); | |
}).then(arr => { | |
const files = arr.reduce((prev, cur) => { | |
cur = cur | |
.filter(filePath => path.extname(filePath) === '.json') | |
.map(filePath => { | |
return new Promise(function(res, rej) { | |
fs.readFile(filePath, 'utf8', (err, fileData) => { | |
if (err) rej(err); | |
res({ | |
content: fileData, | |
filePath | |
}); | |
}); | |
}) | |
}); | |
return prev.concat(cur); | |
}, []); | |
return Promise.all(files) | |
}).then(arr => { | |
const table = new Table({ | |
head: ['Filename', 'Size', 'Left Margin', 'Top Margin', 'Full Path'] | |
}); | |
try { | |
arr.forEach(({content, filePath}) => { | |
const templateObj = JSON.parse(content.trim()); | |
const { left, top } = templateObj.objects[0]; | |
if (left > 0 || top > 0) { | |
const filename = path.parse(filePath).name; | |
const leftResult = left > 0 ? left : 'valid'; | |
const topResult = top > 0 ? top : 'valid'; | |
const fullPath = path.join.apply(path, filePath.split(path.sep).slice(5)); | |
const size = filename.match(/320x50|300x250|800x160/)[0]; | |
table.push([filename, size, leftResult, topResult, fullPath]); | |
} | |
}); | |
} catch (err) { | |
console.err(err) | |
} | |
console.info(table.toString()); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment