Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

style: prettier #3719

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
15 changes: 1 addition & 14 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,4 @@
{
"extends": "hexo",
"root": true,
"rules": {
"one-var": 0,
"operator-linebreak": [
2,
"after"
],
"comma-spacing": [
2,
{
"after": true
}
]
}
"root": true
}
219 changes: 136 additions & 83 deletions lib/box/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ const defaultPattern = new Pattern(() => ({}));
function Box(ctx, base, options) {
Reflect.apply(EventEmitter, this, []);

this.options = Object.assign({
persistent: true
}, options);
this.options = Object.assign(
{
persistent: true
},
options
);

if (!base.endsWith(sep)) {
base += sep;
Expand Down Expand Up @@ -44,7 +47,8 @@ function getHash(path) {
const src = fs.createReadStream(path);
const hasher = new HashStream();

src.pipe(hasher)
src
.pipe(hasher)
.on('finish', () => {
resolve(hasher.read().toString('hex'));
})
Expand All @@ -69,15 +73,23 @@ Box.prototype._createFileClass = function() {
options = {};
}

return ctx.render.render({
path: this.source
}, options).asCallback(callback);
return ctx.render
.render(
{
path: this.source
},
options
)
.asCallback(callback);
};

_File.prototype.renderSync = function(options) {
return ctx.render.renderSync({
path: this.source
}, options);
return ctx.render.renderSync(
{
path: this.source
},
options
);
};

return _File;
Expand Down Expand Up @@ -105,16 +117,24 @@ Box.prototype._readDir = function(base, fn, prefix = '') {
return Promise.resolve('Ignoring dir.');
}

return fs.readdir(base).map(path => fs.stat(join(base, path)).then(stats => {
if (stats.isDirectory()) {
return this._readDir(join(base, path), fn, `${prefix + path}/`);
}

return this._checkFileStatus(prefix + path).then(file => fn(file).thenReturn(file));
})).catch(err => {
if (err.cause && err.cause.code === 'ENOENT') return;
throw err;
}).reduce((files, item) => files.concat(item), []);
return fs
.readdir(base)
.map(path =>
fs.stat(join(base, path)).then(stats => {
if (stats.isDirectory()) {
return this._readDir(join(base, path), fn, `${prefix + path}/`);
}

return this._checkFileStatus(prefix + path).then(file =>
fn(file).thenReturn(file)
);
})
)
.catch(err => {
if (err.cause && err.cause.code === 'ENOENT') return;
throw err;
})
.reduce((files, item) => files.concat(item), []);
};

Box.prototype._checkFileStatus = function(path) {
Expand All @@ -135,19 +155,34 @@ Box.prototype._checkFileStatus = function(path) {
Box.prototype.process = function(callback) {
const { base, Cache, context: ctx } = this;

return fs.stat(base).then(stats => {
if (!stats.isDirectory()) return;

// Check existing files in cache
const relativeBase = escapeBackslash(base.substring(ctx.base_dir.length));
const cacheFiles = Cache.filter(item => item._id.startsWith(relativeBase)).map(item => item._id.substring(relativeBase.length));

// Read files from directory
return this._readDir(base, file => this._processFile(file.type, file.path)).map(file => file.path).then(files => // Handle deleted files
Promise.filter(cacheFiles, path => !files.includes(path)).map(path => this._processFile(File.TYPE_DELETE, path)));
}).catch(err => {
if (err.cause && err.cause.code !== 'ENOENT') throw err;
}).asCallback(callback);
return fs
.stat(base)
.then(stats => {
if (!stats.isDirectory()) return;

// Check existing files in cache
const relativeBase = escapeBackslash(base.substring(ctx.base_dir.length));
const cacheFiles = Cache.filter(item =>
item._id.startsWith(relativeBase)
).map(item => item._id.substring(relativeBase.length));

// Read files from directory
return this._readDir(base, file =>
this._processFile(file.type, file.path)
)
.map(file => file.path)
.then((
files // Handle deleted files
) =>
Promise.filter(cacheFiles, path => !files.includes(path)).map(path =>
this._processFile(File.TYPE_DELETE, path)
)
);
})
.catch(err => {
if (err.cause && err.cause.code !== 'ENOENT') throw err;
})
.asCallback(callback);
};

Box.prototype.load = Box.prototype.process;
Expand All @@ -166,38 +201,49 @@ Box.prototype._processFile = function(type, path) {
path
});

return Promise.reduce(this.processors, (count, processor) => {
const params = processor.pattern.match(path);
if (!params) return count;

const file = new File({
source: join(base, path),
path,
params,
type
});

return Reflect.apply(Promise.method(processor.process), ctx, [file])
.thenReturn(count + 1);
}, 0).then(count => {
if (count) {
ctx.log.debug('Processed: %s', chalk.magenta(path));
}

this.emit('processAfter', {
type,
path
});
}).catch(err => {
ctx.log.error({err}, 'Process failed: %s', chalk.magenta(path));
}).finally(() => {
this._processingFiles[path] = false;
}).thenReturn(path);
return Promise.reduce(
this.processors,
(count, processor) => {
const params = processor.pattern.match(path);
if (!params) return count;

const file = new File({
source: join(base, path),
path,
params,
type
});

return Reflect.apply(Promise.method(processor.process), ctx, [
file
]).thenReturn(count + 1);
},
0
)
.then(count => {
if (count) {
ctx.log.debug('Processed: %s', chalk.magenta(path));
}

this.emit('processAfter', {
type,
path
});
})
.catch(err => {
ctx.log.error({ err }, 'Process failed: %s', chalk.magenta(path));
})
.finally(() => {
this._processingFiles[path] = false;
})
.thenReturn(path);
};

Box.prototype.watch = function(callback) {
if (this.isWatching()) {
return Promise.reject(new Error('Watcher has already started.')).asCallback(callback);
return Promise.reject(new Error('Watcher has already started.')).asCallback(
callback
);
}

const { base } = this;
Expand All @@ -206,28 +252,35 @@ Box.prototype.watch = function(callback) {
return escapeBackslash(path.substring(base.length));
}

return this.process().then(() => fs.watch(base, this.options)).then(watcher => {
this.watcher = watcher;

watcher.on('add', path => {
this._processFile(File.TYPE_CREATE, getPath(path));
});

watcher.on('change', path => {
this._processFile(File.TYPE_UPDATE, getPath(path));
});

watcher.on('unlink', path => {
this._processFile(File.TYPE_DELETE, getPath(path));
});

watcher.on('addDir', path => {
let prefix = getPath(path);
if (prefix) prefix += '/';

this._readDir(path, file => this._processFile(file.type, file.path), prefix);
});
}).asCallback(callback);
return this.process()
.then(() => fs.watch(base, this.options))
.then(watcher => {
this.watcher = watcher;

watcher.on('add', path => {
this._processFile(File.TYPE_CREATE, getPath(path));
});

watcher.on('change', path => {
this._processFile(File.TYPE_UPDATE, getPath(path));
});

watcher.on('unlink', path => {
this._processFile(File.TYPE_DELETE, getPath(path));
});

watcher.on('addDir', path => {
let prefix = getPath(path);
if (prefix) prefix += '/';

this._readDir(
path,
file => this._processFile(file.type, file.path),
prefix
);
});
})
.asCallback(callback);
};

Box.prototype.unwatch = function() {
Expand Down
8 changes: 5 additions & 3 deletions lib/extend/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ Console.prototype.register = function(name, desc, options, fn) {
if (typeof options === 'function') {
fn = options;

if (typeof desc === 'object') { // name, options, fn
if (typeof desc === 'object') {
// name, options, fn
options = desc;
desc = '';
} else { // name, desc, fn
} else {
// name, desc, fn
options = {};
}
} else {
Expand All @@ -52,7 +54,7 @@ Console.prototype.register = function(name, desc, options, fn) {
fn = Promise.method(fn);
}

const c = this.store[name.toLowerCase()] = fn;
const c = (this.store[name.toLowerCase()] = fn);
c.options = options;
c.desc = desc;

Expand Down
12 changes: 7 additions & 5 deletions lib/extend/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Filter.prototype.register = function(type, fn, priority) {
type = typeAlias[type] || type;
priority = priority == null ? 10 : priority;

const store = this.store[type] = this.store[type] || [];
const store = (this.store[type] = this.store[type] || []);

fn.priority = priority;
store.push(fn);
Expand All @@ -57,10 +57,12 @@ Filter.prototype.exec = function(type, data, options = {}) {

args.unshift(data);

return Promise.each(filters, filter => Reflect.apply(Promise.method(filter), ctx, args).then(result => {
args[0] = result == null ? args[0] : result;
return args[0];
})).then(() => args[0]);
return Promise.each(filters, filter =>
Reflect.apply(Promise.method(filter), ctx, args).then(result => {
args[0] = result == null ? args[0] : result;
return args[0];
})
).then(() => args[0]);
};

Filter.prototype.execSync = function(type, data, options = {}) {
Expand Down
Loading