Skip to content
This repository was archived by the owner on Jan 21, 2024. It is now read-only.

Commit cfb369e

Browse files
committed
Fix: Fixes #32 - Build in docker.
1 parent da2a5e1 commit cfb369e

File tree

10 files changed

+129
-57
lines changed

10 files changed

+129
-57
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,18 @@ $ npm install -g api-console-cli
3030

3131
## Command Overview
3232

33-
### `api-console [COMMAND] --help`
33+
### General options
34+
35+
#### `api-console [COMMAND] --help`
3436

3537
Run `api-console --help` to get a list of supported commands. Pass it a command name (ex: `api-console build --help`) to get detailed information about that command and the options it supports.
3638

39+
#### `api-console [COMMAND] [--options] --no-ga`
40+
41+
Disables Google Analytics and asking to enable Google Analytics. Should be used in automated environment. The library detects most common CI environments.
42+
43+
## Commands
44+
3745
### `api-console build [options] -a path/to/api.raml -t "RAML 1.0"`
3846

3947
__NOTE__ until API console version 5 is in preview add `-n "5.0.0-preview-1"` to the command to install preview version.

bin/api-console-build.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ program
3939
.option('--no-web-animations', docs.noWebAnimations)
4040
.option('--no-cache', docs.noCache)
4141
.option('--verbose', 'Print verbose messages.')
42-
.option('--no-ga', 'Disallow Google Analytics when running this command')
42+
.option('--no-ga', docs.noGa)
4343
.on('--help', function() {
4444
console.log('\n\n Examples:');
4545
console.log();

bin/api-console-cli.js

Lines changed: 103 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
#!/usr/bin/env node
2-
32
'use strict';
4-
53
process.title = 'api-console';
6-
74
const semver = require('semver');
85
// Early exit if the user's node version is too low.
96
if (!semver.satisfies(process.version, '>=6.4')) {
@@ -16,46 +13,110 @@ if (!semver.satisfies(process.version, '>=6.4')) {
1613
process.exit(1);
1714
}
1815

19-
function isCi() {
20-
const env = process.env;
21-
return !!(env.CI ||
22-
env.CONTINUOUS_INTEGRATION ||
23-
env.BUILD_NUMBER ||
24-
env.RUN_ID ||
25-
exports.name ||
26-
false);
27-
}
28-
29-
const noGa = process.argv.indexOf('--no-ga') !== -1;
16+
const fs = require('fs-extra');
17+
const path = require('path');
18+
const {GaHelper} = require('../lib/ga-helper');
19+
class ApiConsoleCli {
20+
/**
21+
* Tests if current environment is CI environment.
22+
* @return {Boolean}
23+
*/
24+
get isCi() {
25+
const env = process.env;
26+
const exists = fs.pathExistsSync(path.join('/', '.dockerenv'));
27+
return !!(exists || env.CI || env.CONTINUOUS_INTEGRATION ||
28+
env.BUILD_NUMBER || env.RUN_ID || exports.name || false);
29+
}
30+
/**
31+
* Tests if Google Analytics should not be allowed when running the command.
32+
* This includes GA question.
33+
* @return {Boolean} True when GA command cannot run.
34+
*/
35+
get noGa() {
36+
return process.argv.indexOf('--no-ga') !== -1 || process.argv.indexOf('--help') !== -1;
37+
}
38+
/**
39+
* Runs the CLI command.
40+
*
41+
* @return {Promise}
42+
*/
43+
runCommand() {
44+
if (this.running) {
45+
return;
46+
}
47+
this.running = true;
48+
require('./run');
49+
return Promise.resolve();
50+
}
51+
/**
52+
* Initializes the library.
53+
* @return {Promise}
54+
*/
55+
init() {
56+
if (this.noGa || this.isCi) {
57+
return this.runCommand();
58+
}
59+
return this._initGa();
60+
}
61+
/**
62+
* Initializes GA configuration.
63+
* Asks the user to allows GA when configuration is missing.
64+
* @return {Promise}
65+
*/
66+
_initGa() {
67+
this.helper = new GaHelper();
68+
return this.helper.gaAllowed()
69+
.then((allowed) => this._processGaAllowed(allowed));
70+
}
71+
/**
72+
* Runs instructions after reading the configuration.
73+
* @param {?Boolean} allowed Optional, state of GA enabled flag.
74+
* @return {Promise}
75+
*/
76+
_processGaAllowed(allowed) {
77+
if (typeof allowed === 'boolean') {
78+
return this.runCommand();
79+
}
80+
return this._askUser();
81+
}
82+
/**
83+
* Asks the user to enable GA, saves the answer, and runs the command.
84+
* The question is dissmissed after about 10s.
85+
*
86+
* @return {Promise}
87+
*/
88+
_askUser() {
89+
this.queryTimeout = setTimeout(() => {
90+
this.queryTimeout = undefined;
91+
this.runCommand();
92+
return;
93+
}, 10000);
94+
return this._getAnswer()
95+
.then((answer) => this._processAnswer(answer))
96+
.then(() => this.runCommand())
97+
.catch(() => {});
98+
}
3099

31-
function run() {
32-
require('./run');
33-
}
100+
_getAnswer() {
101+
const inquirer = require('inquirer');
102+
return inquirer
103+
.prompt([{
104+
type: 'confirm',
105+
name: 'gaEnabled',
106+
message: 'Allow anonymous usage statistics to help improve our CLI tools?',
107+
default: true
108+
}]);
109+
}
34110

35-
if (noGa) {
36-
run();
37-
} else {
38-
const {GaHelper} = require('../lib/ga-helper');
39-
const helper = new GaHelper();
40-
helper.gaAllowed()
41-
.then((allowed) => {
42-
if (allowed === undefined) {
43-
if (isCi()) {
44-
run();
45-
return;
46-
}
47-
const inquirer = require('inquirer');
48-
inquirer
49-
.prompt([{
50-
type: 'confirm',
51-
name: 'gaEnabled',
52-
message: 'Allow anonymous usage statistics to help improve our CLI tools?',
53-
default: true
54-
}])
55-
.then((answer) => helper.updatePermissions(answer.gaEnabled))
56-
.then(() => run());
57-
} else {
58-
run();
111+
_processAnswer(answer) {
112+
if (!this.queryTimeout) {
113+
throw new Error('Timed out');
59114
}
60-
});
115+
clearTimeout(this.queryTimeout);
116+
this.queryTimeout = undefined;
117+
return this.helper.updatePermissions(answer.gaEnabled);
118+
}
61119
}
120+
121+
const cli = new ApiConsoleCli();
122+
cli.init();

bin/api-console-generate-model.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ program
1414
.option('-o, --output [value]', 'Output file. Default to "./api-model.json"')
1515
.option('-p, --pretty-print', 'Generated JSON will be formatted')
1616
.option('--verbose', 'Print verbose messages')
17-
.option('--no-ga', 'Disallow Google Analytics when running this command')
17+
.option('--no-ga', 'Disable Google Analytics when running this command')
1818
.action(function(apiFile, options) {
1919
console.log();
2020
if (!apiFile) {

bin/api-console-serve.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ program
2626
.option('-P, --protocol [value]', docs.protocol)
2727
.option('-k, --key-path [value]', docs.keyPath)
2828
.option('-c, --cert-path [value]', docs.certPath)
29-
.option('--no-ga', 'Disallow Google Analytics when running this command')
29+
.option('--no-ga', docs.noGa)
3030
.on('--help', function() {
3131
console.log('\n\n Examples:');
3232
console.log();

bin/build-help.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@
1414
"noXhr": "Embedded build only. API console HTTP transport library is not included",
1515
"noWebAnimations": "Embedded build only. Currentlty do not use this option. Web animations library is not included into the build",
1616
"noCache": "Always generate new version of the console, event if a build is cached",
17-
"themeFile": "Replaces default API console theme file with custom version"
17+
"themeFile": "Replaces default API console theme file with custom version",
18+
"noGa": "Disable Google Analytics when running any command"
1819
}

bin/run.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ program
1616
'Generates a JSON file from API spec file')
1717
.command('serve [path]',
1818
'Creates a www server to run the console locally')
19+
.option('--no-ga', 'Disable Google Analytics when running any command')
1920
.parse(process.argv);

bin/serve-help.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@
1010
"openPath": "The URL path to open in each browser",
1111
"protocol": "The protocol, choice of [http, https], defaults to http",
1212
"keyPath": "The file path to ssl key",
13-
"certPath": "The file path to ssl cert"
13+
"certPath": "The file path to ssl cert",
14+
"noGa": "Disable Google Analytics when running any command"
1415
}

package-lock.json

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "api-console-cli",
3-
"version": "1.0.10",
3+
"version": "1.0.11",
44
"description": "A set of dev tools for API console",
55
"main": "main.js",
66
"scripts": {

0 commit comments

Comments
 (0)