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

Commit 279d674

Browse files
committed
Adding support for console v5
1 parent 6bac517 commit 279d674

File tree

7 files changed

+200
-25
lines changed

7 files changed

+200
-25
lines changed

README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@ manager.installDependencies()
5454
| Property | Type | Default | Description |
5555
| -------- | -------- | -------- | -------- |
5656
| `verbose` | `Boolean` | `false` | Prints verbose output while installing dependencies. This is used to build install command for bower which is printing console output by default. |
57-
| `parser` | `Boolean` | `false` | If set it installs RAML javascript parser with the enhancer. |
58-
| `app` | `Boolean` | `false` | If set it installs `app-route` element to support standalone application architecture of the API console. |
57+
| `parser` | `Boolean` | `false` | If set it installs RAML javascript parser with the enhancer. **Deprecated**: This option is only relevant for the console version 4. It may be removed in future releases. **Set `isV4` option when using this one.** |
58+
| `app` | `Boolean` | `false` | If set it installs `app-route` element to support standalone application architecture of the API console. **Deprecated**: This option is only relevant for the console version 4. It may be removed in future releases. **Set `isV4` option when using this one.** |
59+
| `isV4` | `Boolean` | `false` | Compatibility mode with API console version 4. If set then it works as module created for v4 (versions 0.x.x of the module) |
60+
| `optionalDependencies` | `Array<String>` | `undefined` | List of optional dependencies to install besides the ones installed with bower file. It contains the list to of bower dependencies as it would be defined in `bower.json` file. This isn relevant for console v5 build process which manages dependencies differently. |
5961

6062
### DependendenciesManager
6163

@@ -65,9 +67,9 @@ manager.installDependencies()
6567

6668
| Property | Type | Description |
6769
| -------- | -------- | -------- |
68-
| `workingDir` | `String` | Path to a working directory where the console is processed. Usually it's where the bower.json file is. |
69-
| `logger` | `Object` | Any logger with the interface compatible with platform's `console` object. |
70+
| `workingDir` | `String` | Path to a working directory where the console is processed. Usually it's where the `bower.json` file is. |
7071
| `options` | `Object` or `DependenciesOptions` | Build options passed to the module. |
72+
| `logger` | `Object` | Any logger with the interface compatible with platform's `console` object. |
7173

7274
#### `installDependencies()`
7375

@@ -76,3 +78,9 @@ Installs dependencies in the `workingDir`.
7678
##### Return <Promise>
7779

7880
Resolved promise when operation is completed.
81+
82+
## Changes in stable version
83+
84+
- Order of arguments is reversed
85+
- It uses locally installed Bower dependency and does not checks if bower is installed on local machine.
86+
- Now additional dependencies has to be defined in the configuration options

lib/dependencies-options.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,47 @@ class DependenciesOptions {
2424
this.verbose = typeof opts.verbose === 'boolean' ? opts.verbose : false;
2525
/**
2626
* If set it installs RAML javascript parser with the enhancer.
27+
*
28+
* @deprecated This option is only relevant for the console version 4. It
29+
* may be removed in future releases.
2730
* @type {Boolean}
2831
*/
2932
this.parser = typeof opts.parser === 'boolean' ? opts.parser : false;
3033
/**
3134
* If set it installs `app-route` element to support standalone application
3235
* architecture of the API console.
36+
*
37+
* @deprecated This option is only relevant for the console version 4. It
38+
* may be removed in future releases.
3339
* @type {Boolean}
3440
*/
3541
this.app = typeof opts.app === 'boolean' ? opts.app : false;
42+
/**
43+
* If true it installs dependencies for API console version 4.
44+
*
45+
* Version has different build system and different dependencies.
46+
* In 5 logic to manage additional dependencies is moved to the
47+
* builder module. This only installs dependencies.
48+
*
49+
* @type {Boolean}
50+
*/
51+
this.isV4 = typeof opts.isV4 === 'boolean' ? opts.isV4 : false;
52+
/**
53+
* List of optional dependencies to install besides the ones installed
54+
* with bower file. It contains the list to of bower dependencies as it
55+
* would be defined in bower.json file.
56+
*
57+
* This isn relevant for console v5 build process which manages dependencies
58+
* differently.
59+
*
60+
* #### Example
61+
*
62+
* ```
63+
* ["PolymerElements\app-route#^2.0.0"]
64+
* ```
65+
* @type {Array<String>}
66+
*/
67+
this.optionalDependencies = opts.optionalDependencies;
3668
}
3769
}
3870
exports.DependenciesOptions = DependenciesOptions;

lib/dependencies.js

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -103,35 +103,61 @@ class DependendenciesManager {
103103
return this._installAdditionalDependencies();
104104
});
105105
}
106+
/**
107+
* Installs additional dependencies if nescesary.
108+
* In v4 this module decided which dependencies to install.
109+
* Since APIC version 5 the builder prepares dependencies list and just
110+
* pushes ready list to the manager.
111+
*
112+
* @return {Promise}
113+
*/
114+
_installAdditionalDependencies() {
115+
if (this.opts.isV4) {
116+
return this._addDependenciesV4();
117+
} else if (this.opts.optionalDependencies &&
118+
this.opts.optionalDependencies.length) {
119+
return this._addDependencies(this.opts.optionalDependencies);
120+
}
121+
return Promise.resolve();
122+
}
106123
/**
107124
* If required (for certain types of the console build options) it installs
108125
* additional bower dependencies like router, parser and enhancer.
109126
*
110127
* @return {Promise} Promise resolved when additional dependencies are
111128
* installed.
112129
*/
113-
_installAdditionalDependencies() {
114-
let deps = [];
130+
_addDependenciesV4() {
131+
const deps = [];
115132
if (this.opts.parser) {
116133
deps[deps.length] = 'advanced-rest-client/raml-js-parser';
117134
deps[deps.length] = 'advanced-rest-client/raml-json-enhance';
118135
}
119136
if (this.opts.app) {
120137
deps[deps.length] = 'PolymerElements/app-route#^1.0.0';
121138
}
122-
if (deps.length) {
123-
this.logger.info('Installing additional bower dependencies...');
124-
this.logger.info('Installing: ', deps);
125-
return new Promise((resolve, reject) => {
126-
const factory = bower.commands.install(deps, {}, {
127-
cwd: this.workingDir,
128-
quiet: true
129-
});
130-
factory.on('end', () => resolve());
131-
factory.on('error', (e) => reject(e));
132-
});
139+
if (!deps.length) {
140+
return Promise.resolve();
133141
}
134-
return Promise.resolve();
142+
return this._addDependencies(deps);
143+
}
144+
/**
145+
* Manually installs dependencies.
146+
*
147+
* @param {Array<String>} dependencies List of dependencies to install
148+
* @return {Promise}
149+
*/
150+
_addDependencies(dependencies) {
151+
this.logger.info('Installing additional bower dependencies...');
152+
this.logger.info('Installing: ', dependencies);
153+
return new Promise((resolve, reject) => {
154+
const factory = bower.commands.install(dependencies, {}, {
155+
cwd: this.workingDir,
156+
quiet: true
157+
});
158+
factory.on('end', () => resolve());
159+
factory.on('error', (e) => reject(e));
160+
});
135161
}
136162
}
137163
exports.DependendenciesManager = DependendenciesManager;

main.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ const {DependenciesOptions} = require('./lib/dependencies-options.js');
1414
* Sorthand function to `DependendenciesManager#installDependencies()`.
1515
*
1616
* @param {String} workingDir Path to a working directory instance.
17-
* @param {Winston} logger Logger to use to log debug output
1817
* @param {DependenciesOptions} opts Options passed to the module
18+
* @param {Winston} logger Logger to use to log debug output
1919
* @return {Promise} Resolved promise when operation is completed.
2020
*/
21-
module.exports.installDependencies = function(workingDir, logger, opts) {
22-
const manager = new DependendenciesManager(workingDir, logger, opts);
21+
module.exports.installDependencies = function(workingDir, opts, logger) {
22+
const manager = new DependendenciesManager(workingDir, opts, logger);
2323
return manager.installDependencies();
2424
};
2525
/**

test/dependency.test.js renamed to test/dependency-v4.test.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const assert = require('chai').assert;
55
const fs = require('fs-extra');
66
const path = require('path');
77

8-
describe('DependencyProcessor', () => {
8+
describe('DependencyProcessor - APIC v4', function() {
99
const logger = {
1010
warn: function() {
1111
// console.warn.apply(console, arguments);
@@ -21,7 +21,7 @@ describe('DependencyProcessor', () => {
2121
}
2222
};
2323
const workingDir = 'test/dependency-test';
24-
describe('_processDependencies()', () => {
24+
describe('_processDependencies()', function() {
2525
let processor;
2626
const bowerFile = path.join(workingDir, 'bower.json');
2727
const bowerContent = {
@@ -69,7 +69,8 @@ describe('DependencyProcessor', () => {
6969
beforeEach(function() {
7070
const options = {
7171
app: false,
72-
parser: false
72+
parser: false,
73+
isV4: true
7374
};
7475
processor = new DependendenciesManager(workingDir, options, logger);
7576
processor.runningRoot = true;

test/dependency-v45.test.js

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
'use strict';
2+
3+
const {DependendenciesManager} = require('../lib/dependencies.js');
4+
const assert = require('chai').assert;
5+
const fs = require('fs-extra');
6+
const path = require('path');
7+
8+
describe('DependencyProcessor - APIC v5', function() {
9+
const logger = {
10+
warn: function() {
11+
// console.warn.apply(console, arguments);
12+
},
13+
info: function() {
14+
// console.info.apply(console, arguments);
15+
},
16+
log: function() {
17+
// console.log.apply(console, arguments);
18+
},
19+
error: function() {
20+
// console.error.apply(console, arguments);
21+
}
22+
};
23+
const workingDir = 'test/dependency-test';
24+
let processor;
25+
const bowerFile = path.join(workingDir, 'bower.json');
26+
const bowerContent = {
27+
name: 'test',
28+
description: 'test',
29+
version: '0.0.1',
30+
license: 'Apache-2.0 OR CC-BY-4.0',
31+
authors: [
32+
'The Advanced REST client authors <arc@mulesoft.com>'
33+
],
34+
dependencies: {
35+
'arc-definitions': 'advanced-rest-client/arc-definitions#^2.0.0'
36+
}
37+
};
38+
before(function() {
39+
return fs.ensureDir(workingDir);
40+
});
41+
42+
after(function() {
43+
fs.remove(workingDir);
44+
});
45+
46+
/**
47+
* @param {Array<String>} files
48+
* @return {Promise}
49+
*/
50+
function finishTest(files) {
51+
let promise = [];
52+
if (files instanceof Array) {
53+
let list = files.map((file) => fs.pathExists(file));
54+
promise = Promise.all(list);
55+
} else {
56+
promise = fs.pathExists(files);
57+
}
58+
return promise
59+
.then((result) => {
60+
if (result instanceof Array) {
61+
result = result.some((item) => item === false);
62+
assert.isFalse(result);
63+
} else {
64+
assert.isTrue(result);
65+
}
66+
});
67+
}
68+
69+
beforeEach(function() {
70+
const options = {
71+
app: false,
72+
parser: false,
73+
isV4: false,
74+
optionalDependencies: ['advanced-rest-client/bytes-counter#^2.0.0']
75+
};
76+
processor = new DependendenciesManager(workingDir, options, logger);
77+
processor.runningRoot = true;
78+
return fs.ensureDir(workingDir);
79+
});
80+
81+
afterEach(function() {
82+
return fs.remove(workingDir);
83+
});
84+
85+
it('Should install basic dependencies', function() {
86+
this.timeout(300000);
87+
return fs.writeJson(bowerFile, bowerContent)
88+
.then(() => processor.installDependencies())
89+
.then(() => {
90+
return finishTest([
91+
path.join(workingDir, 'bower_components'),
92+
path.join(workingDir, 'bower_components', 'arc-definitions')
93+
]);
94+
});
95+
});
96+
97+
it('Should install additional dependencies', function() {
98+
this.timeout(300000);
99+
return fs.writeJson(bowerFile, bowerContent)
100+
.then(() => processor.installDependencies())
101+
.then(() => {
102+
return finishTest([
103+
path.join(workingDir, 'bower_components', 'bytes-counter')
104+
]);
105+
});
106+
});
107+
});

test/general.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ describe('General test', () => {
6767
beforeEach(function() {
6868
options = {
6969
app: false,
70-
parser: false
70+
parser: false,
71+
isV4: true
7172
};
7273
return fs.ensureDir(workingDir)
7374
.then(() => fs.writeJson(bowerFile, bowerContent));

0 commit comments

Comments
 (0)