|
1 | | -const { getAccountManager, loadConfig, loadUsernames } = require('./common') |
| 1 | +const fs = require('fs') |
2 | 2 | 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#') |
3 | 13 |
|
4 | 14 | module.exports = function (program) { |
5 | 15 | program |
6 | 16 | .command('updateindex') |
7 | 17 | .description('Update index.html in root of all PODs that haven\'t been marked otherwise') |
8 | | - .action((options) => { |
| 18 | + .action(async (options) => { |
9 | 19 | 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 }) |
10 | 24 | 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`) |
12 | 51 | }) |
13 | 52 | } |
14 | 53 |
|
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 | | -} |
|
0 commit comments