Skip to content

Commit 0f6ffde

Browse files
more registry and cli files to ts
1 parent ff58725 commit 0f6ffde

23 files changed

+291
-204
lines changed

src/cli/domain/get-mocked-plugins.js renamed to src/cli/domain/get-mocked-plugins.ts

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,45 @@
1-
'use strict';
2-
3-
const fs = require('fs-extra');
4-
const path = require('path');
5-
const _ = require('lodash');
6-
7-
const settings = require('../../resources/settings').default;
8-
const strings = require('../../resources/').default;
9-
10-
const isMockValid = plugin => {
11-
const isFunction = _.isFunction(plugin);
1+
import fs from 'fs-extra';
2+
import path from 'path';
3+
import _ from 'lodash';
4+
5+
import settings from '../../resources/settings';
6+
import strings from '../../resources/';
7+
import { Logger } from '../logger';
8+
9+
interface MockedPlugin {
10+
register: Function;
11+
execute: Function;
12+
}
13+
14+
interface PluginMock {
15+
name: string;
16+
register: {
17+
register: Function;
18+
execute: Function;
19+
};
20+
}
21+
22+
const isMockValid = (plugin: unknown): plugin is MockedPlugin | Function => {
23+
const isFunction = typeof plugin === 'function';
1224
const isValidObject =
13-
_.isObject(plugin) &&
14-
_.isFunction(plugin.register) &&
15-
_.isFunction(plugin.execute);
25+
!!plugin &&
26+
typeof plugin === 'object' &&
27+
typeof (plugin as MockedPlugin).register === 'function' &&
28+
typeof (plugin as MockedPlugin).execute === 'function';
29+
1630
return isFunction || isValidObject;
1731
};
1832

19-
const defaultRegister = (options, dependencies, next) => next();
33+
const defaultRegister = (
34+
options: unknown,
35+
dependencies: unknown,
36+
next: () => void
37+
) => next();
2038

21-
const registerStaticMocks = (mocks, logger) =>
39+
const registerStaticMocks = (mocks, logger): PluginMock[] =>
2240
_.map(mocks, (mockedValue, pluginName) => {
2341
logger.ok(`├── ${pluginName} () => ${mockedValue}`);
42+
2443
return {
2544
name: pluginName,
2645
register: {
@@ -30,7 +49,7 @@ const registerStaticMocks = (mocks, logger) =>
3049
};
3150
});
3251

33-
const registerDynamicMocks = (ocJsonLocation, mocks, logger) =>
52+
const registerDynamicMocks = (ocJsonLocation: string, mocks, logger) =>
3453
_.map(mocks, (source, pluginName) => {
3554
let pluginMock;
3655
try {
@@ -46,18 +65,21 @@ const registerDynamicMocks = (ocJsonLocation, mocks, logger) =>
4665
return;
4766
}
4867

49-
const register = pluginMock.register || defaultRegister;
50-
const execute = pluginMock.execute || pluginMock;
68+
const register = (pluginMock as MockedPlugin).register || defaultRegister;
69+
const execute = (pluginMock as MockedPlugin).execute || pluginMock;
5170

5271
logger.ok(`├── ${pluginName} () => [Function]`);
5372

5473
return {
5574
name: pluginName,
5675
register: { execute, register }
5776
};
58-
}).filter(pluginMock => pluginMock);
77+
}).filter((pluginMock): pluginMock is PluginMock => !!pluginMock);
5978

60-
const findPath = function(pathToResolve, fileName) {
79+
const findPath = (
80+
pathToResolve: string,
81+
fileName: string
82+
): string | undefined => {
6183
const rootDir = fs.realpathSync('.');
6284
const fileToResolve = path.join(pathToResolve, fileName);
6385

@@ -80,10 +102,13 @@ const findPath = function(pathToResolve, fileName) {
80102
return fileToResolve;
81103
};
82104

83-
module.exports = function(logger, componentsDir) {
105+
export default function getMockedPlugins(
106+
logger: Logger,
107+
componentsDir: string
108+
): PluginMock[] {
84109
componentsDir = path.resolve(componentsDir || '.');
85110

86-
let plugins = [];
111+
let plugins: PluginMock[] = [];
87112
const ocJsonFileName = settings.configFile.src.replace('./', '');
88113
const ocJsonPath = findPath(componentsDir, ocJsonFileName);
89114

@@ -108,4 +133,4 @@ module.exports = function(logger, componentsDir) {
108133
);
109134

110135
return plugins;
111-
};
136+
}

src/cli/domain/init-template/index.js

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

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import async from 'async';
2+
import fs from 'fs-extra';
3+
import path from 'path';
4+
5+
import installTemplate from './install-template';
6+
import * as npm from '../../../utils/npm-utils';
7+
import scaffold from './scaffold';
8+
import { Logger } from '../../logger';
9+
10+
export default function initTemplate(
11+
options: {
12+
componentPath: string;
13+
templateType: string;
14+
componentName: string;
15+
compiler: string;
16+
logger: Logger;
17+
},
18+
callback: Callback<{ ok: true }, string>
19+
) {
20+
const { compiler, componentPath } = options;
21+
const compilerPath = path.join(componentPath, 'node_modules', compiler);
22+
const npmOptions = { initPath: componentPath, silent: true };
23+
24+
async.series(
25+
[
26+
cb => fs.ensureDir(componentPath, cb),
27+
cb => npm.init(npmOptions, cb),
28+
cb => installTemplate(options, cb),
29+
cb => scaffold(Object.assign(options, { compilerPath }), cb)
30+
],
31+
callback
32+
);
33+
}

src/cli/domain/init-template/install-template.js renamed to src/cli/domain/init-template/install-template.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
1-
'use strict';
2-
3-
const tryRequire = require('try-require');
4-
5-
const isTemplateValid = require('../../../utils/is-template-valid').default;
6-
const npm = require('../../../utils/npm-utils');
7-
const strings = require('../../../resources').default;
8-
9-
module.exports = function installTemplate(options, callback) {
1+
import tryRequire from 'try-require';
2+
3+
import isTemplateValid from '../../../utils/is-template-valid';
4+
import * as npm from '../../../utils/npm-utils';
5+
import strings from '../../../resources';
6+
import { Logger } from '../../logger';
7+
8+
interface Options {
9+
componentPath: string;
10+
templateType: string;
11+
compiler: string;
12+
logger: Logger;
13+
}
14+
15+
export default function installTemplate(
16+
options: Options,
17+
callback: Callback<{ ok: true }, string>
18+
) {
1019
const { compiler, componentPath, logger, templateType } = options;
1120

1221
const npmOptions = {
@@ -21,12 +30,14 @@ module.exports = function installTemplate(options, callback) {
2130
npm.installDependency(npmOptions, (err, result) => {
2231
const errorMessage = 'template type not valid';
2332
if (err) {
33+
// @ts-ignore
2434
return callback(errorMessage);
2535
}
2636

2737
const installedCompiler = tryRequire(result.dest);
2838

2939
if (!isTemplateValid(installedCompiler, { compiler: true })) {
40+
// @ts-ignore
3041
return callback(errorMessage);
3142
}
3243
const version = installedCompiler.getInfo().version;
@@ -40,4 +51,4 @@ module.exports = function installTemplate(options, callback) {
4051

4152
return callback(null, { ok: true });
4253
});
43-
};
54+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ export default function scaffold(
2525

2626
const baseComponentPath = path.join(compilerPath, 'scaffold');
2727
const baseComponentFiles = path.join(baseComponentPath, 'src');
28-
const compilerPackage = fs.readJSONSync(
28+
const compilerPackage = fs.readJsonSync(
2929
path.join(compilerPath, 'package.json')
3030
);
3131

3232
try {
3333
fs.copySync(baseComponentFiles, componentPath);
3434

35-
const componentPackage = fs.readJSONSync(
35+
const componentPackage = fs.readJsonSync(
3636
path.join(componentPath, 'package.json')
3737
);
3838
componentPackage.name = componentName;

src/cli/domain/local.js

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

77
const clean = require('./clean');
88
const getComponentsByDir = require('./get-components-by-dir').default;
9-
const initTemplate = require('./init-template');
9+
const initTemplate = require('./init-template').default;
1010
const isTemplateLegacy = require('../../utils/is-template-legacy').default;
1111
const mock = require('./mock').default;
1212
const packageComponents = require('./package-components');

src/cli/domain/mock.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import settings from '../../resources/settings';
66
export default function mock() {
77
return function(
88
params: { targetType: string; targetValue: string; targetName: string },
9-
callback: Callback
9+
callback: (err: Error) => void
1010
) {
1111
fs.readJson(settings.configFile.src, (err, localConfig) => {
1212
localConfig = localConfig || {};

src/cli/facade/dev.js

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

10-
const getMockedPlugins = require('../domain/get-mocked-plugins');
10+
const getMockedPlugins = require('../domain/get-mocked-plugins').default;
1111
const handleDependencies = require('../domain/handle-dependencies');
1212
const oc = require('../../index');
1313
const strings = require('../../resources/index').default;

src/registry/domain/validators/node-version.ts

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

5-
const packageInfo = fs.readJSONSync(
5+
const packageInfo = fs.readJsonSync(
66
path.join(__dirname, '..', '..', '..', '..', 'package.json')
77
);
88

src/registry/domain/validators/oc-cli-version.ts

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

5-
const packageInfo = fs.readJSONSync(
5+
const packageInfo = fs.readJsonSync(
66
path.join(__dirname, '..', '..', '..', '..', 'package.json')
77
);
88

0 commit comments

Comments
 (0)