UNPKG

4 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3const request = require('request')
4const traverse = require('traverse')
5const uniq = require('lodash/uniq')
6const urlencode = require('urlencode')
7const fs = require('fs')
8const path = require('path')
9const pump = require('pump')
10const rimraf = require('rimraf')
11const mkdirp = require('mkdirp')
12
13const CONFIG = require('../config.json')
14const markerColors = CONFIG.colors
15markerColors.push(CONFIG.defaultColor)
16
17const markerPath = path.join(__dirname, '..', 'svg')
18const marker = fs.readFileSync(path.join(markerPath, 'marker.svg'), 'utf8')
19const markerHover = fs.readFileSync(path.join(markerPath, 'marker-hover.svg'), 'utf8')
20
21require('dotenv').config()
22
23const MAPBOX_TOKEN = process.env.MAPBOX_TOKEN
24const outputDir = path.join(process.cwd(), process.argv[2])
25rimraf.sync(outputDir)
26mkdirp.sync(outputDir)
27
28const mapStyle = CONFIG.defaultMapStyle
29const mapStyleBaseUrl = mapStyle.replace(/^mapbox:\/\/styles\//, 'https://api.mapbox.com/styles/v1/')
30const mapStyleUrl = mapStyleBaseUrl + '?access_token=' + MAPBOX_TOKEN
31
32const RANGES = ['0-255', '65280-65535', '65024-65279', '12288-12543', '65024-65279']
33
34let pending = markerColors.length * 2
35markerColors.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
49function done (err, res, body) {
50 if (err) return console.error(err)
51 if (--pending === 0) {
52 request(mapStyleUrl, onStyle)
53 }
54}
55
56function 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
88function 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
107function 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}