-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.js
executable file
·86 lines (74 loc) · 2.52 KB
/
install.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
#!/usr/bin/env node
const LibFs = require('fs');
const LibOs = require('os');
const LibPath = require('path');
const mkdirp = require('mkdirp');
const { Command } = require('commander');
const pkg = require('./package.json');
const program = new Command();
const PLUGIN_NAME = 'obsidian-post-gallery';
program
.version(pkg.version)
.description('Obsidian plugin "obsidian-post-gallery" installer, help to install the plugin')
.requiredOption('-v, --vault <dir>', 'directory of target vault')
.parse(process.argv);
const options = program.opts();
const CMD_ARGS_VAULT_PATH = options.vault;
class Installer {
run() {
this._validate();
this._process();
}
_validate() {
console.log('Validating ...');
if (LibOs.platform() !== 'darwin') {
console.log('Only MacOS supported!');
process.exit(1);
}
if (!LibFs.existsSync(CMD_ARGS_VAULT_PATH) || !LibFs.statSync(CMD_ARGS_VAULT_PATH).isDirectory()) {
console.log('Invalid vault path specified!');
process.exit(1);
}
}
_process() {
// check dir ".obsidian"
const configPath = LibPath.join(CMD_ARGS_VAULT_PATH, '.obsidian');
if (!LibFs.existsSync(configPath) || !LibFs.statSync(configPath).isDirectory()) {
console.log('Vault has no ".obsidian" config folder, exit ...');
process.exit(1);
}
// make plugin dir
const pluginPath = LibPath.join(configPath, 'plugins', PLUGIN_NAME);
if (!LibFs.existsSync(pluginPath)) {
mkdirp.sync(pluginPath);
}
// add plugin name into plugins list
const configFilePath = LibPath.join(configPath, 'community-plugins.json');
let pluginsList = [];
if (LibFs.existsSync(configFilePath)) {
pluginsList = JSON.parse(LibFs.readFileSync(configFilePath).toString());
}
if (!pluginsList.includes(PLUGIN_NAME)) {
pluginsList.push(PLUGIN_NAME);
}
LibFs.writeFileSync(configFilePath, JSON.stringify(pluginsList, null, 2));
// copy plugin files
const distPath = LibPath.join(__dirname, 'dist');
for (const file of LibFs.readdirSync(distPath)) {
if (file === '.DS_Store') {
continue;
}
const source = LibPath.join(distPath, file);
const dest = LibPath.join(pluginPath, file);
LibFs.copyFileSync(source, dest);
}
console.log('done');
}
}
new Installer().run();
process.on('uncaughtException', (error) => {
console.error(`Process on uncaughtException error = ${error.stack}`);
});
process.on('unhandledRejection', (error) => {
console.error(`Process on unhandledRejection error`, error);
});