-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
74 lines (62 loc) · 2.79 KB
/
index.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
const {writeFile, mkdir, rm, cp, readdir} = require ('fs/promises')
const {join} = require ('path')
const tap = (fn) => (x) => ((fn (x)), x)
const map = (fn) => (xs) => xs .map (x => fn (x))
const filter = (fn) => (xs) => xs .filter (x => fn (x))
const display = msg => tap (() => console .log (msg))
const allPromises = (ps) => Promise .all (ps)
const delay = (ms) => (v) => new Promise ((res) => setTimeout (() => res (v), ms))
const main = (markdownDir, outputFile) =>
Promise .resolve (join (__dirname, '_workspace'))
.then (tap (deleteOutputDir)) // ensure there's no detritus from previous runs
.then (delay (500)) // TODO: this really should not be necessary
.then (display ('Deleted old workspace'))
.then (createOutputDir)
.then (display ('Created new workspace'))
.then (copyMarkdown (markdownDir))
.then (display ('Copied markdown tiddlers'))
.then (map (makeMeta))
.then (allPromises)
.then (display ('Added meta files'))
.then (tap (writeDefaults))
.then (display ('Wrote $:/DefaultTiddlers.tid'))
.then (delay (500)) // TODO: this really should not be necessary
.then (display ('Creating wiki'))
.then (buildWiki (outputFile))
.then (display (`Wrote wiki to ${outputFile}`))
.catch (console .warn)
const deleteOutputDir = (od) =>
rm (join (__dirname, '_workspace'), {force: true, recursive: true})
const createOutputDir = () =>
mkdir (join (__dirname, '_workspace', 'tiddlers'), {force: true, recursive: true})
.then (() => cp (join (__dirname, 'src'), join (__dirname, '_workspace'), {force: true, recursive: true}))
const copyMarkdown = (dir) => () =>
readdir (dir)
.then (filter (name => name .endsWith ('.md')))
.then (map (handleFile (dir)))
.then (allPromises)
const handleFile = (dir) => (file) =>
cp (join (dir, file), join (__dirname, '_workspace', 'tiddlers', 'imported', file), {force: true})
.then (() => file)
const makeMeta = (file, now = new Date().toISOString().replace(/\D/g, '')) =>
writeFile (join (__dirname, '_workspace', 'tiddlers', 'imported', file + '.meta'), `
created: ${now}
modified: ${now}
tags:
title: ${file .slice (0, -3)}
type: text/x-markdown
`) .then (() => file)
const writeDefaults = (files, now = new Date().toISOString().replace(/\D/g, '')) =>
writeFile (join (__dirname, '_workspace', 'tiddlers', '$__DefaultTiddlers.tid'),
`created: ${now}
modified: ${now}
title: $:/DefaultTiddlers
type: text/vnd.tiddlywiki
${files .map (file => `[[${file .slice (0, -3)}]]`) .join ('\n')}
`)
const buildWiki = (outputFile) => () => {
const $tw = require("tiddlywiki/boot/boot.js").TiddlyWiki();
$tw .boot .argv = [join(__dirname, '_workspace'), '--output', outputFile, '--build', 'index']
$tw .boot .boot ()
}
main (process .argv [2], process .argv [3])