-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathladon.js
executable file
·166 lines (139 loc) · 5.14 KB
/
ladon.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env node
var argv, async, cp, glob, _help, mkdirp, os, parser, path, quote, render, run;
async = require('async');
cp = require('child_process');
glob = require('glob');
mkdirp = require('mkdirp');
os = require('os');
path = require('path');
// Surround a string with quotes
quote = exports.quote = function (str) {
return '"' + str + '"';
};
// Render a path template with the given filename
render = exports.render = function (filename, relStart, doQuote, template) {
var ext = filename.split('.').pop();
var q = doQuote ? quote : function (s) { return s; };
return template
.replace(new RegExp('FULLPATH', 'g'), q(filename))
.replace(new RegExp('DIRNAME', 'g'), q(path.dirname(filename)))
.replace(new RegExp('BASENAME', 'g'), q(path.basename(filename, '.' + ext)))
.replace(new RegExp('EXT', 'g'), q(ext))
.replace(new RegExp('RELDIR', 'g'), q(path.dirname(filename).substr(relStart)))
.replace(new RegExp('RELPATH', 'g'), q(filename.substr(relStart)));
};
// Setup argument parser and options
parser = exports.parser = require('yargs')
.usage('$0 ' + require('./package.json').version +
' via nodejs-' + process.versions.node + '\n' +
'Usage: $0 [options] glob -- command [args]')
.example('$0 "**/*.txt" -- echo RELPATH', 'List all text files')
.example('$0 "**/*.txt" -- cat FULLPATH >combined.txt', 'Combine all text files')
.example('', '')
.example('https://github.com/danielgtaylor/ladon#readme', 'More examples')
.options('f', {
alias: 'fail',
describe: 'Fail on first error',
boolean: true
})
.options('m', {
alias: 'makedirs',
describe: 'Make directories (supports variables)',
string: true
})
.options('p', {
alias: 'processes',
describe: 'Maximum number of processes',
'default': os.cpus().length
})
.options('v', {
alias: 'verbose',
describe: 'Verbose output to sdterr',
boolean: true,
'default': false
});
// Print extra help information
_help = parser.help;
parser.help = function () {
var helpStr = _help();
var variables = [
['FULLPATH', 'Full path, equivalent to DIRNAME' + path.sep + 'BASENAME.EXT'],
['DIRNAME', 'Directory name'],
['BASENAME', 'File name without extension'],
['EXT', 'File name extension'],
['RELDIR', 'Relative directory name'],
['RELPATH', 'Relative file path']
];
helpStr += '\n\nVariables:\n';
helpStr += variables.map(function (x) {
var str = ' ' + x[0];
for (var i = x[0].length; i < 10; i++) {
str += ' ';
}
return str + x[1];
}).join('\n');
return helpStr;
};
run = exports.run = function (argv, done) {
if (argv instanceof Function) {
done = argv;
argv = undefined;
}
if (argv === undefined) argv = parser.argv;
if (done === undefined) done = function () {};
if (argv._.length < 2) {
parser.showHelp();
return done(new Error('Must pass at least a glob and command!'));
}
// Handle basic home directory expansion
if (argv._[0][0] == '~') {
argv._[0] = (process.env.HOME || process.env.USERPROFILE) + argv._[0].substr(1);
}
// Resolve full path to glob
argv._[0] = path.resolve(argv._[0]);
// Get relative start position for paths for RELDIR and RELNAME
var relativeStart = argv._[0].indexOf('**');
if (relativeStart == -1) {
relativeStart = process.cwd().length + 1;
}
// Find and process files
new glob.Glob(argv._[0], {
nocase: true
}, function(err, filenames) {
var _process;
if (err) return done(err);
if (argv.verbose)
console.error('Processing ' + filenames.length + ' files...');
_process = function(filename, processDone) {
var cmd = render(filename, relativeStart, true, argv._.slice(1).join(' '));
if (argv.makedirs) {
// Ensure directories exist before running commands!
mkdirp.sync(render(filename, relativeStart, false, argv.makedirs));
}
if (argv.verbose)
console.error("Processing " + filename + '\n' + cmd);
// Run the command and dump the output
cp.exec(cmd, function(err, stdout, stderr) {
if (err) {
if (argv.fail) {
return processDone(err);
} else {
console.error(err);
}
}
if (stdout) process.stdout.write(stdout);
if (stderr) process.stderr.write(stderr);
processDone();
});
};
async.eachLimit(filenames, argv.processes, _process, function(err) {
if (err) return done(err);
done();
});
});
};
if (require.main === module) {
run(function (err) {
if (err) console.error(err.toString());
});
}