Skip to content

Commit

Permalink
Improve error messages when compiling app_settings.json
Browse files Browse the repository at this point in the history
  • Loading branch information
alxndrsn committed May 30, 2017
1 parent 094f2f1 commit c0f9c3d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 17 deletions.
45 changes: 30 additions & 15 deletions src/fn/compile-app-settings.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,48 @@
const fs = require('../lib/sync-fs');

const FILES = {
app_settings: 'json',
'contact-summary': 'js',
nools: 'js',
tasks: 'json',
targets: 'json',
};

module.exports = (project /*, couchUrl */) => {
return Promise.resolve()
.then(() => {
const readProjectFile = f => fs.read(`${project}/${f}`);
const files = filesFor(project);

const files = {};
[
'app_settings.json',
'contact-summary.js',
'nools.js',
'tasks.json',
'targets.json',
].forEach(f => files[simple(f)] = readProjectFile(f));
const app_settings = files.app_settings;

const app_settings = JSON.parse(files.app_settings);

app_settings.contact_summary = cleanJs(files.contact_summary);
app_settings.contact_summary = files.contact_summary;

app_settings.tasks = {
rules: cleanJs(files.nools),
schedules: JSON.parse(files.tasks),
targets: JSON.parse(files.targets),
rules: files.nools,
schedules: files.tasks,
targets: files.targets,
};

fs.writeJson(`${project}/app_settings.json`, app_settings);
});
};

const filesFor = project => {
const files = {};
for(const name in FILES) {
const type = FILES[name];
const path = `${project}/${name}.${type}`;

files[simple(name)] = readerFor[type](path);
}
return files;
};

const readerFor = {
js: path => cleanJs(fs.read(path)),
json: fs.readJson,
};

const simple = s => s.replace(/\..*/, '').replace('-', '_');
const cleanJs = js =>
js.split('\n')
Expand Down
19 changes: 17 additions & 2 deletions src/lib/sync-fs.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
const fs = require('fs');
const warn = require('../lib/log').warn;

module.exports = {
exists: fs.existsSync,
mkdir: path => { try { fs.mkdirSync(path); } catch(e) { /* yum yum */ } },
read: path => fs.readFileSync(path, { encoding:'utf8' }),
readJson: path => JSON.parse(module.exports.read(path)),
read: path => {
try {
return fs.readFileSync(path, { encoding:'utf8' });
} catch(e) {
warn(`Error reading file: ${path}`);
throw e;
}
},
readJson: path => {
try {
return JSON.parse(module.exports.read(path));
} catch(e) {
warn(`Error parsing JSON in: ${path}`);
throw e;
}
},
readBinary: path => fs.readFileSync(path),
readdir: fs.readdirSync,
write: (path, content) => fs.writeFileSync(path, content, 'utf8'),
Expand Down

0 comments on commit c0f9c3d

Please sign in to comment.