You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
1.3 KiB
54 lines
1.3 KiB
#!/usr/bin/env node
|
|
|
|
import { exit } from "process";
|
|
import { resolve, basename } from "path";
|
|
import { readCliArgument } from "./lib/arguments.js";
|
|
import { ERROR } from "./lib/result.js";
|
|
import { readFile, writeFile } from "./lib/file.js";
|
|
import { parse } from "./lib/gpx.js";
|
|
|
|
async function main() {
|
|
// Read cli argument. Exit if there are no one.
|
|
const argumentReadingResult = readCliArgument();
|
|
|
|
if (argumentReadingResult[0] === ERROR) {
|
|
console.error(argumentReadingResult[1]);
|
|
exit(1);
|
|
}
|
|
|
|
const filePath = resolve(argumentReadingResult[1]);
|
|
|
|
// Try to open file. Exit if some errors.
|
|
const fileReadingResult = await readFile(filePath);
|
|
|
|
if (fileReadingResult[0] === ERROR) {
|
|
console.error(fileReadingResult[1]);
|
|
exit(1);
|
|
}
|
|
|
|
const parsedSegments = parse(fileReadingResult[1]);
|
|
|
|
if (parsedSegments[0] === ERROR) {
|
|
console.error(parsedSegments[1]);
|
|
exit(1);
|
|
}
|
|
|
|
const segmentsToCsv = parsedSegments[1]
|
|
.map((segment) => segment.join(","))
|
|
.join("\n");
|
|
|
|
const baseFileName = basename(filePath, ".gpx");
|
|
const fileName = resolve(`${baseFileName}.csv`);
|
|
const writeFileResult = await writeFile(fileName, segmentsToCsv);
|
|
|
|
if (writeFileResult[0] === ERROR) {
|
|
console.error(writeFileResult[1]);
|
|
exit(1);
|
|
} else {
|
|
console.log(writeFileResult[1]);
|
|
exit(0);
|
|
}
|
|
}
|
|
|
|
main();
|