mirror of https://github.com/thumbsup/thumbsup
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
1.0 KiB
JavaScript
34 lines
1.0 KiB
JavaScript
const _ = require('lodash')
|
|
const fs = require('fs')
|
|
const debug = require('debug')('thumbsup:debug')
|
|
const Observable = require('zen-observable')
|
|
const path = require('path')
|
|
const readdir = require('fs-readdir-recursive')
|
|
|
|
exports.run = function (fileCollection, outputRoot, dryRun) {
|
|
return new Observable(observer => {
|
|
const mediaRoot = path.join(outputRoot, 'media')
|
|
const diskFiles = readdir(mediaRoot).map(f => path.join(mediaRoot, f))
|
|
const requiredFiles = []
|
|
fileCollection.forEach(f => {
|
|
Object.keys(f.output).forEach(out => {
|
|
const dest = path.join(outputRoot, f.output[out].path)
|
|
requiredFiles.push(dest)
|
|
})
|
|
})
|
|
const useless = _.difference(diskFiles, requiredFiles)
|
|
if (useless.length) {
|
|
useless.forEach(f => {
|
|
const relativePath = path.relative(outputRoot, f)
|
|
if (dryRun) {
|
|
debug(`Dry run, would delete: ${relativePath}`)
|
|
} else {
|
|
observer.next(relativePath)
|
|
fs.unlinkSync(f)
|
|
}
|
|
})
|
|
}
|
|
observer.complete()
|
|
})
|
|
}
|