Skip to content

Commit bc096b7

Browse files
authored
Merge pull request #19 from orval/gen_test_data
A tool to generate test data
2 parents ea8f75d + 8f2604b commit bc096b7

File tree

5 files changed

+117
-1
lines changed

5 files changed

+117
-1
lines changed

cli/gentest.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
'use strict'
2+
import { appendFileSync, mkdirSync } from 'node:fs'
3+
import { join, resolve } from 'node:path'
4+
5+
import slugify from 'slugify'
6+
import log from 'loglevel'
7+
import { LoremIpsum } from 'lorem-ipsum'
8+
9+
import { FsLayout } from './lib/fs_layout.js'
10+
11+
const GENTEST_NAME = 'Rakosh Test'
12+
const DEPTH = 4
13+
const BREADTH = 10
14+
const NUGS = 3
15+
16+
const lorem = new LoremIpsum()
17+
18+
log.setLevel('WARN')
19+
20+
export default {
21+
command: 'gentest <dir>',
22+
describe: false, // 'Command for generating rakosh test data',
23+
builder: (yargs) => {
24+
return yargs
25+
.positional('dir', {
26+
describe: 'name of directory to create',
27+
string: true,
28+
normalize: true,
29+
coerce: d => {
30+
d = resolve(d)
31+
mkdirSync(d)
32+
return d
33+
}
34+
})
35+
.check((argv) => {
36+
return true
37+
})
38+
},
39+
40+
handler: (argv) => {
41+
if (argv.verbose) log.setLevel('INFO')
42+
let fsLayout
43+
44+
try {
45+
fsLayout = new FsLayout(argv.dir)
46+
} catch (err) {
47+
log.error(`Failed to obtain FsLayout ${err}`)
48+
return false
49+
}
50+
51+
fsLayout.createAdit(join(argv.dir, slugify(GENTEST_NAME).toLowerCase() + '.md'), GENTEST_NAME)
52+
53+
makePassages(argv.dir, fsLayout, 0)
54+
55+
const check = new FsLayout(argv.dir)
56+
log.info(`created ${check.size()} entities`)
57+
}
58+
}
59+
60+
function makePassages (parent, fsLayout, depth) {
61+
depth++
62+
for (let i = 0; i < BREADTH && depth < DEPTH; i++) {
63+
const dir = join(parent, slugify(lorem.generateWords(2)).toLowerCase())
64+
mkdirSync(dir)
65+
generateNuggets(fsLayout, dir)
66+
makePassages(dir, fsLayout, depth)
67+
}
68+
}
69+
70+
function generateNuggets (fsLayout, dir) {
71+
for (let i = 1; i <= NUGS; i++) {
72+
const title = lorem.generateWords(3)
73+
const nugPath = join(dir, slugify(title).toLowerCase() + '.md')
74+
fsLayout.add(nugPath, title)
75+
appendContent(nugPath)
76+
}
77+
}
78+
79+
function appendContent (path) {
80+
appendFileSync(path, `
81+
${lorem.generateParagraphs(1)}
82+
83+
${lorem.generateParagraphs(1)}
84+
`)
85+
}

cli/lib/fs_layout.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,22 @@ export class FsLayout {
100100
log.info(`new nugget "${title}" with _key ${tags._key} added at [${path}]`)
101101
}
102102

103+
createAdit (path, title) {
104+
const tags = {
105+
_key: 'adit',
106+
fs_layout: RAKOSH_FS_LAYOUT_VERSION
107+
}
108+
109+
writeFileSync(path, [
110+
'---',
111+
yaml.dump(tags).trim(),
112+
'---',
113+
`\n# ${title}\n`
114+
].join('\n'))
115+
116+
log.info(`adit created at [${path}]`)
117+
}
118+
103119
addPassage (path, title) {
104120
const mdFile = path + '.md'
105121
if (this.#fileDoesNotExist(mdFile)) {

cli/main.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import yargs from 'yargs/yargs'
44
import { hideBin } from 'yargs/helpers'
55

66
import fs from './fs.js'
7+
import gentest from './gentest.js'
78
import uuid from './uuid.js'
89

910
const importCommand = async (moduleName) => {
@@ -18,6 +19,7 @@ yargs(hideBin(process.argv))
1819
.command(await importCommand('pdf'))
1920
.command(await importCommand('confluence'))
2021
.command(fs)
22+
.command(gentest)
2123
.command(uuid)
2224
.demandCommand(1)
2325
.option('verbose', {

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rakosh",
3-
"version": "0.13.0",
3+
"version": "0.13.1",
44
"description": "yet another knowledge base system -- this time with nuggets",
55
"author": "Duncan Rance",
66
"license": "MIT",
@@ -31,6 +31,7 @@
3131
"front-matter": "^4.0.2",
3232
"lodash": "^4.17.21",
3333
"loglevel": "^1.8.1",
34+
"lorem-ipsum": "^2.0.8",
3435
"markdown-toc": "^1.2.0",
3536
"markdownlint": "^0",
3637
"marked": "^5.0.0",

yarn.lock

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,11 @@ comma-separated-tokens@^2.0.0:
767767
resolved "https://registry.yarnpkg.com/comma-separated-tokens/-/comma-separated-tokens-2.0.3.tgz#4e89c9458acb61bc8fef19f4529973b2392839ee"
768768
integrity sha512-Fu4hJdvzeylCfQPp9SGWidpzrMs7tTrlu6Vb8XGaRGck8QSNZJJp538Wrb60Lax4fPwR64ViY468OIUTbRlGZg==
769769

770+
commander@^9.3.0:
771+
version "9.5.0"
772+
resolved "https://registry.yarnpkg.com/commander/-/commander-9.5.0.tgz#bc08d1eb5cedf7ccb797a96199d41c7bc3e60d30"
773+
integrity sha512-KRs7WVDKg86PWiuAqhDrAQnTXZKraVcCc6vFdL14qrZ/DcWwuRo7VoiYXalXO7S5GKpqYiVEwCbgFDfxNHKJBQ==
774+
770775
771776
version "0.0.1"
772777
resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b"
@@ -2526,6 +2531,13 @@ loose-envify@^1.1.0, loose-envify@^1.4.0:
25262531
dependencies:
25272532
js-tokens "^3.0.0 || ^4.0.0"
25282533

2534+
lorem-ipsum@^2.0.8:
2535+
version "2.0.8"
2536+
resolved "https://registry.yarnpkg.com/lorem-ipsum/-/lorem-ipsum-2.0.8.tgz#f969a089f2ac6f19cf01b854b61beabb0e6f3cbc"
2537+
integrity sha512-5RIwHuCb979RASgCJH0VKERn9cQo/+NcAi2BMe9ddj+gp7hujl6BI+qdOG4nVsLDpwWEJwTVYXNKP6BGgbcoGA==
2538+
dependencies:
2539+
commander "^9.3.0"
2540+
25292541
loupe@^2.3.6:
25302542
version "2.3.7"
25312543
resolved "https://registry.yarnpkg.com/loupe/-/loupe-2.3.7.tgz#6e69b7d4db7d3ab436328013d37d1c8c3540c697"

0 commit comments

Comments
 (0)