Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

commoner

Package Overview
Dependencies
Maintainers
1
Versions
55
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

commoner - npm Package Compare versions

Comparing version 0.6.1 to 0.6.3

lib/output.js

126

lib/commoner.js
var assert = require("assert");
var path = require("path");
var fs = require("fs");
var Q = require("q");
var Watcher = require("./watcher").Watcher;
var BuildContext = require("./context").BuildContext;
var contextModule = require("./context");
var BuildContext = contextModule.BuildContext;
var PreferredFileExtension = contextModule.PreferredFileExtension;
var ModuleReader = require("./reader").ModuleReader;
var output = require("./output");
var DirOutput = output.DirOutput;
var StdOutput = output.StdOutput;
var util = require("./util");

@@ -55,2 +61,5 @@ var log = util.log;

var waiting = 0;
var output = outputDir
? new DirOutput(outputDir)
: new StdOutput;

@@ -64,2 +73,8 @@ watcher.on("changed", function(file) {

function outputModules(modules) {
// Note that output.outputModules comes pre-bound.
modules.forEach(output.outputModule);
return modules;
}
function finish(result) {

@@ -84,3 +99,3 @@ rebuild.ing = false;

var context = new BuildContext(config, watcher, outputDir);
var context = new BuildContext(config, watcher);

@@ -94,2 +109,4 @@ if (self.preferredFileExtension)

context.setIgnoreDependencies(self.ignoreDependencies);
return new ModuleReader(

@@ -100,5 +117,5 @@ context,

).readMultiP(context.expandIdsOrGlobsP(roots))
.then(collectDepsP)
.then(writeVersionsP)
.then(printModuleIds)
.then(context.ignoreDependencies ? pass : collectDepsP)
.then(outputModules)
.then(outputDir ? printModuleIds : pass)
.then(finish, function(err) {

@@ -113,2 +130,6 @@ log.err(err.stack);

function pass(modules) {
return modules;
}
function collectDepsP(rootModules) {

@@ -135,17 +156,7 @@ var modules = [];

function writeVersionsP(modules) {
return Q.all(modules.map(function(module) {
return module.writeVersionP().then(function() {
return module;
});
}));
}
function printModuleIds(modules) {
var ids = modules.map(function(module) {
log.out(JSON.stringify(modules.map(function(module) {
return module.id;
});
})));
log.out(JSON.stringify(ids));
return modules;

@@ -155,2 +166,7 @@ }

function lockOutputDirP(outputDir) {
if (!outputDir) {
// If outputDir is falsy, then there's nothing to lock.
return Q.resolve(outputDir);
}
return util.mkdirP(outputDir).then(function(dir) {

@@ -166,2 +182,9 @@ var lockFile = path.join(outputDir, ".lock.pid");

Cp.forceResolve = function(forceId, source) {
this.resolvers.unshift(function(id) {
if (id === forceId)
return source;
});
};
Cp.cliBuildP = function(version) {

@@ -171,5 +194,8 @@ var commoner = this;

var workingDir = process.cwd();
var sourceDir = workingDir;
var outputDir = null;
var roots;
options.version(version)
.usage("[options] <source directory> <output directory> <module ID> [<module ID> [<module ID> ...]]")
.usage("[options] <source directory> <output directory> [<module ID> [<module ID> ...]]")
.option("-c, --config [file]", "JSON configuration file (no file means STDIN)")

@@ -182,22 +208,66 @@ .option("-w, --watch", "Continually rebuild")

var pfe = new PreferredFileExtension(options.extension || "js");
// TODO Decide whether passing options to buildP via instance
// variables is preferable to passing them as arguments.
this.preferredFileExtension = options.extension || "js";
this.preferredFileExtension = pfe;
this.persistent = options.watch;
this.cacheDir = absolutePath(workingDir, options.cacheDir);
this.ignoreDependencies = false;
if (options.args.length < 2) {
options.help();
process.exit(-1);
function fileToId(file) {
file = absolutePath(workingDir, file);
assert.ok(fs.statSync(file).isFile(), file);
return pfe.trim(path.relative(sourceDir, file));
}
var roots = options.args.slice(2);
if (roots.length === 0)
roots.push("**/*." + this.preferredFileExtension);
var args = options.args.slice(0);
var argc = args.length;
if (argc === 0) {
if (options.config === true) {
log.err("Cannot read --config from STDIN when reading " +
"source from STDIN");
process.exit(-1);
}
sourceDir = workingDir;
outputDir = null;
roots = ["<stdin>"];
commoner.forceResolve("<stdin>", util.readFromStdinP());
// Ignore dependencies because we wouldn't know how to find them.
this.ignoreDependencies = true;
} else {
var first = absolutePath(workingDir, args[0]);
var stats = fs.statSync(first);
if (argc === 1) {
var firstId = fileToId(first);
sourceDir = workingDir;
outputDir = null;
roots = [firstId];
commoner.forceResolve(firstId, util.readFileP(first));
// Ignore dependencies because we wouldn't know how to find them.
this.ignoreDependencies = true;
} else if (stats.isDirectory(first)) {
sourceDir = first;
outputDir = absolutePath(workingDir, args[1]);
roots = args.slice(2);
if (roots.length === 0)
roots.push(this.preferredFileExtension.glob());
} else {
options.help();
process.exit(-1);
}
}
return Q.all([
getConfigP(workingDir, options.config),
absolutePath(workingDir, options.args[0]), // source directory
absolutePath(workingDir, options.args[1]), // output directory
roots // root module identifiers or file globs
sourceDir,
outputDir,
roots
]).spread(commoner.buildP.bind(commoner));

@@ -204,0 +274,0 @@ };

@@ -10,7 +10,6 @@ var assert = require("assert");

function BuildContext(config, watcher, outputDir) {
function BuildContext(config, watcher) {
var self = this;
assert.ok(self instanceof BuildContext);
assert.ok(watcher instanceof Watcher);
assert.strictEqual(typeof outputDir, "string");

@@ -22,3 +21,2 @@ config = config || {};

watcher: { value: watcher },
outputDir: { value: outputDir },
config: { value: config },

@@ -82,2 +80,11 @@ configHash: { value: util.deepHash(config) }

BCp.setIgnoreDependencies = function(value) {
Object.defineProperty(this, "ignoreDependencies", {
value: !!value
});
};
// This default can be overridden by individual BuildContext instances.
BCp.setIgnoreDependencies(false);
BCp.setCacheDirectory = function(dir) {

@@ -98,21 +105,20 @@ assert.strictEqual(typeof dir, "string");

BCp.setPreferredFileExtension = function(ext) {
function PreferredFileExtension(ext) {
assert.strictEqual(typeof ext, "string");
Object.defineProperty(this, "preferredFileExtension", {
assert.ok(this instanceof PreferredFileExtension);
Object.defineProperty(this, "extension", {
value: ext.toLowerCase()
});
};
}
// This default can be overridden by individual BuildContext instances.
BCp.setPreferredFileExtension("js");
var PFEp = PreferredFileExtension.prototype;
BCp.hasPreferredFileExtension = function(file) {
return file.split(".").pop().toLowerCase() ===
this.preferredFileExtension;
PFEp.check = function(file) {
return file.split(".").pop().toLowerCase() === this.extension;
};
BCp.trimPreferredFileExtension = function(file) {
if (this.hasPreferredFileExtension(file)) {
PFEp.trim = function(file) {
if (this.check(file)) {
var len = file.length;
var extLen = 1 + this.preferredFileExtension.length;
var extLen = 1 + this.extension.length;
file = file.slice(0, len - extLen);

@@ -123,2 +129,15 @@ }

PFEp.glob = function() {
return "**/*." + this.extension;
};
exports.PreferredFileExtension = PreferredFileExtension;
BCp.setPreferredFileExtension = function(pfe) {
assert.ok(pfe instanceof PreferredFileExtension);
Object.defineProperty(this, "preferredFileExtension", { value: pfe });
};
BCp.setPreferredFileExtension(new PreferredFileExtension("js"));
BCp.expandIdsOrGlobsP = function(idsOrGlobs) {

@@ -163,3 +182,3 @@ var context = this;

callback(null, files.map(function(file) {
return context.trimPreferredFileExtension(file);
return context.preferredFileExtension.trim(file);
}));

@@ -173,3 +192,3 @@ }

return this.watcher.readFileP(
id + "." + this.preferredFileExtension
id + "." + this.preferredFileExtension.extension
);

@@ -195,3 +214,3 @@ };

if (!idToPath.hasOwnProperty(id) ||
context.hasPreferredFileExtension(path))
context.preferredFileExtension.check(path))
idToPath[id] = path;

@@ -198,0 +217,0 @@ });

@@ -21,6 +21,7 @@ var assert = require("assert");

function hashCallbacks(salt, cbs) {
function hashCallbacks(salt) {
hash.update(salt + "\0");
cbs = cbs.concat(slice.call(arguments, 2));
var cbs = util.flatten(slice.call(arguments, 1));
cbs.forEach(function(cb) {

@@ -35,5 +36,8 @@ assert.strictEqual(typeof cb, "function");

resolvers = hashCallbacks("resolvers", resolvers, warnMissingModule);
processors = hashCallbacks(
"processors", processors, require("./relative").relativizeP);
var procArgs = [processors];
if (!context.ignoreDependencies)
procArgs.push(require("./relative").relativizeP);
processors = hashCallbacks("processors", procArgs);
Object.defineProperties(self, {

@@ -140,4 +144,2 @@ context: { value: context },

var commonJsFile = path.join(reader.context.outputDir, id + ".js");
Object.defineProperties(self, {

@@ -147,3 +149,2 @@ reader: { value: reader },

hash: { value: hash },
commonJsFile: { value: commonJsFile },
deps: { value: deps },

@@ -159,4 +160,6 @@ source: { value: source }

writeVersionP: function() {
return this.reader.cache.linkP(this.hash, this.commonJsFile);
writeVersionP: function(outputDir) {
assert.strictEqual(typeof outputDir, "string");
var commonJsFile = path.join(outputDir, this.id + ".js");
return this.reader.cache.linkP(this.hash, commonJsFile);
},

@@ -163,0 +166,0 @@

@@ -5,5 +5,3 @@ var assert = require("assert");

var recast = require("recast");
var types = require("ast-types");
var n = types.namedTypes;
var isString = types.builtInTypes.string;
var n = recast.namedTypes;

@@ -35,3 +33,3 @@ exports.relativizeP = function(id, source) {

if (n.Literal.check(arg) &&
isString.check(arg.value))
typeof arg.value === "string")
{

@@ -38,0 +36,0 @@ arg.value = relativize(this.moduleId, arg.value);

@@ -103,3 +103,3 @@ var assert = require("assert");

exports.readJsonFromStdinP = function() {
function readFromStdinP(timeLimit) {
var deferred = Q.defer();

@@ -112,9 +112,26 @@ var stdin = process.stdin;

timeLimit = timeLimit || 1000;
var timeout = setTimeout(function() {
log.err(
"Warning: still waiting for STDIN after " +
timeLimit + "ms",
"yellow"
);
}, timeLimit);
stdin.on("data", function(data) {
ins.push(data);
}).on("end", function() {
deferred.resolve(JSON.parse(ins.join("")));
clearTimeout(timeout);
deferred.resolve(ins.join(""));
});
return deferred.promise;
}
exports.readFromStdinP = readFromStdinP;
exports.readJsonFromStdinP = function(timeLimit) {
return readFromStdinP(timeLimit).then(function(input) {
return JSON.parse(input);
});
};

@@ -248,3 +265,3 @@

exports.isValidModuleId = function(id) {
return moduleIdExp.test(id);
return id === "<stdin>" || moduleIdExp.test(id);
};

@@ -270,1 +287,7 @@

exports.flatten = flatten;
exports.inherits = function(ctor, base) {
return ctor.prototype = Object.create(base.prototype, {
constructor: { value: ctor }
});
};

@@ -17,3 +17,3 @@ {

],
"version": "0.6.1",
"version": "0.6.3",
"homepage": "http://github.com/benjamn/commoner",

@@ -33,3 +33,2 @@ "repository": {

"q": ">= 0.9.1",
"ast-types": ">= 0.2.8",
"recast": ">= 0.3.3",

@@ -36,0 +35,0 @@ "commander": ">= 1.1.1",

@@ -56,3 +56,3 @@ Commoner

Usage: commonize [options] <source directory> <output directory> <module ID> [<module ID> [<module ID> ...]]
Usage: commonize [options] <source directory> <output directory> [<module ID> [<module ID> ...]]

@@ -59,0 +59,0 @@ Options:

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