-
-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathimport-from.js
More file actions
103 lines (85 loc) · 2.96 KB
/
Copy pathimport-from.js
File metadata and controls
103 lines (85 loc) · 2.96 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
import fs from 'node:fs';
import path from 'node:path';
import url from 'node:url';
/**
* Import Custom Properties from Object
* @param {{environmentVariables: Record<string, string>, 'environment-variables': Record<string, string>}} object
* @returns {Object}
*/
function importEnvironmentVariablesFromObject(object) {
return Object.assign(
{},
Object(object).environmentVariables || Object(object)['environment-variables'],
);
}
/**
* Import Custom Properties from JSON file
* @param {string} from
* @returns {Promise<Record<string, import('postcss-value-parser').Root>>}
*/
async function importEnvironmentVariablesFromJSONFile(from) {
const object = await readJSON(path.resolve(from));
return importEnvironmentVariablesFromObject(object);
}
/**
* Import Custom Properties from JS file
* @param {string} from
* @returns {Promise<Record<string, import('postcss-value-parser').Root>>}
*/
async function importEnvironmentVariablesFromJSFile(from) {
const object = await import(url.pathToFileURL(path.resolve(from)));
if ('default' in object) {
return importEnvironmentVariablesFromObject(object.default);
}
return importEnvironmentVariablesFromObject(object);
}
/**
* Import Custom Properties from Sources
* @param {(string|Function|Promise|{type:string,environmentVariables: Record<string, string>, 'environment-variables': Record<string, string>})[]} sources
* @returns {Promise<Record<string, import('postcss-value-parser').Root>>}
*/
export default function importEnvironmentVariablesFromSources(sources) {
return sources.map(source => {
if (source instanceof Promise) {
return source;
} else if (source instanceof Function) {
return source();
}
// read the source as an object
const opts = source === Object(source) ? source : { from: String(source) };
// skip objects with Custom Properties
if (opts.environmentVariables || opts['environment-variables']) {
return opts;
}
// source pathname
const from = String(opts.from || '');
// type of file being read from
const type = (opts.type || path.extname(from).slice(1)).toLowerCase();
return { type, from };
}).reduce(async (environmentVariables, source) => {
const { type, from } = await source;
if (type === 'js' || type === 'cjs') {
return Object.assign(environmentVariables, await importEnvironmentVariablesFromJSFile(from));
}
if (type === 'json') {
return Object.assign(environmentVariables, await importEnvironmentVariablesFromJSONFile(from));
}
return Object.assign(environmentVariables, importEnvironmentVariablesFromObject(await source));
}, {});
}
/* Helper utilities
/* ========================================================================== */
/**
* @param {string} from
* @returns {Promise<string>}
*/
const readFile = from => new Promise((resolve, reject) => {
fs.readFile(from, 'utf8', (error, result) => {
if (error) {
reject(error);
} else {
resolve(result);
}
});
});
const readJSON = async from => JSON.parse(await readFile(from));