Restructure destructured requires (and rewrite require paths sanely)
Transforms this:
let {
doSomeStuff,
doMoreThings,
} = require('./whatever-it-is');
// run doSomeStuff and then run doMoreThings
doSomeStuff().then(doMoreThings);
Into this:
let WhateverItIs = require('./things/whatever-it-is.js');
// run WhateverItIs.doSomeStuff and then run WhateverItIs.doMoreThings
WhateverItIs.doSomeStuff().then(WhateverItIs.doMoreThings);
git clone [email protected]:coolaj86/restructure.js.git ./de-destructure/
pushd ./de-destructure/
npm ci
node ./restructure.js ~/path/to/project/source/
- Better package structure
Foo.create // (not createFoo)
- Easier to refactor
sd 'Foo.createFoo' 'Foo.create' *.js */*.js
- Solves trivial circular dependency issues
(node:29094) Warning: Accessing non-existent property 'Foo' of module exports inside circular dependency (Use `node --trace-warnings ...` to show where the warning was created)
It uses RegExp rather than a full parser, so it can make mistakes on things like this:
let {
create
} = require('./foo');
console.log('create another foo');
create();
let Foo = require('./foo.js');
console.log('Foo.create another foo');
Foo.create();