1 | 'use strict';
|
2 |
|
3 | const fs = require('fs');
|
4 | const path = require('path');
|
5 | const {
|
6 | extendDefaultPlugins,
|
7 | optimize,
|
8 | createContentItem,
|
9 | } = require('./svgo.js');
|
10 |
|
11 | const importConfig = async configFile => {
|
12 | try {
|
13 | const config = require(configFile);
|
14 | if (config == null || typeof config !== 'object' || Array.isArray(config)) {
|
15 | throw Error(`Invalid config file "${configFile}"`);
|
16 | }
|
17 | return config;
|
18 | } catch (error) {
|
19 | if (error.code === 'MODULE_NOT_FOUND') {
|
20 | return null;
|
21 | }
|
22 | throw error;
|
23 | }
|
24 | };
|
25 |
|
26 | const loadConfig = async (configFile, cwd = process.cwd()) => {
|
27 | if (configFile != null) {
|
28 | if (path.isAbsolute(configFile)) {
|
29 | return await importConfig(configFile);
|
30 | } else {
|
31 | return await importConfig(path.join(cwd, configFile));
|
32 | }
|
33 | }
|
34 | let dir = cwd;
|
35 | while (true) {
|
36 | try {
|
37 | const file = path.join(dir, "svgo.config.js");
|
38 | const stats = await fs.promises.stat(file);
|
39 | if (stats.isFile()) {
|
40 | return await importConfig(file);
|
41 | }
|
42 | } catch {}
|
43 | const parent = path.dirname(dir);
|
44 | if (dir === parent) {
|
45 | return null;
|
46 | }
|
47 | dir = parent;
|
48 | }
|
49 | };
|
50 |
|
51 | exports.loadConfig = loadConfig;
|
52 | exports.extendDefaultPlugins = extendDefaultPlugins;
|
53 | exports.optimize = optimize;
|
54 | exports.createContentItem = createContentItem;
|