forked from TryGhost/Ghost-CLI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.js
103 lines (83 loc) · 3.15 KB
/
run.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
'use strict';
const spawn = require('child_process').spawn;
const merge = require('lodash/merge');
const Command = require('../command');
class RunCommand extends Command {
run() {
const path = require('path');
const shouldUseGhostUser = require('../utils/use-ghost-user');
// If the user is running this command directly, output a little note
// telling them they're likely looking for `ghost start`
if (process.stdin.isTTY) {
this.ui.log('The `ghost run` command is used by the configured Ghost process manager and for debugging. ' +
'If you\'re not running this to debug something, you should run `ghost start` instead.', 'yellow');
}
const instance = this.system.getInstance();
if (!instance.config.has('paths.contentPath')) {
instance.config.set('paths.contentPath', path.join(instance.dir, 'content')).save();
}
if (shouldUseGhostUser(path.join(instance.dir, 'content'))) {
return this.useGhostUser(instance);
}
return this.useDirect(instance);
}
useGhostUser(instance) {
const errors = require('../errors');
this.ui.log('Running sudo command: node current/index.js', 'gray');
this.child = spawn('sudo', `-E -u ghost ${process.execPath} current/index.js`.split(' '), {
cwd: instance.dir,
stdio: 'inherit'
});
this.child.on('error', (error) => {
if (error.code !== 'EPERM') {
this.ui.fail(error);
process.exit(1);
}
});
this.child.on('message', (message) => {
// we still want to catch errors from Ghost here,
// but we don't need the additional instance.process functionality
if (message.error) {
throw new errors.GhostError(message.error);
}
});
this.sudo = true;
}
useDirect(instance, options) {
options = merge({delayErrorChaining: true}, options || {});
this.child = spawn(process.execPath, ['current/index.js'], {
cwd: instance.dir,
stdio: [0, 1, 2, 'ipc']
});
this.child.on('error', (error) => {
this.ui.fail(error);
process.exit(1);
});
this.child.on('message', (message) => {
if (message.started) {
instance.process.success();
return;
}
if (!options.delayErrorChaining) {
instance.process.error(message.error);
} else {
// NOTE: Backwards compatibility of https://github.com/TryGhost/Ghost/pull/9440
setTimeout(() => {instance.process.error(message.error);}, 1000);
}
});
}
cleanup() {
if (!this.child) {
return;
}
try {
this.child.kill();
} catch (e) {
if (!this.sudo || e.code !== 'EPERM') {
throw e;
}
}
}
}
RunCommand.description = 'Run a Ghost instance directly (used by process managers and for debugging)';
module.exports = RunCommand;