Files
asciidisco.com/build/tasks/fonts.js
2023-08-01 13:49:46 +02:00

44 lines
1.5 KiB
JavaScript

const fs = require('fs')
const path = require('path')
const mkdirp = require('mkdirp')
const rimraf = require('rimraf')
const filewalker = require('filewalker')
const chalk = require('chalk')
const log = console.log;
/* Path matching functions */
// find local files that should be processed
const find_paths = font_path => {
return new Promise((resolve, reject) => {
let paths = []
filewalker(font_path)
.on('file', file => paths.push(file))
.on('done', () => resolve(paths))
.walk()
})
}
// copy files
const copy_files = (files, source, destination) => files.forEach(file => fs.createReadStream(path.join(source, file)).pipe(fs.createWriteStream(path.join(destination, file))))
module.exports = config => {
const destination = path.join(config.basepath, config.dist_path, config.font_path)
const source = path.join(config.basepath, config.font_path)
return new Promise((resolve, reject) => {
log(' ', chalk.green('>>'), 'Deleting old destination folder')
rimraf(destination, err => {
if (err) return reject(err)
log(' ', chalk.green('>>'), 'Recreating destination folder')
mkdirp(destination, async err => {
if (err) return reject(err)
try {
const files = await find_paths(source)
log(' ', chalk.green('>>'), `Copying ${files.length} files`)
copy_files(files, source, destination)
resolve(files)
} catch(err) {
reject(err)
}
})
})
})
}