Skip to content

Commit

Permalink
Merge branch 'hexojs/v7.0.0' into ppwwyyxx/master
Browse files Browse the repository at this point in the history
  • Loading branch information
uiolee committed Oct 13, 2023
2 parents 7af4ff0 + 7b588e7 commit f019efc
Show file tree
Hide file tree
Showing 293 changed files with 3,450 additions and 2,012 deletions.
17 changes: 13 additions & 4 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
{
"extends": "hexo",
"root": true
}
{
"root": true,
"extends": "hexo/ts.js",
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 2020
},
"rules": {
"@typescript-eslint/no-explicit-any": 0,
"@typescript-eslint/no-var-requires": 0,
"node/no-missing-require": 0
}
}
17 changes: 15 additions & 2 deletions .github/workflows/linter.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
name: Linter

on: [push, pull_request]
on:
push:
branches:
- "master"
- "v7.0.0"
paths:
- "lib/**"
- "test/**"
- ".github/workflows/linter.yml"
pull_request:
paths:
- "lib/**"
- "test/**"
- ".github/workflows/linter.yml"

permissions:
contents: read
Expand All @@ -13,7 +26,7 @@ jobs:
- name: Use Node.js 14.x
uses: actions/setup-node@v3
with:
node-version: '14.x'
node-version: "14.x"
- name: Install Dependencies
run: npm install
- name: Lint
Expand Down
25 changes: 20 additions & 5 deletions .github/workflows/tester.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
name: Tester

on: [push, pull_request]
on:
push:
branches:
- "master"
- "v7.0.0"
paths:
- "lib/**"
- "test/**"
- "package.json"
- ".github/workflows/tester.yml"
pull_request:
paths:
- "lib/**"
- "test/**"
- "package.json"
- ".github/workflows/tester.yml"

permissions:
contents: read
Expand All @@ -11,7 +26,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
node-version: ['14.x', '16.x', '18.x']
node-version: ["14.x", "16.x", "18.x"]
fail-fast: false
steps:
- uses: actions/checkout@v3
Expand All @@ -27,13 +42,13 @@ jobs:
CI: true
coverage:
permissions:
checks: write # for coverallsapp/github-action to create new checks
contents: read # for actions/checkout to fetch code
checks: write # for coverallsapp/github-action to create new checks
contents: read # for actions/checkout to fetch code
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest]
node-version: ['14.x']
node-version: ["14.x"]
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ tmp/
.idea/
yarn.lock
package-lock.json
pnpm-lock.yaml
.nyc_output/
coverage/
.tmp*
.vscode
dist/
3 changes: 0 additions & 3 deletions .lintstagedrc

This file was deleted.

4 changes: 4 additions & 0 deletions .lintstagedrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"*.js": "eslint --fix",
"*.ts": "eslint --fix"
}
15 changes: 11 additions & 4 deletions lib/box/file.js → lib/box/file.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
'use strict';

const { readFile, readFileSync, stat, statSync } = require('hexo-fs');
import { readFile, readFileSync, stat, statSync } from 'hexo-fs';

class File {
public source: any;
public path: any;
public params: any;
public type: any;
static TYPE_CREATE: 'create';
static TYPE_UPDATE: 'update';
static TYPE_SKIP: 'skip';
static TYPE_DELETE: 'delete';

constructor({ source, path, params, type }) {
this.source = source;
this.path = path;
Expand Down Expand Up @@ -32,4 +39,4 @@ File.TYPE_UPDATE = 'update';
File.TYPE_SKIP = 'skip';
File.TYPE_DELETE = 'delete';

module.exports = File;
export = File;
90 changes: 59 additions & 31 deletions lib/box/index.js → lib/box/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
'use strict';

const { join, sep } = require('path');
const Promise = require('bluebird');
const File = require('./file');
const { Pattern, createSha1Hash } = require('hexo-util');
const { createReadStream, readdir, stat, watch } = require('hexo-fs');
const { magenta } = require('picocolors');
const { EventEmitter } = require('events');
const { isMatch, makeRe } = require('micromatch');
import { join, sep } from 'path';
import BlueBirdPromise from 'bluebird';
import File from './file';
import { Pattern, createSha1Hash } from 'hexo-util';
import { createReadStream, readdir, stat, watch } from 'hexo-fs';
import { magenta } from 'picocolors';
import { EventEmitter } from 'events';
import { isMatch, makeRe } from 'micromatch';

const defaultPattern = new Pattern(() => ({}));

interface Processor {
pattern: Pattern;
process: (file: File) => void;
}

class Box extends EventEmitter {
constructor(ctx, base, options) {
public options: any;
public context: any;
public base: any;
public processors: Processor[];
public _processingFiles: any;
public watcher: any;
public Cache: any;
// TODO: replace runtime class _File
public File: any;
public ignore: any;
public source: any;
public emit: any;
public ctx: any;

constructor(ctx, base, options?: object) {
super();

this.options = Object.assign({
Expand Down Expand Up @@ -40,10 +57,13 @@ class Box extends EventEmitter {
this.ignore = targets;
this.options.ignored = targets.map(s => toRegExp(ctx, s)).filter(x => x);
}

_createFileClass() {
const ctx = this.context;

class _File extends File {
public box: Box;

render(options) {
return ctx.render.render({
path: this.source
Expand Down Expand Up @@ -78,8 +98,9 @@ class Box extends EventEmitter {
}

_readDir(base, prefix = '') {
const { context: ctx } = this;
const results = [];
return readDirWalker(base, results, this.ignore, prefix)
return readDirWalker(ctx, base, results, this.ignore, prefix)
.return(results)
.map(path => this._checkFileStatus(path))
.map(file => this._processFile(file.type, file.path).return(file.path));
Expand All @@ -99,7 +120,7 @@ class Box extends EventEmitter {
}));
}

process(callback) {
process(callback?) {
const { base, Cache, context: ctx } = this;

return stat(base).then(stats => {
Expand All @@ -120,7 +141,7 @@ class Box extends EventEmitter {

_processFile(type, path) {
if (this._processingFiles[path]) {
return Promise.resolve();
return BlueBirdPromise.resolve();
}

this._processingFiles[path] = true;
Expand All @@ -132,7 +153,7 @@ class Box extends EventEmitter {
path
});

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

Expand All @@ -143,7 +164,7 @@ class Box extends EventEmitter {
type
});

return Reflect.apply(Promise.method(processor.process), ctx, [file])
return Reflect.apply(BlueBirdPromise.method(processor.process), ctx, [file])
.thenReturn(count + 1);
}, 0).then(count => {
if (count) {
Expand All @@ -155,15 +176,15 @@ class Box extends EventEmitter {
path
});
}).catch(err => {
ctx.log.error({err}, 'Process failed: %s', magenta(path));
ctx.log.error({ err }, 'Process failed: %s', magenta(path));
}).finally(() => {
this._processingFiles[path] = false;
}).thenReturn(path);
}

watch(callback) {
watch(callback?) {
if (this.isWatching()) {
return Promise.reject(new Error('Watcher has already started.')).asCallback(callback);
return BlueBirdPromise.reject(new Error('Watcher has already started.')).asCallback(callback);
}

const { base } = this;
Expand Down Expand Up @@ -217,7 +238,7 @@ function getHash(path) {
const src = createReadStream(path);
const hasher = createSha1Hash();

const finishedPromise = new Promise((resolve, reject) => {
const finishedPromise = new BlueBirdPromise((resolve, reject) => {
src.once('error', reject);
src.once('end', resolve);
});
Expand Down Expand Up @@ -245,23 +266,30 @@ function isIgnoreMatch(path, ignore) {
return path && ignore && ignore.length && isMatch(path, ignore);
}

function readDirWalker(base, results, ignore, prefix) {
if (isIgnoreMatch(base, ignore)) return Promise.resolve();
function readDirWalker(ctx, base, results, ignore, prefix) {
if (isIgnoreMatch(base, ignore)) return BlueBirdPromise.resolve();

return Promise.map(readdir(base).catch(err => {
return BlueBirdPromise.map(readdir(base).catch(err => {
ctx.log.error({ err }, 'Failed to read directory: %s', base);
if (err && err.code === 'ENOENT') return [];
throw err;
}), async path => {
const fullpath = join(base, path);
const stats = await stat(fullpath);
const prefixdPath = `${prefix}${path}`;
if (stats.isDirectory()) {
return readDirWalker(fullpath, results, ignore, `${prefixdPath}/`);
}
if (!isIgnoreMatch(fullpath, ignore)) {
results.push(prefixdPath);
const stats = await stat(fullpath).catch(err => {
ctx.log.error({ err }, 'Failed to stat file: %s', fullpath);
if (err && err.code === 'ENOENT') return null;
throw err;
});
const prefixPath = `${prefix}${path}`;
if (stats) {
if (stats.isDirectory()) {
return readDirWalker(ctx, fullpath, results, ignore, `${prefixPath}/`);
}
if (!isIgnoreMatch(fullpath, ignore)) {
results.push(prefixPath);
}
}
});
}

module.exports = Box;
export = Box;
Loading

0 comments on commit f019efc

Please sign in to comment.