Skip to content

Commit 916a932

Browse files
added local plugin to only update modified files
1 parent 227070a commit 916a932

6 files changed

+134
-61
lines changed

.dockerignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
**/build
44
**/build-pdf
55
**/build-swagger
6-
**/build-ngindox
6+
**/build-ngindox
7+
.revision

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ npm-debug.log
99
.DS_Store
1010
./**/*.tmp
1111
.tmp
12+
.revision

index.js

+22-21
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ const serve = require('metalsmith-serve');
1010
const webpack = require('metalsmith-webpack2');
1111
const anchor = require('markdown-it-anchor');
1212
const attrs = require('markdown-it-attrs');
13-
const incremental = require('metalsmith-incremental').default;
13+
//const incremental = require('metalsmith-incremental').default;
1414
const timer = require('metalsmith-timer');
1515

1616
// Local Plugins
17+
const reduce = require('./plugins/metalsmith-revision').reduce;
18+
const restore = require('./plugins/metalsmith-revision').restore;
1719
const hierarchy = require('./plugins/metalsmith-hierarchy');
1820
const hierarchyRss = require('./plugins/metalsmith-hierarchy-rss');
1921
const headings = require('./plugins/metalsmith-headings');
@@ -115,13 +117,8 @@ CB.use(hierarchyRss({
115117
CB.use(timer('CB: Hierarchy RSS'))
116118

117119
// Filter unmodified files
118-
CB.use(incremental({
119-
plugin: 'filter',
120-
rename: {
121-
from: /.md$/,
122-
to: '.html',
123-
}
124-
}))
120+
CB.use(reduce())
121+
CB.use(timer('CB: Reduce'))
125122

126123
//
127124
// Slow Plugins
@@ -184,26 +181,27 @@ CB.use(timer('CB: Layouts'))
184181
//
185182

186183
// Restore unmodified files
187-
CB.use(incremental({
188-
plugin: 'cache'
189-
}))
184+
CB.use(restore())
185+
CB.use(timer('CB: Reduce'))
190186

191187
// Enable watching
192188
if(process.env.NODE_ENV === 'development') {
189+
/*
193190
CB.use(incremental({
194191
plugin: 'watch',
195192
paths: {
196-
'./layouts/*': '*',
193+
'layouts/*': '*',
197194
}
198-
/*
199-
paths: {
200-
'./pages/*': '*',
201-
'./layouts/*': '*',
202-
'./scss/*': '*',
203-
'./js/*': '*',
204-
},
205-
*/
206195
}))
196+
*/
197+
CB.use(
198+
watch({
199+
paths: {
200+
'pages/**/*': '**/*.md',
201+
'layouts/**/*': '**/*.pug',
202+
},
203+
})
204+
)
207205
}
208206

209207
// Search Indexing
@@ -227,7 +225,9 @@ if(process.env.NODE_ENV == "pdf") {
227225

228226
// In case you have restored all files with cache plugin
229227
// call filter plugin as last middleware
228+
/*
230229
CB.use(incremental({ plugin: 'filter' }))
230+
*/
231231

232232
// Serve
233233
if(process.env.NODE_ENV == "development") {
@@ -248,7 +248,8 @@ if(process.env.NODE_ENV === 'development') {
248248
AB.use(
249249
watch({
250250
paths: {
251-
"js/**/*": "**/*.js",
251+
'js/**/*': '**/*.js',
252+
'scss/**/*': '**/*.scss',
252253
},
253254
})
254255
)

package-lock.json

-38
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
"metalsmith-assets": "^0.1.0",
4747
"metalsmith-branch": "0.0.5",
4848
"metalsmith-headings": "^0.2.0",
49-
"metalsmith-incremental": "^0.1.1",
5049
"metalsmith-layouts": "^1.8.1",
5150
"metalsmith-markdownit": "^0.4.0",
5251
"metalsmith-permalinks": "^0.5.0",

plugins/metalsmith-revision.js

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
const resolve = require('path').resolve;
2+
const relative = require('path').relative;
3+
const fs = require('fs');
4+
const crypto = require('crypto');
5+
//const recurse = require('recursive-readdir');
6+
7+
function checksum (str, algorithm, encoding) {
8+
return crypto
9+
.createHash(algorithm || 'md5')
10+
.update(str, 'utf8')
11+
.digest(encoding || 'hex')
12+
}
13+
14+
const default_options = { layout: false, layoutDir: './layouts' }
15+
16+
function plugin(opts) {
17+
const options = Object.assign({}, default_options, opts)
18+
return (files, metalsmith, done) => {
19+
20+
const configPath = resolve(metalsmith._directory, '.revision')
21+
const hash_table = { layouts: {}, src: {} }
22+
const layoutDirectory = resolve(metalsmith._directory, options.layoutDir)
23+
const revision = fs.existsSync(configPath)
24+
? JSON.parse(fs.readFileSync(configPath, 'utf-8'))
25+
: null;
26+
27+
if(revision == null) {
28+
fs.writeFileSync(configPath, JSON.stringify(hash_table), 'utf-8')
29+
done();
30+
return;
31+
}
32+
33+
if(!metalsmith.revision) {
34+
metalsmith.revision = {};
35+
}
36+
37+
metalsmith.revision.cachedFiles = Object.assign({}, files);
38+
39+
Object.keys(files).forEach(file => {
40+
const hash = checksum(files[file].contents)
41+
hash_table.src[file] = hash
42+
if(revision.src[file] === hash) {
43+
delete files[file];
44+
}
45+
});
46+
47+
console.log('Updating %s files', Object.keys(files).length);
48+
49+
fs.writeFileSync(configPath, JSON.stringify(hash_table), 'utf-8')
50+
done()
51+
52+
/*
53+
if(options.layout){
54+
recurse(
55+
layoutDirectory
56+
, (err, layouts) => {
57+
if(err) throw(err)
58+
layouts.map(l => {
59+
const path = relative(layoutDirectory, l)
60+
const content = fs.readFileSync(l, 'utf-8')
61+
const hash = checksum(content)
62+
hash_table.layouts[path] = hash
63+
})
64+
65+
Object.keys(files).forEach(file => {
66+
const hash = checksum(files[file].contents)
67+
hash_table.src[file] = hash
68+
if(
69+
(revision !== null && revision.src[file] === hash) &&
70+
( (!options.layout) ||
71+
(options.layout && revision.layouts[files[file].layout] === hash_table.layouts[files[file].layout]) ) )
72+
delete files[file]
73+
})
74+
75+
fs.writeFileSync(configPath, JSON.stringify(hash_table), 'utf-8')
76+
done()
77+
})
78+
}
79+
else{
80+
Object.keys(files).forEach(file => {
81+
const hash = checksum(files[file].contents)
82+
hash_table.src[file] = hash
83+
if(
84+
(revision !== null && revision.src[file] === hash) &&
85+
( (!options.layout) ||
86+
(options.layout && revision.layouts[files[file].layout] === hash_table.layouts[files[file].layout]) ) )
87+
delete files[file]
88+
})
89+
90+
fs.writeFileSync(configPath, JSON.stringify(hash_table), 'utf-8')
91+
done()
92+
}
93+
*/
94+
}
95+
}
96+
97+
function restore(opts) {
98+
const options = Object.assign({}, default_options, opts)
99+
return (files, metalsmith, done) => {
100+
files = metalsmith.revision.cachedFiles;
101+
console.log('Restored %s files.', Object.keys(files).length);
102+
done();
103+
}
104+
}
105+
106+
module.exports = {
107+
reduce: plugin,
108+
restore: restore
109+
};

0 commit comments

Comments
 (0)