-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
Copy pathgen.js
40 lines (32 loc) · 1.07 KB
/
gen.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
const fs = require("fs-extra")
const faker = require(`faker`)
const N = parseInt(process.env.N, 10) || 100
function createArticle(n, sentence, slug) {
const desc = faker.lorem.sentence()
return {
articleNumber: String(n),
title: sentence,
description: desc,
slug,
date: faker.date.recent(1000).toISOString().slice(0, 10),
html: [faker.lorem.paragraphs(), faker.lorem.paragraphs()],
}
}
(async function() {
console.log("Start of gen")
console.log("Now generating " + N + " articles")
let comma = '';
await fs.writeFile("gendata.json", "[\n") // Replace contents, regardless
for (let i = 0; i < N; ++i) {
const sentence = faker.lorem.sentence()
const slug = faker.helpers.slugify(sentence).toLowerCase()
await fs.appendFile(
"gendata.json",
comma + JSON.stringify(createArticle(i, sentence, slug)) + "\n"
)
comma = ',' // No comma before the first entry, no comma after the last
}
await fs.appendFile("gendata.json", "]\n")
console.log("Finished generating " + N + " articles")
console.log("End of gen")
})();