Skip to content

Commit

Permalink
crx --zip-output
Browse files Browse the repository at this point in the history
  • Loading branch information
oncletom committed Nov 28, 2014
1 parent 641ab93 commit fe4d158
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
test/*.crx
test/*.zip
test/*.xml
node_modules
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,13 @@ crx.load(join(__dirname, "myFirstExtension"))

## CLI API

### crx pack [directory] [-o file] [-p private-key]
### crx pack [directory] [-o file] [--zip-output file] [-p private-key]

Pack the specified directory into a .crx package, and output it to stdout. If no directory is specified, the current working directory is used.

Use the `-o` option to output to a file instead of stdout.
Use the `-o` option to write the signed extension to a file instead of stdout.

Use the `--zip-output` option to write the unsigned extension to a file.

Use the `-p` option to specify an external private key. If this is not used, `key.pem` is used from within the directory. If this option is not used and no `key.pem` file exists, one will be generated automatically.

Expand Down
57 changes: 36 additions & 21 deletions bin/crx.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ var join = path.join;
var cwd = process.cwd();

program.version(pkg.version)
// coming soon
// .option("-x, --xml", "output autoupdate xml instead of extension ")
// coming soon
// .option("-x, --xml", "output autoupdate xml instead of extension ")

program
.command("keygen [directory]")
Expand All @@ -26,27 +26,28 @@ program
program
.command("pack [directory]")
.description("pack [directory] into a .crx extension")
.option("-o, --output <file>", "write to <file> instead of stdout")
.option("-o, --output <file>", "write the crx content to <file> instead of stdout")
.option("--zip-output <file>", "write the zip content to <file>")
.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")
.action(pack);

program.parse(process.argv);

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

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

fs.exists(keyPath, function(exists) {
fs.exists(keyPath, function (exists) {
if (exists && !program.force) {
throw new Error('key.pem already exists in the given location.');
}

var key = new rsa({ b: 1024 });
var key = new rsa({b: 1024});

fs.writeFile(keyPath, key.getPrivatePEM(), function(err){
if (err){
fs.writeFile(keyPath, key.getPrivatePEM(), function (err) {
if (err) {
throw err;
}

Expand All @@ -55,37 +56,51 @@ function keygen(dir, program) {
})
}

function pack(dir, program) {
function pack (dir, program) {
var input = resolve(cwd, dir);
var outStream = process.stdout;
var key = program.privateKey ? resolve(cwd, program.privateKey) : join(input, "key.pem");

if (program.output) {
var outFile = resolve(cwd, program.output);

if (path.extname(outFile) !== '.crx') {
throw new Error('-o file is expected to have a `.crx` suffix: [' + outFile + '] was given.');
if (path.extname(program.output) !== '.crx') {
throw new Error('-o file is expected to have a `.crx` suffix: [' + program.output + '] was given.');
}
}

outStream = fs.createWriteStream(outFile);
if (program.zipOutput) {
if (path.extname(program.zipOutput) !== '.zip') {
throw new Error('--zip-output file is expected to have a `.zip` suffix: [' + program.zipOutput + '] was given.');
}
}

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

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

crx.privateKey = data;

crx.pack().then(function(crxBuffer) {
outStream.end(crxBuffer);
crx.load().then(function () {
return crx.loadContents();
})
.then(function (zipBuffer) {
if (program.zipOutput) {
var outFile = resolve(cwd, program.zipOutput);

fs.createWriteStream(outFile).end(zipBuffer);
}

return crx.pack(zipBuffer);
})
.then(function (crxBuffer) {
var outFile = resolve(cwd, program.output);
(outFile ? fs.createWriteStream(outFile) : process.stdout).end(crxBuffer);

return crx.destroy();
});
return crx.destroy();
});
});
}

0 comments on commit fe4d158

Please sign in to comment.