|
1 | 1 | 'use strict' |
2 | 2 |
|
3 | | -const fs = require('fs-extra') |
4 | 3 | const path = require('path') |
5 | 4 | const mime = require('mime-types') |
6 | 5 | const recursiveRead = require('recursive-readdir') |
7 | | -const Handlebars = require('handlebars') |
| 6 | +const fsUtils = require('../common/fs-utils') |
| 7 | +const templateUtils = require('../common/template-utils') |
8 | 8 |
|
9 | 9 | const RDF_MIME_TYPES = require('../ldp').RDF_MIME_TYPES |
10 | 10 | const TEMPLATE_EXTENSIONS = [ '.acl', '.meta', '.json', '.hbs', '.handlebars' ] |
@@ -62,13 +62,7 @@ class AccountTemplate { |
62 | 62 | * @return {Promise} |
63 | 63 | */ |
64 | 64 | static copyTemplateDir (templatePath, accountPath) { |
65 | | - return new Promise((resolve, reject) => { |
66 | | - fs.copy(templatePath, accountPath, (error) => { |
67 | | - if (error) { return reject(error) } |
68 | | - |
69 | | - resolve() |
70 | | - }) |
71 | | - }) |
| 65 | + return fsUtils.copyTemplateDir(templatePath, accountPath) |
72 | 66 | } |
73 | 67 |
|
74 | 68 | /** |
@@ -130,58 +124,7 @@ class AccountTemplate { |
130 | 124 | */ |
131 | 125 | processAccount (accountPath) { |
132 | 126 | return this.readTemplateFiles(accountPath) |
133 | | - .then(files => { |
134 | | - return Promise.all( |
135 | | - files.map((path) => { return this.processFile(path) }) |
136 | | - ) |
137 | | - }) |
138 | | - } |
139 | | - |
140 | | - /** |
141 | | - * Reads a file, processes it (performing template substitution), and saves |
142 | | - * back the processed result. |
143 | | - * |
144 | | - * @param filePath {string} |
145 | | - * |
146 | | - * @return {Promise} |
147 | | - */ |
148 | | - processFile (filePath) { |
149 | | - return new Promise((resolve, reject) => { |
150 | | - fs.readFile(filePath, 'utf8', (error, rawSource) => { |
151 | | - if (error) { return reject(error) } |
152 | | - |
153 | | - let output = this.processTemplate(rawSource) |
154 | | - |
155 | | - fs.writeFile(filePath, output, (error) => { |
156 | | - if (error) { return reject(error) } |
157 | | - resolve() |
158 | | - }) |
159 | | - }) |
160 | | - }) |
161 | | - } |
162 | | - |
163 | | - /** |
164 | | - * Performs a Handlebars string template substitution, and returns the |
165 | | - * resulting string. |
166 | | - * |
167 | | - * @see https://www.npmjs.com/package/handlebars |
168 | | - * |
169 | | - * @param source {string} e.g. 'Hello, {{name}}' |
170 | | - * |
171 | | - * @return {string} Result, e.g. 'Hello, Alice' |
172 | | - */ |
173 | | - processTemplate (source) { |
174 | | - let template, result |
175 | | - |
176 | | - try { |
177 | | - template = Handlebars.compile(source) |
178 | | - result = template(this.substitutions) |
179 | | - } catch (error) { |
180 | | - console.log('Error processing template: ', error) |
181 | | - return source |
182 | | - } |
183 | | - |
184 | | - return result |
| 127 | + .then(files => Promise.all(files.map(path => templateUtils.processHandlebarFile(path, this.substitutions)))) |
185 | 128 | } |
186 | 129 |
|
187 | 130 | /** |
|
0 commit comments