Skip to content

Commit bb18199

Browse files
committed
A bit more refactoring
I propose we put utils-methods into `lib/common`, and distinguish them a bit more than just `utils`
1 parent 43b06df commit bb18199

File tree

5 files changed

+79
-75
lines changed

5 files changed

+79
-75
lines changed

lib/common/fs-utils.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
module.exports.copyTemplateDir = copyTemplateDir
2+
module.exports.processFile = processFile
3+
4+
const fs = require('fs-extra')
5+
6+
async function copyTemplateDir (templatePath, targetPath) {
7+
return new Promise((resolve, reject) => {
8+
fs.copy(templatePath, targetPath, (error) => {
9+
if (error) { return reject(error) }
10+
11+
resolve()
12+
})
13+
})
14+
}
15+
16+
async function processFile (filePath, manipulateSourceFn) {
17+
return new Promise((resolve, reject) => {
18+
fs.readFile(filePath, 'utf8', (error, rawSource) => {
19+
if (error) {
20+
return reject(error)
21+
}
22+
23+
const output = manipulateSourceFn(rawSource)
24+
25+
fs.writeFile(filePath, output, (error) => {
26+
if (error) {
27+
return reject(error)
28+
}
29+
resolve()
30+
})
31+
})
32+
})
33+
}

lib/common/template-utils.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
module.exports.processHandlebarFile = processHandlebarFile
2+
3+
const Handlebars = require('handlebars')
4+
const debug = require('../debug').errors
5+
const { processFile } = require('./fs-utils')
6+
7+
/**
8+
* Reads a file, processes it (performing template substitution), and saves
9+
* back the processed result.
10+
*
11+
* @param filePath {string}
12+
* @param substitutions {Object}
13+
*
14+
* @return {Promise}
15+
*/
16+
async function processHandlebarFile (filePath, substitutions) {
17+
return processFile(filePath, (rawSource) => processHandlebarTemplate(rawSource, substitutions))
18+
}
19+
20+
/**
21+
* Performs a Handlebars string template substitution, and returns the
22+
* resulting string.
23+
*
24+
* @see https://www.npmjs.com/package/handlebars
25+
*
26+
* @param source {string} e.g. 'Hello, {{name}}'
27+
*
28+
* @return {string} Result, e.g. 'Hello, Alice'
29+
*/
30+
function processHandlebarTemplate (source, substitutions) {
31+
try {
32+
const template = Handlebars.compile(source)
33+
return template(substitutions)
34+
} catch (error) {
35+
debug(`Error processing template: ${error}`)
36+
return source
37+
}
38+
}

lib/models/account-template.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
'use strict'
22

3-
const fs = require('fs-extra')
43
const path = require('path')
54
const mime = require('mime-types')
65
const recursiveRead = require('recursive-readdir')
7-
const utils = require('../utils')
6+
const fsUtils = require('../common/fs-utils')
7+
const templateUtils = require('../common/template-utils')
88

99
const RDF_MIME_TYPES = require('../ldp').RDF_MIME_TYPES
1010
const TEMPLATE_EXTENSIONS = [ '.acl', '.meta', '.json', '.hbs', '.handlebars' ]
@@ -62,13 +62,7 @@ class AccountTemplate {
6262
* @return {Promise}
6363
*/
6464
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)
7266
}
7367

7468
/**
@@ -130,7 +124,7 @@ class AccountTemplate {
130124
*/
131125
processAccount (accountPath) {
132126
return this.readTemplateFiles(accountPath)
133-
.then(files => Promise.all(files.map(path => utils.processHandlebarFile(path, this.substitutions))))
127+
.then(files => Promise.all(files.map(path => templateUtils.processHandlebarFile(path, this.substitutions))))
134128
}
135129

136130
/**

lib/server-config.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
const fs = require('fs-extra')
88
const path = require('path')
9-
const utils = require('./utils')
9+
const templateUtils = require('./common/template-utils')
10+
const fsUtils = require('./common/fs-utils')
1011

1112
/**
1213
* Ensures that a directory has been copied / initialized. Used to ensure that
@@ -51,8 +52,8 @@ async function ensureWelcomePage (argv) {
5152
if (!fs.existsSync(existingIndexPage)) {
5253
console.log(argv)
5354
fs.mkdirp(serverRootDir)
54-
await utils.copyTemplateDir(templates.server, serverRootDir)
55-
await utils.processHandlebarFile(existingIndexPage, {
55+
await fsUtils.copyTemplateDir(templates.server, serverRootDir)
56+
await templateUtils.processHandlebarFile(existingIndexPage, {
5657
serverName: server ? server.name : host.hostname,
5758
serverDescription: server ? server.description : '',
5859
serverLogo: server ? server.logo : '',

lib/utils.js

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,12 @@ module.exports.debrack = debrack
1313
module.exports.stripLineEndings = stripLineEndings
1414
module.exports.fullUrlForReq = fullUrlForReq
1515
module.exports.routeResolvedFile = routeResolvedFile
16-
module.exports.processHandlebarFile = processHandlebarFile
17-
module.exports.copyTemplateDir = copyTemplateDir
1816

1917
const fs = require('fs-extra')
2018
const path = require('path')
2119
const $rdf = require('rdflib')
2220
const from = require('from2')
2321
const url = require('url')
24-
const Handlebars = require('handlebars')
25-
const debug = require('./debug').errors
2622

2723
/**
2824
* Returns a fully qualified URL from an Express.js Request object.
@@ -151,64 +147,6 @@ function parse (data, baseUri, contentType, callback) {
151147
}
152148
}
153149

154-
/**
155-
* Reads a file, processes it (performing template substitution), and saves
156-
* back the processed result.
157-
*
158-
* @param filePath {string}
159-
* @param substitutions {Object}
160-
*
161-
* @return {Promise}
162-
*/
163-
async function processHandlebarFile (filePath, substitutions) {
164-
return new Promise((resolve, reject) => {
165-
fs.readFile(filePath, 'utf8', (error, rawSource) => {
166-
if (error) {
167-
return reject(error)
168-
}
169-
170-
const output = processHandlebarTemplate(rawSource, substitutions)
171-
172-
fs.writeFile(filePath, output, (error) => {
173-
if (error) {
174-
return reject(error)
175-
}
176-
resolve()
177-
})
178-
})
179-
})
180-
}
181-
182-
/**
183-
* Performs a Handlebars string template substitution, and returns the
184-
* resulting string.
185-
*
186-
* @see https://www.npmjs.com/package/handlebars
187-
*
188-
* @param source {string} e.g. 'Hello, {{name}}'
189-
*
190-
* @return {string} Result, e.g. 'Hello, Alice'
191-
*/
192-
function processHandlebarTemplate (source, substitutions) {
193-
try {
194-
const template = Handlebars.compile(source)
195-
return template(substitutions)
196-
} catch (error) {
197-
debug(`Error processing template: ${error}`)
198-
return source
199-
}
200-
}
201-
202-
async function copyTemplateDir (templatePath, targetPath) {
203-
return new Promise((resolve, reject) => {
204-
fs.copy(templatePath, targetPath, (error) => {
205-
if (error) { return reject(error) }
206-
207-
resolve()
208-
})
209-
})
210-
}
211-
212150
function serialize (graph, baseUri, contentType, callback) {
213151
try {
214152
// target, kb, base, contentType, callback

0 commit comments

Comments
 (0)