-
Notifications
You must be signed in to change notification settings - Fork 308
Expand file tree
/
Copy pathaccount-template.mjs
More file actions
81 lines (70 loc) · 2.7 KB
/
Copy pathaccount-template.mjs
File metadata and controls
81 lines (70 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import path from 'path'
import mime from 'mime-types'
import recursiveRead from 'recursive-readdir'
import * as fsUtils from '../common/fs-utils.mjs'
import * as templateUtils from '../common/template-utils.mjs'
import LDP from '../ldp.mjs'
import { URL } from 'url'
export const TEMPLATE_EXTENSIONS = ['.acl', '.meta', '.json', '.hbs', '.handlebars']
export const TEMPLATE_FILES = ['card']
class AccountTemplate {
constructor (options = {}) {
this.substitutions = options.substitutions || {}
this.templateExtensions = options.templateExtensions || TEMPLATE_EXTENSIONS
this.templateFiles = options.templateFiles || TEMPLATE_FILES
}
static for (userAccount, options = {}) {
const substitutions = AccountTemplate.templateSubstitutionsFor(userAccount)
options = Object.assign({ substitutions }, options)
return new AccountTemplate(options)
}
static copyTemplateDir (templatePath, accountPath) {
return fsUtils.copyTemplateDir(templatePath, accountPath)
}
static templateSubstitutionsFor (userAccount) {
const webUri = new URL(userAccount.webId)
const podRelWebId = userAccount.webId.replace(webUri.origin, '')
const substitutions = {
name: userAccount.displayName,
webId: userAccount.externalWebId ? userAccount.webId : podRelWebId,
email: AccountTemplate.encodeMailtoAddress(userAccount.email),
idp: userAccount.idp
}
return substitutions
}
static encodeMailtoAddress (email) {
if (!email) {
return email
}
const [localPart, domain] = email.split('@')
if (!domain) {
return encodeURIComponent(email)
}
return `${encodeURIComponent(localPart)}@${domain}`
}
readAccountFiles (accountPath) {
return new Promise((resolve, reject) => {
recursiveRead(accountPath, (error, files) => {
if (error) { return reject(error) }
resolve(files)
})
})
}
readTemplateFiles (accountPath) {
return this.readAccountFiles(accountPath)
.then(files => files.filter((file) => this.isTemplate(file)))
}
processAccount (accountPath) {
return this.readTemplateFiles(accountPath)
.then(files => Promise.all(files.map(path => templateUtils.processHandlebarFile(path, this.substitutions))))
}
isTemplate (filePath) {
const parsed = path.parse(filePath)
const mimeType = mime.lookup(filePath)
const isRdf = LDP.mimeTypeIsRdf(mimeType)
const isTemplateExtension = this.templateExtensions.includes(parsed.ext)
const isTemplateFile = this.templateFiles.includes(parsed.base) || this.templateExtensions.includes(parsed.base)
return isRdf || isTemplateExtension || isTemplateFile
}
}
export default AccountTemplate