UNPKG

3.12 kBJavaScriptView Raw
1/**
2 * Windows process functions.
3 *
4 * @module process
5 *
6 * @copyright
7 * Copyright (c) 2014-2016 by Appcelerator, Inc. All Rights Reserved.
8 *
9 * @license
10 * Licensed under the terms of the Apache Public License.
11 * Please see the LICENSE included with this distribution for details.
12 */
13
14const
15 appc = require('node-appc'),
16 async = require('async'),
17 fs = require('fs'),
18 magik = require('./utilities').magik,
19 path = require('path'),
20 __ = appc.i18n(__dirname).__,
21 LIST_RE = /^([^:]+):\s+(.*)$/;
22
23exports.list = list;
24exports.find = find;
25
26/**
27 * Returns a list of running processes.
28 *
29 * @param {Object} [options] - An object containing various settings.
30 * @param {String} [options.tasklist] - The path to the 'tasklist' executable.
31 * @param {Function} [callback(err, results)] - A function to call with the list of processes.
32 *
33 * @emits module:process#processes
34 * @emits module:process#error
35 *
36 * @returns {EventEmitter}
37 */
38function list(options, callback) {
39 return magik(options, callback, function (emitter, options, callback) {
40 appc.subprocess.run(options.tasklist || 'tasklist', ['/FO', 'LIST', '/V'], function (code, out, err) {
41 var processes = [];
42
43 if (code) {
44 var ex = new Error(__('Failed to run "%s"', 'tasklist'));
45 emitter.emit('error', ex);
46 return callback(ex);
47 }
48
49 out.trim().split(/\r\n\r\n|\n\n/).forEach(function (chunk) {
50 var obj = parseProcessOutput(chunk);
51 processes.push(obj);
52 });
53
54 emitter.emit('processes', processes);
55 callback(null, processes);
56 });
57 });
58};
59
60/**
61 * Returns a single running process's info. If it's not running, we return null.
62 *
63 * @param {String} pid - The process if od the process we're looking for.
64 * @param {Object} [options] - An object containing various settings.
65 * @param {String} [options.tasklist] - The path to the 'tasklist' executable.
66 * @param {Function} [callback(err, results)] - A function to call with the process.
67 *
68 * @emits module:process#process
69 * @emits module:process#error
70 *
71 * @returns {EventEmitter}
72 */
73function find(pid, options, callback) {
74 return magik(options, callback, function (emitter, options, callback) {
75 appc.subprocess.run(options.tasklist || 'tasklist', ['/FI', 'PID eq ' + pid, '/FO', 'LIST', '/V'], function (code, out, err) {
76 var processes = [];
77
78 if (code) {
79 var ex = new Error(__('Failed to run "%s"', 'tasklist'));
80 emitter.emit('error', ex);
81 return callback(ex);
82 }
83
84 if (out.indexOf('PID:') == -1) {
85 // not running
86 return callback();
87 }
88 var obj = parseProcessOutput(out.trim());
89 emitter.emit('process', obj);
90 callback(null, obj);
91 });
92 });
93};
94
95function parseProcessOutput(chunk) {
96 var obj = {};
97 chunk.split(/\r\n|\n/).forEach(function (line) {
98 var m = line.match(LIST_RE);
99 if (!m) return;
100
101 switch (m[1].toLowerCase()) {
102 case 'image name': obj.name = m[2]; break;
103 case 'pid': obj.pid = ~~m[2]; break;
104 case 'window title': obj.title = m[2]; break;
105 }
106 });
107 return obj;
108}