diff options
author | eric <thul.eric@gmail.com> | 2016-06-12 16:06:34 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-06-12 16:06:34 -0400 |
commit | 27638f63256b6d38745eef251b3327536766e7c3 (patch) | |
tree | 75e135abd95c9fee60ef9d9ba7d35440fd2c1ddf /src/PsModuleMap.js | |
parent | 7243be70a2163be2230a5f2739768137305a24ef (diff) | |
parent | 0b853815ef14d35cedebc2c7806fd2f9ff9d5ab5 (diff) | |
download | purs-loader-27638f63256b6d38745eef251b3327536766e7c3.tar.gz purs-loader-27638f63256b6d38745eef251b3327536766e7c3.tar.zst purs-loader-27638f63256b6d38745eef251b3327536766e7c3.zip |
Merge pull request #61 from ethul/topic/issues
Reduce building of PureScript module map
Diffstat (limited to 'src/PsModuleMap.js')
-rw-r--r-- | src/PsModuleMap.js | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/src/PsModuleMap.js b/src/PsModuleMap.js new file mode 100644 index 0000000..2193f02 --- /dev/null +++ b/src/PsModuleMap.js | |||
@@ -0,0 +1,66 @@ | |||
1 | 'use strict'; | ||
2 | |||
3 | const path = require('path'); | ||
4 | |||
5 | const Promise = require('bluebird'); | ||
6 | |||
7 | const fs = Promise.promisifyAll(require('fs')); | ||
8 | |||
9 | const globby = require('globby'); | ||
10 | |||
11 | const debug = require('debug')('purs-loader') | ||
12 | |||
13 | const srcModuleRegex = /(?:^|\n)module\s+([\w\.]+)/i | ||
14 | |||
15 | function match(str) { | ||
16 | const matches = str.match(srcModuleRegex); | ||
17 | return matches && matches[1]; | ||
18 | } | ||
19 | module.exports.match = match; | ||
20 | |||
21 | function makeMapEntry(filePurs) { | ||
22 | const dirname = path.dirname(filePurs); | ||
23 | |||
24 | const basename = path.basename(filePurs, '.purs'); | ||
25 | |||
26 | const fileJs = path.join(dirname, `${basename}.js`); | ||
27 | |||
28 | const result = Promise.props({ | ||
29 | filePurs: fs.readFileAsync(filePurs, 'utf8'), | ||
30 | fileJs: fs.readFileAsync(fileJs, 'utf8').catch(() => undefined) | ||
31 | }).then(fileMap => { | ||
32 | const sourcePurs = fileMap.filePurs; | ||
33 | |||
34 | const sourceJs = fileMap.fileJs; | ||
35 | |||
36 | const moduleName = match(sourcePurs); | ||
37 | |||
38 | const map = {}; | ||
39 | |||
40 | map[moduleName] = map[moduleName] || {}; | ||
41 | |||
42 | map[moduleName].src = path.resolve(filePurs); | ||
43 | |||
44 | if (sourceJs) { | ||
45 | map[moduleName].ffi = path.resolve(fileJs); | ||
46 | } | ||
47 | |||
48 | return map; | ||
49 | }); | ||
50 | |||
51 | return result; | ||
52 | } | ||
53 | module.exports.makeMapEntry = makeMapEntry; | ||
54 | |||
55 | function makeMap(src) { | ||
56 | debug('loading PureScript source and FFI files from %o', src); | ||
57 | |||
58 | const globs = [].concat(src); | ||
59 | |||
60 | return globby(globs).then(paths => | ||
61 | Promise.all(paths.map(makeMapEntry)).then(result => | ||
62 | result.reduce(Object.assign, {}) | ||
63 | ) | ||
64 | ); | ||
65 | } | ||
66 | module.exports.makeMap = makeMap; | ||