-
-
Save Lythom/d49e1fa67d2a4cb14b79e42554b05c68 to your computer and use it in GitHub Desktop.
Quelques essais nodejs rapport à https://n.survol.fr/n/resoudre-le-callback-hell-javascript-102
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 program = require('commander') | |
const glob = require('glob') | |
const path = require('path') | |
const jsonfile = require('jsonfile') | |
const sizeOf = require('image-size') | |
program | |
.version("1.0.0") | |
.arguments('<manifest> <dir>') | |
.action(writeFilesDimensions) | |
.parse(process.argv); | |
function writeFilesDimensions(manifest, dir) { | |
const source_path = path.join(dir, "**/*.png"); | |
const options = {}; | |
glob(source_path, options, function(err, files) { | |
if(err) return error(err); | |
Promise.all(files.map(getFileDimensionData)) | |
.then(data => writeManifest(manifest, data)) | |
.then(result => console.info("SUCCESS!")) | |
.catch(err => error(err)); | |
}); | |
} | |
function error(err) { | |
console.error("ERROR: %s", err.message); | |
} | |
function writeManifest(manifest, data) { | |
return new Promise(function(resolve, reject) { | |
jsonfile.writeFile(manifest, data, function(err, result) { | |
if (err) return reject(err); | |
return resolve(result); | |
}); | |
}); | |
} | |
function getFileDimensionData(file) { | |
return new Promise(function(resolve, reject) { | |
sizeOf(file, function(err, dimensions) { | |
if (err) return reject(err); | |
dimensions.path = file; | |
dimensions.name = path.basename(file); | |
return resolve(dimensions); | |
}) | |
}); | |
} | |
if (process.argv.length < 3) { | |
program.outputHelp(); | |
exit(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment