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