barrel_create 1.2.1 copy "barrel_create: ^1.2.1" to clipboard
barrel_create: ^1.2.1 copied to clipboard

CLI tool that creates a barrel file for a given directory or all directories in your project.

CREATE BARREL #

Create Barrel is a CLI tool that creates a barrel file for the passed directory(s).

A barrel file is a dart library that exports a list of files so that you can import the barrel file rather than each individual file.

For example if you had a directory call dao with the following files:

/dao
dao_job.dart
dao_customer.dart
dao_contact.dart
copied to clipboard

Create Barrel would create a barrel file in that directory called dao.g.dart:

dao.g.dart

//
// Generated file. Do not modify.
// Created by `barrel_create`
//
export 'dao_job.dart';
export 'dao_customer.dart';
export 'dao_contact.dart';
copied to clipboard

So now you can import the barrel file rather than each individual file:

import 'dao_job.dart';
import 'dao_customer.dart';
import 'dao_contact.dart';
copied to clipboard

becomes:

import 'dao/dao.g.dart';
copied to clipboard

To create a barrel file:

dart pub global activate barrel_create
barrel_create [--threshold=n] [--recursion] [--[no-]quiet] <path to directory> [path to directory]...
copied to clipboard

Examples #

To avoid having to retype the arguments you can create a settings file for each project that controls where barrel_create creates barrel files.

recursively create barrel files for every directory under the lib directory that contains at least 3 Dart files. #

cd my/project/root
barrel_create 
copied to clipboard

Barrel Create also has a short cut brl

cd my/project/root
brl
copied to clipboard

create a barrel file for a specific directory #

cd my/project/root
brl lib/src/dao
copied to clipboard

create a barrel file for multiple directory #

cd my/project/root
brl lib/src/dao lib/src/entity
copied to clipboard

create a barrel file for any directory in my project with at least 10 dart libraries #

cd my/project/root
brl -t 10 
copied to clipboard

create a barrel file for all directory under lib/src/ui with at least 4 libraries. #

cd my/project/root
brl -t 4 -r lib/src/ui
copied to clipboard

Advanced options #

directories #

By default barrel_create only scans libraries that exists under the project lib directory.

You can pass one or more directories to tell barrel_create to only scan specific directories or to scan directories outside the project lib directory.

Directories MUST be relative to the current project's root directory.

You can pass a list of directories to barrel_create and it will process each directory in turn:

brl tool test lib
copied to clipboard

recursion #

By passing in the --recursion (-r) flag, barrel create will recursively process all sub-directories under each of the passed directories.

brl -r  tool  test
copied to clipboard

threshold #

By default, when recursing, barrel_create will only create a barrel file for directories which contain at least three dart libraries.

If you pass a directory, but don't specify the --recursive option, then a barrel file will be created even if only a two dart files exist in the directory (i.e. threshold is set to 2).

If there is only a single library in a directory it makes no sense to have a barrel file.

You can change the threshold by passing the --threshold (-t) flag

brl -t 10 tool test
copied to clipboard

quiet #

When recursing barrel_create reports any directory that it inspected but didn't have enough .dart files to trigger the creation of a Barrel file.

By default these messages are suppressed. You can enable the messages by passing --no-quiet (-no-q) flag.

generated directories #

If barrel_create detects a directory that has generated files (*.g.dart) in it (other than its own file) then it will not generate a barrel file in that directory.

Most generated directories already have a barrel file created by the generation tool.

If you want barrel_create to create a barrel file then use the touch command (or an editor) to create an empty barrel file in that directory and from then on barrel_create will create a barrel file in that directory.

So if you have directory:

generated
generate\customer.json.g.dart
copied to clipboard

Create an empty file: generate\generate.g.dart

generated
generate\generate.g.dart
generate\customer.json.g.dart
copied to clipboard

On the next run barrel_create will overwrite generate.g.dart with a barrel file.

Settings File #

To save you from having to type the same args for barrel_create each time you run it, you can instead create a settings file in your dart projects tool directory.

Whenever you run barrel_create without any arguments and a settings file exist, then the settings will be used.

The settings file is placed in: <my project>/tool/barrel_create.yaml

Example #

quiet: true
threshold: 10
recursive: true
directories:
  - dao
  - entity

copied to clipboard

To use the settings file:

cd myproject
brl
copied to clipboard
1
likes
150
points
350
downloads

Publisher

unverified uploader

Weekly Downloads

2024.08.14 - 2025.02.26

CLI tool that creates a barrel file for a given directory or all directories in your project.

Repository (GitHub)

Documentation

API reference

License

Apache-2.0 (license)

Dependencies

args, collection, dcli, path, settings_yaml, strings

More

Packages that depend on barrel_create