Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add symlink support (Fixes #42) #49

Merged
merged 5 commits into from
Dec 28, 2014
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you read in some files, symlink, then symlink again. you have the original files, a pointer to the original files, and a pointer to the pointer to the original files. does that sound right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so. Basically a vinyl file object is read in from the stream, a symlink is created pointing to it on disk, then the vinyl object is altered to point to the symlink and it's written back to the stream.

I thought this behaviour would be the most similar to dest's behaviour.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JonAbrams Yeah, this is how dest works just curious if this seems right for symlinks which is a lot differnet.

@sindresorhus @phated thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had actually first had it return the original files, untouched, after the symlinks were created, but considering the two possibilities, either return the original file or the new symlink, the latter seemed to behave more like dest (since dest returns the newly written file) which would be "least surprising" for the user.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JonAbrams Fair enough, I think we can keep this behavior


[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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

most of the logic in this function is duplicated between dest and symlink. It would be cool if we could split this out to clean them both up

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. I'll do so.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JonAbrams There is another PR open to add support for a mode param to .dest that splitting the logic out will conflict with heavily FYI

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So should we wait until both PRs are merged? If I consolidate the logic into a common file, it will indeed conflict with the other PR.

I have no problem with waiting until after the PRs are accepted.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JonAbrams I'll merge this one and then reimplement the other one on top of these changes, there is some more work to be done on that one anyways

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Let me know if you need any other changes/additions.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JonAbrams Just the logic consolidation and then I think we are ready to merge and publish 👍

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