forked from notion-enhancer/notion-enhancer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbin.mjs
More file actions
executable file
·144 lines (133 loc) · 3.84 KB
/
Copy pathbin.mjs
File metadata and controls
executable file
·144 lines (133 loc) · 3.84 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
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
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env node
/**
* notion-enhancer
* (c) 2021 dragonwocky <thedragonring.bod@gmail.com> (https://dragonwocky.me/)
* (https://notion-enhancer.github.io/) under the MIT license
*/
'use strict';
import os from 'os';
import { pkg, findNotion } from './pkg/helpers.mjs';
import { line, options, log, help, args, lastSpinner } from './pkg/cli.mjs';
import apply from './pkg/apply.mjs';
import remove from './pkg/remove.mjs';
import check from './pkg/check.mjs';
const manifest = pkg(),
opts = options({
y: 'yes',
n: 'no',
d: 'dev',
h: 'help',
v: 'version',
}),
promptRes = opts.get('yes') ? 'y' : opts.get('no') ? 'n' : undefined;
const displayHelp = () => {
help({
name: manifest.name,
version: manifest.version,
link: manifest.homepage,
commands: [
['apply', 'add enhancements to the notion app'],
['remove', 'return notion to its pre-enhanced/pre-modded state'],
['check, status', 'check the current state of the notion app'],
],
options: [
['-y, --yes', 'skip prompts'],
['-n, --no', 'skip prompts'],
['-d, --dev', 'show detailed error messages (for debug purposes)'],
[
'--path=</path/to/notion/resources>',
'provide a file location to enhance (otherwise auto-picked)',
],
['--no-backup', 'skip backup (faster enhancement, but disables removal)'],
['--patch', 'overwrite inserted files (useful for quick development/testing)'],
['-h, --help', 'display usage information'],
['-v, --version', 'display version number'],
],
});
};
if (opts.get('help')) {
displayHelp();
process.exit(0);
}
if (opts.get('version')) {
log(
`${manifest.name}/${manifest.version} ${
process.platform
}-${os.arch()}/${os.release()} node/${process.version}`
);
process.exit(0);
}
function handleError(err) {
if (opts.get('dev')) {
const strs = [],
tags = [],
stack = err.stack.split('\n');
for (let i = 0; i < stack.length; i++) {
const text = stack[i].replace(/^ /, ' ');
if (i === 0) {
const [type, msg] = text.split(/:((.+)|$)/);
strs.push('{bold.red ');
tags.push(type);
strs.push(':} ');
tags.push(msg);
} else {
strs.push('{grey ');
tags.push(text);
strs.push('}');
tags.push('');
}
if (i !== stack.length - 1) {
strs.push('\n');
tags.push('');
}
}
log(strs, ...tags);
} else {
log`{bold.red Error:} ${err.message} {grey (run with -d for more information)}`;
}
}
try {
const notionPath = opts.get('path') || findNotion();
switch (args()[0]) {
case 'apply': {
log`{bold.rgb(245,245,245) [NOTION-ENHANCER] APPLY}`;
const res = await apply(notionPath, {
overwritePrevious: promptRes,
patchPrevious: opts.get('patch') ? true : false,
takeBackup: opts.get('no-backup') ? false : true,
});
if (res) {
log`{bold.rgb(245,245,245) SUCCESS} {green ✔}`;
} else log`{bold.rgb(245,245,245) CANCELLED} {red ✘}`;
break;
}
case 'remove': {
log`{bold.rgb(245,245,245) [NOTION-ENHANCER] REMOVE}`;
const res = await remove(notionPath, { delCache: promptRes });
if (res) {
log`{bold.rgb(245,245,245) SUCCESS} {green ✔}`;
} else log`{bold.rgb(245,245,245) CANCELLED} {red ✘}`;
break;
}
case 'check':
case 'status': {
log`{bold.rgb(245,245,245) [NOTION-ENHANCER] CHECK}`;
const status = check(notionPath);
line.prev();
if (opts.get('dev')) {
line.forward(24);
console.log(status);
} else {
line.forward(23);
line.write(': ' + status.message + '\r\n');
}
break;
}
default:
displayHelp();
}
} catch (err) {
if (lastSpinner) lastSpinner.stop();
handleError(err);
process.exit(1);
}