Skip to content

Commit 17717aa

Browse files
committed
A somewhat brute force solution
Will improve on it to make it easier to read and some functionality more reusable
1 parent c4dc018 commit 17717aa

File tree

4 files changed

+210
-10
lines changed

4 files changed

+210
-10
lines changed

bin/lib/common.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module.exports.loadUsernames = loadUsernames
1818
* @returns {AccountManager}
1919
*/
2020
function getAccountManager (config, options = {}) {
21-
const ldp = new LDP(config)
21+
const ldp = options.ldp || new LDP(config)
2222
const host = options.host || SolidHost.from({ port: config.port, serverUri: config.serverUri })
2323
return AccountManager.from({
2424
host,

bin/lib/updateIndex.js

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,53 @@
1-
const { getAccountManager, loadConfig, loadUsernames } = require('./common')
1+
const fs = require('fs')
22
const path = require('path')
3+
const cheerio = require('cheerio')
4+
const Handlebars = require('handlebars')
5+
const LDP = require('../../lib/ldp')
6+
const $rdf = require('rdflib')
7+
const { URL } = require('url')
8+
9+
const { getAccountManager, loadConfig, loadUsernames } = require('./common')
10+
const { initConfigDir, initTemplateDirs } = require('../../lib/server-config')
11+
12+
const SOLID = $rdf.Namespace('http://www.w3.org/ns/solid/terms#')
313

414
module.exports = function (program) {
515
program
616
.command('updateindex')
717
.description('Update index.html in root of all PODs that haven\'t been marked otherwise')
8-
.action((options) => {
18+
.action(async (options) => {
919
const config = loadConfig(program, options)
20+
const ldp = new LDP(config)
21+
const configPath = initConfigDir(config)
22+
const templates = initTemplateDirs(configPath)
23+
const accountManager = getAccountManager(config, { ldp })
1024
const usernames = loadUsernames(config)
11-
usernames.forEach(username => updateIndex(username, config))
25+
const indexTemplatePath = path.join(templates.account, 'index.html')
26+
const indexTemplateSource = fs.readFileSync(indexTemplatePath, 'utf-8')
27+
const indexTemplate = Handlebars.compile(indexTemplateSource)
28+
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) {
36+
return
37+
}
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')
48+
})
49+
await Promise.all(usersProcessed)
50+
console.log(`Processed ${usersProcessed.length} users`)
1251
})
1352
}
1453

15-
function updateIndex (username, config) {
16-
const accountManager = getAccountManager(config)
17-
const userDirectory = accountManager.accountDirFor(username)
18-
const indexFile = path.join(userDirectory, 'index.html')
19-
console.log(indexFile)
20-
}

package-lock.json

Lines changed: 166 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"busboy": "^0.2.12",
4646
"cached-path-relative": "^1.0.2",
4747
"camelize": "^1.0.0",
48+
"cheerio": "^1.0.0-rc.2",
4849
"colorette": "^1.0.5",
4950
"commander": "^2.9.0",
5051
"cors": "^2.7.1",

0 commit comments

Comments
 (0)