Skip to content

Commit

Permalink
🤘Don't require jsons to run
Browse files Browse the repository at this point in the history
  • Loading branch information
VandeurenGlenn committed Mar 8, 2017
1 parent a5536d5 commit a8e2a28
Show file tree
Hide file tree
Showing 12 changed files with 9,084 additions and 4,655 deletions.
52 changes: 37 additions & 15 deletions bin/backed.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -256,12 +256,12 @@ class Server {
}

const {readFileSync} = require('fs');

class Config {
constructor() {
let config = this.importConfig();
const name = this.importPackageName() || this.importBowerName();
return this.updateConfig(config, name);
this.importConfig().then(config => {
const name = this.importPackageName() || this.importBowerName();
return this.updateConfig(config, name);
});
}

/**
Expand All @@ -271,34 +271,56 @@ class Config {
* @return {object|array|function|class} module or file
*/
require(path) {
let root = process.cwd();
root += `/${path}`;
try {
return require(root);
} catch (error) {
return console.warn(error);
}
return new Promise((resolve, reject) => {
let root = process.cwd();
root += `/${path}`;
try {
let required = require(root);
resolve(required);
} catch (error) {
reject(error);
}
});
}

/**
* @return {object} value of 'backed.json'
*/
importConfig() {
return this.require('backed.json');
return new Promise((resolve, reject) => {
this.require('backed.json').then(config => {
resolve(config);
}).catch(() => {
logger.warn('backed.json:: not found, using default options.');
resolve({
name: 'your-element'
});
});
})
}

/**
* @return {string} name from 'package.json'
*/
importPackageName() {
return JSON.parse(readFileSync(`${process.cwd()}/package.json`)).name;
try {
return JSON.parse(readFileSync(`${process.cwd()}/package.json`)).name;
} catch(e) {
logger.warn('no package.json found');
}
return null;
}

/**
* @return {string} name from 'package.json'
* @return {string} name from 'bower.json'
*/
importBowerName() {
return JSON.parse(readFileSync(`${process.cwd()}/bower.json`)).name;
try {
return JSON.parse(readFileSync(`${process.cwd()}/bower.json`)).name;
} catch(e) {
logger.warn('no bower.json found');
}
return null;
}

/**
Expand Down
Loading

0 comments on commit a8e2a28

Please sign in to comment.