-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
50 lines (44 loc) · 1.15 KB
/
index.js
File metadata and controls
50 lines (44 loc) · 1.15 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
'use strict';
var cjs = require('pure-cjs');
var relative = require('path').relative;
var Promise = require('davy');
var whenReadFile = Promise.wrap(require('fs').readFile);
var src = require('aster-src');
module.exports = function (options) {
if (typeof options === 'string') {
options = {input: options};
}
if (!options.input) {
throw new Error('Please specify main file with `input` option');
}
return function (files) {
return files.toArray().flatMap(function (filesArray) {
var filesMap = filesArray.reduce(function (obj, file) {
obj[relative('.', file.loc.source)] = file.program;
return obj;
}, {});
options.getFileAST = function (options) {
var path = relative('.', options.source);
if (filesMap[path]) {
return Promise.resolve(filesMap[path]);
} else {
return new Promise(function (resolve, reject) {
src(path)
.concatAll()
.pluck('program')
.subscribe(resolve, reject);
});
}
};
return cjs.transformAST(options).then(function (result) {
return {
type: 'File',
program: result.ast,
loc: {
source: result.options.output
}
};
});
});
}
};