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); return fileCb("couldn't find file: " + file.filename);
} }
let fileContent = fs.readFileSync('./.embark/' + file.filename).toString(); let fileContent = fs.readFileSync('./.embark/' + file.filename).toString();
self.runPlugins(file, fileContent, fileCb);
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});
}
}); });
} else { } else {
file.content(function(fileContent) {
let pipelinePlugins = self.plugins.getPluginsFor('pipeline'); self.runPlugins(file, fileContent, fileCb);
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});
});
}
} }
}, },
function (err, contentFiles) { function (err, contentFiles) {
if (err) { 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) { webpackRun(filename, options, includeModules, importsList, detectErrors, callback) {
let defaultOptions = { let defaultOptions = {
entry: utils.joinPath(fs.dappPath(), filename), entry: utils.joinPath(fs.dappPath(), filename),