/
svgo-node.js
60 lines (55 loc) · 1.36 KB
/
svgo-node.js
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
'use strict';
const fs = require('fs');
const path = require('path');
const {
extendDefaultPlugins,
optimize,
createContentItem,
} = require('./svgo.js');
const importConfig = async configFile => {
try {
const config = require(configFile);
if (config == null || typeof config !== 'object' || Array.isArray(config)) {
throw Error(`Invalid config file "${configFile}"`);
}
return config;
} catch (error) {
if (error.code === 'MODULE_NOT_FOUND') {
return null;
}
throw error;
}
};
const getStats = async (file) => {
try {
return await fs.promises.stat(file);
} catch {
return null;
}
}
const loadConfig = async (configFile, cwd = process.cwd()) => {
if (configFile != null) {
if (path.isAbsolute(configFile)) {
return await importConfig(configFile);
} else {
return await importConfig(path.join(cwd, configFile));
}
}
let dir = cwd;
while (true) {
const file = path.join(dir, "svgo.config.js");
const stats = await getStats(file);
if (stats && stats.isFile()) {
return await importConfig(file);
}
const parent = path.dirname(dir);
if (dir === parent) {
return null;
}
dir = parent;
}
};
exports.loadConfig = loadConfig;
exports.extendDefaultPlugins = extendDefaultPlugins;
exports.optimize = optimize;
exports.createContentItem = createContentItem;