1+ let { FileFinder } = require ( "./util.js" ) ;
12let { readFile, stat } = require ( "node:fs/promises" ) ;
23let path = require ( "node:path" ) ;
3- let { FileFinder } = require ( "./lib.js" ) ;
4- /** @import {
5- * Config,
6- * AssetManager,
7- * FaucetPlugin,
8- * FaucetPluginOptions,
9- * FaucetPluginFunc,
10- * CompactorMap,
11- * WriteFileOpts,
12- * ProcessFile
13- * } from "./types.ts"
14- **/
154
165module . exports = {
176 key : "static" ,
@@ -49,33 +38,18 @@ function makeCopier(copyConfig, assetManager, { compact } = {}) {
4938 filter : copyConfig . filter
5039 } ) ;
5140 let { fingerprint } = copyConfig ;
52- let compactors = determineCompactors ( compact , copyConfig ) ;
41+ let compactors = ( compact && copyConfig . compact ) || { } ;
5342
54- return filepaths => {
55- return Promise . all ( [
43+ return async filepaths => {
44+ let [ filenames , targetDir ] = await Promise . all ( [
5645 ( filepaths ? fileFinder . match ( filepaths ) : fileFinder . all ( ) ) ,
5746 determineTargetDir ( source , target )
58- ] ) . then ( ( [ fileNames , targetDir ] ) => {
59- return processFiles ( fileNames , {
60- assetManager, source, target, targetDir, compactors, fingerprint
61- } ) ;
62- } ) ;
63- } ;
64- }
65-
66- /**
67- * Determine which compactors should be used
68- *
69- * @param {boolean | undefined } compact
70- * @param {Config } copyConfig
71- * @returns {CompactorMap }
72- */
73- function determineCompactors ( compact , copyConfig ) {
74- if ( ! compact ) {
75- return { } ;
76- }
47+ ] ) ;
7748
78- return copyConfig . compact || { } ;
49+ return Promise . all ( filenames . map ( filename => processFile ( filename , {
50+ assetManager, source, target, targetDir, compactors, fingerprint
51+ } ) ) ) ;
52+ } ;
7953}
8054
8155/**
@@ -86,63 +60,54 @@ function determineCompactors(compact, copyConfig) {
8660 * @param {string } target
8761 * @returns {Promise<string> }
8862 */
89- function determineTargetDir ( source , target ) {
90- return stat ( source ) .
91- then ( results => results . isDirectory ( ) ? target : path . dirname ( target ) ) ;
63+ async function determineTargetDir ( source , target ) {
64+ let results = await stat ( source ) ;
65+ return results . isDirectory ( ) ? target : path . dirname ( target ) ;
9266}
9367
9468/**
95- * @param {string[] } fileNames
96- * @param {ProcessFile } config
97- * @returns {Promise<unknown> }
98- */
99- function processFiles ( fileNames , config ) {
100- return Promise . all ( fileNames . map ( fileName => processFile ( fileName , config ) ) ) ;
101- }
102-
103- /**
104- * @param {string } fileName
69+ * @param {string } filename
10570 * @param {ProcessFile } opts
10671 * @returns {Promise<unknown> }
10772 */
108- async function processFile ( fileName ,
73+ async function processFile ( filename ,
10974 { source, target, targetDir, fingerprint, assetManager, compactors } ) {
110- let sourcePath = path . join ( source , fileName ) ;
111- let targetPath = path . join ( target , fileName ) ;
75+ let sourcePath = path . join ( source , filename ) ;
76+ let targetPath = path . join ( target , filename ) ;
11277
11378 try {
11479 var content = await readFile ( sourcePath ) ; // eslint-disable-line no-var
11580 } catch ( err ) {
116- // @ts -ignore
81+ // @ts -expect-error TS2345
11782 if ( err . code !== "ENOENT" ) {
11883 throw err ;
11984 }
12085 console . error ( `WARNING: \`${ sourcePath } \` no longer exists` ) ;
12186 return ;
12287 }
12388
124- let type = determineFileType ( sourcePath ) ;
125- if ( type && compactors [ type ] ) {
126- let compactor = compactors [ type ] ;
89+ let fileExtension = path . extname ( sourcePath ) . substr ( 1 ) . toLowerCase ( ) ;
90+ if ( fileExtension && compactors [ fileExtension ] ) {
91+ let compactor = compactors [ fileExtension ] ;
12792 content = await compactor ( content ) ;
12893 }
12994
130- /**
131- * @type WriteFileOpts
132- */
95+ /** @type WriteFileOpts */
13396 let options = { targetDir } ;
13497 if ( fingerprint !== undefined ) {
13598 options . fingerprint = fingerprint ;
13699 }
137100 return assetManager . writeFile ( targetPath , content , options ) ;
138101}
139102
140- /**
141- * The filetype is the lower case file extension
142- *
143- * @param {string } sourcePath
144- * @returns {string }
145- */
146- function determineFileType ( sourcePath ) {
147- return path . extname ( sourcePath ) . substr ( 1 ) . toLowerCase ( ) ;
148- }
103+ /** @import {
104+ * Config,
105+ * AssetManager,
106+ * FaucetPlugin,
107+ * FaucetPluginOptions,
108+ * FaucetPluginFunc,
109+ * CompactorMap,
110+ * WriteFileOpts,
111+ * ProcessFile
112+ * } from "./types.ts"
113+ **/
0 commit comments