Skip to content

Commit

Permalink
添加 CardDav 服务器 Radicale 输出格式的支持 (#232)
Browse files Browse the repository at this point in the history
* add: vcard-ext.js add uid & REV to vCard

* update: add radicale script

* fix

* fix

* update: ignore radicale
  • Loading branch information
dallaslu authored Apr 13, 2022
1 parent 2328d11 commit e81ad44
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ temp
public
*.zip
.DS_Store
radicale
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"type": "module",
"scripts": {
"build": "npm run gulp build",
"radicale": "npm run gulp radicale",
"gulp": "gulp --gulpfile src/gulpfile.js --cwd ./",
"test": "npx ava src/test.js"
},
Expand Down
35 changes: 34 additions & 1 deletion src/gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import fs from 'fs'
import del from 'del'
import through2 from 'through2'

Expand All @@ -8,6 +9,7 @@ import rename from 'gulp-rename'
import concatFolders from 'gulp-concat-folders'

import plugin_vcard from './plugins/vcard.js'
import plugin_vcard_ext from './plugins/vcard-ext.js'

const generator = () => {
return gulp.src('data/*/*.yaml')
Expand All @@ -16,6 +18,13 @@ const generator = () => {
.pipe(gulp.dest('./temp'))
}

const generator_ext = () => {
return gulp.src('data/*/*.yaml')
.pipe(through2.obj(plugin_vcard_ext))
.pipe(rename({ extname: '.vcf' }))
.pipe(gulp.dest('./temp'))
}

const archive = () => {
return gulp.src('temp/**')
.pipe(zip('archive.zip'))
Expand All @@ -42,12 +51,36 @@ const clean = () => {
])
}

const createRadicale = () => {
let folders = fs.readdirSync('temp')
.filter(function(f) {
return fs.statSync(path.join('temp', f)).isDirectory();
})
folders.map(function(folder){
fs.writeFileSync(path.join('temp', folder, '/.Radicale.props'), '{"D:displayname": "' + folder + '", "tag": "VADDRESSBOOK"}')
})
return gulp.src('temp/**', {})
}

const cleanRadicale = () => {
return del([
'radicale'
], {force: true})
}

const distRadicale = () => {
return gulp.src('temp/**', {dot: true})
.pipe(gulp.dest('./radicale'))
}

const build = gulp.series(clean, generator, combine, allinone, archive)
const radicale = gulp.series(clean, generator_ext, createRadicale, cleanRadicale, distRadicale)

export {
generator,
combine,
allinone,
archive,
build
build,
radicale
}
33 changes: 33 additions & 0 deletions src/plugins/vcard-ext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import fs from 'fs'
import yaml from 'js-yaml'
import vCardsJS from 'vcards-js'
import {execSync} from 'child_process'

const plugin = (file, _, cb) => {
const path = file.path
const data = fs.readFileSync(path, 'utf8')
const json = yaml.load(data)

let vCard = vCardsJS()
vCard.isOrganization = true
for (const [key, value] of Object.entries(json.basic)) {
vCard[key] = value
}

if (!vCard.uid){
vCard.uid = vCard.organization
}

vCard.photo.embedFromFile(path.replace('.yaml', '.png'))
let lastYamlChangeDateString = execSync('git log -1 --pretty="format:%ci" ' + path).toString().trim().replace(/\s\+\d+/, '')
let lastPngChangeDateString = execSync('git log -1 --pretty="format:%ci" ' + path.replace('.yaml', '.png')).toString().trim().replace(/\s\+\d+/, '')

let rev = new Date(Math.max(new Date(lastYamlChangeDateString), new Date(lastPngChangeDateString))).toISOString()

let formatted = vCard.getFormattedString()
formatted = formatted.replace(/REV:[\d\-:T\.Z]+/, 'REV:' + rev)
file.contents = Buffer.from(formatted)
cb(null, file)
}

export default plugin

0 comments on commit e81ad44

Please sign in to comment.