-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec.js
133 lines (124 loc) · 4.43 KB
/
exec.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
/**
* The module for execute command for jsDoc
*/
var util = require("util");
var path = require("path");
var child_process = require('child_process');
var spawn = child_process.spawn;
var isWin = process.platform === 'win32';
var DEFAULT_JSDOC_PATH = "node_modules/fis-command-jsdoc/node_modules/jsdoc/" + (isWin ? "jsdoc.cmd" : "jsdoc");
var execModule = {
getNpmGlobalPath: function(callback){
var getPrefix = spawn((isWin ? 'npm.cmd' : 'npm'), ['config','get','prefix']);
getPrefix.stdout.on('data', function(data) {
var spath = data.toString().trim();
var npmPath = isWin ? spath : path.join(spath, "lib");
callback(npmPath);
});
getPrefix.stderr.on('data', function(error){
process.stderr.write(error);
});
getPrefix.on('error', function(err) {
console.log("Can not find npm prefix");
process.stderr.write(err);
});
getPrefix.on('close', function(code) {
if (code !== 0){
process.exit(code);
}
});
},
/**
* Return the npm Global installs path
* You can see [here]{@link https://www.npmjs.org/doc/files/npm-folders.html} for more information
*/
getNpmGlobalScript: function(npmPath, callback){
var script = (isWin ? "jsdoc.cmd" : "jsdoc"),
jsDocVersion = spawn(script, ['-v']);
jsDocVersion.on('error', function(){
if (npmPath){
script = path.join(npmPath, DEFAULT_JSDOC_PATH);
callback(script);
}else{
execModule.getNpmGlobalPath(function(npmPath){
if (npmPath){
script = path.join(npmPath, DEFAULT_JSDOC_PATH);
callback(script);
}else{
callback(null);
}
});
}
});
jsDocVersion.stderr.on('data', function(error){
process.stderr.write(error);
});
jsDocVersion.on('close', function(code){
if (code === 0){
//Jsdoc exist
callback(script);
}else if (npmPath){
script = path.join(npmPath, DEFAULT_JSDOC_PATH);
callback(script);
}else{
execModule.getNpmGlobalPath(function(npmPath){
if (npmPath){
script = path.join(npmPath, DEFAULT_JSDOC_PATH);
callback(script);
}else{
callback(null);
}
});
}
});
},
/**
* Execute the jsdoc command
*/
spawn : function(sources, npmPath, options, callback){
var args = [];
//var script = isWin ? "cmd /c " : "";
var script = "";
execModule.getNpmGlobalScript(npmPath, function(path){
script += path;
fis.log.debug("JsDoc command : " + script);
fis.util.map(options, function(key, value) {
if (value){
fis.log.debug('Reading options: ' + key);
fis.log.debug('>>>' + value);
args.push("--" + key);
if (fis.util.is(value, 'String')){
args.push(value);
}
}
});
if (!util.isArray(sources)){
sources = [sources];
}
args.push.apply(args, sources);
if (isWin){
// Windows: quote paths that have spaces
args = args.map(function(item){
if (item.indexOf(' ')>=0) {
return '"' + item + '"';
} else {
return item.replace(/\//g, "\\");
}
});
} else {
// Unix: escape spaces in paths
args = args.map(function(item){
return item.replace(' ', '\\ ');
});
}
fis.log.debug("Running : "+ script + " " + args.join(' '));
var execp = spawn(script, args, {
windowsVerbatimArguments: isWin // documentation PR is pending: https://github.com/joyent/node/pull/4259
});
if (callback) {
callback(execp);
}
});
}
};
module.exports = execModule;