UNPKG

1.17 kBJavaScriptView Raw
1var fs = require('fs');
2var log = require('../lib/log');
3var _ = require('lodash');
4
5// Register a new profile in the 4front global config file
6module.exports = function(program, done) {
7 if (_.isEmpty(program.profileName))
8 return done("Please provide a profile name with --profile-name option");
9
10 if (_.isEmpty(program.endpoint))
11 return done("Please provide an endpoint url with --endpoint option");
12
13 // Check if this profile already exists
14 if (_.any(program.globalConfig.profiles, {name: program.profileName}))
15 return done("There is already a profile with the name " + program.profileName);
16
17 var profile = {
18 name: program.profileName,
19 endpoint: program.endpoint
20 };
21
22 if (program.globalConfig.profiles.length === 0)
23 profile.default = true;
24
25 program.globalConfig.profiles.push(profile);
26
27 log.debug("writing global config to %s", program.globalConfigPath);
28 var configJson = JSON.stringify(program.globalConfig, null, 2);
29 fs.writeFile(program.globalConfigPath, configJson, function(err) {
30 if (err) return done(err);
31
32 log.success("Profile %s added to the .4front.json config", program.profileName);
33 done();
34 });
35};