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