Skip to content

Commit 569934c

Browse files
merge master
2 parents e2eb060 + ffc3df2 commit 569934c

20 files changed

+296
-206
lines changed

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

Lines changed: 65 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,56 @@
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: (options: unknown, dependencies: unknown, next: () => void) => void;
11+
execute: (...args: unknown[]) => unknown;
12+
}
13+
14+
interface PluginMock {
15+
name: string;
16+
register: {
17+
register: (
18+
options: unknown,
19+
dependencies: unknown,
20+
next: () => void
21+
) => void;
22+
execute: (...args: unknown[]) => unknown;
23+
};
24+
}
25+
26+
const isMockValid = (
27+
plugin: unknown
28+
): plugin is MockedPlugin | ((...args: unknown[]) => unknown) => {
29+
const isFunction = typeof plugin === 'function';
1230
const isValidObject =
13-
_.isObject(plugin) &&
14-
_.isFunction(plugin.register) &&
15-
_.isFunction(plugin.execute);
31+
!!plugin &&
32+
typeof plugin === 'object' &&
33+
typeof (plugin as MockedPlugin).register === 'function' &&
34+
typeof (plugin as MockedPlugin).execute === 'function';
35+
1636
return isFunction || isValidObject;
1737
};
1838

19-
const defaultRegister = (options, dependencies, next) => next();
39+
const defaultRegister = (
40+
options: unknown,
41+
dependencies: unknown,
42+
next: () => void
43+
) => {
44+
next();
45+
};
2046

21-
const registerStaticMocks = (mocks, logger) =>
47+
const registerStaticMocks = (
48+
mocks: Dictionary<string>,
49+
logger: Logger
50+
): PluginMock[] =>
2251
_.map(mocks, (mockedValue, pluginName) => {
2352
logger.ok(`├── ${pluginName} () => ${mockedValue}`);
53+
2454
return {
2555
name: pluginName,
2656
register: {
@@ -30,7 +60,11 @@ const registerStaticMocks = (mocks, logger) =>
3060
};
3161
});
3262

33-
const registerDynamicMocks = (ocJsonLocation, mocks, logger) =>
63+
const registerDynamicMocks = (
64+
ocJsonLocation: string,
65+
mocks: Dictionary<string>,
66+
logger: Logger
67+
) =>
3468
_.map(mocks, (source, pluginName) => {
3569
let pluginMock;
3670
try {
@@ -46,26 +80,29 @@ const registerDynamicMocks = (ocJsonLocation, mocks, logger) =>
4680
return;
4781
}
4882

49-
const register = pluginMock.register || defaultRegister;
50-
const execute = pluginMock.execute || pluginMock;
83+
const register = (pluginMock as MockedPlugin).register || defaultRegister;
84+
const execute = (pluginMock as MockedPlugin).execute || pluginMock;
5185

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

5488
return {
5589
name: pluginName,
5690
register: { execute, register }
5791
};
58-
}).filter(pluginMock => pluginMock);
92+
}).filter((pluginMock): pluginMock is PluginMock => !!pluginMock);
5993

60-
const findPath = function(pathToResolve, fileName) {
94+
const findPath = (
95+
pathToResolve: string,
96+
fileName: string
97+
): string | undefined => {
6198
const rootDir = fs.realpathSync('.');
6299
const fileToResolve = path.join(pathToResolve, fileName);
63100

64101
if (!fs.existsSync(fileToResolve)) {
65102
if (pathToResolve === rootDir) {
66103
return undefined;
67104
} else {
68-
const getParent = pathToResolve =>
105+
const getParent = (pathToResolve: string) =>
69106
pathToResolve
70107
.split('/')
71108
.slice(0, -1)
@@ -80,10 +117,13 @@ const findPath = function(pathToResolve, fileName) {
80117
return fileToResolve;
81118
};
82119

83-
module.exports = function(logger, componentsDir) {
120+
export default function getMockedPlugins(
121+
logger: Logger,
122+
componentsDir: string
123+
): PluginMock[] {
84124
componentsDir = path.resolve(componentsDir || '.');
85125

86-
let plugins = [];
126+
let plugins: PluginMock[] = [];
87127
const ocJsonFileName = settings.configFile.src.replace('./', '');
88128
const ocJsonPath = findPath(componentsDir, ocJsonFileName);
89129

@@ -108,4 +148,4 @@ module.exports = function(logger, componentsDir) {
108148
);
109149

110150
return plugins;
111-
};
151+
}

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+
): void {
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 as any),
28+
cb => installTemplate(options, cb as any),
29+
cb => scaffold(Object.assign(options, { compilerPath }), cb as any)
30+
],
31+
callback as any
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+
): void {
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import settings from '../../resources/settings';
66
export default function mock() {
77
return function(
88
params: { targetType: string; targetValue: string; targetName: string },
9-
callback: Callback
10-
) {
9+
callback: (err: Error) => void
10+
): void {
1111
fs.readJson(settings.configFile.src, (err, localConfig) => {
1212
localConfig = localConfig || {};
1313

src/registry/router.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const ComponentPreviewRoute = require('./routes/component-preview');
99
const IndexRoute = require('./routes');
1010
const PublishRoute = require('./routes/publish');
1111
const StaticRedirectorRoute = require('./routes/static-redirector');
12-
const PluginsRoute = require('./routes/plugins');
12+
const PluginsRoute = require('./routes/plugins').default;
1313
const DependenciesRoute = require('./routes/dependencies');
1414
const settings = require('../resources/settings').default;
1515

0 commit comments

Comments
 (0)