Skip to content

Commit b51a7c9

Browse files
move more registry/domain files to TS
1 parent 9cb6da2 commit b51a7c9

23 files changed

+485
-347
lines changed

src/registry/domain/components-cache/components-list.js

Lines changed: 0 additions & 82 deletions
This file was deleted.
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import async from 'async';
2+
import semver from 'semver';
3+
import getUnixUTCTimestamp from 'oc-get-unix-utc-timestamp';
4+
import { Cdn, ComponentsList, Config } from '../../../types';
5+
6+
export default function componentsList(conf: Config, cdn: Cdn) {
7+
const filePath = (): string =>
8+
`${conf.storage.options.componentsDir}/components.json`;
9+
10+
const componentsList = {
11+
getFromJson: (callback: Callback<any>) =>
12+
cdn.getJson(filePath(), true, callback),
13+
14+
getFromDirectories: (callback: Callback<ComponentsList, string>) => {
15+
const componentsInfo = {};
16+
17+
const getVersionsForComponent = (
18+
componentName: string,
19+
cb: Callback<string[]>
20+
) => {
21+
cdn.listSubDirectories(
22+
`${conf.storage.options.componentsDir}/${componentName}`,
23+
(err, versions) => {
24+
if (err) {
25+
return cb(err, undefined as any);
26+
}
27+
cb(null, versions.sort(semver.compare));
28+
}
29+
);
30+
};
31+
32+
cdn.listSubDirectories(
33+
conf.storage.options.componentsDir,
34+
(err, components) => {
35+
if (err) {
36+
if (err.code === 'dir_not_found') {
37+
return callback(null, {
38+
lastEdit: getUnixUTCTimestamp(),
39+
components: [] as any
40+
});
41+
}
42+
43+
return callback(err as any, undefined as any);
44+
}
45+
46+
async.mapLimit(
47+
components,
48+
cdn.maxConcurrentRequests,
49+
getVersionsForComponent,
50+
(errors, versions) => {
51+
if (errors) {
52+
return callback(errors, undefined as any);
53+
}
54+
55+
components.forEach((component, i) => {
56+
componentsInfo[component] = versions[i];
57+
});
58+
59+
callback(null, {
60+
lastEdit: getUnixUTCTimestamp(),
61+
components: componentsInfo
62+
});
63+
}
64+
);
65+
}
66+
);
67+
},
68+
69+
refresh(callback: Callback<ComponentsList, string>) {
70+
componentsList.getFromDirectories((err, components) => {
71+
if (err) {
72+
return callback(err, undefined as any);
73+
}
74+
componentsList.save(components, err => {
75+
if (err) {
76+
return callback(err, undefined as any);
77+
}
78+
callback(err, components);
79+
});
80+
});
81+
},
82+
83+
save: (data: ComponentsList, callback: Callback<unknown, string>) =>
84+
cdn.putFileContent(JSON.stringify(data), filePath(), true, callback)
85+
};
86+
87+
return componentsList;
88+
}

src/registry/domain/components-cache/index.js renamed to src/registry/domain/components-cache/index.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
'use strict';
1+
import _ from 'lodash';
2+
import getComponentsList from './components-list';
3+
import * as eventsHandler from '../events-handler';
4+
import getUnixUTCTimestamp from 'oc-get-unix-utc-timestamp';
5+
import { Cdn, ComponentsList, Config } from '../../../types';
26

3-
const _ = require('lodash');
7+
export default function componentsCache(conf: Config, cdn: Cdn) {
8+
let cachedComponentsList: ComponentsList, refreshLoop;
49

5-
const ComponentsList = require('./components-list');
6-
const eventsHandler = require('../events-handler');
7-
const getUnixUTCTimestamp = require('oc-get-unix-utc-timestamp');
8-
9-
module.exports = (conf, cdn) => {
10-
let cachedComponentsList, refreshLoop;
11-
12-
const componentsList = ComponentsList(conf, cdn);
10+
const componentsList = getComponentsList(conf, cdn);
1311

1412
const poll = () =>
1513
setTimeout(() => {
@@ -30,7 +28,10 @@ module.exports = (conf, cdn) => {
3028
});
3129
}, conf.pollingInterval * 1000);
3230

33-
const cacheDataAndStartPolling = (data, callback) => {
31+
const cacheDataAndStartPolling = (
32+
data: ComponentsList,
33+
callback: (err: null, data: ComponentsList) => void
34+
) => {
3435
cachedComponentsList = data;
3536
refreshLoop = poll();
3637
callback(null, data);
@@ -42,7 +43,7 @@ module.exports = (conf, cdn) => {
4243
};
4344

4445
return {
45-
get: callback => {
46+
get(callback: Callback<ComponentsList>) {
4647
if (!cachedComponentsList) {
4748
return returnError(
4849
'components_cache_empty',
@@ -54,7 +55,7 @@ module.exports = (conf, cdn) => {
5455
callback(null, cachedComponentsList);
5556
},
5657

57-
load: callback => {
58+
load(callback) {
5859
componentsList.getFromJson((jsonErr, jsonComponents) => {
5960
componentsList.getFromDirectories((dirErr, dirComponents) => {
6061
if (dirErr) {
@@ -75,7 +76,7 @@ module.exports = (conf, cdn) => {
7576
});
7677
});
7778
},
78-
refresh: callback => {
79+
refresh(callback) {
7980
clearTimeout(refreshLoop);
8081
componentsList.refresh((err, components) => {
8182
if (err) {
@@ -86,4 +87,4 @@ module.exports = (conf, cdn) => {
8687
});
8788
}
8889
};
89-
};
90+
}

src/registry/domain/components-details.js

Lines changed: 0 additions & 92 deletions
This file was deleted.

0 commit comments

Comments
 (0)