Created
August 30, 2015 13:30
-
-
Save mikezhuyuan/bcdcca1a135668b03e48 to your computer and use it in GitHub Desktop.
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
var fs = require('fs'); | |
var pathHelper = require('path'); | |
// var files = fs.readdirSync(pathHelper.join(__dirname, 'root')); | |
// files.forEach(function(a) { | |
// console.log(a); | |
// }); | |
// console.log(readConfig('root/a')); | |
// console.log(reorder(['a', 'b', 'c'], ['c', '*', 'a'])); | |
// console.log(reorder(['a', 'b', 'c'], ['c', '*'])); | |
// console.log(reorder(['a', 'b', 'c'], ['*', 'a'])); | |
// console.log(reorder(['a', 'b', 'c'], ['*'])); | |
walk(pathHelper.join(__dirname, 'root')); | |
function walk(path) { | |
var config = readConfig(path); | |
var files = fs.readdirSync(path); | |
if(!config.length) { | |
files = reorder(files, config); | |
} | |
files.forEach(function(a) { | |
var p = pathHelper.join(path, a); | |
if(fs.lstatSync(p).isDirectory()) { | |
walk(p); | |
} else if(/\.js/i.test(pathHelper.extname(a))) { | |
console.log(a); | |
} | |
}); | |
} | |
function reorder(files, orders) { | |
var middle = null; | |
orders.forEach(function(a, i) { | |
if (a === '*') { | |
if (middle != null) { | |
throw new Error('* can only be used once'); | |
} | |
middle = i; | |
return; | |
} | |
remove(files, a); | |
}); | |
if (middle == null) { | |
return orders; | |
} | |
return orders.slice(0, middle) | |
.concat(files) | |
.concat(orders.slice(middle + 1, orders.length)); | |
} | |
function remove(array, item) { | |
var index = array.indexOf(item); | |
array.splice(index, 1); | |
return array; | |
} | |
function readConfig(path) { | |
var config = pathHelper.join(path, '.includejs'); | |
if (!fs.existsSync(config)) { | |
return null; | |
} | |
var contents = fs.readFileSync(config, { | |
encoding: 'utf-8' | |
}); | |
var lines = contents.split('\n'); | |
return lines.filter(function(a) { | |
return /^\s*$/.test(a) === false; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment