Skip to content

Commit

Permalink
Add eslint and reformat using Prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Parisot committed Feb 3, 2019
1 parent 6542a83 commit 7e493ad
Show file tree
Hide file tree
Showing 12 changed files with 935 additions and 192 deletions.
18 changes: 18 additions & 0 deletions .eslintrc.js
@@ -0,0 +1,18 @@
module.exports = {
env: {
browser: true,
commonjs: true,
es6: true,
node: true
},
extends: "eslint:recommended",
parserOptions: {
ecmaVersion: 2016
},
rules: {
indent: ["error", 2],
"linebreak-style": ["error", "unix"],
quotes: ["error", "double"],
semi: ["error", "always"]
}
};
6 changes: 0 additions & 6 deletions .jshintrc

This file was deleted.

4 changes: 2 additions & 2 deletions LICENSE.txt → LICENSE
@@ -1,4 +1,4 @@
Copyright (c) 2014 Jed Schmidt, Thomas Parisot
Copyright (c) 2019 Jed Schmidt, Thomas Parisot and contributors

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand All @@ -17,4 +17,4 @@ 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.
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 changes: 5 additions & 25 deletions README.md
Expand Up @@ -10,7 +10,7 @@ Packages are available to use `crx` with:

Massive hat tip to the [node-rsa project](https://npmjs.com/node-rsa) for the pure JavaScript encryption!

**Compatibility**: this extension is compatible with `node>=6`.
**Compatibility**: this extension is compatible with `node>=10`.

## Install

Expand Down Expand Up @@ -117,7 +117,7 @@ Generate a 2048-bit RSA private key within the directory. This is called automat

Use the `--force` option to overwrite an existing private key located in the same given folder.

### crx -h
### crx --help

Show information about using this utility, generated by [commander](https://github.com/visionmedia/commander.js).

Expand Down Expand Up @@ -179,26 +179,6 @@ $ crx pack myFirstExtension -p myPrivateKey.pem -o

to sign your package without keeping the key in the directory.

Copyright
---------

Copyright (c) 2016 Jed Schmidt, Thomas Parisot and collaborators

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.
# License

[MIT License](LICENSE).
2 changes: 0 additions & 2 deletions appveyor.yml
Expand Up @@ -2,8 +2,6 @@ image: Visual Studio 2017

environment:
matrix:
- nodejs_version: "6"
- nodejs_version: "8"
- nodejs_version: "10"

install:
Expand Down
132 changes: 79 additions & 53 deletions bin/crx.js
Expand Up @@ -2,11 +2,11 @@

var path = require("path");
var fs = require("fs");
var rsa = require('node-rsa');
var rsa = require("node-rsa");

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

var resolve = path.resolve;
var join = path.join;
Expand All @@ -26,10 +26,16 @@ program
program
.command("pack [directory]")
.description("pack [directory] into a .crx extension")
.option("-o, --output <file>", "write the crx content 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")
.option(
"-b, --max-buffer <total>",
"max amount of memory allowed to generate the crx, in byte"
)
.action(pack);

program.parse(process.argv);
Expand All @@ -40,8 +46,8 @@ program.parse(process.argv);
* @returns {Promise}
*/
function readKeyFile(keyPath) {
return new Promise(function (resolve, reject) {
fs.readFile(keyPath, function (err, data) {
return new Promise(function(resolve, reject) {
fs.readFile(keyPath, function(err, data) {
if (err) {
reject(err);
} else {
Expand All @@ -57,93 +63,113 @@ function readKeyFile(keyPath) {
* @returns {Promise}
*/
function generateKeyFile(keyPath) {
return new Promise(function(resolve, reject) {
var key = new rsa({b: 2048}),
keyVal = key.exportKey('pkcs1-private-pem');
return new Promise(function(resolve) {
var key = new rsa({ b: 2048 }),
keyVal = key.exportKey("pkcs1-private-pem");

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

console.log('Key file has been generated at %s', keyPath);
// eslint-disable-next-line no-console
console.log("Key file has been generated at %s", keyPath);

resolve(keyVal);
});
});
}

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

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.');
throw new Error("key.pem already exists in the given location.");
}

generateKeyFile(keyPath);
});
}

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

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

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.');
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
});

readKeyFile(keyPath).then(null, function (err) {
// If the key file doesn't exist, create one
if (err.code === 'ENOENT') {
return generateKeyFile(keyPath);
} else {
throw err;
}
}).then(function (key) {
crx.privateKey = key;
}).then(function () {
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) {

if (program.output) {
output = program.output;
readKeyFile(keyPath)
.then(null, function(err) {
// If the key file doesn't exist, create one
if (err.code === "ENOENT") {
return generateKeyFile(keyPath);
} else {
output = path.basename(cwd) + '.crx';
throw err;
}

var outFile = resolve(cwd, output);
(outFile ? fs.createWriteStream(outFile) : process.stdout).end(crxBuffer);
}).then(function () {
console.log('%s has been generated in %s', output, cwd);
})
.then(function(key) {
crx.privateKey = key;
})
.then(function() {
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) {
if (program.output) {
output = program.output;
} else {
output = path.basename(cwd) + ".crx";
}

var outFile = resolve(cwd, output);
(outFile ? fs.createWriteStream(outFile) : process.stdout).end(
crxBuffer
);
})
.then(function() {
// eslint-disable-next-line no-console
console.log("%s has been generated in %s", output, cwd);
});
});
});
}

module.exports = program;
Binary file removed build.crx
Binary file not shown.

0 comments on commit 7e493ad

Please sign in to comment.