Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# master

* Short-circuit on non-Windows platforms unless `forceTest` option is passed
* Use directory instead of file to test symlinking

# 1.0.0

* Initial release
33 changes: 31 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,32 @@
# Can you create symlinks?
# node-can-symlink

Its easy to find out. Install using `npm install -g can-symlink`. Once installed run `can-symlink` to find out if you have what it takes to create symlinks.
This package tests whether you can create symlinks on this system.

## Command-line usage

```sh
npm install -g can-symlink

can-symlink
```

## Programmatic usage

```sh
npm install --save can-symlink
```

```js
let canSymlink = require("can-symlink")();
```

`canSymlink` will be either `true` or `false`. Note that we are calling the
module.

On non-Windows platforms, `require("can-symlink")()` automatically returns true.
To force testing for symlinkability on non-Windows platforms, pass the
`forceTest` option:

```js
let canSymlink = require("can-symlink")({ forceTest: true });
```
31 changes: 18 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
var tmp = require('tmp');
var tmp = require("tmp");

module.exports = function testCanSymlink (options) {
module.exports = function testCanSymlink(options) {
options = options || {};
var fs = options.fs || require('fs');

var canLinkSrc = tmp.tmpNameSync();
if (!options.forceTest && process.platform !== "win32") {
return true;
}

var fs = options.fs || require("fs");

var canLinkSrc = tmp.tmpNameSync();
var canLinkDest = tmp.tmpNameSync();

try {
fs.writeFileSync(canLinkSrc, '');
fs.mkdirSync(canLinkSrc);
} catch (e) {
return false
return false;
}

try {
fs.symlinkSync(canLinkSrc, canLinkDest)
fs.symlinkSync(canLinkSrc, canLinkDest, "dir");
} catch (e) {
fs.unlinkSync(canLinkSrc)
return false
return false;
} finally {
fs.rmdirSync(canLinkSrc);
}

fs.unlinkSync(canLinkSrc)
fs.unlinkSync(canLinkDest)
fs.unlinkSync(canLinkDest);

return true
}
return true;
};
50 changes: 26 additions & 24 deletions tests/tests.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,42 @@
var assert = require('assert');
var canSymlink = require('..');
var assert = require("assert");
var canSymlink = require("..");

describe('can-symlink', function() {
it('returns true if no exceptions are thrown', function() {
describe("can-symlink", function() {
it("returns true if no exceptions are thrown", function() {
var options = {
fs: {
symlinkSync: function() {
// noop
},
writeFileSync: function() {

},
unlinkSync: function() {

}
}
symlinkSync: function() {},
mkdirSync: function() {},
rmdirSync: function() {},
unlinkSync: function() {}
},
forceTest: true
};

assert.equal(canSymlink(options), true);
});

it('returns false if exceptions are thrown', function() {
it("returns false if exceptions are thrown", function() {
var options = {
fs: {
symlinkSync: function() {
throw Error('Symlink failed');
throw Error("Symlink failed");
},
writeFileSync: function() {

},
unlinkSync: function() {

}
}
mkdirSync: function() {},
rmdirSync: function() {},
unlinkSync: function() {}
},
forceTest: true
};

assert.equal(canSymlink(options), false);
});
});

it("works on the real file-system", function() {
let result = canSymlink({ forceTest: true });
// On win32 we only care that we do not get an exception
if (process.platform !== "win32") {
assert.equal(result, true);
}
});
});