Skip to content

Commit

Permalink
fix(plugin): sort matches found by glob
Browse files Browse the repository at this point in the history
  • Loading branch information
mohatt committed Mar 5, 2021
1 parent e5cc225 commit a561b59
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 34 deletions.
19 changes: 19 additions & 0 deletions src/files/base.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Promise } from 'bluebird'
import path from 'path'
import { SUPPORTS } from './index'
import { Postbuild } from '../postbuild'
import { Tasks } from '../tasks'
import { Filesystem } from '../filesystem'
Expand Down Expand Up @@ -42,6 +43,24 @@ export abstract class File {
this.relative = rel
}

/**
* Creates a new file instance based on the given extension
*/
static factory = (ext: string, rel: string, postbuild: Postbuild): File => {
if (!(ext in SUPPORTS)) {
ext = '*'
}
// @ts-expect-error
return new SUPPORTS[ext](rel, postbuild)
}

/**
* Checks if a file extension is supported
*/
static supports = (ext: string): boolean => {
return ext in SUPPORTS
}

/**
* Returns an object to be passed to event callbacks
*/
Expand Down
13 changes: 10 additions & 3 deletions src/files/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
export { File } from './base'
export { FileGeneric } from './generic'
export { FileHtml } from './html'
import { File } from './base'
import { FileGeneric } from './generic'
import { FileHtml } from './html'

export const SUPPORTS: { [ext: string]: typeof File } = {
'*': FileGeneric,
html: FileHtml
}

export { File, FileGeneric, FileHtml }
27 changes: 9 additions & 18 deletions src/postbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { Promise } from 'bluebird'
import path from 'path'
import _ from 'lodash'
import { Filesystem } from './filesystem'
import { ITask, ITaskOptions, Tasks } from './tasks'
import { DEFAULTS, IOptions, schema } from './options'
import { CORE_TASKS, debug, ERROR_MAP } from './common'
import { Tasks, ITask, ITaskOptions } from './tasks'
import { File } from './files'
import { DEFAULTS, schema, IOptions } from './options'
import { CORE_TASKS, ERROR_MAP, debug } from './common'
import type { GatsbyJoi, GatsbyNodeArgs, GatsbyPluginOptions } from './gatsby'
import { File, FileGeneric, FileHtml } from './files'

/**
* Interface for the main postbuild object thats is passed
Expand Down Expand Up @@ -152,10 +152,7 @@ export class Postbuild {
.then(filenames => {
for (const ext in filenames) {
status.total += filenames[ext].length
files[ext] = filenames[ext].map(file =>
ext === 'html'
? new FileHtml(file, this)
: new FileGeneric(file, this))
files[ext] = filenames[ext].map(file => File.factory(ext, file, this))
}
})

Expand All @@ -169,16 +166,10 @@ export class Postbuild {
async function runExtension (ext: string, parallel = true): Promise<void> {
if (parallel) {
await Promise.map(files[ext], (file, i) => {
return file.read().then(() => {
tick('read')
return file.process()
}).then(() => {
tick('process')
return file.write()
}).then(() => {
tick('write')
delete files[ext][i]
})
return file.read()
.then(() => { tick('read'); return file.process() })
.then(() => { tick('process'); return file.write() })
.then(() => { tick('write'); delete files[ext][i] })
}, concLimit)
return
}
Expand Down
30 changes: 17 additions & 13 deletions src/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { Promise } from 'bluebird'
import path from 'path'
import _ from 'lodash'
import { Filesystem } from './filesystem'
import { IOptions } from './options'
import { File } from './files'
import { createDebug, PostbuildError } from './common'
import type { File, FileGeneric, FileHtml } from './files'
import type { FileGeneric, FileHtml } from './files'
import type { Node as parse5Node } from 'parse5'
import type { IOptions } from './options'
import type { GatsbyJoi } from './gatsby'
import type { IPostbuildArgs } from './postbuild'
const debug = createDebug('tasks')
Expand Down Expand Up @@ -211,21 +212,26 @@ export class Tasks {
return res
}, {})
const files: Tasks['fileEventsMap'] = {}
const extFiles: { [ext: string]: string[] } = {}
const filesOrder: { [file: string]: number } = {}
const result: { [ext: string]: string[] } = {}
return Promise
.map(Object.keys(extensions), ext =>
.map(Object.keys(extensions), (ext, i) =>
this.fs.glob(ext.indexOf('/') === 0 ? ext.slice(1) : `**/*.${ext}`, { nodir: true })
.then(matchs => matchs.forEach(f => {
if (this.options.ignore.includes(f)) return
files[f] = (files[f] ||= []).concat(extensions[ext])
filesOrder[f] = Math.min(i, filesOrder[f] ||= Infinity)
})))
.then(() => {
for (const f in files) {
const sortedFiles = Object.entries(filesOrder).sort(([,a], [,b]) => {
return a - b
})
for (const [f] of sortedFiles) {
files[f] = files[f].filter(([task, ext]) => {
if (this.options[task.id].ignore.includes(f)) return false
if (ext.indexOf('/') === 0) ext = 'unknown'
extFiles[ext] ??= []
if (!extFiles[ext].includes(f)) extFiles[ext].push(f)
if (!File.supports(ext)) ext = 'unknown'
result[ext] ??= []
if (!result[ext].includes(f)) result[ext].push(f)
return true
})
if (files[f].length === 0) {
Expand All @@ -234,7 +240,7 @@ export class Tasks {
}
this.fileEventsMap = files
debug('Updated file-events map', this.fileEventsMap)
return extFiles
return result
})
}

Expand Down Expand Up @@ -282,10 +288,8 @@ export class Tasks {
const file = payload?.file as File | undefined
if (file !== undefined) {
const fileEvents = this.fileEventsMap[file.relative] ?? []
for (const [task, ext] of fileEvents) {
if ((type === ext || (type === 'glob' && ext.indexOf('/') === 0)) && event in task.api.events[ext]) {
events.push([task, ext])
}
for (const fe of fileEvents) {
if (event in fe[0].api.events[fe[1]]) events.push(fe)
}
} else {
for (const task of this.tasks) {
Expand Down

0 comments on commit a561b59

Please # to comment.