1 | #!/usr/bin/env node
|
2 |
|
3 | const request = require('request')
|
4 | const traverse = require('traverse')
|
5 | const uniq = require('lodash/uniq')
|
6 | const urlencode = require('urlencode')
|
7 | const fs = require('fs')
|
8 | const path = require('path')
|
9 | const pump = require('pump')
|
10 | const rimraf = require('rimraf')
|
11 | const mkdirp = require('mkdirp')
|
12 |
|
13 | const CONFIG = require('../config.json')
|
14 | const markerColors = CONFIG.colors
|
15 | markerColors.push(CONFIG.defaultColor)
|
16 |
|
17 | const markerPath = path.join(__dirname, '..', 'svg')
|
18 | const marker = fs.readFileSync(path.join(markerPath, 'marker.svg'), 'utf8')
|
19 | const markerHover = fs.readFileSync(path.join(markerPath, 'marker-hover.svg'), 'utf8')
|
20 |
|
21 | require('dotenv').config()
|
22 |
|
23 | const MAPBOX_TOKEN = process.env.MAPBOX_TOKEN
|
24 | const outputDir = path.join(process.cwd(), process.argv[2])
|
25 | rimraf.sync(outputDir)
|
26 | mkdirp.sync(outputDir)
|
27 |
|
28 | const mapStyle = CONFIG.defaultMapStyle
|
29 | const mapStyleBaseUrl = mapStyle.replace(/^mapbox:\/\/styles\//, 'https://api.mapbox.com/styles/v1/')
|
30 | const mapStyleUrl = mapStyleBaseUrl + '?access_token=' + MAPBOX_TOKEN
|
31 |
|
32 | const RANGES = ['0-255', '65280-65535', '65024-65279', '12288-12543', '65024-65279']
|
33 |
|
34 | let pending = markerColors.length * 2
|
35 | markerColors.forEach(function (color) {
|
36 | const markerBaseName = 'marker-' + color.replace('#', '')
|
37 | const coloredMarker = marker.replace('{{fillColor}}', color)
|
38 | const coloredMarkerHover = markerHover.replace('{{fillColor}}', color)
|
39 | request.put({
|
40 | url: mapStyleBaseUrl + '/sprite/' + markerBaseName + '?access_token=' + MAPBOX_TOKEN,
|
41 | body: coloredMarker
|
42 | }, done)
|
43 | request.put({
|
44 | url: mapStyleBaseUrl + '/sprite/' + markerBaseName + '-hover?access_token=' + MAPBOX_TOKEN,
|
45 | body: coloredMarkerHover
|
46 | }, done)
|
47 | })
|
48 |
|
49 | function done (err, res, body) {
|
50 | if (err) return console.error(err)
|
51 | if (--pending === 0) {
|
52 | request(mapStyleUrl, onStyle)
|
53 | }
|
54 | }
|
55 |
|
56 | function onStyle (err, res, body) {
|
57 | if (err) return console.error(err)
|
58 | const style = JSON.parse(body)
|
59 | const fonts = []
|
60 | traverse(style).forEach(function (x) {
|
61 | if (this.key === 'text-font') {
|
62 | if (Array.isArray(x)) {
|
63 | fonts.push.apply(fonts, x)
|
64 | } else if (typeof x === 'string') {
|
65 | fonts.push(x)
|
66 | } else {
|
67 | traverse(x).forEach(fontsFromStops)
|
68 | }
|
69 | }
|
70 | })
|
71 | function fontsFromStops (x) {
|
72 | if (typeof x === 'string') {
|
73 | fonts.push(x)
|
74 | }
|
75 | }
|
76 | const fontStack = uniq(fonts).map(s => urlencode(s)).join(',')
|
77 | style.glyphs = 'mapfilter://fonts/{range}.pbf?stack={fontstack}'
|
78 | const originalSprite = style.sprite
|
79 | style.sprite = 'mapfilter://sprites/sprite'
|
80 | fs.writeFileSync(path.join(outputDir, 'style.json'), JSON.stringify(style, null, 4))
|
81 | downloadFonts(fontStack, done)
|
82 | downloadSprites(originalSprite, done)
|
83 | function done () {
|
84 | console.log('DONE!')
|
85 | }
|
86 | }
|
87 |
|
88 | function downloadFonts (fontStack, cb) {
|
89 | mkdirp.sync(path.join(outputDir, 'fonts'))
|
90 | let pending = RANGES.length
|
91 | RANGES.forEach(function (range) {
|
92 | const url = 'https://api.mapbox.com/fonts/v1/gmaclennan/' +
|
93 | fontStack + '/' + range + '.pbf' + '?access_token=' + MAPBOX_TOKEN
|
94 | const filepath = path.join(outputDir, 'fonts', range + '.pbf')
|
95 | const req = request({
|
96 | url: url,
|
97 | gzip: true
|
98 | })
|
99 | pump(req, fs.createWriteStream(filepath), done)
|
100 | })
|
101 | function done (err) {
|
102 | if (err) return console.error(err)
|
103 | if (--pending === 0) cb(err)
|
104 | }
|
105 | }
|
106 |
|
107 | function downloadSprites (sprite, cb) {
|
108 | mkdirp.sync(path.join(outputDir, 'sprites'))
|
109 | const postfixes = ['.png', '.json', '@2x.png', '@2x.json']
|
110 | let pending = postfixes.length
|
111 | postfixes.forEach(function (postfix) {
|
112 | const url = sprite.replace(/^mapbox:\/\/sprites\//, 'https://api.mapbox.com/styles/v1/') +
|
113 | '/sprite' + postfix + '?access_token=' + MAPBOX_TOKEN + '&cacheBust=' + Date.now()
|
114 | const filepath = path.join(outputDir, 'sprites', 'sprite' + postfix)
|
115 | pump(request(url), fs.createWriteStream(filepath), done)
|
116 | })
|
117 | function done (err) {
|
118 | if (err) return console.error(err)
|
119 | if (--pending === 0) cb(err)
|
120 | }
|
121 | }
|