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

Differentiate JSON errors between missing and invalid JSON #10

Merged
merged 1 commit into from
Dec 3, 2020
Merged
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
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
};