-
Notifications
You must be signed in to change notification settings - Fork 3
/
tozan.js
121 lines (105 loc) · 2.45 KB
/
tozan.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
#!/usr/bin/env node
/**
* tozan
* https://github.com/paazmaya/tozan
* Index filesystem by creating metadata database
*
* Copyright (c) Juga Paazmaya <paazmaya@yahoo.com> (https://paazmaya.fi)
* Licensed under the MIT license
*/
import fs from 'node:fs';
import path from 'node:path';
import optionator from 'optionator';
import tozan from '../index.js';
import constants from '../lib/constants.js';
let pkg;
try {
const packageJson = fs.readFileSync(new URL('../package.json', import.meta.url), 'utf8');
pkg = JSON.parse(packageJson);
}
catch (error) {
console.error('Could not read/parse "package.json", quite strange...');
console.error(error);
process.exit(1);
}
const optsParser = optionator({
prepend: `${pkg.name} [options] <directory>`,
append: `Version ${pkg.version}`,
options: [
{
option: 'help',
alias: 'h',
type: 'Boolean',
description: 'Help and usage instructions'
},
{
option: 'version',
alias: 'V',
type: 'Boolean',
description: 'Version number',
example: '-V'
},
{
option: 'database',
alias: 'D',
type: 'String',
default: constants.DEFAULT_DATABASE,
description: 'SQLite database to use'
},
{
option: 'hash',
alias: 'H',
type: 'String',
default: constants.DEFAULT_ALG,
description: 'Hashing algorithm understood by OpenSSL'
},
{
option: 'ignore-dot-files',
alias: 'i',
type: 'Boolean',
description: 'Ignore files and directories that begin with a dot'
}
]
});
let opts;
try {
opts = optsParser.parse(process.argv);
}
catch (error) {
console.error(error.message);
console.log(optsParser.generateHelp());
process.exit(1);
}
if (opts.version) {
console.log(pkg.version);
process.exit(0);
}
if (opts.help) {
console.log(optsParser.generateHelp());
process.exit(0);
}
if (opts._.length !== 1) {
console.error('Directory was not specified');
console.log(optsParser.generateHelp());
process.exit(1);
}
const directory = path.resolve(opts._[0]);
try {
fs.accessSync(directory);
}
catch (error) {
console.error(`Directory "${directory}" does not exist`);
process.exit(1);
}
// Fire away
tozan(directory, {
ignoreDotFiles: typeof opts.ignoreDotFiles === 'boolean' ?
opts.ignoreDotFiles :
false,
algorithm: typeof opts.hash === 'string' ?
opts.hash :
null,
database: typeof opts.database === 'string' ?
opts.database :
null
});