Skip to content

Commit

Permalink
fix the darn require analyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
bmeck committed Jun 15, 2011
1 parent c8d3173 commit 3fe9171
Showing 1 changed file with 54 additions and 54 deletions.
108 changes: 54 additions & 54 deletions lib/jitsu/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* (C) 2010, Nodejitsu Inc.
*
*/

var util = require('util'),
path = require('path'),
fs = require('fs'),
Expand Down Expand Up @@ -49,12 +49,12 @@ package.get = function (dir, options, callback) {
callback = options;
options = {};
}

package.read(dir, function (err, pkg) {
if (err) {
return package.create(dir, callback);
}

package.validate(pkg, dir, options, function (err, updated) {
return err ? callback(err) : callback(null, updated);
});
Expand All @@ -69,7 +69,7 @@ package.get = function (dir, options, callback) {
//
package.read = function (dir, callback) {
var file = path.join(dir, 'package.json');

fs.readFile(file, function (err, data) {
if (err) {
return callback(err);
Expand All @@ -96,11 +96,11 @@ package.read = function (dir, callback) {
// #### @callback {function} Continuation to respond to on error.
// #### @success {function} Continuation to respond to on success.
// Attempts to read the package.json file from the specified `dir`; responds
// to `callback` on error and `success` if the read operation worked.
// to `callback` on error and `success` if the read operation worked.
//
package.tryRead = function (dir, callback, success) {
package.read(dir, function (err, pkg) {
return err ? callback(new Error('No package.json in ' + dir), true) : success(pkg);
return err ? callback(new Error('No package.json in ' + dir), true) : success(pkg);
});
};

Expand All @@ -120,21 +120,21 @@ package.create = function (dir, callback) {
'Press ^C at any time to quit.',
'to select a default value, press ENTER'
];

winston.warn('There in no valid package.json file in ' + dir.grey);
winston.warn('Creating package.json at ' + (dir + '/package.json').grey);

help.forEach(function (line) {
winston.help(line);
});

jitsu.prompt.get(package.properties(dir), function (err, result) {
if (err) {
//
// TODO: Something here...
//
}

var pkg = {
name: result.name,
subdomain: result.subdomain,
Expand All @@ -143,12 +143,12 @@ package.create = function (dir, callback) {
},
version: result.version
};

package.analyzeDependencies(pkg, dir, function (err, addedDeps, updates) {
if (err) {
return callback(err);
}

package.write(addedDeps, dir, true, callback);
});
});
Expand All @@ -159,39 +159,39 @@ package.create = function (dir, callback) {
// #### @pkg {Object} Parsed package.json to validate
// #### @dir {string} Directory containing the package.json file
// #### @callback {function} Continuation to respond to when complete
// Validates the specified `pkg` against the properties list
// Validates the specified `pkg` against the properties list
// returned from `package.properties(dir)`.
//
package.validate = function (pkg, dir, options, callback) {
if (!callback) {
callback = options;
options = {};
}

var properties = package.properties(dir),
missing = [];

function checkProperty (desc, next) {
var nested = desc.name.split('.'),
var nested = desc.name.split('.'),
value = pkg[nested[0]];

if (nested.length > 1 && value) {
value = value[nested[1]];
}

if (!value || (missing.validator && !missing.validator.test(value))) {
missing.push(desc);
}

next();
}

async.forEach(properties, checkProperty, function () {
if (missing.length > 0) {
var help, names = missing.map(function (prop) {
return ' ' + (prop.message || prop.name).grey;
});

help = [
'',
'Your package.json file is missing required fields:',
Expand All @@ -202,41 +202,41 @@ package.validate = function (pkg, dir, options, callback) {
'Press ^C at any time to quit.',
''
];

help.forEach(function (line) {
winston.warn(line);
});

return jitsu.prompt.addProperties(pkg, missing, function (err, updated) {
return err ? callback(err) : tryAnalyze(updated);
});
}

function tryAnalyze (target) {
var resolve = jitsu.config.get('resolveDependencies'),
analyze = (resolve === undefined ? !optimist.argv.noanalyze : !resolve);

if(!analyze) {
winston.info('skipping require-analyzer because ' + '--noanalyze'.magenta + ' option is set');
return callback(null, pkg);
}

package.analyzeDependencies(target, dir, function (err, addedDeps, updates) {

if (err) {
return callback(err);
}

return updates ? package.write(addedDeps, dir, true, callback) : callback(null, addedDeps);
});
}

var resolve = jitsu.config.get('resolveDependencies'),
analyze = (resolve === undefined ? !optimist.argv.noanalyze : !resolve);

if (analyze) {
return tryAnalyze(pkg);
}

winston.info('skipping require-analyzer because ' + '--noanalyze'.magenta + ' option is set');
callback(null, pkg);
return tryAnalyze(pkg);
});
};

//
// ### function writePackage (pkg, dir, callback)
// ### function writePackage (pkg, dir, callback)
// #### @pkg {Object} Data for the package.json
// #### @dir {string} Directory to write the package.json in
// #### @callback {function} Continuation to respond to when complete
Expand All @@ -249,7 +249,7 @@ package.write = function (pkg, dir, create, callback) {
callback = create;
create = null;
}

winston.warn('About to write ' + path.join(dir, 'package.json').magenta);
jitsu.log.putObject(pkg, 2);
jitsu.prompt.get({
Expand All @@ -260,15 +260,15 @@ package.write = function (pkg, dir, create, callback) {
if (result.answer !== 'yes' && result.answer !== 'y') {
return create ? package.create(dir, callback) : callback(new Error('Save package.json cancelled.'));
}

fs.writeFile(path.join(dir, 'package.json'), JSON.stringify(pkg, null, 2), function (err) {
return err ? callback(err) : callback(null, pkg, dir);
})
});
};

//
// ### function checkDependencies (pkg, dir, callback)
// ### function checkDependencies (pkg, dir, callback)
// #### @pkg {Object} Parsed package.json to check dependencies for.
// #### @dir {string} Directory containing the package.json file.
// #### @callback {function} Continuation to respond to when complete.
Expand All @@ -281,26 +281,26 @@ package.analyzeDependencies = function (pkg, dir, callback) {
// Create a hash of `'package': '>= version'` for the new dependencies
//
var updates, versions = analyzer.extractVersions(pkgs);

if (package.newDependencies(pkg.dependencies, versions)) {
//
// If there are new dependencies, indicate this to the user.
//
winston.info('Found new dependencies. They will be added automatically');

//
// Extract, merge, and display the updates found by `require-analyzer`
//
updates = analyzer.updates(pkg.dependencies, versions);
updates = analyzer.merge({}, updates.added, updates.updated);
jitsu.log.putObject(updates);

//
//
// Update the package.json dependencies
//
pkg.dependencies = analyzer.merge({}, pkg.dependencies || {}, updates);
}

callback(null, pkg, updates);
});
};
Expand All @@ -316,23 +316,23 @@ package.createTarball = function (dir, version, callback) {
callback = version;
version = null;
}

package.read(dir, function (err, pkg) {
if (err) {
return callback(err);
}

if (dir.slice(-1) === '/') {
dir = dir.slice(0, -1);
}

var name = [jitsu.config.get('username'), pkg.name, version || pkg.version].join('-') + '.tgz',
tarball = path.join(jitsu.config.get('tmproot'), name);

npm.load({ exit: false }, function () {
npmtar.pack(tarball, dir, pkg, true, function (err) {
return err ? callback(err) : callback(null, pkg, tarball);
});
});
});
});
};
Expand All @@ -343,19 +343,19 @@ package.createTarball = function (dir, version, callback) {
// #### @pkg {Object} Current package.json file on disk
// #### @existing {Object} Remote package.json stored at Nodejitsu
// #### @callback {function} Continuation to respond to when complete.
//
//
//
package.updateTarball = function (version, pkg, existing, firstSnapshot, callback) {
if (!callback) {
callback = firstSnapshot;
firstSnapshot = false;
}

function executeCreate (err) {
if (err) {
return callback(err, true);
}

version = version || pkg.version;
jitsu.package.createTarball(process.cwd(), version, function (err, ign, filename) {
if (err) {
Expand All @@ -370,9 +370,9 @@ package.updateTarball = function (version, pkg, existing, firstSnapshot, callbac
});
});
}

var old = false;

if (!firstSnapshot) {
winston.silly('Existing version: ' + existing.version.magenta);
winston.silly('Local version: ' + pkg.version.magenta);
Expand All @@ -387,7 +387,7 @@ package.updateTarball = function (version, pkg, existing, firstSnapshot, callbac
pkg.version = semver.inc(existing.version, 'build');
}
}

return old
? package.write(pkg, process.cwd(), executeCreate)
: executeCreate();
Expand All @@ -408,13 +408,13 @@ package.newDependencies = function (current, updated) {
//
// ### function properties (dir)
// #### @dir {string} Directory in which the package.json properties are being used
// Returns a new set of properties to be consumed by `jitsu.prompt` to walk a user
// Returns a new set of properties to be consumed by `jitsu.prompt` to walk a user
// through creating a new `package.json` file.
//
package.properties = function (dir) {
return [
{
name: 'name',
name: 'name',
message: 'App name',
validator: /[\w|\-]+/,
default: path.basename(dir)
Expand Down

0 comments on commit 3fe9171

Please sign in to comment.