Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

load if not loaded on pack

  • Loading branch information...
commit 392cf760d8e58de74e3794fd0300908365f71377 1 parent b78dc76
@jed jed authored
Showing with 16 additions and 44 deletions.
  1. +9 −34 src/crx.js
  2. +7 −10 test/index.js
View
43 src/crx.js
@@ -6,18 +6,14 @@ var fs = require("fs")
, exec = child.exec
module.exports = new function() {
- function ChromeExtension(attrs, cb) {
+ function ChromeExtension(attrs) {
if (this instanceof ChromeExtension) {
for (var name in attrs) this[name] = attrs[name]
this.path = join("/tmp", "crx-" + (Math.random() * 1e17).toString(36))
-
- cb && this.load(this.rootDirectory, function(err) {
- err ? cb(err) : this.pack(cb)
- })
}
- else return new ChromeExtension(attrs, cb)
+ else return new ChromeExtension(attrs)
}
ChromeExtension.prototype = this
@@ -27,6 +23,10 @@ module.exports = new function() {
}
this.pack = function(cb) {
+ if (!this.loaded) return this.load(function(err) {
+ return err ? cb(err) : this.pack(cb)
+ })
+
this.generatePublicKey(function(err) {
if (err) return cb(err)
@@ -47,19 +47,12 @@ module.exports = new function() {
})
}
- this.load = function(path, cb) {
- fs.stat(path, function(err, stat) {
- if (stat.isDirectory()) this.loadFromDir(path || this.rootDirectory, cb)
-
- else if (stat.isFile()) this.loadFromFile(path, cb)
- }.bind(this))
- }
-
- this.loadFromDir = function(path, cb) {
- var child = spawn("cp", ["-R", path, this.path])
+ this.load = function(cb) {
+ var child = spawn("cp", ["-R", this.rootDirectory, this.path])
child.on("exit", function() {
this.manifest = require(join(this.path, "manifest.json"))
+ this.loaded = true
cb.call(this)
}.bind(this))
@@ -85,24 +78,6 @@ module.exports = new function() {
}.bind(this))
}
- this.loadFromFile = function(path, cb) {
- fs.readFile(path, function(err, data) {
- if (err) return cb.call(this, err)
-
- path = this.path + ".zip"
- data = data.slice(16 + crx[8] + crx[12])
-
- fs.writeFile(path, data, function(err) {
- if (err) return cb.call(this, err)
-
- spawn("unzip", [path], {dir: this.path}, function() {
- fs.unlink(path)
- cb.call(this)
- })
- }.bind(this))
- }.bind(this))
- }
-
this.generatePublicKey = function(cb) {
var rsa = spawn("openssl", ["rsa", "-pubout", "-outform", "DER"])
View
17 test/index.js
@@ -3,20 +3,17 @@ var fs = require("fs")
, ChromeExtension = require("../")
, crx = new ChromeExtension({
privateKey: fs.readFileSync(__dirname + "/key.pem"),
- codebase: "http://localhost:8000/myFirstExtension.crx"
+ codebase: "http://localhost:8000/myFirstExtension.crx",
+ rootDirectory: __dirname + "/myFirstExtension"
})
-crx.load(__dirname + "/myFirstExtension", function(err) {
+crx.pack(function(err, data){
if (err) throw err
- this.pack(function(err, data){
- if (err) throw err
+ var updateXML = this.generateUpdateXML()
- var updateXML = this.generateUpdateXML()
+ fs.writeFile(__dirname + "/update.xml", updateXML)
+ fs.writeFile(__dirname + "/myFirstExtension.crx", data)
- fs.writeFile(__dirname + "/update.xml", updateXML)
- fs.writeFile(__dirname + "/myFirstExtension.crx", data)
-
- this.destroy()
- })
+ this.destroy()
})

0 comments on commit 392cf76

Please sign in to comment.
Something went wrong with that request. Please try again.