Skip to content

Commit 90ad919

Browse files
authored
Merge pull request #248 from SeaLife/master
#218 implementing non-interactive config
2 parents 3828514 + b94d640 commit 90ad919

File tree

3 files changed

+123
-10
lines changed

3 files changed

+123
-10
lines changed

src/commands/config.js

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,31 @@ exports.builder = {
123123
alias: 'f',
124124
description: 'generate a new config for function deployment',
125125
},
126+
domain: {
127+
alias: ['d', 'domain'],
128+
description: 'sets the domain (enables non-interactive mode)',
129+
},
130+
project: {
131+
alias: ['p', 'project'],
132+
description: 'sets the project name (enables non-interactive mode)',
133+
},
134+
name: {
135+
alias: ['n', 'name'],
136+
description: 'sets the name (enables non-interactive mode)',
137+
},
138+
restart: {
139+
alias: ['r', 'restart'],
140+
description: 'sets the restart option (enables non-interactive mode)',
141+
},
142+
hostname: {
143+
alias: ['hostname'],
144+
description: 'sets the hostname (enables non-interactive mode)',
145+
},
126146
};
127-
exports.handler = async ({func} = {}) => {
147+
exports.handler = async ({func, ...args} = {}) => {
128148
const workdir = process.cwd();
129149
const folderName = path.basename(workdir);
150+
const nonInteractive = Object.keys(args).some(key => args[key].length > 0);
130151
const configPath = path.join(workdir, 'exoframe.json');
131152
let defaultConfig = {
132153
name: folderName,
@@ -365,12 +386,32 @@ exports.handler = async ({func} = {}) => {
365386
}
366387
};
367388

368-
// get values from user
369-
const newConfig = await inquirer.prompt(prompts);
389+
let newConfig = defaultConfig;
390+
391+
if (nonInteractive) {
392+
console.log(chalk.yellow('Mode changed to'), 'non-interactive');
393+
}
394+
395+
const overrideFromArgument = (key, value) => {
396+
if (!value) return;
397+
console.log('Setting', chalk.red(key), 'to', chalk.yellow(value));
398+
newConfig[key] = value;
399+
};
400+
401+
overrideFromArgument('domain', args.domain);
402+
overrideFromArgument('name', args.name);
403+
overrideFromArgument('project', args.project);
404+
overrideFromArgument('restart', args.restart);
405+
overrideFromArgument('hostname', args.hostname);
406+
407+
if (!nonInteractive) {
408+
// get values from user
409+
newConfig = await inquirer.prompt(prompts);
370410

371-
// update users for auth if needed
372-
if (newConfig.basicAuth) {
373-
newConfig.users = await askForUsers();
411+
// update users for auth if needed
412+
if (newConfig.basicAuth) {
413+
newConfig.users = await askForUsers();
414+
}
374415
}
375416

376417
writeConfig(configPath, {...defaultConfig, ...newConfig});

test/__snapshots__/config.test.js.snap

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,48 @@ Array [
1010
],
1111
]
1212
`;
13+
14+
exports[`Should generate the config with parameters 1`] = `
15+
Array [
16+
Array [
17+
"Creating new config..",
18+
],
19+
Array [
20+
"Mode changed to",
21+
"non-interactive",
22+
],
23+
Array [
24+
"Setting",
25+
"domain",
26+
"to",
27+
"test123.dev",
28+
],
29+
Array [
30+
"Setting",
31+
"name",
32+
"to",
33+
"test name 123",
34+
],
35+
Array [
36+
"Setting",
37+
"project",
38+
"to",
39+
"give-project-name",
40+
],
41+
Array [
42+
"Setting",
43+
"restart",
44+
"to",
45+
"unless-stopped",
46+
],
47+
Array [
48+
"Setting",
49+
"hostname",
50+
"to",
51+
"test123.dev",
52+
],
53+
Array [
54+
"Config created!",
55+
],
56+
]
57+
`;

test/config.test.js

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,7 @@ beforeAll(() => {
6060
} catch (e) {
6161
// no config, just exit
6262
}
63-
});
6463

65-
// test config generation
66-
test('Should generate config file', done => {
67-
// stup inquirer answers
6864
sinon
6965
.stub(inquirer, 'prompt')
7066
.onFirstCall()
@@ -73,6 +69,37 @@ test('Should generate config file', done => {
7369
.callsFake(() => Promise.resolve(users[0]))
7470
.onThirdCall()
7571
.callsFake(() => Promise.resolve(users[1]));
72+
});
73+
74+
test('Should generate the config with parameters', done => {
75+
// spy on console
76+
const consoleSpy = sinon.spy(console, 'log');
77+
78+
config({
79+
domain: 'test123.dev',
80+
restart: 'unless-stopped',
81+
project: 'give-project-name',
82+
name: 'test name 123',
83+
hostname: 'test123.dev',
84+
}).then(() => {
85+
expect(consoleSpy.args).toMatchSnapshot();
86+
// then check config changes
87+
const cfg = yaml.safeLoad(fs.readFileSync(configPath, 'utf8'));
88+
expect(cfg.name).toEqual('test name 123');
89+
expect(cfg.restart).toEqual('unless-stopped');
90+
expect(cfg.domain).toEqual('test123.dev');
91+
expect(cfg.project).toEqual('give-project-name');
92+
expect(cfg.hostname).toEqual('test123.dev');
93+
// restore console
94+
console.log.restore();
95+
// remove corrupted config
96+
fs.unlinkSync(configPath);
97+
done();
98+
});
99+
});
100+
101+
// test config generation
102+
test('Should generate config file', done => {
76103
// spy on console
77104
const consoleSpy = sinon.spy(console, 'log');
78105
// execute login

0 commit comments

Comments
 (0)