Skip to content

Commit 104516e

Browse files
committed
Small, easy improvements
Fix copying of ACL-files for backed up version of index.html
1 parent cdaff9b commit 104516e

File tree

4 files changed

+22
-27
lines changed

4 files changed

+22
-27
lines changed

bin/lib/common.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
const fs = require('fs')
22
const extend = require('extend')
33
const { cyan, bold } = require('colorette')
4-
const util = require('util')
54

6-
module.exports = {}
75
module.exports.loadConfig = loadConfig
86

9-
async function loadConfig (program, options) {
7+
function loadConfig (program, options) {
108
let argv = extend({}, options, { version: program.version() })
119
let configFile = argv['configFile'] || './config.json'
1210

1311
try {
14-
const file = await util.promisify(fs.readFile)(configFile)
12+
const file = fs.readFileSync(configFile)
1513

1614
// Use flags with priority over config file
1715
const config = JSON.parse(file)

bin/lib/invalidUsernames.js

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ const fs = require('fs-extra')
22
const Handlebars = require('handlebars')
33
const path = require('path')
44
const { URL } = require('url')
5-
const util = require('util')
65

76
const { loadConfig } = require('./common')
87
const { isValidUsername } = require('../../lib/common/user-utils')
@@ -15,22 +14,19 @@ const EmailService = require('../../lib/services/email-service')
1514
const LDP = require('../../lib/ldp')
1615
const SolidHost = require('../../lib/models/solid-host')
1716

18-
const fileExists = util.promisify(fs.exists)
19-
const fileRename = util.promisify(fs.rename)
20-
2117
module.exports = function (program) {
2218
program
2319
.command('invalidusernames')
2420
.option('--notify', 'Will notify users with usernames that are invalid')
2521
.option('--delete', 'Will delete users with usernames that are invalid')
2622
.description('Manage usernames that are invalid')
2723
.action(async (options) => {
28-
const config = await loadConfig(program, options)
24+
const config = loadConfig(program, options)
2925
if (!config.multiuser) {
3026
return console.error('You are running a single user server, no need to check for invalid usernames')
3127
}
3228

33-
const invalidUsernames = await getInvalidUsernames(config)
29+
const invalidUsernames = getInvalidUsernames(config)
3430
const host = SolidHost.from({ port: config.port, serverUri: config.serverUri })
3531
const accountManager = getAccountManager(config, host)
3632

@@ -42,18 +38,21 @@ module.exports = function (program) {
4238
return deleteUsers(invalidUsernames, accountManager, config, host)
4339
}
4440

45-
listUsernames(listUsernames)
41+
listUsernames(invalidUsernames)
4642
})
4743
}
4844

49-
async function createNewIndexFile (username, accountManager, invalidUsernameTemplate, dateOfRemoval, supportEmail, fileOptions) {
45+
function createNewIndexFile (username, accountManager, invalidUsernameTemplate, dateOfRemoval, supportEmail, fileOptions) {
5046
const userDirectory = accountManager.accountDirFor(username)
5147
const currentIndex = path.join(userDirectory, 'index.html')
52-
const currentIndexExists = await fileExists(currentIndex)
48+
const currentIndexExists = fs.existsSync(currentIndex)
49+
const currentIndexAcl = path.join(userDirectory, 'index.html.acl')
5350
const backupIndex = path.join(userDirectory, 'index.backup.html')
54-
const backupIndexExists = await fileExists(backupIndex)
51+
const backupIndexExists = fs.existsSync(backupIndex)
52+
const backupIndexAcl = path.join(userDirectory, 'index.backup.html.acl')
5553
if (currentIndexExists && !backupIndexExists) {
56-
await fileRename(currentIndex, backupIndex)
54+
fs.renameSync(currentIndex, backupIndex)
55+
fs.copyFileSync(currentIndexAcl, backupIndexAcl)
5756
const newIndexSource = invalidUsernameTemplate({
5857
username,
5958
dateOfRemoval,
@@ -96,8 +95,8 @@ function getAccountManager (config, host) {
9695
})
9796
}
9897

99-
async function getInvalidUsernames (config) {
100-
const files = await util.promisify(fs.readdir)(config.root)
98+
function getInvalidUsernames (config) {
99+
const files = fs.readdirSync(config.root)
101100
const hostname = new URL(config.serverUri).hostname
102101
const isUserDirectory = new RegExp(`.${hostname}$`)
103102
return files
@@ -108,7 +107,7 @@ async function getInvalidUsernames (config) {
108107

109108
function listUsernames (usernames) {
110109
if (usernames.length === 0) {
111-
console.info('No invalid usernames was found')
110+
return console.info('No invalid usernames was found')
112111
}
113112
console.info(`${usernames.length} invalid usernames were found:${usernames.map(username => `\n- ${username}`)}`)
114113
}
@@ -118,7 +117,7 @@ async function notifyUsers (usernames, accountManager, config) {
118117
const dateOfRemoval = (new Date(twoWeeksFromNow)).toLocaleDateString()
119118
const { supportEmail } = config
120119

121-
await updateIndexFiles(usernames, accountManager, dateOfRemoval, supportEmail)
120+
updateIndexFiles(usernames, accountManager, dateOfRemoval, supportEmail)
122121
await sendEmails(config, usernames, accountManager, dateOfRemoval, supportEmail)
123122
}
124123

@@ -132,9 +131,9 @@ async function sendEmails (config, usernames, accountManager, dateOfRemoval, sup
132131
return { username, emailAddress, accountUri }
133132
}))
134133
const emailService = new EmailService(templates.email, config.email)
135-
const sendingEmails = await users
134+
const sendingEmails = users
136135
.filter(user => !!user.emailAddress)
137-
.map(async user => await emailService.sendWithTemplate('invalid-username', {
136+
.map(user => emailService.sendWithTemplate('invalid-username', {
138137
to: user.emailAddress,
139138
accountUri: user.accountUri,
140139
dateOfRemoval,
@@ -148,13 +147,12 @@ async function sendEmails (config, usernames, accountManager, dateOfRemoval, sup
148147
console.info('Please set it up to send users email about their accounts')
149148
}
150149

151-
async function updateIndexFiles (usernames, accountManager, dateOfRemoval, supportEmail) {
152-
const invalidUsernameFilePath = path.join(process.cwd(), 'default-views/account/invalid-username.hbs')
150+
function updateIndexFiles (usernames, accountManager, dateOfRemoval, supportEmail) {
151+
const invalidUsernameFilePath = path.join(process.cwd(), 'default-views', 'account', 'invalid-username.hbs')
153152
const fileOptions = {
154153
encoding: 'utf-8'
155154
}
156155
const source = fs.readFileSync(invalidUsernameFilePath, fileOptions)
157156
const invalidUsernameTemplate = Handlebars.compile(source)
158-
const updatingFiles = usernames.map(username => createNewIndexFile(username, accountManager, invalidUsernameTemplate, dateOfRemoval, supportEmail, fileOptions))
159-
return Promise.all(updatingFiles)
157+
usernames.forEach(username => createNewIndexFile(username, accountManager, invalidUsernameTemplate, dateOfRemoval, supportEmail, fileOptions))
160158
}

bin/lib/start.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ module.exports = function (program, server) {
3535
start.option('-v, --verbose', 'Print the logs to console')
3636

3737
start.action(async (options) => {
38-
const config = await loadConfig(program, options)
38+
const config = loadConfig(program, options)
3939
bin(config, server)
4040
})
4141
}

default-views/account/invalid-username.hbs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
<meta name="viewport" content="width=device-width, initial-scale=1">
66
<title>Invalid username</title>
77
<link rel="stylesheet" href="/common/css/bootstrap.min.css">
8-
<script></script>
98
</head>
109
<body>
1110
<div class="container">

0 commit comments

Comments
 (0)