Skip to content

Restructure destructured requires (and rewrite require paths sanely)

License

Notifications You must be signed in to change notification settings

therootcompany/restructure.js

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

restructure.js

Restructure destructured requires (and rewrite require paths sanely)

Example

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);

Usage

git clone [email protected]:coolaj86/restructure.js.git ./de-destructure/
pushd ./de-destructure/
npm ci

node ./restructure.js ~/path/to/project/source/

Why?

  • 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)

Caveats

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();

About

Restructure destructured requires (and rewrite require paths sanely)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%