保誠-保戶業務員媒合平台
tomasysh
2022-05-25 43d0eed31f4b2a59e23c06ceba3616aac3f549f6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
'use strict';
 
const fs = require('fs');
const mime = require('mime');
 
const getPathByBasePath = require('./paths').getPathByBasePath;
 
/**
 *
 * @param {PostcssUrl~Asset} asset
 * @param {PostcssUrl~Options} options
 * @param {PostcssUrl~Dir} dir
 * @param {Function} warn
 * @returns {PostcssUrl~File}
 */
const getFile = (asset, options, dir, warn) => {
    const paths = options.basePath
        ? getPathByBasePath(options.basePath, dir.from, asset.pathname)
        : [asset.absolutePath];
    const filePath = paths.find(fs.existsSync);
 
    if (!filePath) {
        warn(`Can't read file '${paths.join()}', ignoring`);
 
        return;
    }
 
    return {
        path: filePath,
        contents: fs.readFileSync(filePath),
        mimeType: mime.getType(filePath)
    };
};
 
module.exports = getFile;
 
/**
 * @typedef {Object} PostcssUrl~File
 * @property {String} path
 * @property {Buffer} contents
 * @property {String} mimeType
 */