-
Notifications
You must be signed in to change notification settings - Fork 0
/
epoche.js
134 lines (122 loc) · 3.62 KB
/
epoche.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
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
#!/usr/bin/env node
'use strict';
const { program } = require('commander');
const promptly = require('promptly');
const fs = require('fs').promises;
const path = require('path');
const db = require('./server/src/database');
const DB_DEFAULT = 'epoche.db';
const fixture1 = [
'MainPage',
'MainPage',
'Welcome to your wiki\n\n* [Sample](/Sample)\n',
true,
'system',
],
fixture2 = [
'Sample',
'Sample page',
'This page is a **sample** page.',
true,
'system',
];
program
.command('start')
.requiredOption('--db <filename>', 'load database at <filename>')
.option('--port <num>', 'use port <num>', parseInt)
.description('start wiki')
.action(async (cmdObj) => {
process.env.DB_PATH = cmdObj.db;
process.env.PORT = cmdObj.port ? cmdObj.port : '';
const meta = await db.getMeta();
process.env.WIKINAME = meta.wikiname;
process.env.BASEPATH = meta.basepath;
require('./server/src/server');
});
program
.command('init <dir_or_filename>')
.description(
'initialize database at <dir_or_filename> (default filename: `epoche.db`)',
)
.action(async (dir_or_filename) => {
try {
const isDir = await fs
.lstat(dir_or_filename)
.then((stat) => stat.isDirectory())
.catch((_err) => {
/* No file or else */
});
process.env.DB_PATH = isDir
? path.join(dir_or_filename, DB_DEFAULT)
: dir_or_filename;
await db.initialize();
const wikiname = await promptly.prompt(
'Wiki name (default: `Epoche`): ',
{
default: 'Epoche',
},
);
await db.setWikiname(wikiname);
const basepath = await promptly.prompt(
'Wiki base path (default: `/`): ',
{
default: '/',
},
);
await db.setBasepath(basepath);
const userid = await promptly.prompt('Default username: ');
const password = await promptly.prompt('Default password: ', {
silent: true,
});
await db.addUser(userid, password);
await db.upsertPage(...fixture1);
await db.upsertPage(...fixture2);
db.destroy();
console.log(
`Done! To start, run: epoche start --db ${process.env.DB_PATH}`,
);
} catch (_) {
// TODO: Clean up if a new file is created
process.exit(-1);
}
});
program
.command('add-user <userid> [password]')
.requiredOption('--db <filename>', 'load database at <filename>')
.description('add user')
.action(async (userid, password, cmdObj) => {
process.env.DB_PATH = cmdObj.db;
password =
password || (await promptly.prompt('Password: ', { silent: true }));
await db.addUser(userid, password);
db.destroy();
});
program
.command('delete-user <userid>')
.requiredOption('--db <filename>', 'load database at <filename>')
.description('delete user')
.action(async (userid, cmdObj) => {
process.env.DB_PATH = cmdObj.db;
await db.deleteUser(userid);
db.destroy();
});
const config = program.command('config').description('configure wiki');
config
.command('set-wikiname <name>')
.requiredOption('--db <filename>', 'load database at <filename>')
.description('set wiki name')
.action(async (name, cmdObj) => {
process.env.DB_PATH = cmdObj.db;
await db.setWikiname(name);
db.destroy();
});
config
.command('set-basepath <path_in_url>')
.requiredOption('--db <filename>', 'load database at <filename>')
.description('set basepath (default: `/`)')
.action(async (path_in_url, cmdObj) => {
process.env.DB_PATH = cmdObj.db;
await db.setBasepath(path_in_url);
db.destroy();
});
program.parse(process.argv);