add

Tracks any set of files as a single or multiple components.

Synopsis

bit add|a <files> [-i|--id <id>] [-m|--main <main file name>] [-t|--tests <tests>] [-n|--namespace <namespace>] [-e|--exclude <files to exclude>] [-o|--override]

Examples

Tracking a single-file component

You can track a component based on a single file.

bit add src/foo/bar.js

This creates a single component with one file, which is also defined as the main file. By default, the directory becomes the namespace, and the file name becomes the component name, so the new component’s id will be foo/bar.

Tracking a directory as a single component

You can track a component based on a whole directory

bit add src/foo/

This creates a single component that contains all the files in the directory. The component namespace becomes the parent directory, and the component name becomes the child directory, so the new component’s id will be src/foo.

Note

Bit searches for an index file to be defined as the component’s main file. If no such file is found, the component won’t be tracked and en error message will be displayed. In order to specify a custom main file, use the --main flag as mentioned in the next section.

Tracking a directory as a single component, with a custom main file

When tracking a component with multiple files, you can specify a custom main file using the --main flag. This is useful when:

  • There is no index file - which means bit has no way of determining which file is the main one.
  • You just want to have the last say ;)
bit add src/foo/ --main bar.js

This will create a component with the id src/foo, with bar.js as the main file (even when there’s an index file in the same directory).

Tracking multiple components using a file list

You can start tracking multiple components at once using a file list - Just mention multiple files with spaces as a separator.

bit add src/foo/bar.js src/utils/connect.js

This will create two components: foo/bar and utils/connect.

Tracking a file list as a single component

You can track files from different places under the same component, by specifying a file list, and the --id flag, which specifies a component id.

bit add src/one/foo.js src/two/bar.js --id my/name --main src/one/foo.js

This will create a single component with the id my/name and the files bar.js and foo.js (which will be defined as the main file).

Note

The --id flag specifies a component id. This means that whenever you use it, bit will try to track a single component with that id.

Tracking a component with a custom id

You can just override bit’s default naming behavior by specifying the --id flag.

bit add src/foo/bar.js --id what/ever

This will create a component with the id what/ever, instead of foo/bar.

Tracking an additional file for an existing component

Forgot to add a file to a component? No worries, just add it a late phase. Let’s say you’ve got a component whose id is foo/bar, with two files: index.js and bar.js. Now you’ve got a new file - new.js.

bit add src/foo/new.js --id foo/bar

By specifying the existing component id, you’ve ensured the file will be tracked as part of the already-existing foo/bar component, which now contains all the three files together.

Tracking components from different places with the same namespace

You can track multiple components at once with the same namespace, even when they’re not under the same directory - just use the --namespace flag.

bit add src/foo/bar.js src/second/hi.js --namespace jaja

This will create two components: jaja/bar, jaja/hi.

Tracking a single directory as multiple components

You can use glob pattern to track a single directory as multiple components.

bit add src/foo/*

This will create as many components as there are files inside the directory, all with foo as their namespace. For example, if foo directory contains two files - bar.js and second.js, two components will be created: foo/bar and foo/second.

Note

If you want to only track components of a specific file extension, just use the glob pattern as follows: bit add src/foo/*.js.

Tracking a component for each sub-directory

You can use glob pattern to track a each sub-directory as a component.

bit add src/**/

This will create a component for each of src’s sub-directories (and files). For example, let’s say this is our file structure:

└───src
    └───one
        └───index.js
    └───two
        └───index.js
    └───single.js

The components created will be: src/single, src/one, src/two.

Note

If you want to exclude some of the files or directories, use the —exclude flag.

Tracking a component for each sub-directory, and setting the main file dynamically

What happens if the main file in each component has the component/directory name?

For example, let’s say this is our file structure:

└───src
    └───one
        └───one.js
        └───not-main.js
        └───README.md
    └───two
        └───two.js
        └───not-main.js
        └───README.md

If we’ll just track each sub-directory as a component (bit add src/**/), bit will have no way of determining which file is the main one.

Fortunately enough, you can use our DSL, which supports PARENT and FILE_NAME.

bit add src/**/ --main '{PARENT}.js'

This will look for a main file with the same name as the parent directory. The components created will be: src/one (with one.js as the main file) and src/two (with two.js as the main file).

Tracking a component for each file in each sub-directory

You can track multiple components for each sub-directory.

bit add src/**/*

This will create a component for each of the files in src and in its sub-directories. For example, let’s say this is our file structure:

└───src
    └───one
        └───first.js
        └───second.js
    └───two
        └───third.js
    └───fourth.js

The components created will be: one/first, one/second, two/third, src/fourth.

Tracking a component with a test file

You can specify which files are the test files, so bit can later test the component.

bit add src/foo/bar.js --tests 'src/foo/bar.spec.js'

This will create a component with the id foo/bar, with two files: bar.js as the main file, and bar.spec.js as the test file.

Now, let’s say this is our file structure:

└───src
    └───foo
        └───bar.js
        └───index.js
        └───tests
            └───index.spec.js
            └───bar.spec.js

You can specify a whole folder of test files.

bit add src/foo --tests 'src/foo/tests/*'

Tracking components for each sub-directory, and tests from a separate directory tree

Now this is a common one: your code is under src folder, and your tests are under tests folder:

└───src
    └───utils
        └───left-pad.js
        └───curry.js
    └───components
        └───NavBar.js
        └───Button.js
└───tests
    └───utils
        └───left-pad.spec.js
        └───curry.spec.js
    └───components
        └───NavBar.spec.js
        └───Button.spec.js

Fortunately enough, you can use our DSL, which supports PARENT and FILE_NAME.

bit add src/**/* --tests 'tests/{PARENT}/{FILE_NAME}.spec.js'

This will create four components: utils/left-pad, utils/curry, components/nav-bar, components/button. Each component will have one main file and one test file - the corresponding one from the tests folder.

Tracking a component with just the right files

When you track a component as a whole folder, or when you track multiple components as a subset of folders, you might discover you have some excessive files in there. We want our components to be lean, and contain exactly what we need, and nothing more. In order to do that, just use the --exclude flag, and exclude whatever you (don’t) want:

You can exclude a single file.

bit add src/foo --exclude dont-want.js

You can exclude a whole folder.

bit add src/** --exclude src/utils

You can exclude using glob pattern.

bit add src/**/* --exclude src/**/*.jpg

You can exclude using a comma-separated-list.

bit add src/foo --exclude 'dont-want.js,thumb.jpg'

Options

-i, —id

Component id - namespace/name. Whenever you specify this flag, bit will try to track a single component with the specified id. If not specified, the name will be as follows:

  • When adding a file: parentdir/filename
  • When adding a directory: parentdir/dirname
bit add src/foo/bar.js --id moon/sun

Will create a component: moon/sun.

-m, —main

Main implementation/index file name. If not specified, bit will look for an index file.

bit add src/foo --main bar.js

You can also use our DSL, which supports PARENT and FILE_NAME.

bit add src/**/ --main 'src/{PARENT}/{PARENT}.js'

-t, —tests <file…>

Specify a test file, test files directory, or tests path using our DSL, which supports PARENT and FILE_NAME.

Track a component with a test file.

bit add src/foo/bar.js --tests src/foo/bar.specs.js

Track a component with a directory of test files

bit add src/foo --tests src/foo/tests/*

Track a component with a test file from another directory

bit add src/foo/bar.js --tests "test/{FILE_NAME}.spec.js"

Track a component with test files from a parallel directory tree

bit add src/foo/bar.js --tests "test/{PARENT}/{FILE_NAME}.spec.js"

-n, —namespace

Component(s) namespace. Used mainly when adding multiple components of the same namespace.

bit add src/foo/bar.js src/second/other.js --namespace moon

Will create two components: moon/bar, moon/other.

-e, —exclude <file…>

Exclude files, directories and patterns.

You can exclude a single file.

bit add src/foo --exclude dont-want.js

You can exclude a whole folder.

bit add src/** --exclude src/utils

You can exclude using glob pattern.

bit add src/**/* --exclude src/**/*.jpg

You can exclude using a comma-separated-list.

bit add src/foo --exclude 'dont-want.js,thumb.jpg'

-o, —override

override existing component if exists (default = false)

bit add src/foo/bar.js --override
  • cli build