-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·95 lines (86 loc) · 2.76 KB
/
cli.js
File metadata and controls
executable file
·95 lines (86 loc) · 2.76 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
#!/usr/bin/env node
import { join, resolve } from 'path';
import { stat, access } from 'fs/promises';
import { constants as fsConstants } from 'fs';
import Database from 'better-sqlite3';
import { collect } from './index.js';
async function main() {
const [, , contentArg] = process.argv;
if (!contentArg || contentArg === '--help' || contentArg === '-h') {
printUsage();
process.exit(contentArg ? 0 : 1);
}
const rootdir = process.cwd();
const contentdir = resolve(rootdir, contentArg);
const outdir = join(rootdir, '.structure');
const dbName = 'structure.db';
await assertDirectory(contentdir);
const dbPath = join(outdir, dbName);
console.log(`content-structure: reading from ${contentdir}`);
console.log(`content-structure: writing to ${dbPath}`);
await collect({
rootdir,
contentdir,
outdir,
db_name: dbName
});
await printDbSummary(join(outdir, dbName));
console.log('content-structure: done');
}
async function assertDirectory(pathValue) {
try {
const stats = await stat(pathValue);
if (!stats.isDirectory()) {
throw new Error(`'${pathValue}' is not a directory`);
}
} catch (error) {
console.error(`content-structure: ${error.message}`);
process.exit(1);
}
}
function printUsage() {
// Keep short output for npx usage
console.log('Usage: content-structure <content_dir>');
console.log('Example: npx content-structure ./example/content');
}
async function pathExists(pathValue) {
try {
await access(pathValue, fsConstants.F_OK);
return true;
} catch {
return false;
}
}
async function printDbSummary(dbPath) {
const exists = await pathExists(dbPath);
if (!exists) {
console.log(`content-structure: no database found at ${dbPath}`);
return;
}
try {
const db = new Database(dbPath, {readonly: true});
const tables = db.prepare(`
SELECT name
FROM sqlite_master
WHERE type='table' AND name NOT LIKE 'sqlite_%'
ORDER BY name
`).all();
if (!tables.length) {
console.log('content-structure: database has no tables');
db.close();
return;
}
console.log('content-structure: tables and row counts:');
for (const {name} of tables) {
const {count} = db.prepare(`SELECT COUNT(*) AS count FROM "${name}"`).get();
console.log(` - ${name}: ${count}`);
}
db.close();
} catch (error) {
console.error(`content-structure: failed reading ${dbPath}: ${error.message}`);
}
}
main().catch((error) => {
console.error(`content-structure: ${error.message}`);
process.exit(1);
});