Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
259 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,4 +55,4 @@ npm-debug.* | |
|
||
# Project specific # | ||
#################### | ||
|
||
test/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,54 @@ | ||
/*jshint es5:true*/ | ||
|
||
'use strict'; | ||
|
||
var fs = require('fs'), | ||
glob = require('glob'), | ||
async = require('async'), | ||
utils = require('mout'), | ||
path = require('path'), | ||
interp = require('../lib/string/interpolate') | ||
var fs = require('fs'), | ||
glob = require('glob'), | ||
async = require('async'), | ||
path = require('path') | ||
; | ||
|
||
var task = { | ||
id : 'scaffolding-file-rename', | ||
author : 'Indigo United', | ||
name : 'Scaffolding: file rename.', | ||
description : 'Replaces {{placeholders}} in a set of files.', | ||
options: { | ||
files: { | ||
description: 'From which dir to start looking for files with placeholders. Accepts a dir and array of dirs. Also note that the dirs can be minimatch patterns.' | ||
}, | ||
data: { | ||
description: 'The data to be used while renaming. Keys are placeholder names and the values are the content for each placeholder.' | ||
}, | ||
glob: { | ||
description: 'The options to pass to glob (check https://npmjs.org/package/glob for details).', | ||
default: null | ||
} | ||
}, | ||
tasks: | ||
[ | ||
{ | ||
task: function (opt, ctx, next) { | ||
var files = utils.lang.isArray(opt.files) ? opt.files : [opt.files]; | ||
|
||
// Do this in series, because it can give problems if the directories intersect eachother | ||
async.forEachSeries(files, function (file, next) { | ||
glob(file, opt.glob, function (err, matches) { | ||
if (err) { | ||
return next(err); | ||
} | ||
|
||
// Grab the list of files to rename | ||
// Note that matches must be traversed backwards | ||
var x; | ||
var filesToRename = []; | ||
var before; | ||
var after; | ||
|
||
for (x = matches.length - 1; x >= 0; --x) { | ||
before = path.basename(matches[x]); | ||
after = interp(before, opt.data); | ||
|
||
if (before !== after) { | ||
filesToRename.push({ before: matches[x], after: path.dirname(matches[x]) + '/' + after }); | ||
} | ||
} | ||
|
||
// Foreach file found, rename it (has to be in series) | ||
async.forEachSeries(filesToRename, function (obj, next) { | ||
ctx.log.debugln('Renaming from ' + obj.before + ' to ' + obj.after); | ||
fs.rename(obj.before, obj.after, next); | ||
}, next); | ||
}); | ||
module.exports = function (task) { | ||
task | ||
.id('scaffolding-file-rename') | ||
.name('Scaffolding: file rename.') | ||
.description('Replaces {{placeholders}} in a set of file names.') | ||
.author('Indigo United') | ||
|
||
.option('files', 'From which dir to start looking for files with placeholders. Accepts a dir and array of dirs. Also note that the dirs can be minimatch patterns.') | ||
.option('data', 'The data to be used while renaming. Keys are placeholder names and the values are the content for each placeholder.') | ||
.option('glob', 'The options to pass to glob (check https://npmjs.org/package/glob for details).', null) | ||
|
||
.do(function (opt, ctx, next) { | ||
var files = Array.isArray(opt.files) ? opt.files : [opt.files]; | ||
|
||
// Do this in series, because it can give problems if the directories intersect eachother | ||
async.forEachSeries(files, function (file, next) { | ||
glob(file, opt.glob, function (err, matches) { | ||
if (err) { | ||
return next(err); | ||
} | ||
|
||
// Grab the list of files to rename | ||
// Note that matches must be traversed backwards | ||
var x; | ||
var filesToRename = []; | ||
var before; | ||
var after; | ||
|
||
for (x = matches.length - 1; x >= 0; --x) { | ||
before = path.basename(matches[x]); | ||
after = ctx.string.interpolate(before, opt.data); | ||
|
||
if (before !== after) { | ||
filesToRename.push({ before: matches[x], after: path.dirname(matches[x]) + '/' + after }); | ||
} | ||
} | ||
|
||
// Foreach file found, rename it (has to be in series) | ||
async.forEachSeries(filesToRename, function (obj, next) { | ||
ctx.log.debugln('Renaming from ' + obj.before + ' to ' + obj.after); | ||
fs.rename(obj.before, obj.after, next); | ||
}, next); | ||
} | ||
} | ||
] | ||
}; | ||
|
||
module.exports = task; | ||
}); | ||
}, next); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
'use strict'; | ||
|
||
var fs = require('fs'), | ||
expect = require('expect.js'), | ||
rimraf = require('rimraf'), | ||
fstream = require('fstream'), | ||
isFile = require('./util/is-file'), | ||
isDir = require('./util/is-dir'), | ||
rename = require('../autofile'), | ||
automaton = require('automaton').create() | ||
; | ||
|
||
describe('scaffolding-close', function () { | ||
function clean(done) { | ||
rimraf(__dirname + '/tmp', done); | ||
} | ||
|
||
beforeEach(function (done) { | ||
clean(function (err) { | ||
if (err) { | ||
throw err; | ||
} | ||
|
||
fs.mkdirSync(__dirname + '/tmp'); | ||
fs.mkdirSync(__dirname + '/tmp/file-rename'); | ||
|
||
// Create some assets in tmp/file-rename | ||
fs.writeFileSync(__dirname + '/tmp/file-rename/file1_{{placeholder1}}_{{placeholder2}}.json', ''); | ||
fs.mkdirSync(__dirname + '/tmp/file-rename/dummy{{empty}}'); | ||
fs.writeFileSync(__dirname + '/tmp/file-rename/dummy{{empty}}/file1_{{placeholder1}}_{{placeholder2}}.json', ''); | ||
|
||
|
||
done(); | ||
}); | ||
}); | ||
after(clean); | ||
|
||
it('should replace filename placeholders with string', function (done) { | ||
// Copy file-rename to file-rename-copy to test multiple dirs | ||
var reader = fstream.Reader(__dirname + '/tmp/file-rename').pipe( | ||
fstream.Writer({ | ||
type: 'Directory', | ||
path: __dirname + '/tmp/file-rename-copy' | ||
}) | ||
); | ||
|
||
reader.on('error', function (err) { | ||
throw err; | ||
}); | ||
|
||
reader.on('end', function () { | ||
automaton.run({ | ||
setup: function (opts, ctx, next) { | ||
opts.__dirname = __dirname; | ||
next(); | ||
}, | ||
tasks: [ | ||
{ | ||
task: rename, | ||
options: { | ||
files: ['{{__dirname}}/tmp/file-rename/**/*', '{{__dirname}}/tmp/file-rename-copy/**/*'], | ||
data: { | ||
placeholder1: 'foo', | ||
placeholder2: 'bar', | ||
empty: '' | ||
} | ||
} | ||
} | ||
] | ||
}, null, function (err) { | ||
if (err) { | ||
throw err; | ||
} | ||
|
||
expect(isFile(__dirname + '/tmp/file-rename/file1_foo_bar.json')).to.equal(true); | ||
expect(isDir(__dirname + '/tmp/file-rename/dummy')).to.equal(true); | ||
expect(isFile(__dirname + '/tmp/file-rename/dummy/file1_foo_bar.json')).to.equal(true); | ||
expect(isFile(__dirname + '/tmp/file-rename-copy/file1_foo_bar.json')).to.equal(true); | ||
expect(isFile(__dirname + '/tmp/file-rename-copy/dummy/file1_foo_bar.json')).to.equal(true); | ||
|
||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
it('should accept minimatch patterns', function (done) { | ||
automaton.run({ | ||
setup: function (opts, ctx, next) { | ||
opts.__dirname = __dirname; | ||
next(); | ||
}, | ||
tasks: [ | ||
{ | ||
task: rename, | ||
options: { | ||
files: ['{{__dirname}}/tmp/file*rename/**/*'], | ||
data: { | ||
placeholder1: 'foo', | ||
placeholder2: 'bar', | ||
empty: '' | ||
} | ||
} | ||
} | ||
] | ||
}, null, function (err) { | ||
if (err) { | ||
throw err; | ||
} | ||
|
||
expect(isFile(__dirname + '/tmp/file-rename/file1_foo_bar.json')).to.equal(true); | ||
expect(isDir(__dirname + '/tmp/file-rename/dummy')).to.equal(true); | ||
expect(isFile(__dirname + '/tmp/file-rename/dummy/file1_foo_bar.json')).to.equal(true); | ||
|
||
done(); | ||
}); | ||
}); | ||
|
||
it('should pass over the glob options', function (done) { | ||
// Rename to .file-rename and tell glob to match files starting with dot | ||
fs.renameSync(__dirname + '/tmp/file-rename', __dirname + '/tmp/.file-rename'); | ||
|
||
automaton.run({ | ||
setup: function (opts, ctx, next) { | ||
opts.__dirname = __dirname; | ||
next(); | ||
}, | ||
tasks: [ | ||
{ | ||
task: rename, | ||
options: { | ||
files: ['{{__dirname}}/tmp/*file-rename/**/*'], | ||
data: { | ||
placeholder1: 'foo', | ||
placeholder2: 'bar', | ||
empty: '' | ||
}, | ||
glob: { | ||
dot: true | ||
} | ||
} | ||
} | ||
] | ||
}, null, function (err) { | ||
if (err) { | ||
throw err; | ||
} | ||
|
||
expect(isFile(__dirname + '/tmp/.file-rename/file1_{{placeholder1}}_{{placeholder2}}.json')).to.equal(false); | ||
expect(isDir(__dirname + '/tmp/.file-rename/dummy')).to.equal(true); | ||
expect(isFile(__dirname + '/tmp/.file-rename/dummy/file1_{{placeholder1}}_{{placeholder2}}.json')).to.equal(false); | ||
|
||
done(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
'use strict'; | ||
|
||
var fs = require('fs'); | ||
|
||
module.exports = function (file) { | ||
var stat; | ||
|
||
try { | ||
stat = fs.statSync(file); | ||
} catch (e) { | ||
return false; | ||
} | ||
|
||
return stat.isDirectory(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
'use strict'; | ||
|
||
var fs = require('fs'); | ||
|
||
module.exports = function (file) { | ||
var stat; | ||
|
||
try { | ||
stat = fs.statSync(file); | ||
} catch (e) { | ||
return false; | ||
} | ||
|
||
return stat.isFile(); | ||
}; |