Skip to content

Commit c4dc018

Browse files
committed
A simple start on the updateindex command
Trying to reuse methods from invalidUsernames command
1 parent 09a4753 commit c4dc018

File tree

4 files changed

+59
-22
lines changed

4 files changed

+59
-22
lines changed

bin/lib/cli.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const loadInit = require('./init')
33
const loadStart = require('./start')
44
const loadInvalidUsernames = require('./invalidUsernames')
55
const loadMigrateLegacyResources = require('./migrateLegacyResources')
6+
const loadUpdateIndex = require('./updateIndex')
67
const { spawnSync } = require('child_process')
78
const path = require('path')
89

@@ -13,6 +14,7 @@ module.exports = function startCli (server) {
1314
loadStart(program, server)
1415
loadInvalidUsernames(program)
1516
loadMigrateLegacyResources(program)
17+
loadUpdateIndex(program)
1618

1719
program.parse(process.argv)
1820
if (program.args.length === 0) program.help()

bin/lib/common.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,31 @@
1-
const fs = require('fs')
1+
const fs = require('fs-extra')
22
const extend = require('extend')
33
const { cyan, bold } = require('colorette')
4+
const { URL } = require('url')
5+
const LDP = require('../../lib/ldp')
6+
const AccountManager = require('../../lib/models/account-manager')
7+
const SolidHost = require('../../lib/models/solid-host')
48

9+
module.exports.getAccountManager = getAccountManager
510
module.exports.loadConfig = loadConfig
11+
module.exports.loadUsernames = loadUsernames
12+
13+
/**
14+
* Returns an instance of AccountManager
15+
*
16+
* @param {Object} config
17+
* @param {Object} [options]
18+
* @returns {AccountManager}
19+
*/
20+
function getAccountManager (config, options = {}) {
21+
const ldp = new LDP(config)
22+
const host = options.host || SolidHost.from({ port: config.port, serverUri: config.serverUri })
23+
return AccountManager.from({
24+
host,
25+
store: ldp,
26+
multiuser: config.multiuser
27+
})
28+
}
629

730
function loadConfig (program, options) {
831
let argv = extend({}, options, { version: program.version() })
@@ -23,3 +46,12 @@ function loadConfig (program, options) {
2346

2447
return argv
2548
}
49+
50+
function loadUsernames (config) {
51+
const files = fs.readdirSync(config.root)
52+
const hostname = new URL(config.serverUri).hostname
53+
const isUserDirectory = new RegExp(`.${hostname}$`)
54+
return files
55+
.filter(file => isUserDirectory.test(file))
56+
.map(userDirectory => userDirectory.substr(0, userDirectory.length - hostname.length - 1))
57+
}

bin/lib/invalidUsernames.js

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
const fs = require('fs-extra')
22
const Handlebars = require('handlebars')
33
const path = require('path')
4-
const { URL } = require('url')
54

6-
const { loadConfig } = require('./common')
5+
const { getAccountManager, loadConfig, loadUsernames } = require('./common')
76
const { isValidUsername } = require('../../lib/common/user-utils')
87
const blacklistService = require('../../lib/services/blacklist-service')
98
const { initConfigDir, initTemplateDirs } = require('../../lib/server-config')
109
const { fromServerConfig } = require('../../lib/models/oidc-manager')
1110

12-
const AccountManager = require('../../lib/models/account-manager')
1311
const EmailService = require('../../lib/services/email-service')
14-
const LDP = require('../../lib/ldp')
1512
const SolidHost = require('../../lib/models/solid-host')
1613

1714
module.exports = function (program) {
@@ -28,7 +25,7 @@ module.exports = function (program) {
2825

2926
const invalidUsernames = getInvalidUsernames(config)
3027
const host = SolidHost.from({ port: config.port, serverUri: config.serverUri })
31-
const accountManager = getAccountManager(config, host)
28+
const accountManager = getAccountManager(config, { host })
3229

3330
if (options.notify) {
3431
return notifyUsers(invalidUsernames, accountManager, config)
@@ -96,23 +93,9 @@ async function deleteUsers (usernames, accountManager, config, host) {
9693
console.info(`Deleted ${deletingUsers.length} users succeeded`)
9794
}
9895

99-
function getAccountManager (config, host) {
100-
const ldp = new LDP(config)
101-
return AccountManager.from({
102-
host,
103-
store: ldp,
104-
multiuser: config.multiuser
105-
})
106-
}
107-
10896
function getInvalidUsernames (config) {
109-
const files = fs.readdirSync(config.root)
110-
const hostname = new URL(config.serverUri).hostname
111-
const isUserDirectory = new RegExp(`.${hostname}$`)
112-
return files
113-
.filter(file => isUserDirectory.test(file))
114-
.map(userDirectory => userDirectory.substr(0, userDirectory.length - hostname.length - 1))
115-
.filter(username => !isValidUsername(username) || !blacklistService.validate(username))
97+
const usernames = loadUsernames(config)
98+
return usernames.filter(username => !isValidUsername(username) || !blacklistService.validate(username))
11699
}
117100

118101
function listUsernames (usernames) {

bin/lib/updateIndex.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const { getAccountManager, loadConfig, loadUsernames } = require('./common')
2+
const path = require('path')
3+
4+
module.exports = function (program) {
5+
program
6+
.command('updateindex')
7+
.description('Update index.html in root of all PODs that haven\'t been marked otherwise')
8+
.action((options) => {
9+
const config = loadConfig(program, options)
10+
const usernames = loadUsernames(config)
11+
usernames.forEach(username => updateIndex(username, config))
12+
})
13+
}
14+
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

Comments
 (0)