]>
git.immae.eu Git - github/fretlink/purs-loader.git/blob - src/PsModuleMap.js
3 const path
= require('path');
5 const Promise
= require('bluebird');
7 const fs
= Promise
.promisifyAll(require('fs'));
9 const globby
= require('globby');
11 const debug
= require('debug')('purs-loader')
13 const srcModuleRegex
= /(?:^|\n)module\s+([\w\.]+)/i
16 const matches
= str
.match(srcModuleRegex
);
17 return matches
&& matches
[1];
19 module
.exports
.match
= match
;
21 function makeMapEntry(filePurs
) {
22 const dirname
= path
.dirname(filePurs
);
24 const basename
= path
.basename(filePurs
, '.purs');
26 const fileJs
= path
.join(dirname
, `${basename}.js`);
28 const result
= Promise
.props({
29 filePurs: fs
.readFileAsync(filePurs
, 'utf8'),
30 fileJs: fs
.readFileAsync(fileJs
, 'utf8').catch(() => undefined)
32 const sourcePurs
= fileMap
.filePurs
;
34 const sourceJs
= fileMap
.fileJs
;
36 const moduleName
= match(sourcePurs
);
40 map
[moduleName
] = map
[moduleName
] || {};
42 map
[moduleName
].src
= path
.resolve(filePurs
);
45 map
[moduleName
].ffi
= path
.resolve(fileJs
);
53 module
.exports
.makeMapEntry
= makeMapEntry
;
55 function makeMap(src
) {
56 debug('loading PureScript source and FFI files from %o', src
);
58 const globs
= [].concat(src
);
60 return globby(globs
).then(paths
=>
61 Promise
.all(paths
.map(makeMapEntry
)).then(result
=>
62 result
.reduce(Object
.assign
, {})
66 module
.exports
.makeMap
= makeMap
;