Skip to content

Commit bf06d5b

Browse files
merge master
2 parents 703bd19 + 7632f0c commit bf06d5b

18 files changed

+390
-237
lines changed

src/cli/domain/get-components-by-dir.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import fs from 'fs-extra';
22
import path from 'path';
33

44
export default function getComponentsByDir() {
5-
return function(componentsDir: string, callback: Callback<string[]>) {
5+
return (componentsDir: string, callback: Callback<string[]>): void => {
66
const isOcComponent = function(file: string) {
77
const filePath = path.resolve(componentsDir, file),
88
packagePath = path.join(filePath, 'package.json');

src/cli/domain/init-template/scaffold.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ interface ScaffoldOptions {
1414
export default function scaffold(
1515
options: ScaffoldOptions,
1616
callback: Callback<{ ok: true }, string>
17-
) {
17+
): void {
1818
const {
1919
compiler,
2020
compilerPath,
@@ -46,6 +46,8 @@ export default function scaffold(
4646
const url =
4747
(compilerPackage.bugs && compilerPackage.bugs.url) ||
4848
`the ${templateType} repo`;
49-
return (callback as any)(strings.errors.cli.scaffoldError(url, error));
49+
return (callback as any)(
50+
strings.errors.cli.scaffoldError(url, String(error))
51+
);
5052
}
5153
}

src/cli/validate-command.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ import strings from '../resources';
55
export default function validateCommand(argv: Arguments, level: number): true {
66
let keys = Object.keys(commands.commands);
77
if (level === 1) {
8-
keys = Object.keys(commands.commands[argv._[0]].commands || {});
8+
keys = Object.keys((commands as any).commands[argv._[0]].commands || {});
99
}
1010

11-
if (argv._.length > level && !keys.includes(argv._[String(level)])) {
12-
throw new Error(strings.messages.cli.NO_SUCH_COMMAND(argv._[level]));
11+
if (argv._.length > level && !keys.includes((argv as any)._[String(level)])) {
12+
throw new Error(
13+
strings.messages.cli.NO_SUCH_COMMAND(String(argv._[level]))
14+
);
1315
}
1416

1517
return true;

src/registry/app-start.js renamed to src/registry/app-start.ts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
1-
'use strict';
1+
import colors from 'colors/safe';
2+
import path from 'path';
3+
import _ from 'lodash';
4+
import fs from 'fs-extra';
5+
import { ComponentsDetails, Config, Repository } from '../types';
26

3-
const colors = require('colors/safe');
4-
const path = require('path');
5-
const _ = require('lodash');
7+
const packageInfo = fs.readJsonSync(
8+
path.join(
9+
__dirname,
10+
'..',
11+
'components',
12+
'oc-client',
13+
'_package',
14+
'package.json'
15+
)
16+
);
617

7-
// @ts-ignore
8-
const packageInfo = require('../components/oc-client/_package/package');
9-
10-
module.exports = function(repository, options, callback) {
18+
export default function appStart(
19+
repository: Repository,
20+
options: Config,
21+
callback: Callback<ComponentsDetails | string, any>
22+
): void {
1123
if (options.local) {
1224
return callback(null, 'ok');
1325
}
@@ -50,7 +62,9 @@ module.exports = function(repository, options, callback) {
5062
if (!err) {
5163
logger.log(colors.green('Component published.'));
5264
} else {
53-
logger.log(colors.red(`Component not published: ${err.message}`));
65+
logger.log(
66+
colors.red(`Component not published: ${(err as any).message}`)
67+
);
5468
}
5569

5670
callback(err, res);
@@ -61,4 +75,4 @@ module.exports = function(repository, options, callback) {
6175
callback(null, 'ok');
6276
}
6377
});
64-
};
78+
}

src/registry/domain/nested-renderer.js

Lines changed: 0 additions & 120 deletions
This file was deleted.
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
import async from 'async';
2+
import _ from 'lodash';
3+
4+
import settings from '../../resources/settings';
5+
import strings from '../../resources';
6+
import { Config } from '../../types';
7+
8+
type Cb = Callback<string, string>;
9+
type Options = {
10+
version?: string;
11+
name?: string;
12+
headers?: Dictionary<string>;
13+
parameters?: Dictionary<string>;
14+
};
15+
type Params = {
16+
components: Options[];
17+
options: Options;
18+
callback: Cb;
19+
};
20+
21+
const sanitise = {
22+
componentParams(component: string, options: Options | Cb, callback?: Cb) {
23+
return {
24+
...sanitise.options(options, callback),
25+
componentName: component
26+
};
27+
},
28+
componentsParams(
29+
components: Options[],
30+
options: Options | Cb,
31+
callback: Cb
32+
): Params {
33+
return {
34+
...sanitise.options(options, callback),
35+
components: components
36+
};
37+
},
38+
headers(h = {}) {
39+
return {
40+
...h,
41+
accept: settings.registry.acceptRenderedHeader
42+
};
43+
},
44+
options(
45+
options: Options | Cb,
46+
callback?: Cb
47+
): { options: Options; callback: Cb } {
48+
const cb = !callback && typeof options === 'function' ? options : callback;
49+
const opts = typeof options === 'function' ? {} : options;
50+
51+
return { callback: cb!, options: opts };
52+
}
53+
};
54+
55+
const validate = {
56+
callback(c: Cb) {
57+
if (!c || typeof c !== 'function') {
58+
throw new Error(
59+
strings.errors.registry.NESTED_RENDERER_CALLBACK_IS_NOT_VALID
60+
);
61+
}
62+
},
63+
componentParams(params: { componentName: string; callback: Cb }) {
64+
if (!params.componentName) {
65+
throw new Error(
66+
strings.errors.registry.NESTED_RENDERER_COMPONENT_NAME_IS_NOT_VALID
67+
);
68+
}
69+
70+
validate.callback(params.callback);
71+
},
72+
componentsParams(params: Params) {
73+
if (_.isEmpty(params.components)) {
74+
throw new Error(
75+
strings.errors.registry.NESTED_RENDERER_COMPONENTS_IS_NOT_VALID
76+
);
77+
}
78+
79+
validate.callback(params.callback);
80+
}
81+
};
82+
83+
export default function nestedRenderer(renderer: any, conf: Config) {
84+
return {
85+
renderComponent(
86+
componentName: string,
87+
renderOptions: Options | Cb,
88+
callback?: Cb
89+
) {
90+
const p = sanitise.componentParams(
91+
componentName,
92+
renderOptions,
93+
callback
94+
);
95+
validate.componentParams(p);
96+
97+
return renderer(
98+
{
99+
conf: conf,
100+
headers: sanitise.headers(p.options.headers),
101+
name: componentName,
102+
parameters: p.options.parameters || {},
103+
version: p.options.version || ''
104+
},
105+
(result: any) => {
106+
if (result.response.error) {
107+
return p.callback(result.response.error, undefined as any);
108+
} else {
109+
return p.callback(null, result.response.html);
110+
}
111+
}
112+
);
113+
},
114+
renderComponents(
115+
components: Options[],
116+
renderOptions: Options,
117+
callback: Cb
118+
) {
119+
const p = sanitise.componentsParams(components, renderOptions, callback);
120+
validate.componentsParams(p);
121+
122+
async.map(
123+
p.components,
124+
(component, cb) => {
125+
renderer(
126+
{
127+
conf: conf,
128+
headers: sanitise.headers(p.options.headers),
129+
name: component.name,
130+
parameters: {
131+
...p.options.parameters,
132+
...component.parameters
133+
},
134+
version: component.version || ''
135+
},
136+
(result: any) => {
137+
const error = result.response.error;
138+
cb(null, error ? new Error(error) : result.response.html);
139+
}
140+
);
141+
},
142+
p.callback as any
143+
);
144+
}
145+
};
146+
}

src/registry/domain/repository.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ module.exports = function(conf) {
321321
return callback({
322322
code: strings.errors.registry.COMPONENT_PUBLISHVALIDATION_FAIL_CODE,
323323
msg: strings.errors.registry.COMPONENT_PUBLISHVALIDATION_FAIL(
324-
validationResult.error
324+
String(validationResult.error)
325325
)
326326
});
327327
}

src/registry/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const express = require('express');
66
const http = require('http');
77
const _ = require('lodash');
88

9-
const appStart = require('./app-start');
9+
const appStart = require('./app-start').default;
1010
const eventsHandler = require('./domain/events-handler');
1111
const middleware = require('./middleware');
1212
const pluginsInitialiser = require('./domain/plugins-initialiser');

src/registry/router.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ const ComponentsRoute = require('./routes/components');
77
const ComponentInfoRoute = require('./routes/component-info');
88
const ComponentPreviewRoute = require('./routes/component-preview');
99
const IndexRoute = require('./routes');
10-
const PublishRoute = require('./routes/publish');
11-
const StaticRedirectorRoute = require('./routes/static-redirector');
10+
const PublishRoute = require('./routes/publish').default;
11+
const StaticRedirectorRoute = require('./routes/static-redirector').default;
1212
const PluginsRoute = require('./routes/plugins').default;
1313
const DependenciesRoute = require('./routes/dependencies').default;
1414
const settings = require('../resources/settings').default;

0 commit comments

Comments
 (0)