-
-
Save edas/8c1f5b351732d499d8c7d3c3c1d80435 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
import program from "commander"; | |
import glob from "glob"; | |
import path from "path"; | |
import jsonfile from "jsonfile"; | |
import sizeOf from "image-size"; | |
program | |
.version("1.0.0") | |
.arguments('<manifest> <dir>') | |
.action(function(manifest, dir) { | |
const source_path = path.join(dir, "**/*.jpg"); | |
const options = { }; | |
glob(source_path, options, function (err, files) { | |
if (err) { | |
console.error("ERROR: %s", err.message); | |
} else { | |
let flag = file.length; | |
let filesMeta = [ ]; | |
files.map(function (file) { | |
sizeOf(file, function(err, dimensions)) { | |
if (err) { | |
console.error("ERROR: %s", err.message); | |
} else { | |
dimensions.path = file; | |
dimensions.name = path.basename(file); | |
filesMeta.push(dimensions); | |
flag--; | |
if (flag===0) { | |
jsonfile.writeFile(manifest, filesMeta, function (err) { | |
if (err) { | |
console.error("ERROR: %s", err.message); | |
} else { | |
console.info("SUCCESS!"); | |
} | |
}); | |
} | |
} | |
}); | |
}); | |
} | |
}); | |
}).parse(process.argv); | |
if (process.argv.length < 3) { | |
program.outputHelp(); | |
exit(1); | |
} | |
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
import program from "commander"; | |
import glob from "glob"; | |
import path from "path"; | |
import jsonfile from "jsonfile"; | |
import sizeOf from "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, "**/*.jpg"); | |
const options = { }; | |
const next_callback = function (err, files) { | |
if (err) { | |
console.error("ERROR: %s", err.message); | |
} else { | |
getFilesDimensions(files, manifest); | |
} | |
}; | |
glob(source_path, options, next_callback); | |
} | |
function getFilesDimensions(files, manifest) { | |
let flag = file.length; | |
let filesMeta = [ ]; | |
files.map(function (file) { | |
sizeOf(file, function(err, dimensions)) { | |
if (err) { | |
console.error("ERROR: %s", err.message); | |
} else { | |
dimensions.path = file; | |
dimensions.name = path.basename(file); | |
filesMeta.push(dimensions); | |
flag--; | |
if (flag===0) { | |
writeFile(filesMeta, manifest); | |
} | |
} | |
}); | |
}); | |
} | |
function writeFile(filesMeta, manifest) { | |
jsonfile.writeFile(manifest, filesMeta, success); | |
} | |
function success(err) { | |
if (err) { | |
console.error("ERROR: %s", err.message); | |
} else { | |
console.info("SUCCESS!"); | |
} | |
} | |
if (process.argv.length < 3) { | |
program.outputHelp(); | |
exit(1); | |
} | |
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
import program from "commander"; | |
import glob from "glob"; | |
import path from "path"; | |
import jsonfile from "jsonfile"; | |
import sizeOf from "image-size"; | |
program | |
.version("1.0.0") | |
.arguments('<manifest> <dir>') | |
.action(writeFilesDimensions) | |
.parse(process.argv); | |
function writeFilesDimensions(manifest, dir) { | |
let dest_file = manifest; | |
let counter = 0; | |
let files_meta = [ ]; | |
const error = function (err) { | |
console.error("ERROR: %s", err.message); | |
} | |
const cb = function(next_cb) { | |
return function (err, data) { | |
if (err) { | |
error(err); | |
} else { | |
next_cb(data); | |
} | |
}; | |
}; | |
const success = function () { | |
console.info("SUCCESS!"); | |
} | |
const writeManifest = function() { | |
jsonfile.writeFile(dest_file, files_meta, success); | |
}; | |
const finishAllFilesMeta = function () { | |
counter--; | |
if (counter === 0) { | |
writeManifest(); | |
} | |
}; | |
const addFileMeta = function (file, dimensions) { | |
dimensions.path = file; | |
dimensions.name = path.basename(file); | |
files_meta.push(dimensions); | |
finishAllFilesMeta(); | |
}; | |
const getFileDimension = function (file) { | |
const next_cb = function (dimensions) { addFileMeta(file, dimensions); }; | |
sizeOf(file, cb(next_cb) ); | |
}; | |
const getFilesDimensions = function(files) { | |
counter = files.length; | |
files.map( getFileDimension ) ; | |
}; | |
const getFiles = function(dir) { | |
}; | |
getFiles(dir); | |
} | |
if (process.argv.length < 3) { | |
program.outputHelp(); | |
exit(1); | |
} | |
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
import program from "commander"; | |
import glob from "glob"; | |
import path from "path"; | |
import jsonfile from "jsonfile"; | |
import sizeOf from "image-size"; | |
import promisify from "promisify-node"; | |
const glob_p = promisify(glob, undefined, true); | |
const write_p = promisify(jsonfile.writeFile, undefined, true); | |
const size_p = promisify(imageSize, undefined, true); | |
program | |
.version("1.0.0") | |
.arguments('<manifest> <dir>') | |
.action(writeFilesDimensions) | |
.parse(process.argv); | |
function writeFilesDimensions(manifest, dir) { | |
const source_path = path.join(dir, "**/*.jpg"); | |
const options = { }; | |
glob_p(source_path, options) | |
.then( getFilesDimensions ) | |
.then( function (data) { writeManifest(manifest, data); } ) | |
.then( success ) | |
.catch( error ); | |
} | |
function error(err) { | |
console.error("ERROR: %s", err.message); | |
} | |
function sucess() { | |
console.info("SUCCESS!"); | |
} | |
function writeManifest(manifest, data) { | |
return write_p(manifest, data); | |
} | |
function getFilesDimensions(files) { | |
const promises = files.map( getFileDimension ) ; | |
return Promise.all( promises ); | |
} | |
function getFileDimension(file) { | |
return size_p(file).then(function (dimensions) { | |
dimensions.path = file; | |
dimensions.name = path.basename(file); | |
return dimensions; | |
}); | |
} | |
if (process.argv.length < 3) { | |
program.outputHelp(); | |
exit(1); | |
} | |
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
import program from "commander"; | |
import glob from "glob"; | |
import path from "path"; | |
import jsonfile from "jsonfile"; | |
import sizeOf from "image-size"; | |
import promisify from "promisify-node"; | |
const glob_p = promisify(glob, undefined, true); | |
const write_p = promisify(jsonfile.writeFile, undefined, true); | |
const size_p = promisify(imageSize, undefined, true); | |
program | |
.version("1.0.0") | |
.arguments('<manifest> <dir>') | |
.action(writeFilesDimensions) | |
.parse(process.argv); | |
async function writeFilesDimensions(manifest, dir) { | |
try { | |
const source_path = path.join(dir, "**/*.jpg"); | |
const options = { }; | |
const files = await glob_p(source_path, options); | |
let files_meta = [ ]; | |
for (const file of files) { | |
let dimensions = await size_p(file); | |
dimensions.path = file; | |
dimensions.name = path.basename(file); | |
files_meta.push(dimensions); | |
} | |
await write_p(manifest, filesMeta); | |
console.info("SUCCESS!"); | |
} catch(err) { | |
console.error("ERROR: %s", err.message); | |
} | |
} | |
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
Avant toute chose, merci pour ce travail de "mise au propre" de tes recherches en JavaScript. J'ai découvert des choses intéressantes (pratiques de CLI en JS notamment) ! J'espère que je vais pouvoir à mon tour t'apporter quelques éléments.
De mon côté j'aurai pris l'approche Promise sans hésiter. J'ai repris ta version et apporté quelques modifications :
https://gist.github.com/Lythom/d49e1fa67d2a4cb14b79e42554b05c68#file-4-promise-js
Liste des changements :
require
à la place deimport
Autres commentaires sur les discussions :