-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from mapbox/inspectJson
Differentiate JSON errors between missing and invalid JSON
- Loading branch information
Showing
3 changed files
with
57 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |