@medplum/cli 1.0.4 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/index.js +76 -94
- package/package.json +1 -1
- package/dist/index.js.map +0 -1
package/README.md
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,14 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
"use strict";
|
|
3
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
4
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
5
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
6
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
7
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
8
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
9
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
10
|
-
});
|
|
11
|
-
};
|
|
12
3
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
13
4
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
14
5
|
};
|
|
@@ -19,97 +10,89 @@ const dotenv_1 = __importDefault(require("dotenv"));
|
|
|
19
10
|
const fs_1 = require("fs");
|
|
20
11
|
const node_fetch_1 = __importDefault(require("node-fetch"));
|
|
21
12
|
const path_1 = require("path");
|
|
22
|
-
function main(medplum, argv) {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
}
|
|
38
|
-
});
|
|
13
|
+
async function main(medplum, argv) {
|
|
14
|
+
if (argv.length < 3) {
|
|
15
|
+
console.log('Usage: medplum <command>');
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
const command = argv[2];
|
|
19
|
+
if (command === 'save-bot') {
|
|
20
|
+
await runBotCommands(medplum, argv, ['save']);
|
|
21
|
+
}
|
|
22
|
+
else if (command === 'deploy-bot') {
|
|
23
|
+
await runBotCommands(medplum, argv, ['save', 'deploy']);
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
console.log(`Unknown command: ${command}`);
|
|
27
|
+
}
|
|
39
28
|
}
|
|
40
29
|
exports.main = main;
|
|
41
|
-
function runBotCommands(medplum, argv, commands) {
|
|
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
|
-
});
|
|
30
|
+
async function runBotCommands(medplum, argv, commands) {
|
|
31
|
+
if (argv.length < 4) {
|
|
32
|
+
console.log(`Usage: medplum ${argv[2]} <bot-name>`);
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
const botName = argv[3];
|
|
36
|
+
const botConfig = readBotConfig(botName);
|
|
37
|
+
if (!botConfig) {
|
|
38
|
+
console.log(`Error: ${botName} not found`);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
let bot;
|
|
42
|
+
try {
|
|
43
|
+
bot = await medplum.readResource('Bot', botConfig.id);
|
|
44
|
+
}
|
|
45
|
+
catch (err) {
|
|
46
|
+
console.log('Error: ' + (0, core_1.normalizeErrorString)(err));
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
if (commands.includes('save')) {
|
|
50
|
+
await saveBot(medplum, botConfig, bot);
|
|
51
|
+
}
|
|
52
|
+
if (commands.includes('deploy')) {
|
|
53
|
+
await deployBot(medplum, botConfig, bot);
|
|
54
|
+
}
|
|
68
55
|
}
|
|
69
|
-
function saveBot(medplum, botConfig, bot) {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
else {
|
|
83
|
-
console.log('Success! New bot version: ' + ((_a = updateResult.meta) === null || _a === void 0 ? void 0 : _a.versionId));
|
|
84
|
-
}
|
|
56
|
+
async function saveBot(medplum, botConfig, bot) {
|
|
57
|
+
const code = readFileContents(botConfig.source);
|
|
58
|
+
if (!code) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
try {
|
|
62
|
+
console.log('Update bot code.....');
|
|
63
|
+
const updateResult = await medplum.updateResource({
|
|
64
|
+
...bot,
|
|
65
|
+
code,
|
|
66
|
+
});
|
|
67
|
+
if (!updateResult) {
|
|
68
|
+
console.log('Bot not modified');
|
|
85
69
|
}
|
|
86
|
-
|
|
87
|
-
console.log('
|
|
70
|
+
else {
|
|
71
|
+
console.log('Success! New bot version: ' + updateResult.meta?.versionId);
|
|
88
72
|
}
|
|
89
|
-
}
|
|
73
|
+
}
|
|
74
|
+
catch (err) {
|
|
75
|
+
console.log('Update error: ', err);
|
|
76
|
+
}
|
|
90
77
|
}
|
|
91
|
-
function deployBot(medplum, botConfig, bot) {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
console.log('Deploy error: ', err);
|
|
107
|
-
}
|
|
108
|
-
});
|
|
78
|
+
async function deployBot(medplum, botConfig, bot) {
|
|
79
|
+
const code = readFileContents(botConfig.dist ?? botConfig.source);
|
|
80
|
+
if (!code) {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
try {
|
|
84
|
+
console.log('Deploying bot...');
|
|
85
|
+
const deployResult = (await medplum.post(medplum.fhirUrl('Bot', bot.id, '$deploy'), {
|
|
86
|
+
code,
|
|
87
|
+
}));
|
|
88
|
+
console.log('Deploy result: ' + deployResult.issue?.[0]?.details?.text);
|
|
89
|
+
}
|
|
90
|
+
catch (err) {
|
|
91
|
+
console.log('Deploy error: ', err);
|
|
92
|
+
}
|
|
109
93
|
}
|
|
110
94
|
function readBotConfig(botName) {
|
|
111
|
-
|
|
112
|
-
return (_b = (_a = readConfig()) === null || _a === void 0 ? void 0 : _a.bots) === null || _b === void 0 ? void 0 : _b.find((b) => b.name === botName);
|
|
95
|
+
return readConfig()?.bots?.find((b) => b.name === botName);
|
|
113
96
|
}
|
|
114
97
|
function readConfig() {
|
|
115
98
|
const content = readFileContents('medplum.config.json');
|
|
@@ -128,10 +111,9 @@ function readFileContents(fileName) {
|
|
|
128
111
|
}
|
|
129
112
|
if (require.main === module) {
|
|
130
113
|
dotenv_1.default.config();
|
|
131
|
-
const medplum = new core_1.MedplumClient({ fetch: node_fetch_1.default });
|
|
114
|
+
const medplum = new core_1.MedplumClient({ fetch: node_fetch_1.default, baseUrl: process.env['MEDPLUM_BASE_URL'] });
|
|
132
115
|
medplum
|
|
133
116
|
.startClientLogin(process.env['MEDPLUM_CLIENT_ID'], process.env['MEDPLUM_CLIENT_SECRET'])
|
|
134
117
|
.then(() => main(medplum, process.argv))
|
|
135
118
|
.catch((err) => console.error('Unhandled error:', err));
|
|
136
119
|
}
|
|
137
|
-
//# sourceMappingURL=index.js.map
|
package/package.json
CHANGED
package/dist/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AACA,wCAAoE;AAEpE,oD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EAAE,CAAC;IAChB,MAAM,OAAO,GAAG,IAAI,oBAAa,CAAC,EAAE,KAAK,EAAL,oBAAK,EAAE,CAAC,CAAC;IAC7C,OAAO;SACJ,gBAAgB,CAAC,OAAO,CAAC,GAAG,CAAC,mBAAmB,CAAW,EAAE,OAAO,CAAC,GAAG,CAAC,uBAAuB,CAAW,CAAC;SAC5G,IAAI,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,OAAO,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC;SACvC,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,kBAAkB,EAAE,GAAG,CAAC,CAAC,CAAC;CAC3D","sourcesContent":["#!/usr/bin/env node\nimport { MedplumClient, normalizeErrorString } from '@medplum/core';\nimport { Bot, OperationOutcome } from '@medplum/fhirtypes';\nimport dotenv from 'dotenv';\nimport { existsSync, readFileSync } from 'fs';\nimport fetch from 'node-fetch';\nimport { resolve } from 'path';\n\ninterface MedplumConfig {\n readonly bots?: MedplumBotConfig[];\n}\n\ninterface MedplumBotConfig {\n readonly name: string;\n readonly id: string;\n readonly source: string;\n readonly dist?: string;\n}\n\nexport async function main(medplum: MedplumClient, argv: string[]): Promise<void> {\n if (argv.length < 3) {\n console.log('Usage: medplum <command>');\n return;\n }\n\n const command = argv[2];\n if (command === 'save-bot') {\n await runBotCommands(medplum, argv, ['save']);\n } else if (command === 'deploy-bot') {\n await runBotCommands(medplum, argv, ['save', 'deploy']);\n } else {\n console.log(`Unknown command: ${command}`);\n }\n}\n\nasync function runBotCommands(medplum: MedplumClient, argv: string[], commands: string[]): Promise<void> {\n if (argv.length < 4) {\n console.log(`Usage: medplum ${argv[2]} <bot-name>`);\n return;\n }\n\n const botName = argv[3];\n const botConfig = readBotConfig(botName);\n if (!botConfig) {\n console.log(`Error: ${botName} not found`);\n return;\n }\n\n let bot;\n try {\n bot = await medplum.readResource('Bot', botConfig.id);\n } catch (err) {\n console.log('Error: ' + normalizeErrorString(err));\n return;\n }\n\n if (commands.includes('save')) {\n await saveBot(medplum, botConfig, bot);\n }\n\n if (commands.includes('deploy')) {\n await deployBot(medplum, botConfig, bot);\n }\n}\n\nasync function saveBot(medplum: MedplumClient, botConfig: MedplumBotConfig, bot: Bot): Promise<void> {\n const code = readFileContents(botConfig.source);\n if (!code) {\n return;\n }\n\n try {\n console.log('Update bot code.....');\n const updateResult = await medplum.updateResource({\n ...bot,\n code,\n });\n if (!updateResult) {\n console.log('Bot not modified');\n } else {\n console.log('Success! New bot version: ' + updateResult.meta?.versionId);\n }\n } catch (err) {\n console.log('Update error: ', err);\n }\n}\n\nasync function deployBot(medplum: MedplumClient, botConfig: MedplumBotConfig, bot: Bot): Promise<void> {\n const code = readFileContents(botConfig.dist ?? botConfig.source);\n if (!code) {\n return;\n }\n\n try {\n console.log('Deploying bot...');\n const deployResult = (await medplum.post(medplum.fhirUrl('Bot', bot.id as string, '$deploy'), {\n code,\n })) as OperationOutcome;\n console.log('Deploy result: ' + deployResult.issue?.[0]?.details?.text);\n } catch (err) {\n console.log('Deploy error: ', err);\n }\n}\n\nfunction readBotConfig(botName: string): MedplumBotConfig | undefined {\n return readConfig()?.bots?.find((b) => b.name === botName);\n}\n\nfunction readConfig(): MedplumConfig | undefined {\n const content = readFileContents('medplum.config.json');\n if (!content) {\n return undefined;\n }\n return JSON.parse(content);\n}\n\nfunction readFileContents(fileName: string): string | undefined {\n const path = resolve(process.cwd(), fileName);\n if (!existsSync(path)) {\n console.log('Error: File does not exist: ' + path);\n return '';\n }\n return readFileSync(path, 'utf8');\n}\n\nif (require.main === module) {\n dotenv.config();\n const medplum = new MedplumClient({ fetch });\n medplum\n .startClientLogin(process.env['MEDPLUM_CLIENT_ID'] as string, process.env['MEDPLUM_CLIENT_SECRET'] as string)\n .then(() => main(medplum, process.argv))\n .catch((err) => console.error('Unhandled error:', err));\n}\n"]}
|