Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): prompt for download parameters #20

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 103 additions & 10 deletions bin/smp-download.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,125 @@
#!/usr/bin/env node
import { Command, InvalidArgumentError } from 'commander'
import { Command, InvalidArgumentError } from '@commander-js/extra-typings'
import { input, number } from '@inquirer/prompts'
import fs from 'fs'
import { pipeline } from 'stream/promises'

import download from '../lib/download.js'
import { ttyReporter } from '../lib/reporters.js'
import { isMapboxURL, API_URL as MAPBOX_API_URL } from '../lib/utils/mapbox.js'

const program = new Command()

program
.description('Download a map style for offline usage')
.option('-o, --output [file]', 'output file (if omitted, writes to stdout)')
.requiredOption(
.option('-o, --output <file>', 'output file (if omitted, writes to stdout)')
.option(
'-b, --bbox <west,south,east,north>',
'bounding box of area to download e.g. 11,47,12,47.5',
parseBbox,
)
.requiredOption(
'-z, --zoom <number>',
'max zoom level to download',
parseZoom,
)
.option('-z, --zoom <number>', 'max zoom level to download', parseZoom)
.option(
'-t, --token <token>',
'Mapbox access token (necessary for Mapbox styles)',
)
.argument('<styleUrl>', 'URL to style to download', parseUrl)
.argument('[styleUrl]', 'URL to style to download', parseUrl)
.action(async (styleUrl, { bbox, zoom, output, token }) => {
const promptOutput =
!output &&
process.stdout.isTTY &&
(!styleUrl || !bbox || zoom === undefined)

if (!styleUrl) {
styleUrl = await input({
message: 'Style URL to download',
required: true,
validate: (value) => {
try {
new URL(value)
return true
} catch {
return 'Please enter a valid URL.'
}
},
})
}

if (!bbox) {
const west = await number({
message: 'Bounding box west',
required: true,
step: 'any',
min: -180,
max: 180,
})
const south = await number({
message: 'Bounding box south',
required: true,
step: 'any',
min: -90,
max: 90,
})
const east = await number({
message: 'Bounding box east',
required: true,
step: 'any',
min: -180,
max: 180,
})
const north = await number({
message: 'Bounding box north',
required: true,
step: 'any',
min: -90,
max: 90,
})
if (
west === undefined ||
south === undefined ||
east === undefined ||
north === undefined
) {
throw new InvalidArgumentError('Bounding box values are required.')
}
bbox = [west, south, east, north]
}

if (zoom === undefined) {
zoom = await number({
message: 'Max zoom level to download',
required: true,
min: 0,
max: 22,
})
if (zoom === undefined) {
throw new InvalidArgumentError('Zoom level is required.')
}
}

if (
(isMapboxURL(styleUrl) || styleUrl.startsWith(MAPBOX_API_URL)) &&
!token
) {
token = await input({
message: 'Mapbox access token',
required: true,
})
}

if (promptOutput) {
output = await input({
message: 'Output filename (.smp extension will be added)',
required: true,
transformer: (value) =>
value.endsWith('.smp') ? value : `${value}.smp`,
})
}

if (output && !output.endsWith('.smp')) {
output += '.smp'
}

const reporter = ttyReporter()
const readStream = download({
bbox,
Expand Down Expand Up @@ -63,7 +156,7 @@ function parseBbox(bbox) {
if (bounds.some(isNaN)) {
throw new InvalidArgumentError('Bounding box values must be numbers.')
}
return bounds
return /** @type {[number, number, number, number]} */ (bounds)
}

/** @param {string} url */
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/mapbox.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'
// from https://github.com/mapbox/mapbox-gl-js/blob/495a695/src/util/mapbox.js

const API_URL = 'https://api.mapbox.com'
export const API_URL = 'https://api.mapbox.com'
const HELP = 'See https://www.mapbox.com/api-documentation/#access-tokens'

/**
Expand Down
Loading