|
1 | 1 | const fs = require('fs') |
2 | 2 | const path = require('path') |
3 | 3 | const cheerio = require('cheerio') |
4 | | -const Handlebars = require('handlebars') |
5 | 4 | const LDP = require('../../lib/ldp') |
6 | | -const $rdf = require('rdflib') |
7 | 5 | const { URL } = require('url') |
8 | 6 |
|
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') |
10 | 10 | const { initConfigDir, initTemplateDirs } = require('../../lib/server-config') |
11 | 11 |
|
12 | | -const SOLID = $rdf.Namespace('http://www.w3.org/ns/solid/terms#') |
13 | | - |
14 | 12 | module.exports = function (program) { |
15 | 13 | program |
16 | 14 | .command('updateindex') |
17 | 15 | .description('Update index.html in root of all PODs that haven\'t been marked otherwise') |
18 | 16 | .action(async (options) => { |
19 | 17 | const config = loadConfig(program, options) |
20 | | - const ldp = new LDP(config) |
21 | 18 | const configPath = initConfigDir(config) |
22 | 19 | 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) |
23 | 23 | const accountManager = getAccountManager(config, { ldp }) |
24 | 24 | 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) |
28 | 25 | 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)) { |
36 | 29 | return |
37 | 30 | } |
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 }) |
48 | 34 | }) |
49 | 35 | await Promise.all(usersProcessed) |
50 | 36 | console.log(`Processed ${usersProcessed.length} users`) |
51 | 37 | }) |
52 | 38 | } |
53 | 39 |
|
| 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 | +} |
0 commit comments