Woodland_Furniture

Форк
0
1
'use strict';
2
const path = require('path');
3
const childProcess = require('child_process');
4
const isWsl = require('is-wsl');
5

6
module.exports = (target, opts) => {
7
	if (typeof target !== 'string') {
8
		return Promise.reject(new Error('Expected a `target`'));
9
	}
10

11
	opts = Object.assign({wait: true}, opts);
12

13
	let cmd;
14
	let appArgs = [];
15
	let args = [];
16
	const cpOpts = {};
17

18
	if (Array.isArray(opts.app)) {
19
		appArgs = opts.app.slice(1);
20
		opts.app = opts.app[0];
21
	}
22

23
	if (process.platform === 'darwin') {
24
		cmd = 'open';
25

26
		if (opts.wait) {
27
			args.push('-W');
28
		}
29

30
		if (opts.app) {
31
			args.push('-a', opts.app);
32
		}
33
	} else if (process.platform === 'win32' || isWsl) {
34
		cmd = 'cmd' + (isWsl ? '.exe' : '');
35
		args.push('/c', 'start', '""', '/b');
36
		target = target.replace(/&/g, '^&');
37

38
		if (opts.wait) {
39
			args.push('/wait');
40
		}
41

42
		if (opts.app) {
43
			args.push(opts.app);
44
		}
45

46
		if (appArgs.length > 0) {
47
			args = args.concat(appArgs);
48
		}
49
	} else {
50
		if (opts.app) {
51
			cmd = opts.app;
52
		} else {
53
			cmd = process.platform === 'android' ? 'xdg-open' : path.join(__dirname, 'xdg-open');
54
		}
55

56
		if (appArgs.length > 0) {
57
			args = args.concat(appArgs);
58
		}
59

60
		if (!opts.wait) {
61
			// `xdg-open` will block the process unless
62
			// stdio is ignored and it's detached from the parent
63
			// even if it's unref'd
64
			cpOpts.stdio = 'ignore';
65
			cpOpts.detached = true;
66
		}
67
	}
68

69
	args.push(target);
70

71
	if (process.platform === 'darwin' && appArgs.length > 0) {
72
		args.push('--args');
73
		args = args.concat(appArgs);
74
	}
75

76
	const cp = childProcess.spawn(cmd, args, cpOpts);
77

78
	if (opts.wait) {
79
		return new Promise((resolve, reject) => {
80
			cp.once('error', reject);
81

82
			cp.once('close', code => {
83
				if (code > 0) {
84
					reject(new Error('Exited with code ' + code));
85
					return;
86
				}
87

88
				resolve(cp);
89
			});
90
		});
91
	}
92

93
	cp.unref();
94

95
	return Promise.resolve(cp);
96
};
97

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.