Add ln support, including both -s and -f options.

This commit is contained in:
Michael Schoonmaker 2013-10-10 18:03:09 -07:00
parent 763e3593ef
commit b715c2c240
4 changed files with 158 additions and 0 deletions

View File

@ -439,6 +439,23 @@ Display the list of currently remembered directories. Returns an array of paths
See also: pushd, popd
### ln(options, source, dest)
### ln(source, dest)
Available options:
+ `s`: symlink
+ `f`: force
Examples:
```javascript
ln('file', 'newlink');
ln('-sf', 'file', 'existing');
```
Links source to dest. Use -f to force the link, should dest already exist.
### exit(code)
Exits the current process with the given exit code.

View File

@ -85,6 +85,10 @@ exports.pushd = common.wrap('pushd', _pushd);
var _popd = require('./src/dirs').popd;
exports.popd = common.wrap("popd", _popd);
//@include ./src/ln
var _ln = require('./src/ln');
exports.ln = common.wrap('ln', _ln);
//@
//@ ### exit(code)
//@ Exits the current process with the given exit code.

52
src/ln.js Normal file
View File

@ -0,0 +1,52 @@
var fs = require('fs');
var path = require('path');
var common = require('./common');
//@
//@ ### ln(options, source, dest)
//@ ### ln(source, dest)
//@ Available options:
//@
//@ + `s`: symlink
//@ + `f`: force
//@
//@ Examples:
//@
//@ ```javascript
//@ ln('file', 'newlink');
//@ ln('-sf', 'file', 'existing');
//@ ```
//@
//@ Links source to dest. Use -f to force the link, should dest already exist.
function _ln(options, source, dest) {
options = common.parseOptions(options, {
's': 'symlink',
'f': 'force'
});
if (!source || !dest) {
common.error('Missing <source> and/or <dest>');
}
source = path.join(process.cwd(), String(source));
dest = path.join(process.cwd(), String(dest));
if (!fs.existsSync(source)) {
common.error('Source file does not exist', true);
}
if (fs.existsSync(dest)) {
if (!options.force) {
common.error('Destination file exists', true);
}
fs.unlinkSync(dest);
}
if (options.symlink) {
fs.symlinkSync(source, dest);
} else {
fs.linkSync(source, dest);
}
}
module.exports = _ln;

85
test/ln.js Normal file
View File

@ -0,0 +1,85 @@
var shell = require('..');
var assert = require('assert'),
fs = require('fs');
shell.config.silent = true;
shell.rm('-rf', 'tmp');
shell.mkdir('tmp');
// Prepare tmp/
shell.cp('resources/*', 'tmp');
//
// Invalids
//
shell.ln();
assert.ok(shell.error());
shell.ln('file');
assert.ok(shell.error());
shell.ln('-f');
assert.ok(shell.error());
shell.ln('tmp/file1', 'tmp/file2');
assert.ok(shell.error());
shell.ln('tmp/noexist', 'tmp/linkfile1');
assert.ok(shell.error());
//
// Valids
//
shell.ln('tmp/file1', 'tmp/linkfile1');
assert(fs.existsSync('tmp/linkfile1'));
assert.equal(
fs.readFileSync('tmp/file1').toString(),
fs.readFileSync('tmp/linkfile1').toString()
);
fs.writeFileSync('tmp/file1', 'new content 1');
assert.equal(
fs.readFileSync('tmp/linkfile1').toString(),
'new content 1'
);
shell.ln('-s', 'tmp/file2', 'tmp/linkfile2');
assert(fs.existsSync('tmp/linkfile2'));
assert.equal(
fs.readFileSync('tmp/file2').toString(),
fs.readFileSync('tmp/linkfile2').toString()
);
fs.writeFileSync('tmp/file2', 'new content 2');
assert.equal(
fs.readFileSync('tmp/linkfile2').toString(),
'new content 2'
);
shell.ln('-f', 'tmp/file1.js', 'tmp/file2.js');
assert(fs.existsSync('tmp/file2.js'));
assert.equal(
fs.readFileSync('tmp/file1.js').toString(),
fs.readFileSync('tmp/file2.js').toString()
);
fs.writeFileSync('tmp/file1.js', 'new content js');
assert.equal(
fs.readFileSync('tmp/file2.js').toString(),
'new content js'
);
shell.ln('-sf', 'tmp/file1.txt', 'tmp/file2.txt');
assert(fs.existsSync('tmp/file2.txt'));
assert.equal(
fs.readFileSync('tmp/file1.txt').toString(),
fs.readFileSync('tmp/file2.txt').toString()
);
fs.writeFileSync('tmp/file1.txt', 'new content txt');
assert.equal(
fs.readFileSync('tmp/file2.txt').toString(),
'new content txt'
);
shell.exit(123);