Repository navigation
Expand file tree
/
Copy pathutil.js
More file actions
107 lines (94 loc) · 2.87 KB
/
Copy pathutil.js
File metadata and controls
107 lines (94 loc) · 2.87 KB
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
const {
exec
} = require('child_process');
// Mac 下无法使用 `process-list` 模块
var snapshot = null;
try {
const processList = require('process-list');
snapshot = processList.snapshot;
} catch (error) {
console.error('Cannot find module', error);
}
const simpleGit = require('simple-git/promise');
const ora = require('ora');
const consoleSeparator = '--------------------------';
/**
* 从当前进程中查找进程的路径
*
* @param {string} processName
* @return {Promise<string>} 进程的路径
*/
function findProcessPathFromCurrentProcessList(processName) {
var spinner = ora(`Find "${processName}" process from the current process list`).start();
if (snapshot) {
return snapshot('pid', 'name', 'path').then(tasks => {
var processPath;
var foundTask = tasks.find(function(task) {
return task.name.indexOf(processName) !== -1;
});
if (foundTask) {
spinner.succeed(`Find "${processName}" process: ${JSON.stringify(foundTask)}`);
processPath = foundTask.path;
} else {
spinner.fail(`Not find "${processName}" process`);
}
return processPath;
}, function(error) {
spinner.fail(`Find "${processName}" process fail: ${error}`);
});
} else {
spinner.fail(`Find "${processName}" process fail: Not find module process-list`);
return Promise.reject('Not find module process-list');
}
}
/**
* 执行命令
*
* @param {string} command
* @param {object} options
* @return {Promise}
*/
function executeCmd(command, options) {
console.log(consoleSeparator);
console.log(`> ${command}`);
console.log(consoleSeparator);
return new Promise(function(resolve, reject) {
var childProcess = exec(command, options, function(error, stout, stderr) {
if (error) {
reject(error);
} else {
resolve(stout);
}
});
childProcess.stdout.on('data', function(msg) {
console.log(msg);
});
childProcess.stderr.on('data', function(msg) {
console.error(msg);
});
});
}
/**
* 获取 GIT 项目最后提交的日志
*
* @param {string} gitProject
* @return {Promise}
*/
function getLastCommitLog(gitProject) {
var spinner = ora(`Get last commit log: ${gitProject}`).start();
var git = simpleGit(gitProject);
return git.log({
n: 1
}).then(function(listLogSummary) {
spinner.succeed(`Get last commit log: ${JSON.stringify(listLogSummary.latest)}`);
return listLogSummary.latest;
}, function(error) {
spinner.fail(`Get last commit log fail: ${error}`);
});
}
module.exports = {
consoleSeparator,
findProcessPathFromCurrentProcessList,
executeCmd,
getLastCommitLog
};