-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.js
27 lines (22 loc) · 952 Bytes
/
update.js
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
const fs = require('fs')
const template = '# authorizedkeys-github\n((.|[\r\n])*)# end authorizedkeys-github'
const templateRegExp = new RegExp(template)
module.exports = update
module.exports.read = read
function update (path, users, cb) {
if (!users.length) return cb()
read(path, users, function (err, content) {
if (err) return cb(err)
fs.writeFile(path, content, {mode: '600'}, cb)
})
}
function read (path, users, cb) {
fs.readFile(path, 'utf8', function (err, str) {
if (err && err.code !== 'ENOENT') return cb(err)
if (/authorizedkeys-github/.test(str)) str = str.replace(templateRegExp, template)
else str = str ? `${str}\n${template}` : template
const keys = users.reduce((a, u) => a.concat(`# github user: ${u.login}`, u.keys, ''), []).join('\n') // eslint-disable-line max-len
str = str.replace(templateRegExp, template.replace('((.|[\r\n])*)', keys)).replace(/\n?$/, '\n')
cb(null, str)
})
}