UNPKG

3.07 kBJavaScriptView Raw
1const inquirer = require( 'inquirer' );
2const chalk = require( 'chalk' );
3const nconf = require( 'nconf' );
4const moment = require( 'moment' );
5const tz = require( 'moment-timezone' );
6const fs = require( 'fs' );
7const p = require( 'path' );
8const sh = require( 'shelljs' );
9const app = require( './app' );
10const prompt = require( './prompt' );
11const util = require( './util' );
12const config = require( './config' );
13
14const configProfile = {
15
16 /**
17 * Load selected profile.
18 * @param {String} path Path to the config file.
19 */
20 load( path ) {
21 nconf.file( path );
22
23 // Ensure certain keys exist.
24 util.requireKeys( [
25 'email',
26 'projects_url',
27 'project_types_url',
28 'default_clone_path',
29 'local_projects',
30 ] );
31 },
32
33 /**
34 * Create a config profile.
35 */
36 async create() {
37 const configProfileNameObj = await inquirer.prompt( prompt.getProfileNamePrompt() );
38 const profilePath = `${app.profilesDir}/${configProfileNameObj.answer}.json`;
39 const currentProfilePath = await util.getCurrentProfilePath();
40 const emailObj = await inquirer.prompt( prompt.getEmailPrompt() );
41 const projectsUrlObj = await inquirer.prompt( prompt.getProjectsUrlPrompt() );
42 const typesUrlObj = await inquirer.prompt( prompt.getTypesUrlPrompt() );
43 const defaultDirObj = await inquirer.prompt( prompt.getDefaultDirPrompt() );
44
45 // Create profiles directory if it does not yet exist.
46 if ( !fs.existsSync( app.profilesDir ) ) {
47 fs.mkdirSync( app.profilesDir );
48 }
49
50 // Create profile file containing empty object.
51 fs.writeFileSync( profilePath, '{}' );
52
53 // Write to profile.
54 nconf.file( profilePath );
55 nconf.set( 'email', emailObj.answer );
56 nconf.set( 'projects_url', projectsUrlObj.answer );
57 nconf.set( 'project_types_url', typesUrlObj.answer );
58 nconf.set( 'default_clone_path', defaultDirObj.answer );
59 nconf.set( 'local_projects', {});
60 nconf.save();
61
62 console.log( chalk`{green Created profile "${configProfileNameObj.answer}"}` );
63
64 if ( currentProfilePath === '' ) {
65 config.updateCurrentProfile( profilePath );
66 }
67 },
68
69 /**
70 * Remove a config profile.
71 */
72 async remove() {
73 const numberOfProfiles = util.getNumberOfProfiles();
74 const currentProfilePath = util.getCurrentProfilePath();
75
76 if ( numberOfProfiles === 1 ) {
77 console.log( chalk`{red You cannot remove your last profile.}` );
78 process.exit( 1 );
79 }
80
81 const changeProfilePromptObj = await inquirer.prompt( prompt.getChangeProfilePrompt() );
82
83 if ( currentProfilePath === changeProfilePromptObj.answer ) {
84 console.log( chalk`{red You cannot remove the current profile.}` );
85 process.exit( 1 );
86 }
87
88 const confirmPromptObj = await inquirer.prompt( prompt.getConfirmPrompt() );
89
90 if ( confirmPromptObj.answer ) {
91 const path = changeProfilePromptObj.answer;
92 const name = util.getProfileName( path );
93
94 if ( path ) {
95 sh.rm( '-rf', path );
96 console.log( chalk`{green.bold Removed ${name}.}` );
97 } else {
98 console.log( chalk`{red.bold Profile path not found.}` );
99 process.exit( 1 );
100 }
101 }
102 },
103
104};
105
106module.exports = configProfile;