Skip to content

Instantly share code, notes, and snippets.

@Raynos
Created January 8, 2014 08:18
Show Gist options
  • Select an option

  • Save Raynos/8313477 to your computer and use it in GitHub Desktop.

Select an option

Save Raynos/8313477 to your computer and use it in GitHub Desktop.

Revisions

  1. James Halliday created this gist Jan 8, 2014.
    68 changes: 68 additions & 0 deletions dotslashtaskdotjs.markdown
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,68 @@
    # why `./task.js`?

    One word: task automation. It's basically zero effort and you can use the [./task.js package manager](https://npmjs.org) to handle any repetitive tasks. You can use `./task.js` to automate everything with minimum effort.

    `./task.js` provides the structure, order, and authority that you as a developer so desperately crave.
    `./task.js` will also take responsibility for your actions if you need it to. It's what everybody is using now.
    `./task.js` is the new hotness. It's all about `./task.js` now, just like that.


    This is compared to [npm run/bash scripts](https://gist.github.com/substack/7819530), which are:
    * scary
    * not cross-platformant for deploying to windows server 2003
    * old news. Nobody uses bash these days.

    # getting started

    To install `./task.js`, first install [node.js](http://nodejs.org).

    Now you'll need to generate some scaffolding for your project.

    You can use the following scaffold generator:

    ``` js
    #!/usr/bin/env node
    var fs = require('fs');
    ['browser','static','style'].forEach(fs.mkdir);
    fs.writeFile('task.js', '#!/usr/bin/env node\n', { mode: 0775 });
    ```

    browser code goes in `browser/`, stylesheets go in `style/`, static assets go in `static/`.
    You are free to change these directory names to be whatever you want. Just modify the scaffold generator.

    Now you can get started writing your `./task.js` script. Here is an example script that uses the [browserify](http://browserify.org), [watchify](https://npmjs.org/package/watchify), and [catw](https://npmjs.org/package/catw) plugins published to the [./task.js plugin repository](https://npmjs.org/):

    ``` js
    #!/usr/bin/env node

    var fs = require('fs');
    var browserify = require('browserify');
    var watchify = require('watchify');
    var catw = require('catw');

    var cmd = process.argv[2];
    if (cmd === 'build') build({ watch: false })
    else if (cmd === 'watch') build({ watch: true })
    else usage(1)

    function build (opts) {
    var js = opts.watch ? watchify : browserify;
    js('./browser/main.js').bundle()
    .pipe(fs.createWriteStream('static/bundle.js'))
    ;

    var css = catw('style/*.css', { watch: opts.watch });
    css.on('stream', function (stream) {
    stream.pipe(fs.createWriteStream('static/bundle.css'));
    });
    }

    function usage (code) {
    console.error('usage: ./task.js { build | watch }');
    if (code) process.exit(code);
    }
    ```

    # install

    To install `./task.js`, first install [node.js](http://nodejs.org).