UNPKG

711 BJavaScriptView Raw
1import _ from 'underscore';
2
3import getFile from './get-file.js';
4
5const getFiles = async ({ env, files = {}, path }) => {
6 if (files[path]) return files;
7
8 files[path] = true;
9 const file = await getFile({ env, path });
10
11 files[path] = file;
12 await Promise.all(
13 _.map(file.requires, path => getFiles({ env, files, path }))
14 );
15
16 return files;
17};
18
19const walk = ({ files, path, visited = {} }) => {
20 const file = files[path];
21 if (visited[path]) return file;
22
23 visited[path] = true;
24 const graph = _.map(file.requires, path => walk({ files, path, visited }));
25 return _.unique(_.flatten(graph));
26};
27
28export default async ({ env, path }) =>
29 walk({ files: await getFiles({ env, path }), path });