GithubHelp home page GithubHelp logo

barrelgun's Introduction

barrelgun

Generate your barrel files like a cowboy 🀠

npm version MIT license GitHub code size in bytes npm

Flexible NodeJS generator for barrel files (index.js) and more!

Usage

Create a config file barrelgun.config.js

/** @type {Array<import("barrelgun").BarrelgunConfigBarrel>} */
const barrels = [
		{
			path: "src/components/index.ts",
			files: "*.component.{ts,tsx}",
		},
		{
			path: "test/tests.spec.js",
			files: "**/*.spec.js",
			lineTemplate: (file) => `export {${file.replace(/[/.]/, '_')} from '${file}';`,
			fileTemplate: (files, lineTemplate: LineTemplate) =>
				`// Do not touch of you will be fired!			
${files.reverse().map(file => lineTemplate(file))}
// You have been warned!`,
		},
	];

module.exports = {barrels};

and run barrelgun

npx barrelgun shoot -c barrelgun.config.js

The following structure will be created

.
β”œβ”€β”€ barrelgun.config.js
β”œβ”€β”€ src
β”‚   └── components
β”‚       β”œβ”€β”€ button.component.tsx
β”‚       β”œβ”€β”€ dialog.component.ts
β”‚       └── index.js  <- this file is created
└── test
    β”œβ”€β”€ ignore.js
    β”œβ”€β”€ all.spec.js
    β”œβ”€β”€ components
    β”‚   └── button.spec.js
    └── tests.spec.js  <- this file is created

src/components/index.js now contains

// Generated by barrelgun. DO NOT EDIT

export * from './button.component.tsx';
export * from './dialog.component.ts';

src/components/index.js content is

// Do not touch of you will be fired!			
export {components_button_spec_js} from './components/button.spec.js';
export {all_spec_js} from './all.spec.js';
// You have been warned!

Features

  • Import files recursively with globs
  • Create all barrels at once with globs
  • Custom templates
  • Sort lines
  • Ignore empty files

Related projects

Inspired by

barrelgun's People

Contributors

petrzjunior avatar

Stargazers

MatΔ›j Kos avatar Filip Ditrich avatar

Watchers

 avatar

barrelgun's Issues

Absolute path import is not valid file URL path on Windows

Running barrelgun shoot on Windows throws the following error:

pnpm barrelgun shoot -c barrelgun.config.js

Reading config from D:\Web_Design\Work\nfctron-account\barrelgun.config.js
node:internal/errors:464
    ErrorCaptureStackTrace(err);
    ^

Error [ERR_UNSUPPORTED_ESM_URL_SCHEME]: Only file and data URLs are supported by the default ESM loader. On Windows, absolute paths must be valid file:// URLs. Received protocol 'd:'
    at new NodeError (node:internal/errors:371:5)
    at defaultResolve (node:internal/modules/esm/resolve:1016:11)
    at ESMLoader.resolve (node:internal/modules/esm/loader:422:30)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:222:40)
    at ESMLoader.import (node:internal/modules/esm/loader:276:22)
    at importModuleDynamically (node:internal/modules/esm/translators:111:35)
    at importModuleDynamicallyCallback (node:internal/process/esm_loader:35:14)
    at loadConfig (file:///D:/Web_Design/Work/nfctron-account/node_modules/.pnpm/[email protected]/node_modules/barrelgun/dist/main.mjs:79:19)
    at shoot (file:///D:/Web_Design/Work/nfctron-account/node_modules/.pnpm/[email protected]/node_modules/barrelgun/dist/main.mjs:84:24)
    at Command.<anonymous> (file:///D:/Web_Design/Work/nfctron-account/node_modules/.pnpm/[email protected]/node_modules/barrelgun/dist/main.mjs:90:122) {
  code: 'ERR_UNSUPPORTED_ESM_URL_SCHEME'
}
 ELIFECYCLE  Command failed with exit code 1.

The problematic code:

const promise = await import(absolutePath);

Probable solution:

 const promise = await import(pathToFileURL(absolutePath).toString()); 

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.