UNPKG

1.01 kBJavaScriptView Raw
1/**
2 * @function writeoutSVG
3 */
4'use strict'
5
6const { ApCaptchaSvg } = require('./shim/node')
7const { randomInt } = require('randomval')
8const co = require('co')
9const uuid = require('uuid')
10const writeout = require('writeout')
11const { markup } = require('breact')
12const path = require('path')
13
14/** @lends writeoutSVG */
15function writeoutSVG (dirname, options = {}) {
16 let {
17 count = 10,
18 width = 512,
19 height = 184
20 } = options
21 return co(function * () {
22 let index = {}
23 for (let i = 0; i < count; i++) {
24 let id = uuid.v4()
25 let text = String(randomInt(1000, 9999))
26 index[ id ] = text
27 let filename = path.resolve(dirname, `${id}.svg`)
28 let content = markup(ApCaptchaSvg, { text, width, height })
29 yield writeout(filename, content, {
30 mkdirp: true
31 })
32 }
33 let indexFile = path.resolve(dirname, '.index.json')
34 yield writeout(indexFile, JSON.stringify(index, null, 2), {
35 force: true
36 })
37 })
38}
39
40module.exports = writeoutSVG