1 | const fs = require('fs');
|
2 | const path = require('path');
|
3 | const NODE_ENV = process.env['NODE_ENV'];
|
4 | const GLAD_ENV = process.env['GLAD_ENV'];
|
5 | let { get } = require('../namespace/object');
|
6 | let { chalk } = require('../namespace/console');
|
7 | let { ok, error } = chalk;
|
8 | let { exit } = process;
|
9 | const debug = require('debug')('glad');
|
10 |
|
11 | module.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 | }
|