1 | 'use strict'
|
2 |
|
3 | const path = require('path')
|
4 | const mm = require('micromatch')
|
5 | const klaw = require('klaw')
|
6 | const rename = require('rename-extension')
|
7 | const execute = require('./execute')
|
8 | const save = require('./save')
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | module.exports = function(routes, srcPath, distPath, opts, next) {
|
20 |
|
21 | const getHandlerFn = (filePath) => {
|
22 |
|
23 |
|
24 | const fileRoute = path.relative(srcPath, filePath)
|
25 |
|
26 |
|
27 | const matches = routes.filter((route) => mm.isMatch(fileRoute, route.path))
|
28 | const route = matches[0]
|
29 |
|
30 |
|
31 | if (route==null) return Promise.resolve()
|
32 |
|
33 |
|
34 |
|
35 | const fileSave = (() => {
|
36 |
|
37 |
|
38 | const fileDist = filePath.replace(srcPath, distPath)
|
39 |
|
40 |
|
41 | const hasFn = (typeof route.handler.out==='function')
|
42 |
|
43 | return (hasFn===true ? rename(fileDist, route.handler.out(route.opts)) : fileDist)
|
44 |
|
45 | })()
|
46 |
|
47 |
|
48 | return new Promise((resolve, reject) => {
|
49 |
|
50 |
|
51 | execute(route, fileRoute, filePath, true, (err, data) => {
|
52 |
|
53 | if (err!=null) return reject(err)
|
54 |
|
55 |
|
56 | save(fileSave, data, opts, (err) => {
|
57 |
|
58 | if (err!=null) return reject(err)
|
59 |
|
60 | resolve()
|
61 |
|
62 | })
|
63 |
|
64 | })
|
65 |
|
66 | })
|
67 |
|
68 | }
|
69 |
|
70 |
|
71 | const query = []
|
72 |
|
73 | klaw(srcPath).on('data', (file) => {
|
74 |
|
75 | const fn = getHandlerFn(file.path)
|
76 |
|
77 |
|
78 | query.push(fn)
|
79 |
|
80 | }).on('end', () => {
|
81 |
|
82 |
|
83 |
|
84 | Promise.all(query).then(() => next(), next)
|
85 |
|
86 | })
|
87 |
|
88 | } |
\ | No newline at end of file |