UNPKG

1.31 kBJavaScriptView Raw
1'use strict';
2
3const fs = require('fs');
4const path = require('path');
5const {
6 extendDefaultPlugins,
7 optimize,
8 createContentItem,
9} = require('./svgo.js');
10
11const 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
26const 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
51exports.loadConfig = loadConfig;
52exports.extendDefaultPlugins = extendDefaultPlugins;
53exports.optimize = optimize;
54exports.createContentItem = createContentItem;