-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.js
More file actions
148 lines (124 loc) · 4.04 KB
/
config.js
File metadata and controls
148 lines (124 loc) · 4.04 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**
* Configuration Resolver for rw-elements-tools
*
* Resolves configuration from multiple sources with the following priority:
* 1. CLI arguments (highest priority)
* 2. Environment variables
* 3. package.json "rw-elements-tools" field
* 4. rw-elements-tools.config.js file
* 5. Default values (lowest priority)
*/
import fs from 'fs';
import path from 'path';
import { fileURLToPath, pathToFileURL } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
/**
* Default configuration values
*/
const DEFAULTS = {
packsDir: './packs',
};
/**
* Finds the project root by looking for package.json
* @returns {string} The project root directory
*/
function findProjectRoot() {
let currentDir = process.cwd();
while (currentDir !== path.dirname(currentDir)) {
if (fs.existsSync(path.join(currentDir, 'package.json'))) {
return currentDir;
}
currentDir = path.dirname(currentDir);
}
// Fallback to cwd if no package.json found
return process.cwd();
}
/**
* Reads the package.json from the project root
* @param {string} projectRoot - The project root directory
* @returns {object|null} The package.json contents or null
*/
function readPackageJson(projectRoot) {
const packageJsonPath = path.join(projectRoot, 'package.json');
try {
const content = fs.readFileSync(packageJsonPath, 'utf8');
return JSON.parse(content);
} catch {
return null;
}
}
/**
* Loads the rw-elements-tools.config.js file if it exists
* @param {string} projectRoot - The project root directory
* @returns {Promise<object|null>} The config file contents or null
*/
async function loadConfigFile(projectRoot) {
const configPaths = [
path.join(projectRoot, 'rw-elements-tools.config.js'),
path.join(projectRoot, 'rw-elements-tools.config.mjs'),
];
for (const configPath of configPaths) {
if (fs.existsSync(configPath)) {
try {
const configUrl = pathToFileURL(configPath).href;
const configModule = await import(configUrl);
return configModule.default || configModule;
} catch (err) {
console.warn(`[rw-elements-tools] Warning: Failed to load config file ${configPath}: ${err.message}`);
}
}
}
return null;
}
/**
* Resolves the full configuration from all sources
* @param {object} cliOptions - Options passed via CLI arguments
* @returns {Promise<object>} The resolved configuration
*/
export async function resolveConfig(cliOptions = {}) {
const projectRoot = findProjectRoot();
const packageJson = readPackageJson(projectRoot);
const configFile = await loadConfigFile(projectRoot);
// Build config with priority chain
const config = { ...DEFAULTS };
// 5. Default values (already set above)
// 4. Config file (rw-elements-tools.config.js)
if (configFile) {
if (configFile.packsDir) {
config.packsDir = configFile.packsDir;
}
}
// 3. package.json "rw-elements-tools" field
if (packageJson && packageJson['rw-elements-tools']) {
const pkgConfig = packageJson['rw-elements-tools'];
if (pkgConfig.packsDir) {
config.packsDir = pkgConfig.packsDir;
}
}
// 2. Environment variables
if (process.env.RW_PACKS_DIR) {
config.packsDir = process.env.RW_PACKS_DIR;
}
// 1. CLI arguments (highest priority)
if (cliOptions.packsDir || cliOptions.packs) {
config.packsDir = cliOptions.packsDir || cliOptions.packs;
}
// Resolve packsDir to absolute path from project root
if (!path.isAbsolute(config.packsDir)) {
config.packsDir = path.resolve(projectRoot, config.packsDir);
}
// Add project root and package location to config
config.projectRoot = projectRoot;
config.packageRoot = __dirname;
return config;
}
/**
* Gets the path to a resource within the package
* @param {...string} segments - Path segments relative to package root
* @returns {string} Absolute path to the resource
*/
export function getPackagePath(...segments) {
return path.join(__dirname, ...segments);
}
export default { resolveConfig, getPackagePath };