diff --git a/lib/index.js b/lib/index.js index ef8936b..05b16de 100644 --- a/lib/index.js +++ b/lib/index.js @@ -34,6 +34,9 @@ function runPollinate(input, callback) { function (state, next) { require('./move.js')(state, next); }, + function (state, next) { + require('./link.js')(state, next); + }, function (state, next) { require('./complete.js')(state, next); } diff --git a/lib/link.js b/lib/link.js new file mode 100644 index 0000000..ea66558 --- /dev/null +++ b/lib/link.js @@ -0,0 +1,56 @@ +/* __ __ _ _ _______ ____ _ _ _ _ _ ____ _____ ____ _ + | \/ | / \ | |/ / ____| / ___| | | | / \ | \ | |/ ___| ____/ ___|| | + | |\/| | / _ \ | ' /| _| | | | |_| | / _ \ | \| | | _| _| \___ \| | + | | | |/ ___ \| . \| |___ | |___| _ |/ ___ \| |\ | |_| | |___ ___) |_| + |_| |_/_/ \_\_|\_\_____| \____|_| |_/_/ \_\_| \_|\____|_____|____/(_) + There is a 100% chance that this project can use improvements. + Pull requests are ALWAYS welcome, even if just amounts to a conversation. */ + +'use strict'; + +var async = require('async'); +var fs = require('fs'); +var path = require('path'); +var cwd = process.cwd(); + +module.exports = function (state, callback) { + var link = state.data.link; + + if (link === undefined) { + callback(null, state); + return; + } + + process.chdir(state.template.tmp); + + async.parallel( + link.map( + function (item) { + var symlinkPath = item[Object.keys(item)[0]], + relativeTarget = path.relative(Object.keys(item)[0], symlinkPath); + + return function (done) { + fs.symlink( + relativeTarget, + symlinkPath, + function (err) { + if (err) { + done(err); + return; + } + done(null); + } + ); + }; + } + ), + function (err) { + process.chdir(cwd); + if (err) { + callback(err); + return; + } + callback(null, state); + } + ); +}; diff --git a/schema.json b/schema.json index 943e583..3bf51ee 100644 --- a/schema.json +++ b/schema.json @@ -12,5 +12,8 @@ "move":[ { "origin-file": "new-name" } ], + "link": [ + { "origin-path": "linked/path" } + ], "complete":"some shell command" } diff --git a/tests/mocks/template/template.json b/tests/mocks/template/template.json index 9f1621d..815e85d 100644 --- a/tests/mocks/template/template.json +++ b/tests/mocks/template/template.json @@ -15,5 +15,8 @@ { "project-name-dir": "{{ name }}" }, { "project-name-nested": "{{ name }}/{{ name }}/{{ name }}.txt" } ], + "link": [ + { "{{ name }}.txt": "{{ name }}/{{ name }}/{{ name }}-link.txt" } + ], "complete": "git init {{ name }}" }