UNPKG

2.05 kBJavaScriptView Raw
1const fs = require('fs');
2const path = require('path');
3const NODE_ENV = process.env['NODE_ENV'];
4const GLAD_ENV = process.env['GLAD_ENV'];
5let { get } = require('../namespace/object');
6let { chalk } = require('../namespace/console');
7let { ok, error } = chalk;
8let { exit } = process;
9const debug = require('debug')('glad');
10
11module.exports = class Project {
12
13 constructor (projectPath) {
14 debug('Project:constructor');
15 this.cliPath = path.join(__dirname, '../..');
16 this.projectPath = projectPath || process.cwd();
17 this.packagePath = path.join(this.projectPath, "package.json");
18 this.configPath = path.join(this.projectPath, "config.js");
19 this.hooksPath = path.join(this.projectPath, "hooks.js");
20 this.modelsPath = path.join(this.projectPath, "models");
21 this.controllersPath = path.join(this.projectPath, "controllers");
22 this.routesPath = path.join(this.projectPath, "routes");
23 this.viewsPath = path.join(this.projectPath, "views");
24
25 this.development = !NODE_ENV || GLAD_ENV === "development" || NODE_ENV === "development";
26 this.staging = GLAD_ENV === "development" || NODE_ENV === "staging";
27 this.production = GLAD_ENV === "development" || NODE_ENV === "production";
28 }
29
30 initialize () {
31 debug('Project:initialize');
32 if (this.isProject()) {
33 this.config = require(this.configPath);
34 this.orm = this.config.orm || 'mongoose';
35 ok(`Glad version ${this.cliVersion}`);
36 ok(`${new Date()}`);
37 ok(`Project root ${this.projectPath}`);
38 } else {
39 error(`You don't seem to be in a valid Glad project. Your current cwd is ${this.projectPath}`);
40 exit(1);
41 }
42 }
43
44 isProject () {
45 debug('Project:isProject');
46 let hasPackageFile = fs.existsSync(this.packagePath);
47
48 if (hasPackageFile) {
49 this.package = require(this.packagePath);
50 this.cliVersion = get(this.package, 'dependencies.glad');
51 return !!this.cliVersion;
52 } else {
53 return false;
54 }
55 }
56
57}