Skip to content

Commit 627b1fe

Browse files
committed
Trying to make code more readable
And by that hopefully more maintainable
1 parent 17717aa commit 627b1fe

File tree

8 files changed

+77
-48
lines changed

8 files changed

+77
-48
lines changed
File renamed without changes.

bin/lib/invalidUsernames.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const fs = require('fs-extra')
22
const Handlebars = require('handlebars')
33
const path = require('path')
44

5-
const { getAccountManager, loadConfig, loadUsernames } = require('./common')
5+
const { getAccountManager, loadConfig, loadUsernames } = require('./cli-utils')
66
const { isValidUsername } = require('../../lib/common/user-utils')
77
const blacklistService = require('../../lib/services/blacklist-service')
88
const { initConfigDir, initTemplateDirs } = require('../../lib/server-config')

bin/lib/start.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const options = require('./options')
44
const fs = require('fs')
5-
const { loadConfig } = require('./common')
5+
const { loadConfig } = require('./cli-utils')
66
const { red, bold } = require('colorette')
77

88
module.exports = function (program, server) {

bin/lib/updateIndex.js

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,50 @@
11
const fs = require('fs')
22
const path = require('path')
33
const cheerio = require('cheerio')
4-
const Handlebars = require('handlebars')
54
const LDP = require('../../lib/ldp')
6-
const $rdf = require('rdflib')
75
const { URL } = require('url')
86

9-
const { getAccountManager, loadConfig, loadUsernames } = require('./common')
7+
const { compileTemplate, writeTemplate } = require('../../lib/common/template-utils')
8+
const { getAccountManager, loadConfig, loadUsernames } = require('./cli-utils')
9+
const { getWebId } = require('../../lib/common/user-utils')
1010
const { initConfigDir, initTemplateDirs } = require('../../lib/server-config')
1111

12-
const SOLID = $rdf.Namespace('http://www.w3.org/ns/solid/terms#')
13-
1412
module.exports = function (program) {
1513
program
1614
.command('updateindex')
1715
.description('Update index.html in root of all PODs that haven\'t been marked otherwise')
1816
.action(async (options) => {
1917
const config = loadConfig(program, options)
20-
const ldp = new LDP(config)
2118
const configPath = initConfigDir(config)
2219
const templates = initTemplateDirs(configPath)
20+
const indexTemplatePath = path.join(templates.account, 'index.html')
21+
const indexTemplate = await compileTemplate(indexTemplatePath)
22+
const ldp = new LDP(config)
2323
const accountManager = getAccountManager(config, { ldp })
2424
const usernames = loadUsernames(config)
25-
const indexTemplatePath = path.join(templates.account, 'index.html')
26-
const indexTemplateSource = fs.readFileSync(indexTemplatePath, 'utf-8')
27-
const indexTemplate = Handlebars.compile(indexTemplateSource)
2825
const usersProcessed = usernames.map(async name => {
29-
const userDirectory = accountManager.accountDirFor(name)
30-
const indexFilePath = path.join(userDirectory, 'index.html')
31-
const indexSource = fs.readFileSync(indexFilePath, 'utf-8')
32-
const $ = cheerio.load(indexSource)
33-
const allowAutomaticUpdateValue = $('meta[name="solid-allow-automatic-updates"]').prop('content')
34-
const allowAutomaticUpdate = !allowAutomaticUpdateValue || allowAutomaticUpdateValue === 'true'
35-
if (!allowAutomaticUpdate) {
26+
const accountDirectory = accountManager.accountDirFor(name)
27+
const indexFilePath = path.join(accountDirectory, 'index.html')
28+
if (!isUpdateAllowed(indexFilePath)) {
3629
return
3730
}
38-
const serverUrl = new URL(config.serverUri)
39-
const accountUrl = `${serverUrl.protocol}//${name}.${serverUrl.host}/`
40-
const metaFileUri = `${accountUrl}/${ldp.suffixMeta}`
41-
const metaData = await ldp.readContainerMeta(userDirectory)
42-
const metaGraph = $rdf.graph()
43-
$rdf.parse(metaData, metaGraph, metaFileUri, 'text/turtle')
44-
const webIdNode = metaGraph.any(undefined, SOLID('account'), $rdf.sym(accountUrl))
45-
const webId = webIdNode.value
46-
const newIndexSource = indexTemplate({ name, webId })
47-
fs.writeFileSync(indexFilePath, newIndexSource, 'utf-8')
31+
const accountUrl = getAccountUrl(name, config)
32+
const webId = await getWebId(accountDirectory, accountUrl, { ldp })
33+
writeTemplate(indexFilePath, indexTemplate, { name, webId })
4834
})
4935
await Promise.all(usersProcessed)
5036
console.log(`Processed ${usersProcessed.length} users`)
5137
})
5238
}
5339

40+
function getAccountUrl (name, config) {
41+
const serverUrl = new URL(config.serverUri)
42+
return `${serverUrl.protocol}//${name}.${serverUrl.host}/`
43+
}
44+
45+
function isUpdateAllowed (indexFilePath) {
46+
const indexSource = fs.readFileSync(indexFilePath, 'utf-8')
47+
const $ = cheerio.load(indexSource)
48+
const allowAutomaticUpdateValue = $('meta[name="solid-allow-automatic-updates"]').prop('content')
49+
return !allowAutomaticUpdateValue || allowAutomaticUpdateValue === 'true'
50+
}

default-templates/new-account/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
<head>
44
<meta charset="utf-8">
55
<meta name="viewport" content="width=device-width, initial-scale=1">
6+
<!-- Set content in the next line to false if you do not want your homepage to be automatically updated whenever a new version becomes available. -->
7+
<meta name="solid-allow-automatic-updates" content="true">
68
<title>{{#if name}}{{name}} &ndash; {{/if}}Solid Home</title>
79
<link rel="stylesheet" href="/common/css/bootstrap.min.css">
810
<link rel="stylesheet" href="/common/css/solid.css">

lib/common/fs-utils.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module.exports.copyTemplateDir = copyTemplateDir
22
module.exports.processFile = processFile
3+
module.exports.readFile = readFile
4+
module.exports.writeFile = writeFile
35

46
const fs = require('fs-extra')
57

@@ -31,3 +33,11 @@ async function processFile (filePath, manipulateSourceFn) {
3133
})
3234
})
3335
}
36+
37+
function readFile (filePath, options = 'utf-8') {
38+
return fs.readFileSync(filePath, options)
39+
}
40+
41+
function writeFile (filePath, fileSource, options = 'utf-8') {
42+
fs.writeFileSync(filePath, fileSource, options)
43+
}

lib/common/template-utils.js

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1+
module.exports.compileTemplate = compileTemplate
12
module.exports.processHandlebarFile = processHandlebarFile
3+
module.exports.writeTemplate = writeTemplate
24

35
const Handlebars = require('handlebars')
46
const debug = require('../debug').errors
5-
const { processFile } = require('./fs-utils')
7+
const { processFile, readFile, writeFile } = require('./fs-utils')
8+
9+
async function compileTemplate (filePath) {
10+
const indexTemplateSource = readFile(filePath)
11+
return Handlebars.compile(indexTemplateSource)
12+
}
613

714
/**
815
* Reads a file, processes it (performing template substitution), and saves
@@ -14,25 +21,18 @@ const { processFile } = require('./fs-utils')
1421
* @return {Promise}
1522
*/
1623
async function processHandlebarFile (filePath, substitutions) {
17-
return processFile(filePath, (rawSource) => processHandlebarTemplate(rawSource, substitutions))
24+
return processFile(filePath, (rawSource) => {
25+
try {
26+
const template = Handlebars.compile(rawSource)
27+
return template(substitutions)
28+
} catch (error) {
29+
debug(`Error processing template: ${error}`)
30+
return rawSource
31+
}
32+
})
1833
}
1934

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-
}
35+
function writeTemplate (filePath, template, substitutions) {
36+
const source = template(substitutions)
37+
writeFile(filePath, source)
3838
}

lib/common/user-utils.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
1+
const $rdf = require('rdflib')
2+
3+
const LDP = require('../ldp')
4+
5+
const SOLID = $rdf.Namespace('http://www.w3.org/ns/solid/terms#')
6+
7+
module.exports.getWebId = getWebId
18
module.exports.isValidUsername = isValidUsername
29

10+
async function getWebId (accountDirectory, accountUrl, options = {}) {
11+
if (!options.ldp && !options.config) {
12+
throw new Error('Require ldp or config set in options for getWebId')
13+
}
14+
const ldp = options.ldp || new LDP(options.config)
15+
const metaFileUri = `${accountUrl}/${ldp.suffixMeta}`
16+
const metaData = await ldp.readContainerMeta(accountDirectory)
17+
const metaGraph = $rdf.graph()
18+
$rdf.parse(metaData, metaGraph, metaFileUri, 'text/turtle')
19+
const webIdNode = metaGraph.any(undefined, SOLID('account'), $rdf.sym(accountUrl))
20+
return webIdNode.value
21+
}
22+
323
function isValidUsername (username) {
424
return /^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(username)
525
}

0 commit comments

Comments
 (0)