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

Commit

Permalink
Fix: Fixes #32 - Build in docker.
Browse files Browse the repository at this point in the history
  • Loading branch information
jarrodek committed Jan 8, 2019
1 parent da2a5e1 commit cfb369e
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 57 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,18 @@ $ npm install -g api-console-cli

## Command Overview

### `api-console [COMMAND] --help`
### General options

#### `api-console [COMMAND] --help`

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.

#### `api-console [COMMAND] [--options] --no-ga`

Disables Google Analytics and asking to enable Google Analytics. Should be used in automated environment. The library detects most common CI environments.

## Commands

### `api-console build [options] -a path/to/api.raml -t "RAML 1.0"`

__NOTE__ until API console version 5 is in preview add `-n "5.0.0-preview-1"` to the command to install preview version.
Expand Down
2 changes: 1 addition & 1 deletion bin/api-console-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ program
.option('--no-web-animations', docs.noWebAnimations)
.option('--no-cache', docs.noCache)
.option('--verbose', 'Print verbose messages.')
.option('--no-ga', 'Disallow Google Analytics when running this command')
.option('--no-ga', docs.noGa)
.on('--help', function() {
console.log('\n\n Examples:');
console.log();
Expand Down
145 changes: 103 additions & 42 deletions bin/api-console-cli.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#!/usr/bin/env node

'use strict';

process.title = 'api-console';

const semver = require('semver');
// Early exit if the user's node version is too low.
if (!semver.satisfies(process.version, '>=6.4')) {
Expand All @@ -16,46 +13,110 @@ if (!semver.satisfies(process.version, '>=6.4')) {
process.exit(1);
}

function isCi() {
const env = process.env;
return !!(env.CI ||
env.CONTINUOUS_INTEGRATION ||
env.BUILD_NUMBER ||
env.RUN_ID ||
exports.name ||
false);
}

const noGa = process.argv.indexOf('--no-ga') !== -1;
const fs = require('fs-extra');
const path = require('path');
const {GaHelper} = require('../lib/ga-helper');
class ApiConsoleCli {
/**
* Tests if current environment is CI environment.
* @return {Boolean}
*/
get isCi() {
const env = process.env;
const exists = fs.pathExistsSync(path.join('/', '.dockerenv'));
return !!(exists || env.CI || env.CONTINUOUS_INTEGRATION ||
env.BUILD_NUMBER || env.RUN_ID || exports.name || false);
}
/**
* Tests if Google Analytics should not be allowed when running the command.
* This includes GA question.
* @return {Boolean} True when GA command cannot run.
*/
get noGa() {
return process.argv.indexOf('--no-ga') !== -1 || process.argv.indexOf('--help') !== -1;
}
/**
* Runs the CLI command.
*
* @return {Promise}
*/
runCommand() {
if (this.running) {
return;
}
this.running = true;
require('./run');
return Promise.resolve();
}
/**
* Initializes the library.
* @return {Promise}
*/
init() {
if (this.noGa || this.isCi) {
return this.runCommand();
}
return this._initGa();
}
/**
* Initializes GA configuration.
* Asks the user to allows GA when configuration is missing.
* @return {Promise}
*/
_initGa() {
this.helper = new GaHelper();
return this.helper.gaAllowed()
.then((allowed) => this._processGaAllowed(allowed));
}
/**
* Runs instructions after reading the configuration.
* @param {?Boolean} allowed Optional, state of GA enabled flag.
* @return {Promise}
*/
_processGaAllowed(allowed) {
if (typeof allowed === 'boolean') {
return this.runCommand();
}
return this._askUser();
}
/**
* Asks the user to enable GA, saves the answer, and runs the command.
* The question is dissmissed after about 10s.
*
* @return {Promise}
*/
_askUser() {
this.queryTimeout = setTimeout(() => {
this.queryTimeout = undefined;
this.runCommand();
return;
}, 10000);
return this._getAnswer()
.then((answer) => this._processAnswer(answer))
.then(() => this.runCommand())
.catch(() => {});
}

function run() {
require('./run');
}
_getAnswer() {
const inquirer = require('inquirer');
return inquirer
.prompt([{
type: 'confirm',
name: 'gaEnabled',
message: 'Allow anonymous usage statistics to help improve our CLI tools?',
default: true
}]);
}

if (noGa) {
run();
} else {
const {GaHelper} = require('../lib/ga-helper');
const helper = new GaHelper();
helper.gaAllowed()
.then((allowed) => {
if (allowed === undefined) {
if (isCi()) {
run();
return;
}
const inquirer = require('inquirer');
inquirer
.prompt([{
type: 'confirm',
name: 'gaEnabled',
message: 'Allow anonymous usage statistics to help improve our CLI tools?',
default: true
}])
.then((answer) => helper.updatePermissions(answer.gaEnabled))
.then(() => run());
} else {
run();
_processAnswer(answer) {
if (!this.queryTimeout) {
throw new Error('Timed out');
}
});
clearTimeout(this.queryTimeout);
this.queryTimeout = undefined;
return this.helper.updatePermissions(answer.gaEnabled);
}
}

const cli = new ApiConsoleCli();
cli.init();
2 changes: 1 addition & 1 deletion bin/api-console-generate-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ program
.option('-o, --output [value]', 'Output file. Default to "./api-model.json"')
.option('-p, --pretty-print', 'Generated JSON will be formatted')
.option('--verbose', 'Print verbose messages')
.option('--no-ga', 'Disallow Google Analytics when running this command')
.option('--no-ga', 'Disable Google Analytics when running this command')
.action(function(apiFile, options) {
console.log();
if (!apiFile) {
Expand Down
2 changes: 1 addition & 1 deletion bin/api-console-serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ program
.option('-P, --protocol [value]', docs.protocol)
.option('-k, --key-path [value]', docs.keyPath)
.option('-c, --cert-path [value]', docs.certPath)
.option('--no-ga', 'Disallow Google Analytics when running this command')
.option('--no-ga', docs.noGa)
.on('--help', function() {
console.log('\n\n Examples:');
console.log();
Expand Down
3 changes: 2 additions & 1 deletion bin/build-help.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
"noXhr": "Embedded build only. API console HTTP transport library is not included",
"noWebAnimations": "Embedded build only. Currentlty do not use this option. Web animations library is not included into the build",
"noCache": "Always generate new version of the console, event if a build is cached",
"themeFile": "Replaces default API console theme file with custom version"
"themeFile": "Replaces default API console theme file with custom version",
"noGa": "Disable Google Analytics when running any command"
}
1 change: 1 addition & 0 deletions bin/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ program
'Generates a JSON file from API spec file')
.command('serve [path]',
'Creates a www server to run the console locally')
.option('--no-ga', 'Disable Google Analytics when running any command')
.parse(process.argv);
3 changes: 2 additions & 1 deletion bin/serve-help.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
"openPath": "The URL path to open in each browser",
"protocol": "The protocol, choice of [http, https], defaults to http",
"keyPath": "The file path to ssl key",
"certPath": "The file path to ssl cert"
"certPath": "The file path to ssl cert",
"noGa": "Disable Google Analytics when running any command"
}
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "api-console-cli",
"version": "1.0.10",
"version": "1.0.11",
"description": "A set of dev tools for API console",
"main": "main.js",
"scripts": {
Expand Down

0 comments on commit cfb369e

Please sign in to comment.