refactor: join duplicated code

This commit is contained in:
Iuri Matias 2018-02-28 18:09:10 -05:00
parent 62d4dd456d
commit 5ee99a1e5a
1 changed files with 29 additions and 50 deletions

View File

@ -111,59 +111,13 @@ class Pipeline {
return fileCb("couldn't find file: " + file.filename);
}
let fileContent = fs.readFileSync('./.embark/' + file.filename).toString();
let pipelinePlugins = self.plugins.getPluginsFor('pipeline');
if (pipelinePlugins.length > 0) {
async.eachSeries(pipelinePlugins, function(plugin, pluginCB) {
if (file.options && file.options.skipPipeline) {
return pluginCB();
}
fileContent = plugin.runPipeline({targetFile: file.filename, source: fileContent});
file.modified = true;
pluginCB();
},
function (err) {
if (err) {
self.logger.error(err.message);
}
return fileCb(null, {content: fileContent, filename: file.filename, path: file.path, basedir: file.basedir, modified: true});
});
} else {
return fileCb(null, {content: fileContent, filename: file.filename, path: file.path, basedir: file.basedir, modified: true});
}
self.runPlugins(file, fileContent, fileCb);
});
} else {
let pipelinePlugins = self.plugins.getPluginsFor('pipeline');
if (pipelinePlugins.length > 0) {
file.content(function(fileContent) {
async.eachSeries(pipelinePlugins, function(plugin, pluginCB) {
if (file.options && file.options.skipPipeline) {
return pluginCB();
}
fileContent = plugin.runPipeline({targetFile: file.filename, source: fileContent});
file.modified = true;
pluginCB();
},
function (err) {
if (err) {
self.logger.error(err.message);
}
return fileCb(null, {content: fileContent, filename: file.filename, path: file.path, basedir: file.basedir, modified: true});
});
});
} else {
file.content(function(fileContent) {
return fileCb(null, {content: fileContent, filename: file.filename, path: file.path, basedir: file.basedir, modified: true});
});
}
file.content(function(fileContent) {
self.runPlugins(file, fileContent, fileCb);
});
}
},
function (err, contentFiles) {
if (err) {
@ -208,6 +162,31 @@ class Pipeline {
});
}
runPlugins(file, fileContent, fileCb) {
const self = this;
let pipelinePlugins = self.plugins.getPluginsFor('pipeline');
if (pipelinePlugins.length <= 0) {
return fileCb(null, {content: fileContent, filename: file.filename, path: file.path, basedir: file.basedir, modified: true});
}
async.eachSeries(pipelinePlugins,
function(plugin, pluginCB) {
if (file.options && file.options.skipPipeline) {
return pluginCB();
}
fileContent = plugin.runPipeline({targetFile: file.filename, source: fileContent});
file.modified = true;
pluginCB();
},
function (err) {
if (err) {
self.logger.error(err.message);
}
return fileCb(null, {content: fileContent, filename: file.filename, path: file.path, basedir: file.basedir, modified: true});
}
);
}
webpackRun(filename, options, includeModules, importsList, detectErrors, callback) {
let defaultOptions = {
entry: utils.joinPath(fs.dappPath(), filename),