UNPKG

2.55 kBJavaScriptView Raw
1var controllerDir;
2var appName;
3
4function getAppName() {
5 var parts = __dirname.replace(/\\/g, '/').split('/');
6 return parts[parts.length - 2].split('.')[0];
7}
8
9// Get js-controller directory to load libs
10function getControllerDir(isInstall) {
11 var fs = require('fs');
12 // Find the js-controller location
13 var controllerDir = __dirname.replace(/\\/g, '/');
14 controllerDir = controllerDir.split('/');
15 if (controllerDir[controllerDir.length - 3] == 'adapter') {
16 controllerDir.splice(controllerDir.length - 3, 3);
17 controllerDir = controllerDir.join('/');
18 } else if (controllerDir[controllerDir.length - 3] == 'node_modules') {
19 controllerDir.splice(controllerDir.length - 3, 3);
20 controllerDir = controllerDir.join('/');
21 if (fs.existsSync(controllerDir + '/node_modules/' + appName + '.js-controller')) {
22 controllerDir += '/node_modules/' + appName + '.js-controller';
23 } else if (fs.existsSync(controllerDir + '/node_modules/' + appName.toLowerCase() + '.js-controller')) {
24 controllerDir += '/node_modules/' + appName.toLowerCase() + '.js-controller';
25 } else if (!fs.existsSync(controllerDir + '/controller.js')) {
26 if (!isInstall) {
27 console.log('Cannot find js-controller');
28 process.exit(10);
29 } else {
30 process.exit();
31 }
32 }
33 } else {
34 if (!isInstall) {
35 console.log('Cannot find js-controller');
36 process.exit(10);
37 } else {
38 process.exit();
39 }
40 }
41 return controllerDir;
42}
43
44// Read controller configuration file
45function getConfig() {
46 if (fs.existsSync(controllerDir + '/conf/' + appName + '.json')) {
47 return JSON.parse(fs.readFileSync(controllerDir + '/conf/' + appName + '.json'));
48 } else if (fs.existsSync(controllerDir + '/conf/' + appName.toLowerCase() + '.json')) {
49 return JSON.parse(fs.readFileSync(controllerDir + '/conf/' + appName.toLowerCase() + '.json'));
50 } else {
51 throw new Error('Cannot find ' + controllerDir + '/conf/' + appName + '.json');
52 }
53}
54appName = getAppName();
55controllerDir = getControllerDir(typeof process != 'undefined' && process.argv && process.argv.indexOf('--install') != -1);
56
57exports.controllerDir = controllerDir;
58exports.getConfig = getConfig;
59exports.adapter = require(controllerDir + '/lib/adapter.js');
60exports.appName = appName;