@infcss/config 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/index.cjs +52 -0
- package/dist/index.d.cts +6 -0
- package/dist/index.d.mts +6 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.mjs +45 -0
- package/package.json +28 -0
- package/src/index.ts +53 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Jackie Wong
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const jiti = require('jiti');
|
|
4
|
+
const node_fs = require('node:fs');
|
|
5
|
+
const node_path = require('node:path');
|
|
6
|
+
|
|
7
|
+
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
|
|
8
|
+
|
|
9
|
+
const jiti__default = /*#__PURE__*/_interopDefaultCompat(jiti);
|
|
10
|
+
|
|
11
|
+
const CONFIG_FILES = [
|
|
12
|
+
"infcss.config.ts",
|
|
13
|
+
"infcss.config.js",
|
|
14
|
+
"infcss.config.mjs",
|
|
15
|
+
"infcss.config.cjs"
|
|
16
|
+
];
|
|
17
|
+
function findConfigFile(cwd = process.cwd()) {
|
|
18
|
+
let dir = cwd;
|
|
19
|
+
while (true) {
|
|
20
|
+
for (const name of CONFIG_FILES) {
|
|
21
|
+
const filePath = node_path.join(dir, name);
|
|
22
|
+
if (node_fs.existsSync(filePath)) return filePath;
|
|
23
|
+
}
|
|
24
|
+
const parent = node_path.dirname(dir);
|
|
25
|
+
if (parent === dir) break;
|
|
26
|
+
dir = parent;
|
|
27
|
+
}
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
function loadConfig(cwd = process.cwd()) {
|
|
31
|
+
const configPath = findConfigFile(cwd);
|
|
32
|
+
if (!configPath) {
|
|
33
|
+
console.warn("[infcss] \u672A\u627E\u5230\u914D\u7F6E\u6587\u4EF6\uFF0C\u4F7F\u7528\u9ED8\u8BA4\u914D\u7F6E");
|
|
34
|
+
return {};
|
|
35
|
+
}
|
|
36
|
+
const load = jiti__default(node_path.dirname(configPath), {
|
|
37
|
+
interopDefault: true,
|
|
38
|
+
cache: false,
|
|
39
|
+
requireCache: false
|
|
40
|
+
});
|
|
41
|
+
try {
|
|
42
|
+
const config = load(configPath);
|
|
43
|
+
console.log(`[infcss] \u5DF2\u52A0\u8F7D\u914D\u7F6E: ${configPath}`);
|
|
44
|
+
return config.default ?? config;
|
|
45
|
+
} catch (err) {
|
|
46
|
+
console.error(`[infcss] \u914D\u7F6E\u6587\u4EF6\u52A0\u8F7D\u5931\u8D25: ${configPath}`);
|
|
47
|
+
throw err;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
exports.findConfigFile = findConfigFile;
|
|
52
|
+
exports.loadConfig = loadConfig;
|
package/dist/index.d.cts
ADDED
package/dist/index.d.mts
ADDED
package/dist/index.d.ts
ADDED
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import jiti from 'jiti';
|
|
2
|
+
import { existsSync } from 'node:fs';
|
|
3
|
+
import { join, dirname } from 'node:path';
|
|
4
|
+
|
|
5
|
+
const CONFIG_FILES = [
|
|
6
|
+
"infcss.config.ts",
|
|
7
|
+
"infcss.config.js",
|
|
8
|
+
"infcss.config.mjs",
|
|
9
|
+
"infcss.config.cjs"
|
|
10
|
+
];
|
|
11
|
+
function findConfigFile(cwd = process.cwd()) {
|
|
12
|
+
let dir = cwd;
|
|
13
|
+
while (true) {
|
|
14
|
+
for (const name of CONFIG_FILES) {
|
|
15
|
+
const filePath = join(dir, name);
|
|
16
|
+
if (existsSync(filePath)) return filePath;
|
|
17
|
+
}
|
|
18
|
+
const parent = dirname(dir);
|
|
19
|
+
if (parent === dir) break;
|
|
20
|
+
dir = parent;
|
|
21
|
+
}
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
function loadConfig(cwd = process.cwd()) {
|
|
25
|
+
const configPath = findConfigFile(cwd);
|
|
26
|
+
if (!configPath) {
|
|
27
|
+
console.warn("[infcss] \u672A\u627E\u5230\u914D\u7F6E\u6587\u4EF6\uFF0C\u4F7F\u7528\u9ED8\u8BA4\u914D\u7F6E");
|
|
28
|
+
return {};
|
|
29
|
+
}
|
|
30
|
+
const load = jiti(dirname(configPath), {
|
|
31
|
+
interopDefault: true,
|
|
32
|
+
cache: false,
|
|
33
|
+
requireCache: false
|
|
34
|
+
});
|
|
35
|
+
try {
|
|
36
|
+
const config = load(configPath);
|
|
37
|
+
console.log(`[infcss] \u5DF2\u52A0\u8F7D\u914D\u7F6E: ${configPath}`);
|
|
38
|
+
return config.default ?? config;
|
|
39
|
+
} catch (err) {
|
|
40
|
+
console.error(`[infcss] \u914D\u7F6E\u6587\u4EF6\u52A0\u8F7D\u5931\u8D25: ${configPath}`);
|
|
41
|
+
throw err;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export { findConfigFile, loadConfig };
|
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@infcss/config",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.0.1",
|
|
5
|
+
"description": "Infcss config",
|
|
6
|
+
"main": "src/index.ts",
|
|
7
|
+
"module": "src/index.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./src/index.ts",
|
|
11
|
+
"require": "./src/index.ts"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"index.js",
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"jiti": "^2.7.0",
|
|
20
|
+
"@infcss/shared": "0.0.1"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"unbuild": "^3.6.1"
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build": "unbuild"
|
|
27
|
+
}
|
|
28
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import jiti from 'jiti'
|
|
2
|
+
import { existsSync } from 'node:fs'
|
|
3
|
+
import { join, dirname } from 'node:path'
|
|
4
|
+
|
|
5
|
+
const CONFIG_FILES = [
|
|
6
|
+
'infcss.config.ts',
|
|
7
|
+
'infcss.config.js',
|
|
8
|
+
'infcss.config.mjs',
|
|
9
|
+
'infcss.config.cjs',
|
|
10
|
+
]
|
|
11
|
+
|
|
12
|
+
/** 向上递归查找配置文件 */
|
|
13
|
+
export function findConfigFile(cwd = process.cwd()) {
|
|
14
|
+
let dir = cwd
|
|
15
|
+
|
|
16
|
+
while (true) {
|
|
17
|
+
for (const name of CONFIG_FILES) {
|
|
18
|
+
const filePath = join(dir, name)
|
|
19
|
+
if (existsSync(filePath)) return filePath
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const parent = dirname(dir)
|
|
23
|
+
if (parent === dir) break
|
|
24
|
+
dir = parent
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return null
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** 加载配置文件,找不到则返回空对象 */
|
|
31
|
+
export function loadConfig(cwd = process.cwd()) {
|
|
32
|
+
const configPath = findConfigFile(cwd)
|
|
33
|
+
|
|
34
|
+
if (!configPath) {
|
|
35
|
+
console.warn('[infcss] 未找到配置文件,使用默认配置')
|
|
36
|
+
return {}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const load = jiti(dirname(configPath), {
|
|
40
|
+
interopDefault: true,
|
|
41
|
+
cache: false,
|
|
42
|
+
requireCache: false,
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
try {
|
|
46
|
+
const config = load(configPath)
|
|
47
|
+
console.log(`[infcss] 已加载配置: ${configPath}`)
|
|
48
|
+
return config.default ?? config
|
|
49
|
+
} catch (err) {
|
|
50
|
+
console.error(`[infcss] 配置文件加载失败: ${configPath}`)
|
|
51
|
+
throw err
|
|
52
|
+
}
|
|
53
|
+
}
|