Ts-morph: index.ts barrel files in the codebase?

Created on 10 Dec 2017  路  7Comments  路  Source: dsherret/ts-morph

Having single index.ts barrel files in each directory might help others to understand the codebase a bit better, and remove a lot of extra files that are currently one-off barrels (for instance compiler/class.ts, compiler/common.ts, compiler/decorator.ts, etc.)

It would also be a bit more idiomatic in that you could import directly from a directory, such as:

import { CallExpression } from './compiler';

and:

export * from './compiler';

Thoughts?

code improvement

All 7 comments

I actually didn't know that works. Yes, it should do this.

I was able to easily do this refactor using this script :)

for (const file of ast.getSourceFiles()) {
    const namespaceExports = file.getExports().filter(e => e.isNamespaceExport());
    if (namespaceExports.length === 0)
        continue;

    const fileNameWithoutExtension = path.basename(file.getFilePath()).replace(/\.ts$/, "");
    const dirName = path.dirname(file.getFilePath());

    ast.createSourceFile(path.join(dirName, fileNameWithoutExtension, "index.ts"), {
        exports: namespaceExports.map(n => ({
            moduleSpecifier: n.getModuleSpecifier()!.replace(/^\.\/[^\/]+/g, ".")
        }))
    });

    namespaceExports.forEach(e => e.remove());

    if (file.getStatements().length === 0)
        file.deleteSync();
}

ast.saveUnsavedSourceFilesSync();

Hah, that's awesome! What power to be able to refactor your codebase with such a simple script!

Something like this might also be good in the docs as a full example for others. Perhaps a script for generating index.ts files for all source file exports?

I was thinking about a library that does it, but yeah this is such a simple script to write and it would be more flexible if people maintained it themselves. I'll add it to the docs. Thanks!

Also, these barrel files are so much better. Really helped reduce the noise in the code.

Awesome, glad to have helped!

And yes, I agree. Directory structure is def cleaner now :) Nice job!

@gregjacobs I ended up rolling this into a library. I'll make the repo public soon, but you can try it out now if you're interested:

# install
npm install -g barrel-maintainer
# view the help
barrel-maintainer -h
# run it on a project
barrel-maintainer src

That will run in the background and watch the files for any changes. I still need to make a few improvements before making that public.

Obviously, make sure to have the ability to easily restore the project back to normal based on any changes you make.

Hah, sweet! You're right, that would definitely be useful to maintain barrel files over time. Didn't think of that :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

claudv picture claudv  路  3Comments

rulatir picture rulatir  路  3Comments

HoldYourWaffle picture HoldYourWaffle  路  4Comments

donaldpipowitch picture donaldpipowitch  路  5Comments

HamedFathi picture HamedFathi  路  3Comments