@13w/miri 1.1.10 → 1.1.11
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/dist/cli.js +41 -12
- package/dist/mongodb.js +2 -0
- package/package.json +2 -1
package/dist/cli.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises';
|
|
1
2
|
Error.stackTraceLimit = Infinity;
|
|
2
3
|
import { join } from 'node:path';
|
|
3
4
|
/* eslint-disable no-console */
|
|
@@ -5,27 +6,55 @@ import { Command } from 'commander';
|
|
|
5
6
|
import { Table } from 'console-table-printer';
|
|
6
7
|
import colors from 'colors';
|
|
7
8
|
import { createTunnel } from 'tunnel-ssh';
|
|
9
|
+
import SSHConfig from 'ssh-config';
|
|
8
10
|
import connection from './mongodb.js';
|
|
9
11
|
import Miri, { IndexStatus, PatchStatus } from './miri.js';
|
|
10
12
|
import { readFileSync } from 'fs';
|
|
11
13
|
import { realpathSync } from 'node:fs';
|
|
12
|
-
const
|
|
14
|
+
const pkg = await readFile(join(import.meta.dirname, '../package.json'), 'utf-8').then((content) => JSON.parse(content));
|
|
15
|
+
const mirirc = await readFile(join(process.cwd(), '.mirirc'), 'utf-8').then((content) => JSON.parse(content), () => ({}));
|
|
16
|
+
const { SSH_AUTH_SOCK, HOME } = process.env;
|
|
13
17
|
const program = new Command();
|
|
14
|
-
program.
|
|
15
|
-
program.option('-
|
|
18
|
+
program.version(pkg.version);
|
|
19
|
+
program.option('-e --env <environment>', 'Environment name from .mirirc', 'default');
|
|
20
|
+
program.option('-m --migrations <folder>', `Folder with migrations (default: "${process.cwd(), join(process.cwd(), 'migrations')}")`);
|
|
21
|
+
program.option('-d --db <mongo-uri>', 'MongoDB Connection URI (default: "mongodb://localhost:27017/test")');
|
|
22
|
+
program.option('--ssh-profile <profile>', 'Connect via SSH using profile');
|
|
16
23
|
program.option('--ssh-host <host>', 'Connect via SSH proxy Host');
|
|
17
24
|
program.option('--ssh-port <port>', 'Connect via SSH proxy Port');
|
|
18
25
|
program.option('--ssh-user <user>', 'Connect via SSH proxy User');
|
|
19
26
|
program.option('--ssh-key <path/to/key>', 'Connect via SSH proxy IdentityKey');
|
|
27
|
+
let configCache;
|
|
28
|
+
const getConfig = (programOpts) => {
|
|
29
|
+
if (configCache) {
|
|
30
|
+
return configCache;
|
|
31
|
+
}
|
|
32
|
+
const envs = mirirc.environments ?? { default: mirirc };
|
|
33
|
+
const env = envs[programOpts.env ?? 'default'] ?? {};
|
|
34
|
+
const config = Object.assign({}, mirirc, env, programOpts);
|
|
35
|
+
if (config.sshProfile) {
|
|
36
|
+
const sshConfigPath = join(HOME ?? '', '.ssh/config');
|
|
37
|
+
const sshConfigContent = readFileSync(sshConfigPath, 'utf-8');
|
|
38
|
+
console.log(`Reading profile ${config.sshProfile} from SSH config ${sshConfigPath}`);
|
|
39
|
+
const parsed = SSHConfig.parse(sshConfigContent).compute(config.sshProfile);
|
|
40
|
+
config.sshHost = programOpts.sshHost ?? parsed.Hostname;
|
|
41
|
+
config.sshPort = programOpts.sshPort ?? parsed.Port;
|
|
42
|
+
config.sshKey = programOpts.sshKey ?? parsed.IdentityFile?.[0];
|
|
43
|
+
config.sshUser = programOpts.sshUser ?? parsed.User;
|
|
44
|
+
}
|
|
45
|
+
// console.dir(config)
|
|
46
|
+
return configCache = config;
|
|
47
|
+
};
|
|
20
48
|
const createSSHTunnel = async (opts) => {
|
|
49
|
+
const config = getConfig(opts);
|
|
21
50
|
const sshOptions = {
|
|
22
|
-
host:
|
|
23
|
-
port: Number(
|
|
24
|
-
username:
|
|
51
|
+
host: config.sshHost,
|
|
52
|
+
port: Number(config.sshPort ?? 22),
|
|
53
|
+
username: config.sshUser,
|
|
25
54
|
agent: SSH_AUTH_SOCK,
|
|
26
|
-
privateKey:
|
|
55
|
+
privateKey: config.sshKey ? readFileSync(realpathSync(config.sshKey)) : void 0,
|
|
27
56
|
};
|
|
28
|
-
const dst = new URL(
|
|
57
|
+
const dst = new URL(config.db);
|
|
29
58
|
const forwardOptions = {
|
|
30
59
|
dstPort: Number(dst.port ?? 27017),
|
|
31
60
|
dstAddr: dst.hostname,
|
|
@@ -37,14 +66,14 @@ const createSSHTunnel = async (opts) => {
|
|
|
37
66
|
return dst.toString();
|
|
38
67
|
};
|
|
39
68
|
const getMiri = async () => {
|
|
40
|
-
const
|
|
41
|
-
const db =
|
|
69
|
+
const config = getConfig(program.opts());
|
|
70
|
+
const db = config.sshHost ? await createSSHTunnel(config) : config.db;
|
|
42
71
|
const dbUri = new URL(db);
|
|
43
72
|
dbUri.searchParams.append('directConnection', 'true');
|
|
44
|
-
dbUri.searchParams.append('appName',
|
|
73
|
+
dbUri.searchParams.append('appName', `miri+v${pkg.version}`);
|
|
45
74
|
const client = await connection(dbUri.toString());
|
|
46
75
|
return new Miri(client, {
|
|
47
|
-
localMigrations:
|
|
76
|
+
localMigrations: config.migrations,
|
|
48
77
|
});
|
|
49
78
|
};
|
|
50
79
|
const status = async (remote = false, group) => {
|
package/dist/mongodb.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
/* eslint-disable no-console */
|
|
1
2
|
import { MongoClient } from 'mongodb';
|
|
2
3
|
let client;
|
|
3
4
|
export default async function connect(uri = 'mongodb://localhost:27017/test') {
|
|
4
5
|
if (!client) {
|
|
6
|
+
console.log(`Connecting MongoDB to ${uri}`);
|
|
5
7
|
client = await MongoClient.connect(uri);
|
|
6
8
|
client?.topology?.socket?.unref();
|
|
7
9
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@13w/miri",
|
|
3
3
|
"description": "MongoDB patch manager",
|
|
4
|
-
"version": "1.1.
|
|
4
|
+
"version": "1.1.11",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
7
7
|
"node": "v20"
|
|
@@ -27,6 +27,7 @@
|
|
|
27
27
|
"commander": "^12.0.0",
|
|
28
28
|
"console-table-printer": "^2.12.0",
|
|
29
29
|
"mongodb": "^6.5.0",
|
|
30
|
+
"ssh-config": "^4.4.2",
|
|
30
31
|
"tunnel-ssh": "^5.1.2"
|
|
31
32
|
},
|
|
32
33
|
"devDependencies": {
|