Skip to content

Commit

Permalink
Add symlink support (Fixes #42)
Browse files Browse the repository at this point in the history
  • Loading branch information
JonAbrams committed Dec 26, 2014
1 parent ef031dc commit 4392605
Show file tree
Hide file tree
Showing 4 changed files with 429 additions and 2 deletions.
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,24 @@ This is just [glob-watcher]
- mode - Specify the mode the files should be created with. Default is the mode of the input file (file.stat.mode)
- Returns a Readable/Writable stream.
- On write the stream will save the [vinyl] File to disk at the folder/cwd specified.
- After writing the file to disk, it will be emitted from the stream so you can keep piping these around
- The file will be modified after being written to this stream
- After writing the file to disk, it will be emitted from the stream so you can keep piping these around.
- The file will be modified after being written to this stream:
- `cwd`, `base`, and `path` will be overwritten to match the folder
- `stat.mode` will be overwritten if you used a mode parameter
- `contents` will have it's position reset to the beginning if it is a stream

### symlink(folder[, opt])

- Takes a folder path as the first argument.
- First argument can also be a function that takes in a file and returns a folder path.
- Possible options for the second argument:
- cwd - Specify the working directory the folder is relative to. Default is `process.cwd()`
- Returns a Readable/Writable stream.
- On write the stream will create a symbolic link (i.e. symlink) on disk at the folder/cwd specified.
- After creating the symbolic link, it will be emitted from the stream so you can keep piping these around.
- The file will be modified after being written to this stream:
- `cwd`, `base`, and `path` will be overwritten to match the folder

This comment has been minimized.

Copy link
@OZZlE

OZZlE Dec 15, 2016

so line 86 is essentially useless? I have a source js file in ./vendor/[module]/xx/yy/stuff/script.js that I want to place a symlink to inside ./pub/static/[completly_different_folder_structure]/script.js, I've managed to do this with the gulp-rename .. but now I want to place a symlink to the source file instead of copying it so I added vinyl-fs and changed:

-- .pipe(gulp.dest('./pub/static/'))
++ .pipe(vfs.symlink('./pub/static/'))

but it seems this replaces the path.dirname so it becomes incorrect..


[glob-stream]: https://github.com/wearefractal/glob-stream
[node-glob]: https://github.com/isaacs/node-glob
[gaze]: https://github.com/shama/gaze
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
module.exports = {
src: require('./lib/src'),
dest: require('./lib/dest'),
symlink: require('./lib/symlink'),
watch: require('glob-watcher')
};
61 changes: 61 additions & 0 deletions lib/symlink/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
'use strict';

var assign = require('object-assign');
var path = require('path');
var through2 = require('through2');
var mkdirp = require('mkdirp');
var fs = require('graceful-fs');

function symlink(outFolder, opt) {
opt = opt || {};
if (typeof outFolder !== 'string' && typeof outFolder !== 'function') {
throw new Error('Invalid output folder');
}

var options = assign({
cwd: process.cwd()
}, opt);

var cwd = path.resolve(options.cwd);

function linkFile (file, enc, cb) {
var basePath;
if (typeof outFolder === 'string') {
basePath = path.resolve(cwd, outFolder);
}
if (typeof outFolder === 'function') {
basePath = path.resolve(cwd, outFolder(file));
}
var writePath = path.resolve(basePath, file.relative);
var writeFolder = path.dirname(writePath);
var srcPath = file.path;

// wire up new properties
file.stat = file.stat ? file.stat : new fs.Stats();
file.stat.mode = (options.mode || file.stat.mode);
file.cwd = cwd;
file.base = basePath;
file.path = writePath;

// mkdirp the folder the file is going in
mkdirp(writeFolder, function(err){
if (err) {
return cb(err);
}
fs.symlink(srcPath, writePath, function (err) {
if (err && err.code !== 'EEXIST') {
return cb(err);
}

cb(null, file);
});
});
}

var stream = through2.obj(linkFile);
// TODO: option for either backpressure or lossy
stream.resume();
return stream;
}

module.exports = symlink;
Loading

0 comments on commit 4392605

Please sign in to comment.