1- 'use strict' ;
2-
3- const { join, sep } = require ( 'path' ) ;
4- const Promise = require ( 'bluebird' ) ;
5- const File = require ( './file' ) ;
6- const { Pattern, createSha1Hash } = require ( 'hexo-util' ) ;
7- const { createReadStream, readdir, stat, watch } = require ( 'hexo-fs' ) ;
8- const { magenta } = require ( 'picocolors' ) ;
9- const { EventEmitter } = require ( 'events' ) ;
10- const { isMatch, makeRe } = require ( 'micromatch' ) ;
1+ import { join , sep } from 'path' ;
2+ import BlueBirdPromise from 'bluebird' ;
3+ import File from './file' ;
4+ import { Pattern , createSha1Hash } from 'hexo-util' ;
5+ import { createReadStream , readdir , stat , watch } from 'hexo-fs' ;
6+ import { magenta } from 'picocolors' ;
7+ import { EventEmitter } from 'events' ;
8+ import { isMatch , makeRe } from 'micromatch' ;
119
1210const defaultPattern = new Pattern ( ( ) => ( { } ) ) ;
1311
12+ interface Processor {
13+ pattern : Pattern ;
14+ process : ( file : File ) => void ;
15+ }
16+
1417class Box extends EventEmitter {
15- constructor ( ctx , base , options ) {
18+ public options : any ;
19+ public context : any ;
20+ public base : any ;
21+ public processors : Processor [ ] ;
22+ public _processingFiles : any ;
23+ public watcher : any ;
24+ public Cache : any ;
25+ // TODO: replace runtime class _File
26+ public File : any ;
27+ public ignore : any ;
28+ public source : any ;
29+ public emit : any ;
30+ public ctx : any ;
31+
32+ constructor ( ctx , base , options ?: object ) {
1633 super ( ) ;
1734
1835 this . options = Object . assign ( {
@@ -40,10 +57,13 @@ class Box extends EventEmitter {
4057 this . ignore = targets ;
4158 this . options . ignored = targets . map ( s => toRegExp ( ctx , s ) ) . filter ( x => x ) ;
4259 }
60+
4361 _createFileClass ( ) {
4462 const ctx = this . context ;
4563
4664 class _File extends File {
65+ public box : Box ;
66+
4767 render ( options ) {
4868 return ctx . render . render ( {
4969 path : this . source
@@ -100,7 +120,7 @@ class Box extends EventEmitter {
100120 } ) ) ;
101121 }
102122
103- process ( callback ) {
123+ process ( callback ? ) {
104124 const { base, Cache, context : ctx } = this ;
105125
106126 return stat ( base ) . then ( stats => {
@@ -121,7 +141,7 @@ class Box extends EventEmitter {
121141
122142 _processFile ( type , path ) {
123143 if ( this . _processingFiles [ path ] ) {
124- return Promise . resolve ( ) ;
144+ return BlueBirdPromise . resolve ( ) ;
125145 }
126146
127147 this . _processingFiles [ path ] = true ;
@@ -133,7 +153,7 @@ class Box extends EventEmitter {
133153 path
134154 } ) ;
135155
136- return Promise . reduce ( this . processors , ( count , processor ) => {
156+ return BlueBirdPromise . reduce ( this . processors , ( count , processor ) => {
137157 const params = processor . pattern . match ( path ) ;
138158 if ( ! params ) return count ;
139159
@@ -144,7 +164,7 @@ class Box extends EventEmitter {
144164 type
145165 } ) ;
146166
147- return Reflect . apply ( Promise . method ( processor . process ) , ctx , [ file ] )
167+ return Reflect . apply ( BlueBirdPromise . method ( processor . process ) , ctx , [ file ] )
148168 . thenReturn ( count + 1 ) ;
149169 } , 0 ) . then ( count => {
150170 if ( count ) {
@@ -164,7 +184,7 @@ class Box extends EventEmitter {
164184
165185 watch ( callback ) {
166186 if ( this . isWatching ( ) ) {
167- return Promise . reject ( new Error ( 'Watcher has already started.' ) ) . asCallback ( callback ) ;
187+ return BlueBirdPromise . reject ( new Error ( 'Watcher has already started.' ) ) . asCallback ( callback ) ;
168188 }
169189
170190 const { base } = this ;
@@ -218,7 +238,7 @@ function getHash(path) {
218238 const src = createReadStream ( path ) ;
219239 const hasher = createSha1Hash ( ) ;
220240
221- const finishedPromise = new Promise ( ( resolve , reject ) => {
241+ const finishedPromise = new BlueBirdPromise ( ( resolve , reject ) => {
222242 src . once ( 'error' , reject ) ;
223243 src . once ( 'end' , resolve ) ;
224244 } ) ;
@@ -247,9 +267,9 @@ function isIgnoreMatch(path, ignore) {
247267}
248268
249269function readDirWalker ( ctx , base , results , ignore , prefix ) {
250- if ( isIgnoreMatch ( base , ignore ) ) return Promise . resolve ( ) ;
270+ if ( isIgnoreMatch ( base , ignore ) ) return BlueBirdPromise . resolve ( ) ;
251271
252- return Promise . map ( readdir ( base ) . catch ( err => {
272+ return BlueBirdPromise . map ( readdir ( base ) . catch ( err => {
253273 ctx . log . error ( { err } , 'Failed to read directory: %s' , base ) ;
254274 if ( err && err . code === 'ENOENT' ) return [ ] ;
255275 throw err ;
@@ -272,4 +292,4 @@ function readDirWalker(ctx, base, results, ignore, prefix) {
272292 } ) ;
273293}
274294
275- module . exports = Box ;
295+ export = Box ;
0 commit comments