Skip to content
This repository was archived by the owner on Dec 6, 2023. It is now read-only.

Commit 4079509

Browse files
committed
Refactoring.
1 parent d5305ed commit 4079509

14 files changed

+192
-157
lines changed

bin/_avn

+7-16
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,26 @@
55
var program = require('commander');
66
var fs = require('fs');
77
var path = require('path');
8+
var chalk = require('chalk');
89
var avn = require('..');
910

10-
var hook = function(fn) {
11-
return function() {
12-
// set up stdcmd stream
13-
try { process.stdcmd = fs.createWriteStream(null, { fd: 3 }); }
14-
catch (e) { process.stdcmd = process.stderr; }
15-
16-
// setup colors based on program options
17-
require('chalk').enabled = program.color;
18-
fn.apply(this, arguments);
19-
};
20-
};
21-
2211
program
2312
.version(JSON.parse(fs.readFileSync(__dirname + '/../package.json', 'utf8')).version)
2413
.option('-v --verbose', 'run verbosely')
2514
.option('-c --color', 'use colors (automatic for some commands)');
2615

2716
program
28-
.command('chpwd <path> [avnvc]')
17+
.command('chpwd <path> [versionFile]')
2918
.description('eval actions for chpwd')
30-
.action(hook(function(dir, versionFile) {
19+
.action(function(dir, versionFile) {
20+
chalk.enabled = program.color;
21+
process.stdcmd = fs.createWriteStream(null, { fd: 3 });
3122
avn.hooks.chpwd(path.resolve(dir), versionFile,
3223
{ verbose: program.verbose }).done();
33-
}));
24+
});
3425

3526
program
36-
.command('explain <path> [avnvc]')
27+
.command('explain <path> [versionFile]')
3728
.description('show what will happen for a given path')
3829
.action(function(dir, versionFile) {
3930
process.stdcmd = process.stderr;

lib/hooks.js

+4-16
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
var _ = require('lodash');
44
var Promise = require('bluebird');
5-
var fs = Promise.promisifyAll(require('fs'));
5+
var fs = require('mz/fs');
66
var path = require('path');
77
var plugins = require('./plugins');
88
var fmt = require('./fmt');
9-
var rethrowUnlessNoEntry;
9+
var isNoEntry = require('./util/codes').isNoEntry;
1010

1111
/**
1212
* Find the first plugin that can activate the requested version.
@@ -71,28 +71,16 @@ exports.chpwd = function(/*dir, [versionFile], [options]*/) {
7171
}
7272

7373
return Promise.bind({})
74-
.then(function() { return fs.readFileAsync(path.join(dir, file), 'utf8'); })
74+
.then(function() { return fs.readFile(path.join(dir, file), 'utf8'); })
7575
.then(function(version) { this.version = version.trim(); })
7676
.then(function() { return match(this.version); })
7777
.then(function(result) {
7878
process.stdout.write(fmt.success(this.version, result, via));
7979
process.stdcmd.write(result.command + '\n');
8080
})
81-
.catch(rethrowUnlessNoEntry)
81+
.catch(isNoEntry, _.noop)
8282
.catch(function(e) {
8383
if (e.code !== 'PREDICATE_FAILED') { throw e; }
8484
else { console.warn(fmt.failure(this.version, e, opts.verbose)); }
8585
});
8686
};
87-
88-
/**
89-
* Rethrow when the error code is not `ENOENT`.
90-
*
91-
* @private
92-
* @function hooks.~rethrowUnlessNoEntry
93-
* @param {Error} error The error to check.
94-
*/
95-
rethrowUnlessNoEntry = function(e) {
96-
if (e.code === 'ENOENT') { }
97-
else { throw e; }
98-
};

lib/plugins.js

+20-18
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
var _ = require('lodash');
44
var Promise = require('bluebird');
55
var path = require('path');
6-
var fs = require('fs');
6+
var fs = require('mz/fs');
7+
var isNoEntry = require('./util/codes').isNoEntry;
78
var errorify;
89
var rethrowUnlessFailedRequire;
910

@@ -16,9 +17,9 @@ var rethrowUnlessFailedRequire;
1617
* `~/.avnrc` configuration file.
1718
*/
1819
exports.all = (function() {
19-
var loaded;
20+
var cache;
2021
return function() {
21-
return loaded || (loaded = exports._all());
22+
return cache || (cache = exports._all());
2223
};
2324
})();
2425

@@ -47,10 +48,13 @@ exports._all = function() {
4748
// look at ~/.avnrc file to get the order of plugins, but fall back on a
4849
// default order if there's no config.
4950
var file = path.join(process.env.HOME, '.avnrc');
50-
var contents = fs.existsSync(file) && fs.readFileSync(file, 'utf8');
51-
var config = contents && JSON.parse(contents) || {};
52-
var order = _.union(config.plugins, ['nvm', 'n']);
53-
return order.reduce(function(array, name) {
51+
52+
return fs.readFile(file, 'utf8').catch(isNoEntry, _.noop)
53+
.then(function(contents) {
54+
var config = contents && JSON.parse(contents) || {};
55+
return _.union(config.plugins, ['nvm', 'n']);
56+
})
57+
.reduce(function(array, name) {
5458
var plugin;
5559
var installed = path.join(process.env.HOME, '.avn/plugins', 'avn-' + name);
5660
try { plugin = require(installed); }
@@ -77,19 +81,17 @@ exports._all = function() {
7781
* `details` containing any sub-errors raised by the predicate's promise.
7882
*/
7983
exports.first = function(predicate) {
80-
var result;
8184
var errors = [];
82-
/** local */
83-
var reduction = function(sequence, plugin) {
84-
return sequence
85-
.then(function() { return result || predicate(plugin); })
86-
.then(
87-
function(r) { result = result || r && plugin; },
88-
function(e) { errors.push(errorify(e, { plugin: plugin })); });
89-
};
90-
return exports.all().reduce(reduction, Promise.resolve()).then(function() {
85+
return Promise.reduce(exports.all(), function(result, plugin) {
86+
return result || Promise.resolve(predicate(plugin))
87+
.then(function(r) { return r && plugin; })
88+
.catch(function(e) {
89+
errors.push(errorify(e, { plugin: plugin }));
90+
});
91+
}, null)
92+
.then(function(result) {
9193
if (!result) {
92-
throw errorify('no plugin passed predicate', {
94+
throw _.extend(new Error('no plugin passed predicate'), {
9395
code: 'PREDICATE_FAILED',
9496
details: errors,
9597
});

lib/setup/config.js

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
'use strict';
22

33
var _ = require('lodash');
4-
var Promise = require('bluebird');
5-
var fs = Promise.promisifyAll(require('fs'));
4+
var fs = require('mz/fs');
65
var path = require('path');
76
var chalk = require('chalk');
87
var installedPlugins = require('./plugins').installed;
8+
var isNoEntry = require('../util/codes').isNoEntry;
99

1010
/**
1111
* Update `~/.avnrc` configuration file.
@@ -28,12 +28,16 @@ var installedPlugins = require('./plugins').installed;
2828
* @see {@link plugins.~all}
2929
*/
3030
module.exports.update = function() {
31-
var file = path.join(process.env.HOME, '.avnrc');
32-
var contents = fs.existsSync(file) && fs.readFileSync(file);
33-
var config = contents && JSON.parse(contents) || {};
31+
var contents, config;
3432
var change = 'unchanged';
33+
var file = path.join(process.env.HOME, '.avnrc');
3534

36-
return Promise.resolve()
35+
return fs.readFile(file)
36+
.catch(isNoEntry, _.noop)
37+
.then(function(result) {
38+
contents = result;
39+
config = contents && JSON.parse(contents) || {};
40+
})
3741
.then(function() { return installedPlugins(); })
3842
.then(function(plugins) {
3943
plugins = _.map(plugins, 'name');
@@ -42,7 +46,7 @@ module.exports.update = function() {
4246
if (!_.isEqual(config.plugins, plugins)) {
4347
config.plugins = plugins;
4448
change = (contents ? 'updated' : 'complete');
45-
return fs.writeFileAsync(file, JSON.stringify(config, null, 2));
49+
return fs.writeFile(file, JSON.stringify(config, null, 2));
4650
}
4751
})
4852
.then(function() {

lib/setup/install.js

+38-22
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,29 @@
11
'use strict';
22

3+
var _ = require('lodash');
34
var Promise = require('bluebird');
4-
var fs = require('fs');
5+
var fs = require('mz/fs');
56
var cp = require('child_process');
67
var path = require('path');
78
var chalk = require('chalk');
89
var installedPlugins = require('./plugins').installed;
10+
var isNoEntry = require('../util/codes').isNoEntry;
11+
12+
/**
13+
* Get a module version by reading it's package.json.
14+
*
15+
* @private
16+
* @function setup.install.~moduleVersion
17+
* @param {String} module The path to the module.
18+
* @return {Promise}
19+
*/
20+
var moduleVersion = function(module) {
21+
return fs.readFile(path.join(module, 'package.json'), 'utf8')
22+
.catch(isNoEntry, _.noop).then(function(contents) {
23+
try { return JSON.parse(contents).version; }
24+
catch (e) {}
25+
});
26+
};
927

1028
/**
1129
* Install a module by copying it from the source to the destination.
@@ -17,28 +35,26 @@ var installedPlugins = require('./plugins').installed;
1735
* @return {Promise}
1836
*/
1937
var install = function(src, dst) {
20-
/** local */
21-
var version = function(root) {
22-
try {
23-
var contents = fs.readFileSync(path.join(root, 'package.json'), 'utf8');
24-
return JSON.parse(contents).version;
25-
}
26-
catch (e) {}
27-
};
38+
var srcVersion, dstVersion;
39+
return Promise.resolve()
40+
.then(function() { return moduleVersion(src); })
41+
.then(function(v) { srcVersion = v; })
42+
.then(function() { return moduleVersion(dst); })
43+
.then(function(v) { dstVersion = v; })
44+
.then(function() {
45+
return new Promise(function(resolve, reject) {
46+
if (srcVersion === dstVersion) { resolve(); }
47+
else {
48+
var cmd = cp.spawn('/bin/cp', ['-RL', src, dst]);
49+
cmd.stdout.pipe(process.stdout);
50+
cmd.stderr.pipe(process.stderr);
51+
cmd.on('close', function(code) {
52+
if (code === 0) { resolve(dstVersion ? 'updated' : 'complete'); }
53+
else { reject(new Error('cp exited with status: ' + code)); }
54+
});
55+
}
56+
});
2857

29-
return new Promise(function(resolve, reject) {
30-
var srcVersion = version(src);
31-
var dstVersion = version(dst);
32-
if (srcVersion === dstVersion) { resolve(); }
33-
else {
34-
var cmd = cp.spawn('/bin/cp', ['-RL', src, dst]);
35-
cmd.stdout.pipe(process.stdout);
36-
cmd.stderr.pipe(process.stderr);
37-
cmd.on('close', function(code) {
38-
if (code === 0) { resolve(dstVersion ? 'updated' : 'complete'); }
39-
else { reject(new Error('cp exited with status: ' + code)); }
40-
});
41-
}
4258
});
4359
};
4460

0 commit comments

Comments
 (0)