https://github.com/jquery/node-packager
Build a package for your library or application on the fly on Node.js
https://github.com/jquery/node-packager
Last synced: 3 months ago
JSON representation
Build a package for your library or application on the fly on Node.js
- Host: GitHub
- URL: https://github.com/jquery/node-packager
- Owner: jquery
- License: mit
- Created: 2015-02-05T21:37:39.000Z (almost 11 years ago)
- Default Branch: main
- Last Pushed: 2024-04-15T22:12:37.000Z (over 1 year ago)
- Last Synced: 2025-01-29T13:03:38.472Z (12 months ago)
- Language: JavaScript
- Size: 23.4 KB
- Stars: 2
- Watchers: 9
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE-MIT
Awesome Lists containing this project
README
## 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][].
[Node.js]: http://nodejs.org/
## Usage
package.js:
```javascript
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
```javascript
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.,
```
{
: ,
: ,
...
}
```
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](http://rafael.xavier.blog.br)