简体   繁体   中英

Running Brunch.io directly from node.js rather than CLI

I'm looking for an more efficient build-tool like gulp for huge Projects.

Brunch seems to be the thing for that, but I was wondering if I could run Brunch from an Node.js script, rather than the CLI. The docs doesn't seem to have any detail on that….

Are there any plans or undocumented ways to archive this?

The reasons are still unclear for me, but anyway, here are two approaches that could fit your needs.

init-skeleton

We have awesome package that is used in Brunch internally. It's called init-skeleton . You can write a skeleton for your needs and use that package for scaffolding.

This package has init method that could help you. It has this signature init(skeleton[, options]) . The skeleton might be:

  • Path to the skeleton in your file system
  • Git URI
  • GitHub URI (such as gh:user/project , github:user/project )

options are optional. There are these ones:

  • rootPath — a string which is a root path of the result directory
  • commandName — a string which is a command that will be used in printBanner function .
  • logger — a function that will be used for logging purposes.

For example:

const initSkeleton = require('init-skeleton').init

initSkeleton('path/to/skeleton')
  .then(() => {
    console.log('Done!')

    // do something else
    // ...
  })

If that's not you're looking for, then there is another solution — use Brunch JavaScript API.

Brunch JavaScript API

Yeah, probably that's what you're looking for. Brunch has internal JavaScript API. This API isn't documented. This API could be changed (and apparently wile be), so be careful using this.

Probably, you've already noticed that Brunch's package.json have main field that corresponds to ./lib/index . This exports three methods (which actually correspond to Brunch's commands ):

  1. new method scaffolds a new projects using init-skeleton .
  2. build method runs Brunch for building project once.
  3. watch method runs Brunch watcher for incremental builds.

build and watch might be useful for you. Both of them uses start function . This function is encapsulated for internal usage and has following signature: start(persistent, arg2, arg3) . Here:

Moreover, you can also require a brunch/lib/cli module. This module exports a run method . This method doesn't take any argument. It just parses process.argv and runs the Brunch properly.

That's it. It may look like a hack, it does and it is. Be careful using it, because we are going to change that API in future. Keep in mind that these possibilities aren't documented , so we won't warn you before changes .

Try to find another solution for you problem. Sometimes problem appears not because of problem itself, but because of bad understanding of approach you use.

Hope this would help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM