-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadmin.ts
More file actions
72 lines (67 loc) · 2.33 KB
/
admin.ts
File metadata and controls
72 lines (67 loc) · 2.33 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
// Run when Electron needs elevated privileges
// npm run build && node ./build/helpers/admin.js --operation install --type plugins --id surge-synthesizer/surge
// npm run build && node ./build/helpers/admin.js --operation uninstall --type plugins --id surge-synthesizer/surge
import { RegistryType } from '../types/Registry.js';
import { ManagerLocal } from '../classes/ManagerLocal.js';
import { dirApp } from './file.js';
export interface Arguments {
appDir: string;
operation: string;
type: RegistryType;
id: string;
version?: string;
log?: boolean;
}
export function adminArguments(): Arguments {
const args: Arguments = {
appDir: dirApp(),
operation: 'install',
type: RegistryType.Plugins,
id: 'surge-synthesizer/surge',
};
for (let i = 0; i < process.argv.length; i++) {
const arg = process.argv[i];
if (arg === '--appDir') {
args.appDir = process.argv[i + 1];
} else if (arg === '--operation') {
args.operation = process.argv[i + 1];
} else if (arg === '--type') {
args.type = process.argv[i + 1] as RegistryType;
} else if (arg === '--id') {
args.id = process.argv[i + 1];
} else if (arg === '--ver') {
args.version = process.argv[i + 1];
} else if (arg === '--log') {
args.log = true;
}
}
return args;
}
export async function adminInit() {
const args: Arguments = adminArguments();
const manager = new ManagerLocal(args.type, { appDir: args.appDir });
if (args.log) manager.logEnable();
manager.log('adminInit', args);
await manager.sync();
manager.scan();
try {
if (args.operation === 'install') {
await manager.install(args.id, args.version);
} else if (args.operation === 'uninstall') {
await manager.uninstall(args.id, args.version);
} else if (args.operation === 'installAll') {
await manager.installAll();
}
const result = { status: 'ok', code: 0 };
process.stdout.write('\n');
console.log(JSON.stringify(result));
process.exit(0);
} catch (err: any) {
const message = err && err.message ? err.message : String(err);
const errorResult = { status: 'error', code: err && err.code ? err.code : 1, message };
process.stdout.write('\n');
console.log(JSON.stringify(errorResult));
process.exit(typeof errorResult.code === 'number' ? errorResult.code : 1);
}
}
adminInit();