-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathharper.js
More file actions
executable file
·158 lines (148 loc) · 6.09 KB
/
Copy pathharper.js
File metadata and controls
executable file
·158 lines (148 loc) · 6.09 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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env node
'use strict';
const fs = require('node:fs');
const path = require('node:path');
const logger = require('../utility/logging/harper_logger.js');
const cliOperations = require('./cliOperations.js');
const { packageJson } = require('../utility/packageUtils.js');
const checkNode = require('../launchServiceScripts/utility/checkNodeVersion.js');
const hdbTerms = require('../utility/hdbTerms.ts');
const { SERVICE_ACTIONS_ENUM } = hdbTerms;
if (typeof process.setSourceMapsEnabled === 'function') {
process.setSourceMapsEnabled(true); // this is necessary for source maps to work, at least on the main thread.
}
const HELP = `
Usage: harperdb [command]
With no command, harper will simply run Harper (in the foreground)
By default, the CLI also supports certain Operation APIs. Specify the operation name and any required parameters, and omit the 'operation' command.
Commands:
copy-db <source> <target> - Copies a database from source path to target path
dev <path> - Run the application in dev mode with debugging, foreground logging, no auth
install - Install harperdb
<api-operation> <param>=<value> - Run an API operation and return result to the CLI, not all operations are supported
register - Register harperdb
renew-certs - Generate a new set of self-signed certificates
restart - Restart the harperdb background process
run <path> - Run the application in the specified path
start - Starts a separate background process for harperdb and CLI will exit
status - Print the status of Harper
stop - Stop the harperdb background process
help - Display this output
upgrade - Upgrade harperdb
version - Print the version
deploy - Deploy the application locally or remotely with target=<remote url>
`;
async function harper() {
let nodeResults = checkNode();
if (nodeResults) {
if (nodeResults.error) {
console.error(nodeResults.error);
logger.error(nodeResults.error);
return;
} else if (nodeResults.warn) {
console.warn(nodeResults.warn);
logger.warn(nodeResults.warn);
}
}
let service;
if (process.argv && process.argv[2] && !process.argv[2].startsWith('-')) {
service = process.argv[2].toLowerCase();
}
switch (service) {
case SERVICE_ACTIONS_ENUM.HELP:
return HELP;
case SERVICE_ACTIONS_ENUM.START:
return require('./run.js').launch();
case SERVICE_ACTIONS_ENUM.INSTALL:
return require('./install.js')();
case SERVICE_ACTIONS_ENUM.STOP:
return require('./stop.js')().then(() => {
process.exit(0);
});
case SERVICE_ACTIONS_ENUM.RESTART:
return require('./restart.js').restart({});
case SERVICE_ACTIONS_ENUM.VERSION:
return packageJson.version;
case SERVICE_ACTIONS_ENUM.UPGRADE:
logger.setLogLevel(hdbTerms.LOG_LEVELS.INFO);
// The require is here to better control the flow of imports when this module is called.
return require('./upgrade.js')
.upgrade(null)
.then(() => 'Your instance of Harper is up to date!');
case SERVICE_ACTIONS_ENUM.STATUS:
return require('./status.js')();
case SERVICE_ACTIONS_ENUM.RENEWCERTS:
return require('../security/keys.js')
.renewSelfSigned()
.then(() => 'Successfully renewed self-signed certificates');
case SERVICE_ACTIONS_ENUM.COPYDB: {
let sourceDb = process.argv[3];
let targetDbPath = process.argv[4];
return require('./copyDb.ts').copyDb(sourceDb, targetDbPath);
}
case SERVICE_ACTIONS_ENUM.DEV:
process.env.DEV_MODE = true;
// fall through
case SERVICE_ACTIONS_ENUM.RUN: {
// Run a specific application folder
let appFolder = process.argv[3];
if (appFolder && appFolder[0] !== '-') {
if (!fs.existsSync(appFolder)) {
throw new Error(`The folder ${appFolder} does not exist`);
}
if (!fs.statSync(appFolder).isDirectory()) {
throw new Error(`The path ${appFolder} is not a folder`);
}
appFolder = fs.realpathSync(appFolder);
if (
fs.existsSync(path.join(appFolder, hdbTerms.HARPER_CONFIG_FILE)) ||
(fs.existsSync(path.join(appFolder, hdbTerms.HDB_CONFIG_FILE)) &&
fs.existsSync(path.join(appFolder, 'database')))
) {
// This can be used to run HDB without a boot file
process.env.ROOTPATH = appFolder;
} else {
process.env.RUN_HDB_APP = appFolder;
}
} else if (fs.existsSync(hdbTerms.HDB_COMPONENT_CONFIG_FILE) || fs.existsSync('schema.graphql')) {
console.warn(
`It appears you are running Harper in an application directory, but did not specify the path. I'll go ahead and run the application for you since that's probably what you meant. But to avoid this warning in the future, run applications in the current directory like this: "harper ${service} ."`
);
process.env.RUN_HDB_APP = process.cwd();
} else if (fs.existsSync(hdbTerms.HARPER_CONFIG_FILE) || fs.existsSync(hdbTerms.HDB_CONFIG_FILE)) {
console.warn(
`It appears you are running Harper in a root data directory, but did not specify the path. I'll go ahead and run Harper with its root path set to "." for you since that's probably what you meant. But to avoid this warning in the future, run it like this: "harper ${service} ."`
);
process.env.ROOTPATH = process.cwd();
}
}
// fall through
case undefined: // run harperdb in the foreground in standard mode
return require('./run.js').main();
default:
const cliApiOp = cliOperations.buildRequest();
logger.trace('calling cli operations with:', cliApiOp);
await cliOperations.cliOperations(cliApiOp);
return;
}
}
exports.harper = harper;
if (require.main === module) {
harper()
.then((message) => {
if (message) {
console.log(message);
logger.notify(message);
}
// Intentionally not calling `process.exit(0);` so if a CLI
// command resulted in a long running process (aka `run`),
// it continues to run.
})
.catch((error) => {
if (error) {
console.error(error);
logger.error(error);
}
process.exit(1);
});
}