Skip to content

Instantly share code, notes, and snippets.

@JaminFarr
Forked from Saturate/gulp-to-gulp4-codemod.js
Created January 30, 2020 14:57
Show Gist options
  • Save JaminFarr/18f498fff09b6612757f9dc4b6169347 to your computer and use it in GitHub Desktop.
Save JaminFarr/18f498fff09b6612757f9dc4b6169347 to your computer and use it in GitHub Desktop.
Convert Gulp 3 to Gulp 4 with a codemod
// Use https://astexplorer.net/
export default function transformer(file, api) {
const j = api.jscodeshift;
const root = j(file.source);
const gulpTaskCalls = root.find(j.CallExpression, {
callee: {
object: {
name: 'gulp'
},
property: {
name: 'task'
}
}
});
gulpTaskCalls.forEach(p => {
if(p.node.arguments[1].type === 'ArrayExpression') {
const dependentTasks = p.node.arguments[1].elements;
const isThirdArgFunction = p.node.arguments[2].type === "FunctionExpression" || p.node.arguments[2].type === "ArrowFunctionExpression"
if (isThirdArgFunction) {
dependentTasks.push(p.node.arguments[2])
delete p.node.arguments[2];
}
p.node.arguments[1] = j.callExpression(
j.identifier('gulp.series'),
dependentTasks
);
}
});
return root.toSource({quote: 'single'});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment