UNPKG

3.4 kBJavaScriptView Raw
1const chalk = require( 'chalk' );
2const nconf = require( 'nconf' );
3const moment = require( 'moment' );
4const tz = require( 'moment-timezone' );
5const fs = require( 'fs' );
6const p = require( 'path' );
7const app = require( './app' );
8const util = require( './util' );
9
10const config = {
11
12 /**
13 * Update the name to a local project.
14 * @param {String} slug The project's slug.
15 * @param {String} name Project name.
16 */
17 updateName( slug, name ) {
18 nconf.set( `local_projects:${slug}:name`, name );
19 nconf.save();
20 },
21
22 /**
23 * Update the type to a local project.
24 * @param {String} slug The project's slug.
25 * @param {String} type Project type.
26 */
27 updateType( slug, type ) {
28 nconf.set( `local_projects:${slug}:type`, type );
29 nconf.save();
30 },
31
32 /**
33 * Update the url to a local project.
34 * @param {String} slug The project's slug.
35 * @param {String} url The project's url.
36 */
37 updateUrl( slug, url ) {
38 nconf.set( `local_projects:${slug}:url`, url );
39 nconf.save();
40 },
41
42 /**
43 * Update the path to a local project.
44 * @param {String} slug The project's slug.
45 * @param {String} path Project path.
46 */
47 updatePath( slug, path ) {
48 nconf.set( `local_projects:${slug}:path`, path );
49 nconf.save();
50 },
51
52 /**
53 * Update the project last updated time.
54 * @param {String} slug The project's slug.
55 */
56 updateLastUpdatedTime( slug ) {
57 const now = moment().tz( 'America/New_York' ).toISOString();
58
59 nconf.set( `local_projects:${slug}:last_updated`, now );
60 nconf.save();
61 },
62
63 /**
64 * Update the local project ports.
65 * @param {String} slug The project's slug.
66 * @param {Array} ports The project's reserved ports.
67 */
68 updatePorts( slug, ports ) {
69 nconf.set( `local_projects:${slug}:ports`, ports );
70 nconf.save();
71 },
72
73 /**
74 * Update the local project's docker container name(s).
75 * @param {String} slug The project's slug.
76 * @param {Array} containerNames Project docker container name(s).
77 */
78 updateContainerNames( slug, containerNames ) {
79 nconf.set( `local_projects:${slug}:containers`, containerNames );
80 nconf.save();
81 },
82
83 /**
84 * Remove the path to a local project.
85 * @param {String} slug The project's slug.
86 */
87 removePath( slug ) {
88 nconf.remove( `local_projects:${slug}:path` );
89 nconf.save();
90 },
91
92 /**
93 * Update the projects object.
94 * @param {Object} projects Projects object.
95 */
96 updateLocalProjects( projects ) {
97 nconf.set( 'local_projects', projects );
98 nconf.save();
99 },
100
101 /**
102 * Create main config.
103 */
104 create() {
105 // Create main config directory if it does not yet exist.
106 if ( !fs.existsSync( app.configDir ) ) {
107 fs.mkdirSync( app.configDir );
108 }
109
110 // Create config file containing empty object.
111 fs.writeFileSync( app.config, '{}' );
112
113 // Write to config file.
114 nconf.file( app.config );
115 nconf.set( 'current_profile', '' );
116 nconf.save();
117 },
118
119 /**
120 * Update the current profile object.
121 * @param {String} path The absolute path to the project config.
122 */
123 updateCurrentProfile( path ) {
124 nconf.file( app.config );
125 nconf.set( 'current_profile', path );
126 nconf.save();
127 },
128
129 /**
130 * Load the application config file.
131 */
132 load() {
133 nconf.file( app.config );
134 util.requireKeys( [
135 'current_profile',
136 ] );
137 },
138
139 /**
140 * Check if the main config exist.
141 * @return {Boolean}
142 */
143 async exists() {
144 const exists = await fs.existsSync( app.config );
145
146 return exists;
147 },
148
149};
150
151module.exports = config;