Socket
Socket
Sign inDemoInstall

jspackage

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jspackage - npm Package Compare versions

Comparing version 0.4.9 to 0.5.0

README.md

60

lib/cmd.js

@@ -1,12 +0,18 @@

var compile, fs, path, optparse, switches, parser, printUsage, mainfile, output, options, ext;
compile = require('./jspackage').compile;
fs = require('fs');
path = require('path');
optparse = require('optparse');
switches = [['-h', '--help', "shows this help section and exit"], ['-v', '--version', "print the version number and exit"], ['-b', '--bare', "compile without a top-level function wrapper"], ['-w', '--watch', "watch source files and recompile when any change"], ['-l', '--lib PATH', "add an additional search directory for source files"]];
parser = new optparse.OptionParser(switches);
printUsage = function(){
parser.banner = "Usage: jspackage input_file output_file [options]";
console.log(parser.toString());
};
var compile = require('./jspackage').compile
, fs = require('fs')
, path = require('path')
, optparse = require('optparse')
var switches = [
['-h', '--help', "shows this help section and exit"],
['-v', '--version', "print the version number and exit"],
['-b', '--bare', "compile without a top-level function wrapper"],
['-w', '--watch', "watch source files and recompile when any change"],
['-l', '--lib PATH', "add an additional search directory for source files"]
];
var parser = new optparse.OptionParser(switches);
var mainfile = null;
var output = null;
var options = {};
parser.on('help', function(){

@@ -17,19 +23,11 @@ printUsage();

parser.on('version', function(){
var lib_dir, pkg_path, data, pkg;
lib_dir = path.dirname(fs.realpathSync(__filename));
pkg_path = path.resolve(lib_dir, "..", "package.json");
data = fs.readFileSync(pkg_path, 'utf8');
pkg = JSON.parse(data);
console.log(pkg.version);
console.log(require('../package').version);
process.exit(1);
});
mainfile = null;
parser.on(0, function(it){
mainfile = it;
});
output = null;
parser.on(1, function(it){
output = it;
});
options = {};
parser.on('bare', function(){

@@ -49,10 +47,6 @@ options.bare = true;

}
if ((ext = path.extname(mainfile)).length > 0) {
mainfile = mainfile.substring(0, mainfile.length - ext.length);
}
options.mainfile = mainfile;
options.mainfile = removeExtension(mainfile);
compile(options, function(err, code){
var timestamp;
if (options.watch) {
timestamp = new Date().toLocaleTimeString();
var timestamp = new Date().toLocaleTimeString();
if (err) {

@@ -65,7 +59,13 @@ console.error(timestamp + " - error: " + err);

} else {
if (err) {
throw err;
}
if (err) throw err;
fs.writeFile(output, code);
}
});
});
function printUsage() {
parser.banner = "Usage: jspackage input_file output_file [options]";
console.log(parser.toString());
}
function removeExtension(filename) {
var ext = path.extname(filename);
return ext.length > 0 ? filename.substring(0, filename.length - ext.length) : filename;
}

@@ -1,20 +0,108 @@

var fs, path, async, watchFilesOnce, cached_files, watching, libs, root, parseFile, resolveDepend, resolveDependencyChain, collectDependencies, extensions, out$ = typeof exports != 'undefined' && exports || this;
fs = require('fs');
path = require('path');
async = require('async');
watchFilesOnce = require('./watch').watchFilesOnce;
cached_files = {};
watching = null;
libs = null;
root = null;
parseFile = function(resolved_dep, cb){
var file;
file = {
path: resolved_dep.path,
var fs = require('fs')
, path = require('path')
, async = require('async')
, watchFilesOnce = require('./watch').watchFilesOnce
, cached_files = {}
, watching = null
, libs = null
, root = null
exports.compile = compile;
exports.extensions = {
'.coffee': {
require: 'coffee-script',
compile: function(code, options){
return require('coffee-script').compile(code, {
bare: options.bare
});
},
depend_re: /^#depend "(.+)"( bare)?$/gm
},
'.js': {
require: null,
compile: function(code, options){
if (options.bare) {
return code;
} else {
return "(function(){\n" + code + "}).call(this);";
}
},
depend_re: /^\/\/depend "(.+)"( bare)?;?$/gm
},
'.co': {
require: 'coco',
compile: function(code, options){
return require('coco').compile(code, {
bare: options.bare
});
},
depend_re: /^#depend "(.+)"( bare)?$/gm
},
'.ls': {
require: 'LiveScript',
compile: function(code, options){
return require('LiveScript').compile(code, {
bare: options.bare
});
},
depend_re: /^#depend "(.+)"( bare)?$/gm
},
'.iced': {
require: 'iced-coffee-script',
compile: function(code, options){
return require('iced-coffee-script').compile(code, {
bare: options.bare,
runtime: 'inline',
});
},
depend_re: /^#depend "(.+)"( bare)?$/gm
}
};
function compile(options, cb){
watching = options.watch;
libs = (options.libs || []).map(function(lib) { return path.resolve(lib); });
libs.unshift(".");
root = null;
var dep = {
file: null,
depend: options.mainfile,
options: {
bare: options.bare
},
cwd: process.cwd(),
seen: []
};
collectDependencies(dep, function(collectErr){
if (collectErr && root == null) {
cb(collectErr);
return;
}
resolveDependencyChain(root, function(err, dependencyChain){
var dep, closer, output;
if (watching) {
closer = watchFilesOnce(depsToPaths(dependencyChain), function() {
compile(options, cb);
});
}
if (err) {
cb(err);
} else if (collectErr) {
cb(collectErr);
} else {
output = renderDeps(dependencyChain);
cb(null, output, closer);
}
});
});
}
function parseFile(resolvedDep, cb){
var file = {
path: resolvedDep.path,
compiled_js: null,
mtime: null,
deps: [],
cwd: path.dirname(resolved_dep.path)
cwd: path.dirname(resolvedDep.path)
};
fs.stat(resolved_dep.path, function(err, stat){
fs.stat(resolvedDep.path, function(err, stat){
if (err) {

@@ -25,3 +113,3 @@ cb(err);

file.mtime = +stat.mtime;
fs.readFile(resolved_dep.path, 'utf8', function(err, source){
fs.readFile(resolvedDep.path, 'utf8', function(err, source){
var parser, timestamp, re, result;

@@ -35,8 +123,8 @@ if (err) {

}
parser = extensions[path.extname(resolved_dep.path)];
parser = exports.extensions[path.extname(resolvedDep.path)];
try {
file.compiled_js = parser.compile(source, resolved_dep.options);
file.compiled_js = parser.compile(source, resolvedDep.options);
} catch (e$) {
err = e$;
cb(resolved_dep.path + "\n" + err, file);
cb(resolvedDep.path + "\n" + err, file);
return;

@@ -52,2 +140,3 @@ }

file.deps.push({
file: file,
depend: result[1],

@@ -58,3 +147,3 @@ options: {

cwd: file.cwd,
seen: resolved_dep.seen.concat(file.path)
seen: resolvedDep.seen.concat(file.path)
});

@@ -65,56 +154,66 @@ }

});
};
resolveDepend = function(dep, doneResolvingDepend){
var try_exts, lib_index, tryNextLib;
try_exts = Object.keys(extensions);
lib_index = 0;
(tryNextLib = function(){
var try_lib, resolveWithExt;
if ((try_lib = libs[lib_index++]) != null) {
resolveWithExt = function(ext, cb){
var resolved_path;
resolved_path = path.resolve(dep.cwd, try_lib, dep.depend + ext);
fs.realpath(resolved_path, function(err, real_path){
if (err) {
}
function resolveDepend(dep, doneResolvingDepend){
var tryExts = Object.keys(exports.extensions);
var libIndex = 0;
tryNextLib();
function tryNextLib() {
var tryLib = libs[libIndex++];
if (tryLib == null) {
var source = dep.file ? dep.file.path : '(cli)';
doneResolvingDepend(new Error(source +
": unable to resolve dependency: " + dep.depend));
return;
}
async.map(tryExts, resolveWithExt, function(err, results){
async.filter(results, function(item, cb){
return cb(item != null);
}, function(results){
if (results.length === 1) {
doneResolvingDepend(null, {
path: results[0],
options: dep.options,
seen: dep.seen
});
} else if (results.length === 0) {
tryNextLib();
} else if (results.length > 1) {
var source = dep.file ? dep.file.path : "(cli)";
doneResolvingDepend(new Error(source + ": ambiguous dependency: " + dep.depend));
}
});
});
function resolveWithExt(ext, cb){
var resolved_path = path.resolve(dep.cwd, tryLib, dep.depend + ext);
fs.realpath(resolved_path, function(err, real_path){
if (err) {
cb(null, null);
return;
}
fs.stat(real_path, function(err, stat){
if (err || stat.isDirectory()) {
cb(null, null);
return;
} else {
cb(null, real_path);
}
fs.stat(real_path, function(err, stat){
if (err || stat.isDirectory()) {
cb(null, null);
} else {
cb(null, real_path);
}
});
});
};
async.map(try_exts, resolveWithExt, function(err, results){
async.filter(results, function(item, cb){
return cb(item != null);
}, function(results){
if (results.length === 1) {
doneResolvingDepend(null, {
path: results[0],
options: dep.options,
seen: dep.seen
});
} else if (results.length === 0) {
tryNextLib();
} else if (results.length > 1) {
doneResolvingDepend("ambiguous dependency: " + dep.depend);
}
});
});
} else {
doneResolvingDepend("unable to resolve dependency: " + dep.depend);
}
})();
};
resolveDependencyChain = function(root, doneResolvingDependencyChain){
var files, seen, processNode;
files = [];
seen = {};
processNode = function(node, doneProcessingNode){
}
}
function resolveDependencyChain(root, doneResolvingDependencyChain){
var files = [];
processNode(root, function(err){
// remove duplicates
var seen = {};
var uniqueFiles = files.filter(function(file) {
var notDupe = !seen[file.path];
seen[file.path] = true;
return notDupe;
});
doneResolvingDependencyChain(err, uniqueFiles);
});
function processNode(node, doneProcessingNode){
async.map(node.deps, resolveDepend, function(err, resolved_deps){
var funcs, i$, len$, dep, file;
var i$, len$;
if (err) {

@@ -124,10 +223,6 @@ doneProcessingNode(err);

}
funcs = [];
var funcs = [];
for (i$ = 0, len$ = resolved_deps.length; i$ < len$; ++i$) {
dep = resolved_deps[i$];
file = cached_files[dep.path];
if (seen[file.path] != null) {
continue;
}
seen[file.path] = true;
var dep = resolved_deps[i$];
var file = cached_files[dep.path];
funcs.push(async.apply(processNode, file));

@@ -144,10 +239,7 @@ }

});
};
processNode(root, function(err){
doneResolvingDependencyChain(err, files);
});
};
collectDependencies = function(dep, doneCollectingDependencies){
resolveDepend(dep, function(err, resolved_dep){
var dep_chain, parseAndHandleErr, callNext, cached_file;
}
}
function collectDependencies(dep, doneCollectingDependencies){
resolveDepend(dep, function(err, resolvedDep){
var depChain, parseAndHandleErr, callNext, cached_file;
if (err) {

@@ -157,12 +249,12 @@ doneCollectingDependencies(err);

}
if (of$(resolved_dep.path, dep.seen)) {
dep_chain = dep.seen.concat(resolved_dep.path).join(" depends on\n");
doneCollectingDependencies("circular dependency:\n" + dep_chain);
if (dep.seen.indexOf(resolvedDep.path) >= 0) {
depChain = dep.seen.concat(resolvedDep.path).join(" depends on\n");
doneCollectingDependencies(new Error("circular dependency:\n" + depChain));
return;
}
parseAndHandleErr = function(cb){
parseFile(resolved_dep, function(err, file){
parseFile(resolvedDep, function(err, file){
if (file) {
cached_files[file.path] = file;
root == null && (root = file);
if (root == null) root = file;
}

@@ -179,6 +271,6 @@ if (err) {

};
if ((cached_file = cached_files[resolved_dep.path]) != null) {
fs.stat(resolved_dep.path, function(err, stat){
if ((cached_file = cached_files[resolvedDep.path]) != null) {
fs.stat(resolvedDep.path, function(err, stat){
if (cached_file.mtime === +stat.mtime) {
root == null && (root = cached_file);
if (root == null) root = cached_file;
callNext(cached_file);

@@ -193,116 +285,8 @@ } else {

});
};
out$.compile = compile;
function compile(options, cb){
var ref$, res$, i$, len$, lib, dep;
watching = options.watch;
libs = (ref$ = options.libs) != null
? ref$
: [];
res$ = [];
for (i$ = 0, len$ = libs.length; i$ < len$; ++i$) {
lib = libs[i$];
res$.push(path.resolve(lib));
}
libs = res$;
libs.unshift(".");
root = null;
dep = {
depend: options.mainfile,
options: {
bare: options.bare
},
cwd: process.cwd(),
seen: []
};
collectDependencies(dep, function(collect_err){
if (collect_err && root == null) {
cb(collect_err);
return;
}
resolveDependencyChain(root, function(err, dependency_chain){
var dep, closer, output;
if (watching) {
closer = watchFilesOnce((function(){
var i$, ref$, len$, results$ = [];
for (i$ = 0, len$ = (ref$ = dependency_chain).length; i$ < len$; ++i$) {
dep = ref$[i$];
results$.push(dep.path);
}
return results$;
}()), function(){
compile(options, cb);
});
}
if (err) {
cb(err);
} else if (collect_err) {
cb(collect_err);
} else {
output = (function(){
var i$, ref$, len$, results$ = [];
for (i$ = 0, len$ = (ref$ = dependency_chain).length; i$ < len$; ++i$) {
dep = ref$[i$];
results$.push(dep.compiled_js);
}
return results$;
}()).join("\n");
cb(null, output, closer);
}
});
});
}
out$.extensions = extensions = {
'.coffee': {
require: 'coffee-script',
compile: function(code, options){
return require('coffee-script').compile(code, {
bare: options.bare
});
},
depend_re: /^#depend "(.+)"( bare)?$/gm
},
'.js': {
require: null,
compile: function(code, options){
if (options.bare) {
return code;
} else {
return "(function(){\n" + code + "}).call(this);";
}
},
depend_re: /^\/\/depend "(.+)"( bare)?;?$/gm
},
'.co': {
require: 'coco',
compile: function(code, options){
return require('coco').compile(code, {
bare: options.bare
});
},
depend_re: /^#depend "(.+)"( bare)?$/gm
},
'.ls': {
require: 'LiveScript',
compile: function(code, options){
return require('LiveScript').compile(code, {
bare: options.bare
});
},
depend_re: /^#depend "(.+)"( bare)?$/gm
},
'.iced': {
require: 'iced-coffee-script',
compile: function(code, options){
return require('iced-coffee-script').compile(code, {
bare: options.bare
});
},
depend_re: /^#depend "(.+)"( bare)?$/gm
}
};
function of$(x, arr){
var i = 0, l = arr.length >>> 0;
while (i < l) if (x === arr[i++]) return true;
return false;
}
function depsToPaths(deps) {
return deps.map(function(dep) { return dep.path; });
}
function renderDeps(deps) {
return deps.map(function(dep) { return dep.compiled_js; }).join("\n");
}

@@ -1,101 +0,105 @@

var exec, fs, path, async, temp, tests_dir, tmp_js_file, msg, pass_count, fail_count;
exec = require('child_process').exec;
fs = require('fs');
path = require('path');
async = require('async');
temp = require('temp');
tests_dir = "./tests/";
tmp_js_file = "./.test_out.tmp.js";
msg = "";
pass_count = 0;
fail_count = 0;
var exec = require('child_process').exec
, fs = require('fs')
, path = require('path')
, assert = require('assert')
, async = require('async')
, temp = require('temp')
, tests_dir = "./tests/"
, tmpJsFile = "./.test_out.tmp.js"
, msg = ""
, passCount = 0
, failCount = 0
fs.readdir(tests_dir, function(err, files){
var doTest;
doTest = function(test_dir, testDone){
var main_file, expect_file, switches_file;
main_file = path.join(tests_dir, test_dir, "test");
expect_file = path.join(tests_dir, test_dir, "expected.txt");
switches_file = path.join(tests_dir, test_dir, "switches.txt");
(function(){
var exec_result, expected_output, execTest, readExpected;
exec_result = null;
expected_output = null;
execTest = function(cb){
fs.readFile(switches_file, 'utf8', function(err, switches){
switches = (switches || "").replace(/\n/g, " ");
temp.open("", function(err, tmp_js_file){
var cmdline;
cmdline = "bin/jspackage " + switches + " " + main_file + " " + tmp_js_file.path;
exec(cmdline, function(err, stdout, stderr){
if (stderr.length > 0) {
exec_result = {
compile: false,
msg: stderr
};
cb();
}
exec("node " + tmp_js_file.path, function(err, stdout, stderr){
fs.close(tmp_js_file.fd, function(){
return fs.unlink(tmp_js_file.path);
});
if (stderr.length > 0) {
exec_result = {
compile: true,
run: false,
msg: stderr
};
} else {
exec_result = {
compile: true,
run: true,
output: stdout
};
}
cb();
});
assert.ifError(err);
async.map(files, doTest, function(){
if (msg.length > 0) process.stdout.write(msg);
process.stdout.write("\n" + passCount + " passed, " + failCount + " failed.\n");
fs.unlink(tmpJsFile);
if (failCount > 0) process.exit(1);
});
});
function doTest(test_dir, testDone){
var mainFile = path.join(tests_dir, test_dir, "test");
var expectFile = path.join(tests_dir, test_dir, "expected.txt");
var switchesFile = path.join(tests_dir, test_dir, "switches.txt");
var execResult = null;
var expectedOutput = null;
async.parallel([execTest, readExpected], function(){
if (execResult.compile) {
if (execResult.run) {
if (execResult.output === expectedOutput) {
process.stdout.write('.');
passCount += 1;
} else {
process.stdout.write('F');
failCount += 1;
msg += "\n\n" +
"======== " + test_dir + " failed =========\n" +
"-------- Expected Output: ---------\n" + expectedOutput + "\n" +
"-------------------------------------\n" +
"-------- Actual Output: ---------\n" +execResult.output+"\n" +
"-------------------------------------";
}
} else {
process.stdout.write('E');
failCount += 1;
msg += "\n\n" +
"======== " + test_dir + " crashed =========\n" +
"-------- stderr: ---------\n" + execResult.msg + "\n" +
"-------------------------------------";
}
} else {
process.stdout.write('X');
failCount += 1;
msg += "\n\n" +
"======== " + test_dir + " compile error ====\n" +
"-------- stderr: ---------\n" + execResult.msg + "\n" +
"-------------------------------------";
}
testDone();
});
function execTest(cb) {
fs.readFile(switchesFile, 'utf8', function(err, switches){
switches = (switches || "").replace(/\n/g, " ");
temp.open("", function(err, tmpJsFile){
var cmdline = "bin/jspackage " + switches + " " + mainFile + " " + tmpJsFile.path;
exec(cmdline, function(err, stdout, stderr){
if (stderr.length > 0) {
execResult = {
compile: false,
msg: stderr
};
cb();
}
exec("node " + tmpJsFile.path, function(err, stdout, stderr){
fs.close(tmpJsFile.fd, function(){
return fs.unlink(tmpJsFile.path);
});
if (stderr.length > 0) {
execResult = {
compile: true,
run: false,
msg: stderr
};
} else {
execResult = {
compile: true,
run: true,
output: stdout
};
}
cb();
});
});
};
readExpected = function(cb){
fs.readFile(expect_file, 'utf8', function(err, out){
expected_output = out;
cb();
});
};
async.parallel([execTest, readExpected], function(){
if (exec_result.compile) {
if (exec_result.run) {
if (exec_result.output === expected_output) {
process.stdout.write('.');
pass_count += 1;
} else {
process.stdout.write('F');
fail_count += 1;
msg += "\n\n======== " + test_dir + " failed =========\n-------- Expected Output: ---------\n" + expected_output + "\n-------------------------------------\n-------- Actual Output: ---------\n" + exec_result.output + "\n-------------------------------------";
}
} else {
process.stdout.write('E');
fail_count += 1;
msg += "\n\n======== " + test_dir + " crashed =========\n-------- stderr: ---------\n" + exec_result.msg + "\n-------------------------------------";
}
} else {
process.stdout.write('X');
fail_count += 1;
msg += "\n\n======== " + test_dir + " compile error ====\n-------- stderr: ---------\n" + exec_result.msg + "\n-------------------------------------";
}
testDone();
});
}.call(this));
};
async.map(files, doTest, function(){
if (msg.length > 0) {
process.stdout.write(msg);
}
process.stdout.write("\n" + pass_count + " passed, " + fail_count + " failed.\n");
fs.unlink(tmp_js_file);
if (fail_count > 0) {
process.exit(1);
}
});
});
});
}
function readExpected(cb){
fs.readFile(expectFile, 'utf8', function(err, out){
expectedOutput = out;
cb();
});
}
}

@@ -1,48 +0,10 @@

var fs, watchFile, out$ = typeof exports != 'undefined' && exports || this;
fs = require('fs');
function watchFileFallback(filename, options, cb){
options.interval = 701;
fs.watchFile(filename, options, function(curr, prev){
if (curr.mtime !== prev.mtime) {
cb('change', filename);
}
var chokidar = require('chokidar');
exports.watchFilesOnce = function(files, cb) {
var watcher = chokidar.watch(files, {ignored: /^\./, persistent: true});
watcher.on('change', function() {
cb();
watcher.close();
});
return {
close: function(){
return fs.unwatchFile(filename);
}
};
}
watchFile = fs.watch || watchFileFallback;
out$.watchFilesOnce = watchFilesOnce;
function watchFilesOnce(files, cb){
var watchers, close, doCallback, i$, len$, file, watcher, err;
watchers = [];
close = function(){
var i$, ref$, len$, watcher;
for (i$ = 0, len$ = (ref$ = watchers).length; i$ < len$; ++i$) {
watcher = ref$[i$];
watcher.close();
}
watchers = [];
};
doCallback = function(event){
if (event === 'change') {
close();
cb();
}
};
for (i$ = 0, len$ = files.length; i$ < len$; ++i$) {
file = files[i$];
try {
watcher = fs.watch(file, doCallback);
} catch (e$) {
err = e$;
watcher = watchFileFallback(file, doCallback);
}
watchers.push(watcher);
}
return {
close: close
};
}
return watcher;
};
{
"name": "jspackage",
"description": "build tool which adds client-side import syntax",
"version": "0.4.9",
"version": "0.5.0",
"author": {

@@ -16,3 +16,4 @@ "name": "Andrew Kelley",

"async": "~0.1.22",
"optparse": "~1.0.3"
"optparse": "~1.0.3",
"chokidar": "~0.5.3"
},

@@ -27,15 +28,9 @@ "devDependencies": {

"scripts": {
"test": "node lib/test.js",
"dev": "npm install && npm run watch",
"build": "coco -bco lib/ src/",
"watch": "coco -wbco lib/ src/",
"clean": "rm -rf lib/"
"test": "node lib/test.js"
},
"engines": {
"node": ">=0.6.8 <0.9.0"
},
"bin": {
"jspackage": "./bin/jspackage"
},
"main": "./lib/jspackage.js"
"main": "./lib/jspackage.js",
"license": "MIT"
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc