]>
git.immae.eu Git - github/fretlink/purs-loader.git/blob - src/purs-module-map.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;
15 const importModuleRegex
= /(?:^|\n)\s*import\s+([\w\.]+)/ig;
17 module
.exports
.matchModule
= function matchModule(str
) {
18 const matches
= str
.match(srcModuleRegex
);
19 return matches
&& matches
[1];
22 module
.exports
.matchImports
= function matchImports(str
) {
23 const matches
= str
.match(importModuleRegex
);
24 return (matches
|| []).map(a
=> a
.replace(/\n?\s*import\s+/i, ''));
27 module
.exports
.makeMapEntry
= function makeMapEntry(filePurs
) {
28 const dirname
= path
.dirname(filePurs
);
30 const basename
= path
.basename(filePurs
, '.purs');
32 const fileJs
= path
.join(dirname
, `${basename}.js`);
34 const result
= Promise
.props({
35 filePurs: fs
.readFileAsync(filePurs
, 'utf8'),
36 fileJs: fs
.readFileAsync(fileJs
, 'utf8').catch(() => undefined)
38 const sourcePurs
= fileMap
.filePurs
;
40 const sourceJs
= fileMap
.fileJs
;
42 const moduleName
= module
.exports
.matchModule(sourcePurs
);
44 const imports
= module
.exports
.matchImports(sourcePurs
);
48 map
[moduleName
] = map
[moduleName
] || {};
50 map
[moduleName
].src
= path
.resolve(filePurs
);
52 map
[moduleName
].imports
= imports
;
55 map
[moduleName
].ffi
= path
.resolve(fileJs
);
64 module
.exports
.makeMap
= function makeMap(src
) {
65 debug('loading PureScript source and FFI files from %o', src
);
67 const globs
= [].concat(src
);
69 return globby(globs
).then(paths
=>
70 Promise
.all(paths
.map(module
.exports
.makeMapEntry
)).then(result
=>
71 result
.reduce(Object
.assign
, {})