Skip to content

Instantly share code, notes, and snippets.

@zdying
Created June 8, 2017 07:31
Show Gist options
  • Save zdying/82dbf55d2fde9ec6dfdb877991859a46 to your computer and use it in GitHub Desktop.
Save zdying/82dbf55d2fde9ec6dfdb877991859a46 to your computer and use it in GitHub Desktop.
step-flow example
/**
* @file step-flow example
* @author zdyng
*/
'use strict';
const fs = require('fs');
const path = require('path');
const md5 = require('md5');
const StepFlow = require('step-flow');
const flow = new StepFlow();
flow
.use('read-file', readFile)
.use('add-source-map', addSourceMap)
.use('get-md5', getMD5)
.use('write-file', writeFile)
.catch(onError);
let ctx = {
filePath: path.join(__dirname, 'test.js')
};
flow.run(ctx);
function readFile (ctx, next) {
let {filePath} = ctx;
// fs.readFile(filePath, 'utf-8', next);
fs.readFile(filePath, 'utf-8', function (err, data) {
if (err) {
next(err);
} else {
ctx.fileContent = data;
next();
}
});
}
function addSourceMap (ctx, next, nextTo, data) {
// next(null, data + '\n// # sourceMappingURL=/test.js.map');
ctx.fileContent += '\n// # sourceMappingURL=/test.js.map\n';
next();
}
function getMD5 (ctx, next) {
ctx.md5 = md5(ctx.fileContent);
next();
}
function writeFile (ctx, next) {
let newFileName = ctx.filePath.replace(/^(.*)\.([^.]+)$/, '$1' + '@' + ctx.md5 + '.$2');
fs.writeFile(newFileName, ctx.fileContent, next);
}
function onError (err) {
console.log('[Error]:', err);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment