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

one

Package Overview
Dependencies
Maintainers
1
Versions
180
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

one - npm Package Compare versions

Comparing version 1.0.0 to 1.1.0

lib/logging.js

183

lib/one.js

@@ -7,9 +7,11 @@ var templating = require('./templating'),

path = require('path'),
readFile = require('fs').readFile;
readFile = require('fs').readFile,
logging = require('./logging'),
fs = require('fs');
function build(wd, callback){
loadPackage(wd, function(error, pkg){
function build(manifestPath, callback){
loadPkg(manifestPath, function(error, pkg){
if(error) return callback(error);
var pkgs = flattenPackageTree(pkg),
var pkgs = flattenPkgTree(pkg),
treeName = templating.makeVariableName(pkg.name);

@@ -51,9 +53,11 @@

var dp = declaredDepList[i],
path = pkg.workingDir + 'node_modules/' + dp + '/';
manifestPath = path.join(pkg.wd, 'node_modules/', dp, '/package.json');
if(pkg.packageDict[dp]){
logging.debug('Loading the dependency in "'+manifestPath+'" for the package "'+pkg.name+'"');
if(pkg.pkgDict[dp]){
return next();
}
loadPackage(pkg, path, function(error, subpkg){
loadPkg(pkg, manifestPath, function(error, subpkg){
if(error){

@@ -71,2 +75,8 @@ return callback(error);

function collectModules(pkg, callback){
logging.debug('Collect modules for the package "'+pkg.name+'"');
var dir = path.join(pkg.wd, pkg.dirs.lib || '.');
logging.debug('The directories to search:',dir);
compose.async(combiner.findFiles,

@@ -79,2 +89,3 @@ combiner.includeDirectories,

function(filenames, callback){
logging.debug('Found '+filenames.length+' file(s) under the package "'+pkg.name+'"');
var modules = [];

@@ -90,3 +101,8 @@

loadModule(filenames[i], function(error, module){
module.filename = module.filename.replace(pkg.workingDir,'').replace(/^lib\//,'');
if(error){
logging.error('Failed to load the module "'+filenames[i]+'"');
return callback(error);
}
module.filename = module.filename.replace(dir+'/', '').replace(dir,'');
module.id = module.filename.replace(/\.js$/,'');

@@ -98,6 +114,6 @@ if(!error) modules.push(module);

})(0);
})([pkg.workingDir + 'lib/'],callback);
})([dir],callback);
}
function flattenPackageTree(tree){
function flattenPkgTree(tree){
var l = [tree];

@@ -107,3 +123,3 @@

while(tree.dependencies && ++i<tree.dependencies.length){
Array.prototype.push.apply(l, flattenPackageTree(tree.dependencies[i]));
Array.prototype.push.apply(l, flattenPkgTree(tree.dependencies[i]));
}

@@ -118,2 +134,29 @@

function loadManifest(path, callback){
logging.debug('Loading the manifest @ "'+path+'"');
var manifest;
readFile(path, function(error, bf){
if(error){
logging.error('Failed to read the file "'+path+'"');
return callback(error);
}
logging.debug('Parsing the manifest @ "'+path+'"');
if(!error){
manifestSource = bf.toString();
try {
manifest = JSON.parse(manifestSource);
} catch(exc) {
logging.error('Failed to parse the manifest @ "'+manifestPath+'"');
error = exc;
}
}
callback(error, manifest);
});
}
var loadModule = (function(){

@@ -124,2 +167,3 @@

return function(filename, callback){
logging.debug('Load module "'+filename+'"');
readFile(filename, function(error, bf){

@@ -142,60 +186,43 @@ if(error) return callback(error);

function loadPackage(/*parentPackage, wd, callback*/){
function loadPkg(/*parentPkg, manifestPath, callback*/){
var argsLen = arguments.length,
parentPackage = argsLen == 3 && arguments[0] || undefined,
wd = arguments[argsLen-2],
parentPkg = argsLen == 3 && arguments[0] || undefined,
manifestPath = arguments[argsLen-2],
callback = arguments[argsLen-1];
wd[wd.length-1] != '/' && ( wd = wd+'/' );
wd = wd.replace(/^\.\//,'');
var wd = path.normalize(path.dirname(manifestPath));
var mainModule = undefined,
mainModulePath = undefined,
manifestPath = wd + 'package.json',
manifestSource = undefined,
manifest = undefined,
dependencies = undefined,
modules = undefined,
modulesDict = {},
pkg = undefined,
i = undefined;
logging.info('Loading the package "'+manifestPath+'"');
readFile(manifestPath, function(error, bf){
if(!error){
manifestSource = bf.toString();
try {
manifest = JSON.parse(manifestSource);
} catch(exc) {
error = exc;
}
}
loadManifest(manifestPath, function(error, manifest){
if(error){
return callback(error);
}
pkg = {
var pkg = {
'id':templating.id(),
'dependencies':dependencies,
'main':mainModule,
'dependencies':undefined,
'dirs':undefined,
'main':undefined,
'manifest':manifest,
'manifestPath':manifestPath,
'manifestSource':manifestSource,
'modules':modules,
'modulesDict':modulesDict,
'modules':undefined,
'modulesDict':{},
'name':manifest.name,
'parent':parentPackage,
'packageDict':parentPackage && parentPackage.packageDict || {},
'workingDir':wd
'parent':parentPkg,
'pkgDict':parentPkg ? parentPkg.pkgDict : {},
'wd':wd
};
pkg.packageDict[pkg.name] = pkg;
pkg.pkgDict[pkg.name] = pkg;
pkg.dirs = manifest.directories || { 'lib':'.' };
collectDeps(pkg, function(error, deps){
if(error){
logging.error('An unexpected error occurred during collecting dependencies of the package "'+pkg.name+'".');
return callback(error);
}
logging.debug('Found '+deps.length+' dependencies for the package "'+pkg.name+'"');
pkg.dependencies = deps;

@@ -205,14 +232,18 @@

if(error){
logging.error('An unexpected error occurred during collecting modules of the package "'+pkg.name+'".');
return callback(error);
}
logging.debug('Collected '+modules.length+' modules for the package "'+pkg.name+'"');
pkg.modules = modules;
var i = modules.length,
m;
var i = modules.length, m, mainModulePath;
while(i-->0){
m = modules[i];
modulesDict[m.path] = m;
pkg.modulesDict[m.path] = m;
}
if(!error && manifest.main){
if(manifest.main){
mainModulePath = path.join(wd, manifest.main+'.js');

@@ -224,6 +255,6 @@ pkg.main = pkg.modulesDict[mainModulePath];

});
});
});
}

@@ -236,3 +267,8 @@

function quiet(y){
logging.level = y ? 3 : 1;
}
function renderLibrary(callback){
logging.info('Rendering library...');
render({ 'template':'node/path.js' }, function(error, nodePathSC){

@@ -249,2 +285,5 @@ render({ 'template':'library.js', 'view':{}, 'partials':{ 'node_path':nodePathSC } }, callback);

};
logging.debug('Rendering module "'+view.id+'"');
render({ 'template':'module.js', 'view':view, 'partials':{ 'content':options.module.content } }, callback);

@@ -261,5 +300,7 @@ }

'name':options.pkg.name,
'wd':options.pkg.workingDir
'wd':options.pkg.wd
};
logging.info('Rendering package "'+view.name+'"');
var modules = options.pkg.modules.map(function(el){

@@ -280,3 +321,7 @@ return {

function renderWrapper(options, callback){
logging.info('Rendering wrapper...');
renderLibrary(function(error, librarySC){
if(error){
return callback(error);
}
render({

@@ -293,2 +338,20 @@ 'template':'wrapper.js',

function save(target, content, callback){
logging.info('Saving output into '+target);
fs.writeFile(target, content, function(error) {
if(error) {
logging.error('Failed to write the target file "'+target+'"');
return callback(error);
  }
logging.info('The target file "'+target+'" was saved!');
callback();
});
}
function verbose(y){
logging.level = y ? 0 : 1;
}
module.exports = {

@@ -299,7 +362,11 @@ 'build':build,

'filterFilename':filterFilename,
'flattenPackageTree':flattenPackageTree,
'flattenPkgTree':flattenPkgTree,
'loadManifest':loadManifest,
'loadModule':loadModule,
'loadPackage':loadPackage,
'loadPkg':loadPkg,
'moduleName':moduleName,
'renderModule':renderModule
'quiet':quiet,
'renderModule':renderModule,
'save':save,
'verbose':verbose
}
{
"name":"one",
"version":"1.0.0",
"version":"1.1.0",
"description":"Transform NodeJS packages into single stand-alone script files.",

@@ -10,10 +10,13 @@ "author":"Azer Koculu <azer@kodfabrik.com>",

"bin":{
"onejs":"./bin/onejs"
},
"devDependencies":{
"highkick":"1.x"
},
"dependencies":{
"npm":"*",
"opts":"*",
"combiner":"1.0.x",
"mustache":"0.3.x",
"highkick":"1.0.x"
"optimist":"0.x",
"combiner":"1.x",
"mustache":"0.x",
"colors":"0.x"
}
}
{
"name":"dependency",
"directories":{ "lib":"./lib" },
"dependencies":{

@@ -4,0 +5,0 @@ "subdependency":"*"

{
"name":"sibling",
"directories":{ "lib":"./lib" },
"main":"lib/n"
}
{
"name":"example-project",
"main":"./lib/a",
"directories":{ "lib":"./lib" },
"dependencies":{

@@ -5,0 +6,0 @@ "dependency":"*",

var kick = require('highkick'),
puts = require('sys').puts;
kick({ module:require('./tests'), name:'main' }, function(error, result){
kick({ module:require('./tests'), name:' main' }, function(error, result){
if(error) throw error;
puts('====');
puts('Ran '+result.len+' tests ' + ( result.fail ? 'with ' + result.fail + ' fail.' : 'without any error.') );
});

@@ -7,14 +7,23 @@ var one = require('../lib/one'),

one.quiet(true);
function verifyListContent(a,b){
return a.length == b.length && a.every(function(el){
return b.indexOf(el) > -1;
});
}
function test_verifyListContent(callback){
assert.ok(verifyListContent([3,1,4],[4,3,1]));
assert.ok(!verifyListContent([3,[1],4],[4,3,[1]]));
assert.ok(!verifyListContent([3,1,4],[3,1,6]));
assert.ok(!verifyListContent([3,1,4],[3,1,4,6]));
callback();
}
function test_build(callback){
one.build('test/example-project', function(error, sourceCode){
fs.open('/tmp/__package.js', 'w', undefined, function(error, fd){
fs.write(fd, sourceCode, undefined, undefined, function(err, written){
if(err) throw err;
fs.close(fd, function(err){
if(err) throw err;
kick({ module:require('./templates'), 'silent':1, 'name':'templates' },function(error,result){
callback(result.fail);
});
});
one.build('test/example-project/package.json', function(error, sourceCode){
one.save('/tmp/__package.js', sourceCode, function(error){
kick({ module:require('./templates'), 'silent':0, 'name':' templates' },function(error,result){
callback(result.fail ? new Error('Fail') : undefined);
});

@@ -34,7 +43,8 @@ });

},
'workingDir':'test/example-project/',
'packageDict':{}
'wd':'test/example-project/',
'pkgDict':{}
};
one.collectDeps(pkg, function(error, deps){
if(error) return callback(error);
assert.equal(deps.length, 2);

@@ -58,4 +68,4 @@ assert.equal(deps[0].name, 'dependency');

function test_loadPackage(callback){
one.loadPackage('test/example-project/', function(error, pkg){
function test_loadPkg(callback){
one.loadPkg('test/example-project/package.json', function(error, pkg){
if(error) return callback(error);

@@ -67,7 +77,6 @@

assert.equal(pkg.manifest.name, 'example-project');
assert.equal(pkg.manifestPath, 'test/example-project/package.json');
assert.equal(pkg.dependencies.length, 2);
assert.equal(pkg.main.filename, 'a.js');
var pkgDict = Object.keys(pkg.packageDict);
var pkgDict = Object.keys(pkg.pkgDict);
assert.equal(pkgDict.length, 4);

@@ -82,2 +91,15 @@ assert.equal(pkgDict[0], 'example-project');

assert.equal(pkg.modules[1].filename, 'b.js');
assert.equal(pkg.pkgDict.dependency.modules.length, 2);
verifyListContent(['f.js','g.js'],pkg.pkgDict.dependency.modules);
assert.equal(pkg.pkgDict.subdependency.modules.length, 2);
assert.equal(pkg.pkgDict.subdependency.modules[0].filename, 'i.js');
assert.equal(pkg.pkgDict.subdependency.modules[1].filename, 'j.js');
assert.equal(pkg.pkgDict.sibling.modules.length, 3);
assert.equal(pkg.pkgDict.sibling.modules[0].filename, 'p/r.js');
assert.equal(pkg.pkgDict.sibling.modules[2].filename, 's/t.js');
assert.equal(pkg.pkgDict.sibling.modules[1].filename, 'n.js');
callback();

@@ -91,3 +113,3 @@ } catch(error) {

function test_collectModules(callback){
one.collectModules({ 'workingDir':'test/example-project/' }, function(error, modules){
one.collectModules({ 'name':'example-project', 'dirs':{'lib':'lib'}, 'wd':'test/example-project/' }, function(error, modules){
try {

@@ -155,3 +177,14 @@ assert.equal(modules.length, 2);

function test_flattenPackageTree(callback){
function test_loadManifest(callback){
one.loadManifest('test/example-project/package.json', function(error, manifest){
assert.equal(manifest.name, "example-project");
assert.equal(manifest.main, "./lib/a");
assert.equal(manifest.directories.lib, "./lib");
assert.equal(manifest.dependencies.dependency, "*");
assert.equal(manifest.dependencies.sibling, "*");
callback();
});
}
function test_flattenPkgTree(callback){
var ids = [1,2,3,4,5,6,9,7,8],

@@ -184,3 +217,3 @@ tree = {

var flat = one.flattenPackageTree(tree);
var flat = one.flattenPkgTree(tree);
assert.equal(flat.length, 9);

@@ -198,12 +231,14 @@

module.exports = {
'test_id':test_id,
'test_build':test_build,
'test_collectDeps':test_collectDeps,
'test_collectModules':test_collectModules,
'test_filterFilename':test_filterFilename,
'test_loadPackage':test_loadPackage,
'test_collectDeps':test_collectDeps,
'test_flattenPkgTree':test_flattenPkgTree,
'test_id':test_id,
'test_loadManifest':test_loadManifest,
'test_loadModule':test_loadModule,
'test_loadPkg':test_loadPkg,
'test_makeVariableName':test_makeVariableName,
'test_moduleName':test_moduleName,
'test_flattenPackageTree':test_flattenPackageTree,
'test_makeVariableName':test_makeVariableName
'test_verifyListContent':test_verifyListContent
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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