1 |
|
2 |
|
3 |
|
4 | 'use strict'
|
5 |
|
6 | const { ApCaptchaSvg } = require('./shim/node')
|
7 | const { randomInt } = require('randomval')
|
8 | const co = require('co')
|
9 | const uuid = require('uuid')
|
10 | const writeout = require('writeout')
|
11 | const { markup } = require('breact')
|
12 | const path = require('path')
|
13 |
|
14 |
|
15 | function 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 |
|
40 | module.exports = writeoutSVG
|