From b715c2c240e529c1efcfa0bee9d1bc92e90fd4ff Mon Sep 17 00:00:00 2001 From: Michael Schoonmaker Date: Thu, 10 Oct 2013 18:03:09 -0700 Subject: [PATCH] Add ln support, including both -s and -f options. --- README.md | 17 +++++++++++ shell.js | 4 +++ src/ln.js | 52 +++++++++++++++++++++++++++++++++ test/ln.js | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 158 insertions(+) create mode 100644 src/ln.js create mode 100644 test/ln.js diff --git a/README.md b/README.md index 9120623..efed358 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/shell.js b/shell.js index e56c5de..54402c7 100644 --- a/shell.js +++ b/shell.js @@ -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. diff --git a/src/ln.js b/src/ln.js new file mode 100644 index 0000000..70e1411 --- /dev/null +++ b/src/ln.js @@ -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 and/or '); + } + + 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; diff --git a/test/ln.js b/test/ln.js new file mode 100644 index 0000000..f7f39c9 --- /dev/null +++ b/test/ln.js @@ -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);