Skip to content

Instantly share code, notes, and snippets.

@abruzzihraig abruzzihraig/verify.js
Last active Mar 3, 2017

Embed
What would you like to do?
verifying script for checking templates from KDP
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
You can’t perform that action at this time.