Skip to content

Commit

Permalink
Merge pull request #20 from oncletom/feature-promises
Browse files Browse the repository at this point in the history
Promise-based interface
  • Loading branch information
Thomas Parisot committed Nov 25, 2014
2 parents ab792eb + 593b8af commit a1e6a59
Show file tree
Hide file tree
Showing 6 changed files with 451 additions and 263 deletions.
8 changes: 4 additions & 4 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
Copyright (c) 2014 Jed Schmidt, http://jed.is/
Copyright (c) 2014 Jed Schmidt, Thomas Parisot

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Expand Down
106 changes: 78 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,53 +14,85 @@ crx is a [node.js](http://nodejs.org/) command line app for packing Google Chrom

## Module API

Asynchronous functions returns an [ES6 Promise](https://github.com/jakearchibald/es6-promise).

### ChromeExtension = require("crx")
### crx = new ChromeExtension(attrs)

This module exports the `ChromeExtension` constructor directly, which can take an optional attribute object, which is used to extend the instance.

### crx.load(path, callback)
### crx.load(path)

Asynchronously loads the Chrome Extension from the specified path (or uses the `attr.rootDirectory` value).

Loads the Chrome Extension from the specified path.
```js
crx.load().then(function(crx){
// ...
});
```

### crx.pack(callback)
### crx.pack()

Packs the Chrome Extension, and calls back with a Buffer containing the `.crx` file.
Packs the Chrome Extension and resolves the promise with a Buffer containing the `.crx` file.

```js
crx.pack().then(function(crxBuffer){
fs.writeFile('/tmp/foobar.crx', crxBuffer);
});
```

### crx.generateUpdateXML()

Returns a Buffer containing the update.xml file used for autoupdate, as specified for `update_url` in the manifest. In this case, the instance must have a property called `codebase`.
Returns a Buffer containing the update.xml file used for `autoupdate`, as specified for `update_url` in the manifest. In this case, the instance must have a property called `codebase`.

```js
var crx = new ChromeExtension({ ..., codebase: 'https://autoupdateserver.com/myFirstExtension.crx' });

crx.pack().then(function(crxBuffer){
// ...

var xmlBuffer = crx.generateUpdateXML();
fs.writeFile('/foo/bar/update.xml', xmlBuffer);
});

```

### crx.destroy()

Destroys all of the temporary resources used for packing.

```js
crx.pack().then(function(crxBuffer){
// ...

return crx.destroy();
}).then(function(){
console.log('Extension saved and workspace cleaned up!');
});
```

## Module example

```javascript
var fs = require("fs")
, ChromeExtension = require("crx")
, join = require("path").join
, crx = new ChromeExtension(
codebase: "http://localhost:8000/myFirstExtension.crx",
privateKey: fs.readFileSync(join(__dirname, "key.pem")),
rootDirectory: join(__dirname, "myFirstExtension")
var fs = require("fs"),
var ChromeExtension = require("crx"),
var join = require("path").join,
var crx = new ChromeExtension(
codebase: "http://localhost:8000/myFirstExtension.crx",
privateKey: fs.readFileSync(join(__dirname, "key.pem"))
});

crx.load(join(__dirname, "myFirstExtension"))
.then(function() {
return crx.pack(function(crxBuffer){
var updateXML = crx.generateUpdateXML()

fs.writeFile(join(__dirname, "update.xml"), updateXML)
fs.writeFile(join(__dirname, "myFirstExtension.crx"), crxBuffer)

return crx.destroy();
})

crx.load(function(err) {
if (err) throw err

this.pack(function(err, data){
if (err) throw err

var updateXML = this.generateUpdateXML()

fs.writeFile(join(__dirname, "update.xml"), updateXML)
fs.writeFile(join(__dirname, "myFirstExtension.crx"), data)

this.destroy()
})
})
```

## CLI API
Expand Down Expand Up @@ -130,6 +162,24 @@ to sign your package without keeping the key in the directory.
Copyright
---------

Copyright (c) 2014 Jed Schmidt. See [LICENSE.txt](LICENSE.txt) for details.
Copyright (c) 2014 Jed Schmidt, Thomas Parisot

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Send any questions or comments [here](http://twitter.com/jedschmidt).
96 changes: 47 additions & 49 deletions bin/crx.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
#!/usr/bin/env node

var path = require("path")
, fs = require("fs")
, child = require("child_process")
var path = require("path");
var fs = require("fs");
var child = require("child_process");

, program = require("commander")
, ChromeExtension = require("..")
var program = require("commander");
var ChromeExtension = require("..");
var pkg = require('../package.json');

, resolve = path.resolve
, join = path.join
, spawn = child.spawn
, exec = child.exec
var resolve = path.resolve;
var join = path.join;
var exec = child.exec;

, cwd = process.cwd()
var cwd = process.cwd();

program
.version("0.2.8")
.version(pkg.version)
.option("-f, --file [file]", "input/output <file> instead of stdin/stdout")
.option("-p, --private-key <file>", "relative path to private key [key.pem]")
.option("-b, --max-buffer <total>", "max amount of memory allowed to generate the crx, in byte")
Expand All @@ -25,71 +25,69 @@ program
program
.command("keygen [directory]")
.description("generate a private key in [directory]/key.pem")
.action(keygen)
.action(keygen);

program
.command("pack [directory]")
.description("pack [directory] into a .crx extension")
.action(pack)
.action(pack);

// program
// .command("unpack [directory]")
// .description("unpack a .crx extension into a directory")
// .action(unpack)
// .action(unpack);

program.parse(process.argv)
program.parse(process.argv);

function keygen(dir, cb) {
dir = resolve(cwd, dir)
dir = resolve(cwd, dir);

var key = join(dir, "key.pem")
var key = join(dir, "key.pem");

fs.exists(key, function(exists) {
if (exists) return cb && typeof(cb) == "function" && cb()
if (exists) {
return cb && typeof(cb) == "function" && cb();
}

var pubPath = key + ".pub"
, command = "ssh-keygen -N '' -b 1024 -t rsa -f key.pem"
var pubPath = key + ".pub";
var command = "ssh-keygen -N '' -b 1024 -t rsa -f key.pem";

exec(command, {cwd: dir}, function(err) {
if (err) throw err
if (err){
throw err;
}

// TODO: find a way to prevent .pub output
fs.unlink(pubPath)
cb && typeof(cb) == "function" && cb()
fs.unlink(pubPath);

cb && typeof(cb) == "function" && cb();
})
})
}

function pack(dir) {
var input = resolve(cwd, dir)
, output =
program.file === true ? input + ".crx" :
program.file ? resolve(cwd, program.file) : false

, stream = output ? fs.createWriteStream(output) : process.stdout
, key = program.privateKey
? resolve(cwd, program.privateKey)
: join(input, "key.pem")

, crx = new ChromeExtension({
rootDirectory: input,
maxBuffer: program.maxBuffer
})
var input = resolve(cwd, dir);
var output = program.file === true ? input + ".crx" : (program.file ? resolve(cwd, program.file) : false);

fs.readFile(key, function(err, data) {
if (err) keygen(dir, pack.bind(null, dir))
var stream = output ? fs.createWriteStream(output) : process.stdout;
var key = program.privateKey ? resolve(cwd, program.privateKey) : join(input, "key.pem");

crx.privateKey = data
var crx = new ChromeExtension({
rootDirectory: input,
maxBuffer: program.maxBuffer
});

crx.load(function(err) {
if (err) throw err
fs.readFile(key, function(err, data) {
if (err) {
throw err;
}

this.pack(function(err, data){
if (err) throw err
crx.privateKey = data;

stream.end(data)
this.destroy()
})
})
})
crx.pack().then(function(crxBuffer) {
stream.end(crxBuffer);

return crx.destroy();
});
});
}
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@
"test": "node ./test/index.js"
},
"dependencies": {
"commander": "^2.5.0",
"archiver": "^0.8.0",
"commander": "^2.5.0",
"es6-promise": "^2.0.0",
"rimraf": "^2.2.8",
"wrench": "^1.5.0"
},
"devDependencies": {
"tape": "^3.0.3"
}
}
Loading

0 comments on commit a1e6a59

Please sign in to comment.