Skip to content

Commit c9d542b

Browse files
committed
tweaks
- add version command - fix large uploads - conditionally load .env - fix local paths
1 parent 578ac25 commit c9d542b

8 files changed

Lines changed: 127 additions & 36 deletions

File tree

cli/README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# maps cli
2+
3+
This directory is a Node.js CLI for extracting a set of pmtiles
4+
and uploading them to an S3 bucket.
5+
6+
## usage
7+
8+
You can run the `main.ts` as an executable if you have Node.js 22+ installed.
9+
It will load the `.env` next to it
10+
11+
```
12+
main.ts <command>
13+
14+
Commands:
15+
main.ts config Output config and usage
16+
main.ts run Run the tool
17+
18+
Options:
19+
--version Show version number [boolean]
20+
--help Show help [boolean]
21+
```
22+
23+
## configuration
24+
25+
These are the configurations you can set for how the CLI behaves.
26+
Set them in a `config.json` in this directory or with the corresponding CLI flags or environment variables.
27+
powered by [Gruber](https://gruber.r0b.io).
28+
29+
| name | type | flag | variable | fallback |
30+
| ------------------ | ------ | ---- | ---------------------- | ------------------------------------------------ |
31+
| env | string | ~ | NODE_ENV | production |
32+
| meta.name | string | ~ | APP_NAME | @openlab/protomaps-cli |
33+
| meta.version | string | ~ | APP_VERSION | 0.0.0 |
34+
| protomaps.builds | url | ~ | PROTOMAPS_BUILDS_URL | https://build.protomaps.com/ |
35+
| protomaps.metadata | url | ~ | PROTOMAPS_METADATA_URL | https://build-metadata.protomaps.dev/builds.json |
36+
| s3.accessKey | string | ~ | S3_ACCESS_KEY | gruber://unset |
37+
| s3.bucketName | string | ~ | S3_BUCKET_NAME | gruber://unset |
38+
| s3.endpoint | url | ~ | S3_ENDPOINT | gruber://unset |
39+
| s3.prefix | string | ~ | S3_PREFIX | |
40+
| s3.secretKey | string | ~ | S3_SECRET_KEY | gruber://unset |
41+
42+
Default:
43+
44+
```json
45+
{
46+
"env": "development",
47+
"meta": {
48+
"name": "@openlab/protomaps-cli",
49+
"version": "0.0.0"
50+
},
51+
"targets": [],
52+
"protomaps": {
53+
"metadata": "https://build-metadata.protomaps.dev/builds.json",
54+
"builds": "https://build.protomaps.com/"
55+
},
56+
"s3": {
57+
"prefix": "",
58+
"accessKey": "gruber://unset",
59+
"secretKey": "gruber://unset",
60+
"bucketName": "gruber://unset",
61+
"endpoint": "gruber://unset"
62+
}
63+
}
64+
```
65+
66+
## notes
67+
68+
The CLI relies on minio `8.0.4` **specifically**, this is to remedy [this issue](https://github.com/minio/minio-js/issues/1395).

cli/config.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
{
44
"name": "ncl.pmtiles",
55
"bbox": [-2.072468, 54.730692, -1.112537, 55.248329]
6+
},
7+
{
8+
"name": "uk.pmtiles",
9+
"bbox": [-13.736773, 49.69262, 5.55685, 61.29407]
610
}
711
],
812
"s3": {

cli/config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { getConfiguration, Structure } from "gruber";
22
import pkg from "./package.json" with { type: "json" };
3+
import { loadEnvFile } from "./lib.ts";
34

5+
loadEnvFile();
46
const config = getConfiguration();
57

68
const UNSET = "gruber://unset";

cli/lib.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
import cp from "node:child_process";
2+
import fs from "node:fs";
3+
import process from "node:process";
24
import { promisify } from "node:util";
35

46
export const exec = promisify(cp.exec);
7+
8+
export function loadEnvFile() {
9+
if (fs.existsSync(".env")) process.loadEnvFile(".env");
10+
}

cli/main.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ import process from "node:process";
77
import yargs from "yargs";
88
import { hideBin } from "yargs/helpers";
99

10-
import { outputConfiguration } from "./config.ts";
10+
import { appConfig, outputConfiguration } from "./config.ts";
1111
import { runTool } from "./run.ts";
1212

1313
const cli = yargs()
1414
.demandCommand(1, "A command is required")
1515
.recommendCommands()
16+
.version(appConfig.meta.version)
1617
.help();
1718

1819
cli.command(

cli/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
},
1010
"dependencies": {
1111
"gruber": "^0.9.0-beta.1",
12-
"minio": "^8.0.5",
12+
"minio": "8.0.4",
1313
"yargs": "^18.0.0"
1414
},
1515
"devDependencies": {

cli/run.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import fs from "node:fs";
2+
import { fileURLToPath } from "node:url";
23
import { appConfig } from "./config.ts";
34
import * as Minio from "minio";
45
import { exec } from "./lib.ts";
@@ -62,22 +63,22 @@ export async function runTool(options: RunOptions) {
6263
// Work out where to put the build locally & see if it already exists
6364
const local = new URL(`../tiles/${target.name}`, import.meta.url);
6465
let stat = await fs.promises.stat(local).catch(() => null);
65-
console.log("local=%o", stat?.isFile() ?? false);
66+
console.log("local=%o ", stat?.isFile() ?? false);
6667

6768
// If it doesn't exist, use the pmtiles CLI to extract and download it
6869
if (!stat?.isFile()) {
6970
const command = [
7071
"pmtiles",
7172
"extract",
7273
url.toString(),
73-
local,
74+
fileURLToPath(local),
7475
"--bbox=" + target.bbox.join(","),
7576
].join(" ");
7677

7778
if (options.dryRun) {
7879
console.log("dry-run:\n ", command);
7980
} else {
80-
console.log("downloading file");
81+
console.log("downloading file=%o", fileURLToPath(local));
8182
await exec(command);
8283
}
8384
}
@@ -96,16 +97,24 @@ export async function runTool(options: RunOptions) {
9697
if (options.dryRun) {
9798
console.log("dry-run: upload to s3 object=%o", tilesName);
9899
} else {
99-
console.log("upload", tilesName);
100+
const metadata = {
101+
...appConfig.s3.objectMetadata,
102+
"content-type": "application/vnd.pmtiles",
103+
};
104+
105+
console.log(
106+
"upload name=%o chunk=%o metadata=%O",
107+
tilesName,
108+
s3.calculatePartSize(stat!.size),
109+
metadata,
110+
);
111+
100112
await s3.putObject(
101113
appConfig.s3.bucketName,
102114
tilesName,
103115
stream,
104116
stat!.size,
105-
{
106-
...appConfig.s3.objectMetadata,
107-
"content-type": "application/vnd.pmtiles",
108-
},
117+
metadata,
109118
);
110119
}
111120

package-lock.json

Lines changed: 27 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)