Skip to content

Commit 760adf5

Browse files
committed
Partial work - need to load config from somewhere
1 parent ec5821f commit 760adf5

File tree

4 files changed

+69
-53
lines changed

4 files changed

+69
-53
lines changed

default-templates/server/index.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ <h1>Welcome to the Solid Prototype</h1>
3030

3131
If you have not already done so, please create an account.
3232
</p>
33+
34+
35+
<section>
36+
<h2>Server info</h2>
37+
<dl>
38+
<dt>Server name</dt>
39+
<dd>{{serverName}}</dd>
40+
</dl>
41+
</section>
3342
</div>
3443
</body>
3544
</html>

lib/models/account-template.js

Lines changed: 3 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict'
22

3+
import { processHandlebarFile } from '../utils'
4+
35
const fs = require('fs-extra')
46
const path = require('path')
57
const mime = require('mime-types')
@@ -130,58 +132,7 @@ class AccountTemplate {
130132
*/
131133
processAccount (accountPath) {
132134
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
135+
.then(files => Promise.all(files.map(path => processHandlebarFile(path, this.substitutions))))
185136
}
186137

187138
/**

lib/server-config.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
'use strict'
2+
import { processHandlebarFile } from './utils'
3+
24
/**
35
* Server config initialization utilities
46
*/
@@ -36,7 +38,7 @@ function ensureDirCopyExists (fromDir, toDir) {
3638
*
3739
* @param argv {Function} Express.js app object
3840
*/
39-
function ensureWelcomePage (argv) {
41+
async function ensureWelcomePage (argv) {
4042
let { multiuser, templates } = argv
4143
let rootDir = path.resolve(argv.root)
4244
let serverRootDir
@@ -55,6 +57,9 @@ function ensureWelcomePage (argv) {
5557
if (!fs.existsSync(existingIndexPage)) {
5658
fs.mkdirp(serverRootDir)
5759
fs.copySync(defaultIndexPage, existingIndexPage)
60+
await processHandlebarFile(existingIndexPage, {
61+
// Need to load config from somewhere
62+
})
5863
fs.copySync(defaultIndexPageAcl, existingIndexPageAcl)
5964
}
6065
}

lib/utils.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module.exports.pathBasename = pathBasename
55
module.exports.getFullUri = getFullUri
66
module.exports.hasSuffix = hasSuffix
77
module.exports.parse = parse
8+
module.exports.processHandlebarFile = processHandlebarFile
89
module.exports.serialize = serialize
910
module.exports.translate = translate
1011
module.exports.stringToStream = stringToStream
@@ -19,6 +20,8 @@ const path = require('path')
1920
const $rdf = require('rdflib')
2021
const from = require('from2')
2122
const url = require('url')
23+
const Handlebars = require('handlebars')
24+
const debug = require('./debug').errors
2225

2326
/**
2427
* Returns a fully qualified URL from an Express.js Request object.
@@ -147,6 +150,54 @@ function parse (data, baseUri, contentType, callback) {
147150
}
148151
}
149152

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

0 commit comments

Comments
 (0)