Skip to content

Commit 4392605

Browse files
committed
Add symlink support (Fixes #42)
1 parent ef031dc commit 4392605

File tree

4 files changed

+429
-2
lines changed

4 files changed

+429
-2
lines changed

README.md

+14-2
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,24 @@ This is just [glob-watcher]
7272
- mode - Specify the mode the files should be created with. Default is the mode of the input file (file.stat.mode)
7373
- Returns a Readable/Writable stream.
7474
- On write the stream will save the [vinyl] File to disk at the folder/cwd specified.
75-
- After writing the file to disk, it will be emitted from the stream so you can keep piping these around
76-
- The file will be modified after being written to this stream
75+
- After writing the file to disk, it will be emitted from the stream so you can keep piping these around.
76+
- The file will be modified after being written to this stream:
7777
- `cwd`, `base`, and `path` will be overwritten to match the folder
7878
- `stat.mode` will be overwritten if you used a mode parameter
7979
- `contents` will have it's position reset to the beginning if it is a stream
8080

81+
### symlink(folder[, opt])
82+
83+
- Takes a folder path as the first argument.
84+
- First argument can also be a function that takes in a file and returns a folder path.
85+
- Possible options for the second argument:
86+
- cwd - Specify the working directory the folder is relative to. Default is `process.cwd()`
87+
- Returns a Readable/Writable stream.
88+
- On write the stream will create a symbolic link (i.e. symlink) on disk at the folder/cwd specified.
89+
- After creating the symbolic link, it will be emitted from the stream so you can keep piping these around.
90+
- The file will be modified after being written to this stream:
91+
- `cwd`, `base`, and `path` will be overwritten to match the folder
92+
8193
[glob-stream]: https://github.com/wearefractal/glob-stream
8294
[node-glob]: https://github.com/isaacs/node-glob
8395
[gaze]: https://github.com/shama/gaze

index.js

+1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
module.exports = {
44
src: require('./lib/src'),
55
dest: require('./lib/dest'),
6+
symlink: require('./lib/symlink'),
67
watch: require('glob-watcher')
78
};

lib/symlink/index.js

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
'use strict';
2+
3+
var assign = require('object-assign');
4+
var path = require('path');
5+
var through2 = require('through2');
6+
var mkdirp = require('mkdirp');
7+
var fs = require('graceful-fs');
8+
9+
function symlink(outFolder, opt) {
10+
opt = opt || {};
11+
if (typeof outFolder !== 'string' && typeof outFolder !== 'function') {
12+
throw new Error('Invalid output folder');
13+
}
14+
15+
var options = assign({
16+
cwd: process.cwd()
17+
}, opt);
18+
19+
var cwd = path.resolve(options.cwd);
20+
21+
function linkFile (file, enc, cb) {
22+
var basePath;
23+
if (typeof outFolder === 'string') {
24+
basePath = path.resolve(cwd, outFolder);
25+
}
26+
if (typeof outFolder === 'function') {
27+
basePath = path.resolve(cwd, outFolder(file));
28+
}
29+
var writePath = path.resolve(basePath, file.relative);
30+
var writeFolder = path.dirname(writePath);
31+
var srcPath = file.path;
32+
33+
// wire up new properties
34+
file.stat = file.stat ? file.stat : new fs.Stats();
35+
file.stat.mode = (options.mode || file.stat.mode);
36+
file.cwd = cwd;
37+
file.base = basePath;
38+
file.path = writePath;
39+
40+
// mkdirp the folder the file is going in
41+
mkdirp(writeFolder, function(err){
42+
if (err) {
43+
return cb(err);
44+
}
45+
fs.symlink(srcPath, writePath, function (err) {
46+
if (err && err.code !== 'EEXIST') {
47+
return cb(err);
48+
}
49+
50+
cb(null, file);
51+
});
52+
});
53+
}
54+
55+
var stream = through2.obj(linkFile);
56+
// TODO: option for either backpressure or lossy
57+
stream.resume();
58+
return stream;
59+
}
60+
61+
module.exports = symlink;

0 commit comments

Comments
 (0)