Skip to content

Commit

Permalink
Merge pull request #10 from mapbox/inspectJson
Browse files Browse the repository at this point in the history
Differentiate JSON errors between missing and invalid JSON
  • Loading branch information
jbranigan authored Dec 3, 2020
2 parents 571e2bf + 6682207 commit f737ec3
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 30 deletions.
18 changes: 4 additions & 14 deletions src/estimate.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from "fs";
import path from "path";
import { readConfig, readRecipe } from "./utils";
import ndjson from "ndjson";
import tarea from "@turf/area";
import tilebelt from "@mapbox/tilebelt";
Expand Down Expand Up @@ -85,19 +85,9 @@ const sizeJob = function (pwd, cnf, recipe) {

export default function estimate() {
const pwd = process.cwd();
let cnf, recipe;
try {
cnf = JSON.parse(fs.readFileSync(path.join(pwd, "mts-config.json")));
} catch (err) {
console.log("Missing mts-config.json in this directory.");
console.log("Run mtsds --config to generate.");
}
try {
recipe = JSON.parse(fs.readFileSync(path.join(pwd, "mts-recipe.json")));
} catch (err) {
console.log("Missing mts-recipe.json in this directory.");
console.log("Run mtsds --config to generate.");
}

const cnf = readConfig(pwd);
const recipe = readRecipe(pwd);

if (cnf && recipe) {
sizeJob(pwd, cnf, recipe);
Expand Down
20 changes: 4 additions & 16 deletions src/sync.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// import inquirer from "inquirer";
import fs from "fs";
import path from "path";
import sleep from "await-sleep";
import { readConfig, readRecipe } from "./utils";
import {
initService,
deleteTilesetSource,
Expand Down Expand Up @@ -42,19 +40,9 @@ async function runServices(cnf, recipe) {

export default function sync() {
const pwd = process.cwd();
let cnf, recipe;
try {
cnf = JSON.parse(fs.readFileSync(path.join(pwd, "mts-config.json")));
} catch (err) {
console.log("Missing mts-config.json in this directory.");
console.log("Run mtsds --config to generate.");
}
try {
recipe = JSON.parse(fs.readFileSync(path.join(pwd, "mts-recipe.json")));
} catch (err) {
console.log("Missing mts-recipe.json in this directory.");
console.log("Run mtsds --config to generate.");
}

const cnf = readConfig(pwd);
const recipe = readRecipe(pwd);

if (cnf && recipe) {
runServices(cnf, recipe);
Expand Down
49 changes: 49 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import fs from "fs";
import path from "path";

const checkFileExistsSync = function(filepath){
let flag = true;
try {
fs.accessSync(filepath, fs.constants.F_OK);
} catch (e){
flag = false;
}
return flag;
};

const readConfig = function (pwd) {
const cnfFile = path.join(pwd, "mts-config.json");
if (checkFileExistsSync(cnfFile)) {
try {
const cnf = JSON.parse(fs.readFileSync(cnfFile));
return cnf;
} catch (err) {
console.log("Invalid mts-config.json: parsing failed.");
console.log("Verify that mts-config.json is valid JSON.");
}
} else {
console.log("Missing mts-config.json in this directory.");
console.log("Run mtsds --config to generate.");
}
};

const readRecipe = function (pwd) {
const recipeFile = path.join(pwd, "mts-recipe.json");
if (checkFileExistsSync(recipeFile)) {
try {
const recipe = JSON.parse(fs.readFileSync(recipeFile));
return recipe;
} catch (err) {
console.log("Invalid mts-recipe.json: parsing failed.");
console.log("Verify that mts-recipe.json is valid JSON.");
}
} else {
console.log("Missing mts-recipe.json in this directory.");
console.log("Run mtsds --config to generate.");
}
};

export {
readConfig,
readRecipe
};

0 comments on commit f737ec3

Please sign in to comment.