GithubHelp home page GithubHelp logo

isabella232 / node-packager Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jquery/node-packager

0.0 0.0 0.0 18 KB

Build a package for your library or application on the fly on Node.js

License: MIT License

JavaScript 100.00%

node-packager's Introduction

Why node-packager?

Use node-packager to generate the built package for your library or application.

It's ideal for applications that builds packages on the fly using Node.js.

Usage

package.js:

function Package() {}

extend( Package.prototype, {

  // Shallow copy
  "LICENSE.txt": "LICENSE.txt",

  // Sync processing
  "app.css": function() {

    // Return a String or Buffer with the file data.
    return buildCss();
  },

  // Async processing
  "app.js": function( callback ) {

    // Call `callback( error, data )`, where data is a String with the file data.
    buildJs( callback );
  },

  "images/": function() {
    var files, imagesFiles;

    // this.files and this.runtime are available.
    if ( this.runtime.includeImages ) {
      files = this.files;
      imagesFiles = {};
      Object.keys( this.files ).filter(function( filepath ) {
        return minimatch( filepath, "src/images/*" );
      }).forEach(function( filepath ) {
        imagesFiles[ path.basename( filepath ) ] = files[ filepath ];
      });
      return imagesFiles
    }

    // Skip "images/" by returning null.
    return null;
  }
});

module.exports = Package;

npm install node-packager

var fs = require( "js" );
var glob = require( "glob" );
var extend = require( "util" )._extend;
var Package = require( "./package" )
var Packager = require( "node-packager" );

var files = glob.sync( "+(LICENSE.txt|src/**)", { nodir: true } ).reduce(function( files, filepath ) {
  files[ filepath ] = fs.readFileSync( filepath );
  return files;
}, {} );

var pkg = Packager( files, Package, {
  includeImages: true
});
var stream = fs.createWriteStream( "myapp.zip" );
pkg.toZip( stream, function( error ) {
  if ( error ) {
    return console.error( error );
  }
  console.log( "Built myapp.zip (" + pkg.stats.toZip.size + " bytes) in " + ( pkg.stats.build.time + pkg.stats.toZip.time ) + " ms" );
  console.log( "- app.js took " + pkg.stats[ "app.js" ].time + " ms" );
});

API

  • Packager( files, Package [, runtimeVars] )

files Object containing (path, data) key-value pairs, e.g.,

{
   <path-of-file-1>: <data-of-file-1>,
   <path-of-file-2>: <data-of-file-2>,
   ...
}

Files will be available on Package via this.files attribute.

Package Function/Class The Package class.

runtimeVars Object Optional object including runtime variables.

Runtime variables will be available on Package via this.runtime attribute.

  • Packager.prototype.toJson( callback )

callback Function called with two arguments: null or an Error object and the built files object.

  • Packager.prototype.toZip( target [, options], callback )

target Stream/String The target stream, or the target filename (when string).

options Object

options.basedir String Set the ZIP base directory.

callback Function called when write is complete, with one argument: null or an Error object.

Test

npm test

License

MIT © Rafael Xavier de Souza

node-packager's People

Contributors

jzaefferer avatar mgol avatar rxaviers avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.