Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Refactoring #11

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions index.js
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 = {

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

ignore: /[\\/][.]/,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/\/\./

externalCacheEntries: [],
network: ['*'],
fallback: {},
staticRoot: '.',
manifestFile: 'appcache.appcache'
};

// Merge config

Choose a reason for hiding this comment

The 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) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The whole point of rewrite is to not have this method. Use file.data.

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';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is extra


module.exports = AppCacheCompiler;
169 changes: 0 additions & 169 deletions lib/index.js

This file was deleted.

6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@
"type": "git",
"url": "[email protected]:brunch/appcache-brunch.git"
},
"main": "./lib/index",
"main": "index.js",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is default value. We can remove it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't quite obvious, though

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On pair with index.html.

"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"
Expand Down
107 changes: 0 additions & 107 deletions src/index.coffee

This file was deleted.

Loading