@tenjuu99/blog 0.2.12 → 0.2.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/dir.js +42 -3
- package/lib/distribute.js +20 -16
- package/lib/generate.js +2 -1
- package/package.json +1 -1
package/lib/dir.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import config from './config.js'
|
|
2
2
|
import { watchers } from './watcher.js'
|
|
3
3
|
import fs from 'node:fs'
|
|
4
|
+
import { styleText } from 'node:util'
|
|
4
5
|
|
|
5
6
|
const rootDir = process.cwd()
|
|
6
7
|
const srcDir = `${rootDir}/${config.src_dir}`
|
|
@@ -18,26 +19,63 @@ const watch = {
|
|
|
18
19
|
cssDir: `${srcDir}/css`,
|
|
19
20
|
serverDir: `${srcDir}/server`,
|
|
20
21
|
helperDir: `${srcDir}/helper`,
|
|
22
|
+
jsDir: `${srcDir}/js`,
|
|
21
23
|
}
|
|
22
24
|
|
|
25
|
+
let alreadyCached = false
|
|
26
|
+
|
|
23
27
|
const cache = () => {
|
|
28
|
+
if (alreadyCached) {
|
|
29
|
+
return
|
|
30
|
+
}
|
|
24
31
|
if (config.packages) {
|
|
25
32
|
const packages = config.packages.split(',')
|
|
26
33
|
packages.forEach(dir => {
|
|
27
34
|
if (!fs.existsSync(`${packageDir}/${dir}`)) {
|
|
28
35
|
throw new Error(`"${dir}" package does not exist.`)
|
|
29
36
|
}
|
|
37
|
+
console.log(styleText('blue', `[cache] enable package: ${dir}`))
|
|
30
38
|
fs.cpSync(`${packageDir}/${dir}`, cacheDir, { recursive: true })
|
|
31
39
|
})
|
|
32
40
|
}
|
|
33
|
-
fs.cpSync(srcDir, cacheDir, {
|
|
41
|
+
fs.cpSync(srcDir, cacheDir, {
|
|
42
|
+
recursive: true,
|
|
43
|
+
force: true,
|
|
44
|
+
filter: (src, dist) => !src.startsWith(packageDir)
|
|
45
|
+
})
|
|
46
|
+
console.log(styleText('blue', '[cache] finish copy'))
|
|
47
|
+
alreadyCached = true
|
|
34
48
|
}
|
|
49
|
+
// import パスの解決の関係上、関数コールより前にファイルが読まれることがあるためここで呼んでおく
|
|
50
|
+
// 子プロセスで起動する場合は二度呼ばれることになる
|
|
51
|
+
// generate の中でもコールしているが、同一プロセスであれば alreadyCached 変数で制御される
|
|
35
52
|
cache()
|
|
36
53
|
|
|
54
|
+
const resolveDestinationPath = (path) => {
|
|
55
|
+
for (const key of Object.keys(watch)) {
|
|
56
|
+
if (path.startsWith(watch[key])) {
|
|
57
|
+
const dir = watch[key].split('/').pop()
|
|
58
|
+
const srcFile = `${dir}${path.replace(watch[key], '')}`
|
|
59
|
+
return `${cacheDir}/${srcFile}`
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
37
63
|
watchers.push({
|
|
38
64
|
paths: [srcDir],
|
|
39
65
|
event: ['change', 'add', 'unlink'],
|
|
40
|
-
callback:
|
|
66
|
+
callback: ( path ) => {
|
|
67
|
+
const dest = resolveDestinationPath(path)
|
|
68
|
+
if (fs.existsSync(path)) {
|
|
69
|
+
fs.cpSync(path, dest, { force: true })
|
|
70
|
+
console.log(styleText('blue', `update ${path} => ${dest}`))
|
|
71
|
+
} else {
|
|
72
|
+
fs.unlinkSync(dest)
|
|
73
|
+
console.log(styleText('red', `unlink ${dest}`))
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
watchOptions: {
|
|
77
|
+
ignoreInitial: true
|
|
78
|
+
}
|
|
41
79
|
})
|
|
42
80
|
export {
|
|
43
81
|
rootDir,
|
|
@@ -49,5 +87,6 @@ export {
|
|
|
49
87
|
cacheDir,
|
|
50
88
|
serverDir,
|
|
51
89
|
helperDir,
|
|
52
|
-
watch
|
|
90
|
+
watch,
|
|
91
|
+
cache
|
|
53
92
|
}
|
package/lib/distribute.js
CHANGED
|
@@ -53,24 +53,28 @@ const distribute = async (data, srcDir, distDir) => {
|
|
|
53
53
|
if (!existsSync(cacheDir)) {
|
|
54
54
|
mkdirSync(cacheDir)
|
|
55
55
|
}
|
|
56
|
-
|
|
57
|
-
.
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
56
|
+
if (existsSync(indexFile)) {
|
|
57
|
+
fs.readFile(indexFile, 'utf8')
|
|
58
|
+
.then(text => {
|
|
59
|
+
const oldIndex = JSON.parse(text)
|
|
60
|
+
let deleted = oldIndex.filter(oi => !newIndex.map(ni => ni.__output).includes(oi.__output))
|
|
61
|
+
fs.writeFile(indexFile, JSON.stringify(newIndex))
|
|
62
|
+
if (deleted) {
|
|
63
|
+
for (const obj of deleted) {
|
|
64
|
+
if (existsSync(`${distDir}${obj.__output}`)) {
|
|
65
|
+
console.log(styleText('red', '[unlink]'), `${distDir}${obj.__output}`)
|
|
66
|
+
fs.unlink(`${distDir}${obj.__output}`)
|
|
67
|
+
}
|
|
66
68
|
}
|
|
67
69
|
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
70
|
+
})
|
|
71
|
+
.catch(error => {
|
|
72
|
+
console.log(error)
|
|
73
|
+
fs.writeFile(indexFile, JSON.stringify(newIndex))
|
|
74
|
+
})
|
|
75
|
+
} else {
|
|
76
|
+
fs.writeFile(indexFile, JSON.stringify(newIndex))
|
|
77
|
+
}
|
|
74
78
|
}
|
|
75
79
|
|
|
76
80
|
export default distribute
|
package/lib/generate.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
"use strict"
|
|
2
2
|
import distribute from './distribute.js'
|
|
3
3
|
import { indexing, allData } from './indexer.js'
|
|
4
|
-
import { srcDir, distDir } from './dir.js'
|
|
4
|
+
import { srcDir, distDir, cache } from './dir.js'
|
|
5
5
|
import { styleText } from 'node:util'
|
|
6
6
|
|
|
7
7
|
const generate = async () => {
|
|
8
8
|
let start = performance.now()
|
|
9
|
+
cache()
|
|
9
10
|
await indexing()
|
|
10
11
|
let end = performance.now()
|
|
11
12
|
console.log(styleText('blue', '[indexing: ' + (end - start) + "ms]"))
|