1 | var controllerDir;
|
2 | var appName;
|
3 |
|
4 | function getAppName() {
|
5 | var parts = __dirname.replace(/\\/g, '/').split('/');
|
6 | return parts[parts.length - 2].split('.')[0];
|
7 | }
|
8 |
|
9 |
|
10 | function getControllerDir(isInstall) {
|
11 | var fs = require('fs');
|
12 |
|
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 |
|
45 | function 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 | }
|
54 | appName = getAppName();
|
55 | controllerDir = getControllerDir(typeof process != 'undefined' && process.argv && process.argv.indexOf('--install') != -1);
|
56 |
|
57 | exports.controllerDir = controllerDir;
|
58 | exports.getConfig = getConfig;
|
59 | exports.adapter = require(controllerDir + '/lib/adapter.js');
|
60 | exports.appName = appName;
|