1 | # ![Barrelsby Logo](https://github.com/bencoveney/barrelsby/blob/master/img/logo.png?raw=true)
|
2 |
|
3 | Automatically create TypeScript barrels for your entire code base.
|
4 |
|
5 | [![npm version](https://badge.fury.io/js/barrelsby.svg)](https://badge.fury.io/js/barrelsby)
|
6 | [![Node.js CI](https://github.com/bencoveney/barrelsby/actions/workflows/node.js.yml/badge.svg?branch=master)](https://github.com/bencoveney/barrelsby/actions/workflows/node.js.yml)
|
7 | [![CodeQL](https://github.com/bencoveney/barrelsby/actions/workflows/codeql-analysis.yml/badge.svg?branch=master)](https://github.com/bencoveney/barrelsby/actions/workflows/codeql-analysis.yml)
|
8 |
|
9 | ## About Barrels
|
10 |
|
11 | Barrels are files that rollup exports from several modules into a single convenient module
|
12 | typically named `index.ts`. They tend to help simplify large blocks of import statements at the top
|
13 | of files and help to group up related functionality.
|
14 |
|
15 | A barrel file looks like this:
|
16 |
|
17 | ```TypeScript
|
18 | export * from "./DropDown";
|
19 | export * from "./TextBox";
|
20 | export * from "./CheckBox";
|
21 | export * from "./DateTimePicker";
|
22 | export * from "./Slider";
|
23 | ```
|
24 |
|
25 | It can help you go from messy imports like this:
|
26 |
|
27 | ```TypeScript
|
28 | import {DropDown} from "./src/controls/DropDown";
|
29 | import {TextBox} from "./src/controls/TextBox";
|
30 | import {CheckBox} from "./src/controls/CheckBox";
|
31 | import {DateTimePicker} from "./src/controls/DateTimePicker";
|
32 | import {Slider} from "./src/controls/Slider";
|
33 | ```
|
34 |
|
35 | ...to something tidier like this:
|
36 |
|
37 | ```TypeScript
|
38 | import {DropDown, TextBox, CheckBox, DateTimePicker, Slider} from "./src/controls";
|
39 | ```
|
40 |
|
41 | ...or even this:
|
42 |
|
43 | ```TypeScript
|
44 | import * as Controls from "./src/controls/index";
|
45 | ```
|
46 |
|
47 | ### More Reading
|
48 |
|
49 | * [TattooCoder Blog](http://tattoocoder.com/angular2-barrels/)
|
50 |
|
51 | ### Barrelsby Articles
|
52 |
|
53 | * [Rupesh Tiwari Blog](http://rupeshtiwari.com/create-barrel/)
|
54 | * [Medium Article](https://medium.com/@klauskpm/do-a-barrel-export-aa5b79b76b05)
|
55 |
|
56 | ### Alternatives
|
57 |
|
58 | * [Barrelbot](https://github.com/sw-yx/barrelbot)
|
59 | * [creeate-index](https://github.com/gajus/create-index)
|
60 |
|
61 | ## Usage
|
62 |
|
63 | To install Barrelsby:
|
64 |
|
65 | ```
|
66 | npm install --save-dev barrelsby
|
67 | ```
|
68 |
|
69 | To run barrelsby first add a script to the `package.json` file:
|
70 |
|
71 | ```json
|
72 | {
|
73 | "scripts": {
|
74 | "generate-barrels": "barrelsby --delete"
|
75 | }
|
76 | }
|
77 | ```
|
78 |
|
79 | You can now generate barrels:
|
80 |
|
81 | ```
|
82 | npm run generate-barrels
|
83 | ```
|
84 |
|
85 | ## Configuration Options
|
86 |
|
87 | Barrelsby accepts a number of options to help refine how your barrels are created. These options
|
88 | can be configured from the command line or using a configuration file.
|
89 |
|
90 | ### `-c [path]` or `--config [path]`
|
91 |
|
92 | Specifies the location of the barrelsby configuration file. This file must be a `.json` file. You
|
93 | can include any of the configuration options using their long name.
|
94 |
|
95 | ### `-d [path...]` or `--directory [path...]`
|
96 |
|
97 | Specifies a list of root directories where barrels will be created from. Uses the current directory
|
98 | by default.
|
99 |
|
100 | **Note**: This is backwards compatible with previous versions. Existing configuration files will be
|
101 | parsed correctly. The following two json files will behave identically.
|
102 |
|
103 | ```json lines
|
104 | {
|
105 | "directory": "./src"
|
106 | }
|
107 |
|
108 | {
|
109 | "directory": ["./src"]
|
110 | }
|
111 | ```
|
112 |
|
113 | ### `-D` or `--delete`
|
114 |
|
115 | Deletes any existing barrels encountered by barrelsby. Disabled by default.
|
116 |
|
117 | ### `-e [regex...]` or `--exclude [regex...]`
|
118 |
|
119 | Excludes any files whose paths match any of the specified regular expressions.
|
120 |
|
121 | ### `-E` or `--exportDefault`
|
122 |
|
123 | Also export the default export of the file. Currently works only with the `flat` mode.
|
124 |
|
125 | ```TypeScript
|
126 | export * from "./barrel";
|
127 | export { default as barrel } from "./barrel";
|
128 | ```
|
129 |
|
130 | ### `-F` or `--fullPathname`
|
131 |
|
132 | exportDefault with full pathname to create distinct name. Currently works only with the `flat` mode and exportDefault flag.
|
133 |
|
134 | ```TypeScript
|
135 | export * from "./example/of/the/path";
|
136 | export { default as exampleOfThePath } from "./example/of/the/path";
|
137 | ```
|
138 |
|
139 | ### `-h` or `--help`
|
140 |
|
141 | Displays help information on the command line arguments that barrelsby accepts.
|
142 |
|
143 | ### `-i [regex...]` or `--include [regex...]`
|
144 |
|
145 | Only include files whose paths match any of the specified regular expressions.
|
146 |
|
147 | ### `-l [mode]` or `--location [mode]`
|
148 |
|
149 | The mode that barrelsby should use to determine where which directories to create barrels in.
|
150 | Defaulted to *top*.
|
151 |
|
152 | - `top` only creates a barrel in the target directory.
|
153 | - `below` creates a barrel in every directory just below the target directory.
|
154 | - `all` creates a barrel in every directory below (and including) the target directory.
|
155 | - `replace` only creates barrels in directories where one already existed.
|
156 | - `branch` creates a barrel in every directory that contains other directories.
|
157 |
|
158 | ### `-L` or `--local`
|
159 |
|
160 | Enable this to prevent barrels including modules that exist in the same directory, rather
|
161 | than recursively searching child directories.
|
162 |
|
163 | ### `-n [name]` or `--name [name]`
|
164 |
|
165 | Specifies the name to use for creating new barrels (and identifying old ones). `.ts` wil be
|
166 | appended if not included in the name. Barrels names will be defaulted to `index.ts`.
|
167 |
|
168 | ### `-s [mode]` or `--structure [mode]`
|
169 |
|
170 | The structure that barrelsby should create inside the barrels. Defaulted to *flat*.
|
171 |
|
172 | #### `flat`
|
173 |
|
174 | Exports modules without any nesting.
|
175 |
|
176 | ```TypeScript
|
177 | export * from "./barrel";
|
178 | export * from "./index";
|
179 | export * from "./directory2/script";
|
180 | export * from "./directory2/directory4/deeplyNested";
|
181 | export * from "./directory3/program";
|
182 | ```
|
183 |
|
184 | #### `filesystem`
|
185 |
|
186 | Exports modules as a nested structure that matches the file system directories.
|
187 |
|
188 | ```TypeScript
|
189 | import * as barrelts from "./barrel";
|
190 | import * as directory2directory4deeplyNestedts from "./directory2/directory4/deeplyNested";
|
191 | import * as directory2scriptts from "./directory2/script";
|
192 | import * as directory3programts from "./directory3/program";
|
193 | import * as indexts from "./index";
|
194 | export {barrelts as barrel};
|
195 | export const directory2 = {
|
196 | directory4: {
|
197 | deeplyNested: directory2directory4deeplyNestedts,
|
198 | },
|
199 | script: directory2scriptts,
|
200 | };
|
201 | export const directory3 = {
|
202 | program: directory3programts,
|
203 | };
|
204 | export {indexts as index};
|
205 | ```
|
206 |
|
207 | ### `-q` or `--singleQuotes`
|
208 |
|
209 | Use 'single quotes' in the generated barrel files instead of the default "double quotes".
|
210 |
|
211 | ### `-S` or `--noSemicolon`
|
212 |
|
213 | Omit semicolons from the end of lines in the generated barrel files.
|
214 |
|
215 | ### `-H` or `--noHeader`
|
216 |
|
217 | Omit adding a header comment to the top of the barrel file.
|
218 |
|
219 | ### `-v` or `--version`
|
220 |
|
221 | Display the barrelsby version number.
|
222 |
|
223 | ### `-V` or `--verbose`
|
224 |
|
225 | Display additional debug information.
|
226 |
|
227 | ## Requirements
|
228 |
|
229 | Requires node v6.0.0 or greater for ES6 syntax.
|
230 |
|
231 | ## Contributing
|
232 |
|
233 | See CONTRIBUTING.md
|