-
Notifications
You must be signed in to change notification settings - Fork 9
Refactoring #11
base: master
Are you sure you want to change the base?
Refactoring #11
Changes from 2 commits
69d68c0
f1706b9
f85291f
8dddf25
10d77ca
dd5465c
d2d1c05
7f9e2f8
77d8ba1
343e72b
85d58b1
020659d
df11a6b
487f5fc
c724500
4919101
36c7771
3e605d3
6da4411
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
'use strict'; | ||
|
||
const crypto = require('crypto'); | ||
const fs = require('fs'); | ||
const pathlib = require('path'); | ||
|
||
class AppCacheCompiler { | ||
constructor(config) { | ||
this.publicPath = config.paths.public; | ||
this.config = config.plugins.appcache || {}; | ||
|
||
if ('appcache' in this.config) { | ||
console.warn('Warning: config.appcache is deprecated, please move it to config.plugins.appcache'); | ||
} | ||
|
||
// Defaults options | ||
this.options = { | ||
ignore: /[\\/][.]/, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
externalCacheEntries: [], | ||
network: ['*'], | ||
fallback: {}, | ||
staticRoot: '.', | ||
manifestFile: 'appcache.appcache' | ||
}; | ||
|
||
// Merge config | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove the comment pls |
||
Object.assign(this.options, this.config); | ||
|
||
this.paths = []; | ||
this.shasums = []; | ||
} | ||
|
||
onCompile(files) { | ||
let results = files.map(file => { | ||
const path = file.path; | ||
|
||
// ignore appcache file if it was iether present in assets or set in options | ||
if (!/[.]appcache$/.test(path) && !this.options.ignore.test(path) && !this.paths.includes(path)) { | ||
this.paths.push(path); | ||
this.paths.sort(); | ||
} | ||
|
||
return this._readStream(file.path); | ||
}); | ||
|
||
return Promise.all(results).catch(error => console.log(error)); | ||
} | ||
|
||
_format(obj) { | ||
return (Array.from(Object.keys(obj).sort()).map((k) => `${k} ${obj[k]}`)).join('\n'); | ||
}; | ||
|
||
_readStream(path) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The whole point of rewrite is to not have this method. Use |
||
return new Promise((resolve, reject) => { | ||
try { | ||
let shasum = crypto.createHash('sha1'); | ||
let stream = fs.ReadStream(path); | ||
|
||
stream.on('data', data => shasum.update(data)); | ||
|
||
stream.on('end', () => { | ||
this.shasums.push(shasum.digest('hex')); | ||
if (this.shasums.length === this.paths.length) { | ||
shasum = crypto.createHash('sha1'); | ||
shasum.update(this.shasums.sort().join(), 'ascii'); | ||
this._write(shasum.digest('hex')); | ||
} | ||
}); | ||
stream.on('finish', resolve); | ||
stream.on('error', reject); | ||
|
||
} catch(error) { | ||
reject(error); | ||
} | ||
}); | ||
} | ||
|
||
_write(shasum) { | ||
return fs.writeFileSync(pathlib.join(this.publicPath, this.options.manifestFile), | ||
`\ | ||
CACHE MANIFEST | ||
# ${shasum} | ||
|
||
NETWORK: | ||
${this.options.network.join('\n')} | ||
|
||
FALLBACK: | ||
${this._format(this.options.fallback)} | ||
|
||
CACHE: | ||
${(Array.from(this.paths).map((p) => `${this.options.staticRoot}/${p}`)).join('\n')} | ||
${this.options.externalCacheEntries.join('\n')}\ | ||
` | ||
); | ||
} | ||
} | ||
|
||
AppCacheCompiler.prototype.brunchPlugin = true; | ||
AppCacheCompiler.prototype.staticTargetExtension = 'html'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one is extra |
||
|
||
module.exports = AppCacheCompiler; |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,12 +8,10 @@ | |
"type": "git", | ||
"url": "[email protected]:brunch/appcache-brunch.git" | ||
}, | ||
"main": "./lib/index", | ||
"main": "index.js", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is default value. We can remove it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't quite obvious, though There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On pair with |
||
"scripts": { | ||
"prepublish": "rm -rf lib && coffee --bare --output lib/ src/", | ||
"test": "node_modules/.bin/mocha --require test/common.js" | ||
"test": "mocha --require test/common.js" | ||
}, | ||
"dependencies": { }, | ||
"devDependencies": { | ||
"mocha": "1.11.0", | ||
"chai": "1.7.0" | ||
|
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const defaultOptions =
and remove the comment