Skip to content

Instantly share code, notes, and snippets.

@loopmode
Created June 27, 2018 08:51
Show Gist options
  • Save loopmode/b737ea697f0628e2b6164e93d1fd6e74 to your computer and use it in GitHub Desktop.
Save loopmode/b737ea697f0628e2b6164e93d1fd6e74 to your computer and use it in GitHub Desktop.
monkey-patching jscodeshift for decorators support

Execute scripts/patch/patch-jscodeshift.js. It will monkey-patch node_modules/jscodeshift/parser/babel5compat.js by prepending 'decorators-legacy' to the plugins array.

// file: scripts/patch/patches/jscodeshift-addDecoratorsPlugin.js
const before = `plugins: [
'estree',`;
const after = `plugins: [
'decorators-legacy',
'estree',`;
module.exports = {
files: 'node_modules/jscodeshift/parser/babel5compat.js',
from: before,
to: after
};
#!/usr/bin/env node
// file: scripts/patch/patch-jscodeshift.js
const replace = require('replace-in-file');
const patches = [
'./patches/jscodeshift-addDecoratorsPlugin'
];
patches.forEach(function applyPatch(patchfile) {
const patch = require(patchfile);
if (Array.isArray(patch)) {
patch.forEach(applyPatch);
return;
}
try {
const changes = replace.sync(patch);
console.log(`[patches] ${patchfile}`, changes && changes.length ? { changes } : '(unchanged)');
} catch (error) {
console.log('┎──────────────────────────────────────────────────────────────────');
console.log('┃');
console.log(`┃ Failed to patch ${patch.files}`);
console.log('┃');
console.log('┖──────────────────────────────────────────────────────────────────');
console.log(error);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment