1- let { readFile, stat } = require ( "fs" ) . promises ;
2- let path = require ( "path" ) ;
3- let { FileFinder } = require ( "faucet-pipeline-core/lib/util/files/finder" ) ;
1+ let { readFile, stat } = require ( "node:fs/promises" ) ;
2+ let 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+ **/
415
516module . exports = {
617 key : "static" ,
718 bucket : "static" ,
819 plugin : faucetStatic
920} ;
1021
22+ /**
23+ * The static plugin copies files with an option to define compaction functions
24+ *
25+ * @type FaucetPlugin<Config>
26+ */
1127function faucetStatic ( config , assetManager , { compact } = { } ) {
1228 let copiers = config . map ( copyConfig =>
1329 makeCopier ( copyConfig , assetManager , { compact } ) ) ;
1430
1531 return filepaths => Promise . all ( copiers . map ( copy => copy ( filepaths ) ) ) ;
1632}
1733
34+ /**
35+ * Create a copier for a single configuration
36+ *
37+ * @param {Config } copyConfig
38+ * @param {AssetManager } assetManager
39+ * @param {FaucetPluginOptions } options
40+ * @returns {FaucetPluginFunc }
41+ */
1842function makeCopier ( copyConfig , assetManager , { compact } = { } ) {
1943 let source = assetManager . resolvePath ( copyConfig . source ) ;
2044 let target = assetManager . resolvePath ( copyConfig . target , {
@@ -25,47 +49,71 @@ function makeCopier(copyConfig, assetManager, { compact } = {}) {
2549 filter : copyConfig . filter
2650 } ) ;
2751 let { fingerprint } = copyConfig ;
28- let plugins = determinePlugins ( compact , copyConfig ) ;
52+ let compactors = determineCompactors ( compact , copyConfig ) ;
2953
3054 return filepaths => {
3155 return Promise . all ( [
3256 ( filepaths ? fileFinder . match ( filepaths ) : fileFinder . all ( ) ) ,
3357 determineTargetDir ( source , target )
3458 ] ) . then ( ( [ fileNames , targetDir ] ) => {
3559 return processFiles ( fileNames , {
36- assetManager, source, target, targetDir, plugins , fingerprint
60+ assetManager, source, target, targetDir, compactors , fingerprint
3761 } ) ;
3862 } ) ;
3963 } ;
4064}
4165
42- function determinePlugins ( compact , copyConfig ) {
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 ) {
4374 if ( ! compact ) {
4475 return { } ;
4576 }
4677
4778 return copyConfig . compact || { } ;
4879}
4980
50- // If `source` is a directory, `target` is used as target directory -
51- // otherwise, `target`'s parent directory is used
81+ /**
82+ * If `source` is a directory, `target` is used as target directory -
83+ * otherwise, `target`'s parent directory is used
84+ *
85+ * @param {string } source
86+ * @param {string } target
87+ * @returns {Promise<string> }
88+ */
5289function determineTargetDir ( source , target ) {
5390 return stat ( source ) .
5491 then ( results => results . isDirectory ( ) ? target : path . dirname ( target ) ) ;
5592}
5693
94+ /**
95+ * @param {string[] } fileNames
96+ * @param {ProcessFile } config
97+ * @returns {Promise<unknown> }
98+ */
5799function processFiles ( fileNames , config ) {
58100 return Promise . all ( fileNames . map ( fileName => processFile ( fileName , config ) ) ) ;
59101}
60102
103+ /**
104+ * @param {string } fileName
105+ * @param {ProcessFile } opts
106+ * @returns {Promise<unknown> }
107+ */
61108async function processFile ( fileName ,
62- { source, target, targetDir, fingerprint, assetManager, plugins } ) {
109+ { source, target, targetDir, fingerprint, assetManager, compactors } ) {
63110 let sourcePath = path . join ( source , fileName ) ;
64111 let targetPath = path . join ( target , fileName ) ;
65112
66113 try {
67114 var content = await readFile ( sourcePath ) ; // eslint-disable-line no-var
68115 } catch ( err ) {
116+ // @ts -ignore
69117 if ( err . code !== "ENOENT" ) {
70118 throw err ;
71119 }
@@ -74,18 +122,27 @@ async function processFile(fileName,
74122 }
75123
76124 let type = determineFileType ( sourcePath ) ;
77- if ( type && plugins [ type ] ) {
78- let plugin = plugins [ type ] ;
79- content = await plugin ( content ) ;
125+ if ( type && compactors [ type ] ) {
126+ let compactor = compactors [ type ] ;
127+ content = await compactor ( content ) ;
80128 }
81129
130+ /**
131+ * @type WriteFileOpts
132+ */
82133 let options = { targetDir } ;
83134 if ( fingerprint !== undefined ) {
84135 options . fingerprint = fingerprint ;
85136 }
86137 return assetManager . writeFile ( targetPath , content , options ) ;
87138}
88139
140+ /**
141+ * The filetype is the lower case file extension
142+ *
143+ * @param {string } sourcePath
144+ * @returns {string }
145+ */
89146function determineFileType ( sourcePath ) {
90147 return path . extname ( sourcePath ) . substr ( 1 ) . toLowerCase ( ) ;
91148}
0 commit comments