nclosure

Server-side Google Closure with Node.js

npm install nclosure
1 downloads in the last day
2 downloads in the last week
35 downloads in the last month

nclosure: Server Side Google Closure Tools in Node.js

Overview

The Google Closure Tools are a powerful set of utilities that aim to make large scale JavaScript development more manageable. This project brings the power of the closure tools to the node platform. The closure tools gives developers the utilities required to improve code design and maintainability by providing support for:

  • Enhanced Modularisation (Both logically and physically)
  • Better Encapsulation
  • Type Safety
  • Interfaces and Mixins
  • Rich Source Documentation
  • A Huge Library of Production Ready Utilities, including:
    • Collections / Arrays
    • Testing / Mocking
    • Async Development
    • Encryption
    • Date / Date Range Support
    • Events
    • Internationalisation
    • Locale
    • Math
    • String
    • Structs
    • More...

Installation (core)

  • Install nclosure. By:

          npm install nclosure
    
  • Or better yet, by getting the source:

          git clone git://github.com/gatapia/nclosure.git
          cd nclosure
          npm link   NOTE: npm link does not work on windows! :(
    

Closure Library

For full details on utilities provided in the closure library refer to the official docs. To use any utility provided in the closure library just:

  1. Include nclosure in your application by require(ing) it and initialising it.

     require('nclosure').nclosure(); // nclosure() initialises the framework
    
  2. goog.require any namespace from the Closure library.

     goog.require('goog.structs.Trie');
    
  3. That's it, use the imported namespaces anywhere in your file.

     var trie = new goog.structs.Trie();
    

Closure Compiler

Using the Closure Compiler requires a small investment in learning but once you have worked your way through the docs you can take advantage of the compiler's support for:

  • Enhanced Code Documentation
  • Type Safety
  • Encapsulation
  • Modularisation
  • Enhanced Inheritance and Interfaces
  • Scalability

Once your source code is annotated and ready for compilation just run the following command:

    nccompile source.js

The nccompile command accepts various arguments:

  • -c: Create [C]ompile file - Produces a compiled .min.js file. Running your code using the compiled js file optimises your code and reduces the number of imports your system does hence improving start up time.
  • -d: Create [D]ependencies- Creates a deps.js file that can be used as an additionalDeps in an external project.

JSDoc Documentation

To run nclosure's documentation tool simply run:

ncdoc <directory or source file>

For full documentation details please read the official jsdoc-toolkit docs.

For a sample source code documentation project using nclosure ncdoc see the node.js core libs as they would look if generated by nclosure.

Closure Testing

nclosure supports testing using Closure's 'goog.testing.jsunit' test tools. To set up a unit test simply create a test file like:

#!/usr/bin/env node
// You can now run the test just by executing this file
require('nclosure').nclosure();

goog.require('goog.testing.jsunit');
// Import the code you are testing (may need an additionalDeps defined)
goog.require('nclosure.examples.simple.Example');

// Any testXXX function are auto-discovered and run
var testFunction1 = function() {
  assertNotEquals(typeof(example_), 'undefined');
};

// Also auto discovered
function testFunction2() {
  assertTrue(false);
}

If the tests are not in the same directory as your code you will have to ensure that the deps.js file of the code you are testing is declared in the closure.json file of the tests directory or passed in to the call to nclosure(); like:

require('nclosure').nclosure({additionalDeps:['/pathToDeps/deps.js']});

To run a single test just execute:

./testSourceFile.js <optionalTestName>

To run all tests (files with the word test or suite in them) in a single directory (recursive) run the following command:

  nctest <dirname>

The testing framework also supports test suite files. If you want to have a test suite simply have an array var named suite with the files to test (relative to the suite file). I.e.

  // Run all the tests inside the '../examples/simple/' directory
  // This array can be directories or specific test (or other suite)
  // files
  var suite = ['../examples/simple/'];

Closure Linter

For detailed code style checking you can also use nclosure's linter support. To use linter you will need to download and install Closure Linter.

Closure Linter checks your code against Google's own JavaScript Style Guide which is a mature and highly scalable framework for developing JavaScript code.

To install Closure Linter read the following page.

Once installed simply run the following command to linter your code.

    ncstyle <directory>

Node Wrappers

NClosure contains a set of wrappers around the core node.js libraries. These wrappers can be used to give type safety when using these libraries. Eg:

goog.provide('namespace');

// Import the 'node.fs' namespace. All node core libs live inside the
// 'node' namespace.
goog.require('node.fs');

console.log('Files: ' + node.fs.readdirSync('.'));

Advanced Configuration

nclosure can be configured in several ways. The easiest is to modify the bin/closure.json file with your global settings. Settings here can be extended by placing a closure.json file in your source directory. You can also place a closure.json in the directory running node (The cwd).

Finally, nclosure can also be configured by passing an optional options object to the require('nclosure').nclosure(opts); call.

All configuration files and configuration objects take the following format:

    {
      closureBasePath: Location of the closure-library.  This defaults to
        the closure library included in this package.
      additionalDeps: Any additional dependency files required to run your
        code.  These files generally point to other closure libraries.
        Note these deps files must have paths relative to this setting
        file or be absolute.
      compiler_jar: Path to the compiler jar you want to use.  This defaults
        to the included compiler.jar file so only change this if you want
        to use a custom compiler.
      additionalCompileOptions: Additional compiler options,
        e.g.: "['--jscomp_warning=newWarningType']"
      additionalCompileRoots: These are directories containing source code
        that needs to be included in the compilation.  If this is not
        included then additionalDeps is used to try to guess any additional
        roots required (assumes that the deps.js file is in the root folder
        of the source directory).
      jsdocToolkitDir: The location of jsdoc-toolkit.  This is only required
        if you want to use jsdoc-toolkit to document your source code. This
        defaults to the jsdoc instance included in this package.
      additionalJSDocToolkitOptions: Additional jsdoc-toolkit options,
        e.g.: "['-D="noGlobal:true"']"
      additionalLinterOptions: Additional gjslint and fixjsstyle options,
        e.g.: "['--summary=true']"
      nodeDir: The location of the node source code.  This is only required
        if you are contributing to the nclosure project or would like to
        update your node-extern files.
    }

Note: All paths can be absolute or relative to the location of the current settings file.

More Help

The best way to get going with nclosure is to look at the nclosure code (bin, lib and examples directories). All nclosure code is annotated and should give you a good introduction to what can be acchieved with the tool.

If you have any questions, issues, complaints, suggestions, .... just email me (guido@tapia.com.au) and I'll see what I can do to help.

Known Limitations

Since the Node.js core libs are not jsdoc'ed in any way I could not give any type safety when using these core libs.

The documentation template is lacking in several areas this will be improved in the future.

Linter is a pain to instal.

Poor project documentation.

For an up to date list of issues see the TODO.txt file. Later I will start an issues list on github.

License

Copyright 2011 Guido Tapia (guido@tapia.com.au)

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

npm loves you