1 | const inquirer = require( 'inquirer' );
|
2 | const chalk = require( 'chalk' );
|
3 | const nconf = require( 'nconf' );
|
4 | const moment = require( 'moment' );
|
5 | const tz = require( 'moment-timezone' );
|
6 | const fs = require( 'fs' );
|
7 | const p = require( 'path' );
|
8 | const sh = require( 'shelljs' );
|
9 | const app = require( './app' );
|
10 | const prompt = require( './prompt' );
|
11 | const util = require( './util' );
|
12 | const config = require( './config' );
|
13 |
|
14 | const configProfile = {
|
15 |
|
16 | |
17 |
|
18 |
|
19 |
|
20 | load( path ) {
|
21 | nconf.file( path );
|
22 |
|
23 |
|
24 | util.requireKeys( [
|
25 | 'email',
|
26 | 'projects_url',
|
27 | 'project_types_url',
|
28 | 'default_clone_path',
|
29 | 'local_projects',
|
30 | ] );
|
31 | },
|
32 |
|
33 | |
34 |
|
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 |
|
46 | if ( !fs.existsSync( app.profilesDir ) ) {
|
47 | fs.mkdirSync( app.profilesDir );
|
48 | }
|
49 |
|
50 |
|
51 | fs.writeFileSync( profilePath, '{}' );
|
52 |
|
53 |
|
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 |
|
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 |
|
106 | module.exports = configProfile;
|