-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Expand file tree
/
Copy pathscript-utils.js
More file actions
52 lines (46 loc) · 1.8 KB
/
script-utils.js
File metadata and controls
52 lines (46 loc) · 1.8 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
/* @flow */
import * as fs from './fs.js';
import type {Reporter} from '../reporters/index.js';
export async function checkImportScripts(scripts: any, reporter: Reporter): Promise<any> {
if (typeof scripts === 'string' && scripts.length > 0) {
if (await fs.exists(scripts)) {
const module = require(await fs.realpath(scripts));
if (module && typeof module.scripts === 'object') {
scripts = iterateScripts(module.scripts, '', module.delimiter, reporter);
} else {
reporter &&
reporter.warn(
`Invalid scripts module: ${scripts}. The module must be exported and include a root scripts object.`,
);
}
}
}
return scripts;
}
function iterateScripts(node: Object, path: string = '', delim: string = '.', reporter: Reporter): Object {
const scripts = {};
const addScript = (key, script) => {
scripts[key] &&
reporter &&
reporter.warn(`Duplicate script key detected: ${key}. Scripts should be structured to have unique keys.`);
scripts[key] = script;
};
if (node['script'] && typeof node['script'] === 'string') {
addScript(path, node['script']); // Add script, ignore other non object keys
} else {
Object.keys(node).filter(k => typeof node[k] === 'string').forEach(k => {
if (k === 'default') {
addScript(path, node[k]);
} else {
addScript([path, k].filter(Boolean).join(delim), node[k]);
}
});
}
// Process remaining object nodes
Object.keys(node).filter(k => typeof node[k] === 'object').forEach(k => {
const nodepath = k === 'default' ? path : [path, k].filter(Boolean).join(delim);
const iteratedScripts = iterateScripts(node[k], nodepath, delim, reporter);
Object.keys(iteratedScripts).forEach(k => addScript(k, iteratedScripts[k]));
});
return scripts;
}