|
1 | 1 | const fs = require('fs')
|
2 | 2 | const ejs = require('ejs')
|
3 | 3 | const path = require('path')
|
4 |
| -const walk = require('klaw-sync') |
| 4 | +const globby = require('globby') |
5 | 5 | const isBinary = require('isbinaryfile')
|
6 | 6 | const mergeDeps = require('./util/mergeDeps')
|
7 |
| -const errorParser = require('error-stack-parser') |
8 | 7 |
|
9 | 8 | const isString = val => typeof val === 'string'
|
10 | 9 | const isFunction = val => typeof val === 'function'
|
@@ -52,25 +51,22 @@ module.exports = class GeneratorAPI {
|
52 | 51 | const baseDir = extractCallDir()
|
53 | 52 | if (isString(fileDir)) {
|
54 | 53 | fileDir = path.resolve(baseDir, fileDir)
|
55 |
| - this.injectFileMiddleware(files => { |
| 54 | + this.injectFileMiddleware(async (files) => { |
56 | 55 | const data = Object.assign({
|
57 | 56 | options: this.options,
|
58 | 57 | rootOptions: this.rootOptions
|
59 | 58 | }, additionalData)
|
60 |
| - const _files = walk(fileDir, { |
61 |
| - nodir: true, |
62 |
| - filter: file => path.basename(file.path) !== '.DS_Store' |
63 |
| - }) |
64 |
| - for (const file of _files) { |
65 |
| - let filename = path.basename(file.path) |
| 59 | + const _files = await globby(['**/*'], { cwd: fileDir }) |
| 60 | + for (const rawPath of _files) { |
| 61 | + let filename = path.basename(rawPath) |
66 | 62 | // dotfiles are ignored when published to npm, therefore in templates
|
67 | 63 | // we need to use underscore instead (e.g. "_gitignore")
|
68 | 64 | if (filename.charAt(0) === '_') {
|
69 | 65 | filename = `.${filename.slice(1)}`
|
70 | 66 | }
|
71 |
| - const normalizedPath = path.join(path.dirname(file.path), filename) |
72 |
| - const targetPath = path.relative(fileDir, normalizedPath) |
73 |
| - const content = renderFile(file.path, data, ejsOptions) |
| 67 | + const targetPath = path.join(path.dirname(rawPath), filename) |
| 68 | + const sourcePath = path.resolve(fileDir, rawPath) |
| 69 | + const content = renderFile(sourcePath, data, ejsOptions) |
74 | 70 | // only set file if it's not all whitespace, or is a Buffer (binary files)
|
75 | 71 | if (Buffer.isBuffer(content) || /[^\s]/.test(content)) {
|
76 | 72 | files[targetPath] = content
|
@@ -117,8 +113,9 @@ function extractCallDir () {
|
117 | 113 | // extract api.render() callsite file location using error stack
|
118 | 114 | const obj = {}
|
119 | 115 | Error.captureStackTrace(obj)
|
120 |
| - const stack = errorParser.parse(obj) |
121 |
| - return path.dirname(stack[2].fileName) |
| 116 | + const callSite = obj.stack.split('\n')[3] |
| 117 | + const fileName = callSite.match(/\s\((.*):\d+:\d+\)$/)[1] |
| 118 | + return path.dirname(fileName) |
122 | 119 | }
|
123 | 120 |
|
124 | 121 | function renderFile (name, data, ejsOptions) {
|
|
0 commit comments