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