Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TYPE IMPROVEMENT] remove global callback and dictionary types #1271

Merged
merged 2 commits into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/cli/domain/clean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,27 @@ import path from 'path';

const getComponentsByDir = makeGetComponentsByDir();

export function fetchList(dirPath: string, callback: Callback<string[]>): void {
export function fetchList(
dirPath: string,
callback: (err: Error | null, data: string[]) => void
): void {
return getComponentsByDir(dirPath, (err, list) => {
if (err) return (callback as any)(err);
if (list.length === 0) return callback(null, []);

const toRemove = list.map(folder => path.join(folder, 'node_modules'));
const folderExists = (folder: string, cb: Callback<boolean>) =>
fs.exists(folder, exists => cb(null, exists));
const folderExists = (
folder: string,
cb: (err: Error | null, data: boolean) => void
) => fs.exists(folder, exists => cb(null, exists));

async.filterSeries(toRemove, folderExists, callback as any);
});
}

export function remove(list: string[], callback: Callback<string>): void {
export function remove(
list: string[],
callback: (err: Error | null, data: string) => void
): void {
return async.eachSeries(list, fs.remove, callback as any);
}
5 changes: 4 additions & 1 deletion src/cli/domain/get-components-by-dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import path from 'path';
import { Component } from '../../types';

export default function getComponentsByDir() {
return (componentsDir: string, callback: Callback<string[]>): void => {
return (
componentsDir: string,
callback: (err: Error | null, data: string[]) => void
): void => {
const isOcComponent = function (file: string) {
const filePath = path.resolve(componentsDir, file);
const packagePath = path.join(filePath, 'package.json');
Expand Down
4 changes: 2 additions & 2 deletions src/cli/domain/get-mocked-plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const defaultRegister = (
};

const registerStaticMocks = (
mocks: Dictionary<string>,
mocks: Record<string, string>,
logger: Logger
): PluginMock[] =>
_.map(mocks, (mockedValue, pluginName) => {
Expand All @@ -62,7 +62,7 @@ const registerStaticMocks = (

const registerDynamicMocks = (
ocJsonLocation: string,
mocks: Dictionary<string>,
mocks: Record<string, string>,
logger: Logger
) =>
_.map(mocks, (source, pluginName) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default function ensureCompilerIsDeclaredAsDevDependency(
pkg: Component;
template: string;
},
cb: Callback<string, string>
cb: (err: string | null, data: string) => void
): void {
const { componentPath, pkg, template } = options;
const compilerDep = `${template}-compiler`;
Expand Down
4 changes: 2 additions & 2 deletions src/cli/domain/handle-dependencies/get-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ export default function getCompiler(
compilerDep: string;
componentPath: string;
logger: Logger;
pkg: { devDependencies: Dictionary<string> };
pkg: { devDependencies: Record<string, string> };
},
cb: Callback<Template, string | number>
cb: (err: string | number | null, data: Template) => void
): void {
const { compilerDep, componentPath, logger, pkg } = options;
const compilerPath = path.join(componentPath, 'node_modules', compilerDep);
Expand Down
38 changes: 21 additions & 17 deletions src/cli/domain/handle-dependencies/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { Component } from '../../../types';

const getComponentPackageJson = (
componentPath: string,
cb: Callback<Component>
cb: (err: Error | null, data: Component) => void
) => fs.readJson(path.join(componentPath, 'package.json'), cb);

const union = (a: ReadonlyArray<string>, b: ReadonlyArray<string>) => [
Expand All @@ -27,25 +27,25 @@ export default function handleDependencies(
logger: Logger;
useComponentDependencies?: boolean;
},
callback: Callback<
{
callback: (
err: string | null,
data: {
modules: string[];
templates: Array<(...args: unknown[]) => unknown>;
},
string
>
}
) => void
): void {
const { components, logger, useComponentDependencies } = options;

const dependencies: Dictionary<string> = {};
const addDependencies = (componentDependencies?: Dictionary<string>) =>
const dependencies: Record<string, string> = {};
const addDependencies = (componentDependencies?: Record<string, string>) =>
Object.entries(componentDependencies || {}).forEach(
([dependency, version]) => {
dependencies[dependency] = version;
}
);

const templates: Dictionary<(...args: unknown[]) => unknown> = {};
const templates: Record<string, (...args: unknown[]) => unknown> = {};
const addTemplate = (
templateName: string,
template: (...args: unknown[]) => unknown
Expand All @@ -59,15 +59,19 @@ export default function handleDependencies(
) =>
async.waterfall(
[
(cb: Callback<Component>) => getComponentPackageJson(componentPath, cb),
(cb: (err: Error | null, data: Component) => void) =>
getComponentPackageJson(componentPath, cb),
(
pkg: Component,
cb: Callback<{
componentPath: string;
logger: Logger;
pkg: Component;
template: string;
}>
cb: (
err: Error | null,
data: {
componentPath: string;
logger: Logger;
pkg: Component;
template: string;
}
) => void
) => {
addDependencies(pkg.dependencies);

Expand Down Expand Up @@ -96,7 +100,7 @@ export default function handleDependencies(
options: {
componentPath: string;
logger: Logger;
pkg: Component & { devDependencies: Dictionary<string> };
pkg: Component & { devDependencies: Record<string, string> };
template: string;
compilerDep: string;
},
Expand Down
2 changes: 1 addition & 1 deletion src/cli/domain/handle-dependencies/install-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default function installCompiler(
dependency: string;
logger: Logger;
},
cb: Callback<Template, string | number>
cb: (err: string | number | null, data: Template) => void
): void {
const { compilerPath, componentPath, dependency, logger } = options;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import strings from '../../../resources/index';
import { Logger } from '../../logger';

export default function installMissingDependencies(
options: { dependencies: Dictionary<string>; logger: Logger },
options: { dependencies: Record<string, string>; logger: Logger },
callback: (err: string | null) => void
): void {
const { dependencies, logger } = options;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Logger } from '../../logger';
export default function linkMissingDependencies(
options: {
componentPath: string;
dependencies: Dictionary<string>;
dependencies: Record<string, string>;
logger: Logger;
},
callback: (err: string | null) => void
Expand Down
2 changes: 1 addition & 1 deletion src/cli/domain/handle-dependencies/require-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ interface Template {
verbose: boolean;
production: boolean | undefined;
},
cb: Callback<Component>
cb: (err: Error | null, data: Component) => void
) => void;
}

Expand Down
2 changes: 1 addition & 1 deletion src/cli/domain/init-template/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default function initTemplate(
compiler: string;
logger: Logger;
},
callback: Callback<{ ok: true }, string>
callback: (err: string | null, data: { ok: true }) => void
): void {
const { compiler, componentPath } = options;
const compilerPath = path.join(componentPath, 'node_modules', compiler);
Expand Down
2 changes: 1 addition & 1 deletion src/cli/domain/init-template/install-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface Options {

export default function installTemplate(
options: Options,
callback: Callback<{ ok: true }, string>
callback: (err: string | null, data: { ok: true }) => void
): void {
const { compiler, componentPath, logger, templateType } = options;

Expand Down
2 changes: 1 addition & 1 deletion src/cli/domain/init-template/scaffold.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface ScaffoldOptions {

export default function scaffold(
options: ScaffoldOptions,
callback: Callback<{ ok: true }, string>
callback: (err: string | null, data: { ok: true }) => void
): void {
const { compiler, compilerPath, componentName, componentPath, templateType } =
options;
Expand Down
5 changes: 4 additions & 1 deletion src/cli/domain/package-components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ interface PackageOptions {

const packageComponents =
() =>
(options: PackageOptions, callback: Callback<Component>): void => {
(
options: PackageOptions,
callback: (err: Error | null, data: Component) => void
): void => {
const production = options.production;
const componentPath = options.componentPath;
const minify = options.minify === true;
Expand Down
12 changes: 6 additions & 6 deletions src/cli/domain/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default function registry(opts: RegistryOptions = {}): RegistryCli {
};

return {
add(registry: string, callback: Callback<null, string>) {
add(registry: string, callback: (err: string | null, data: null) => void) {
if (registry.slice(registry.length - 1) !== '/') {
registry += '/';
}
Expand Down Expand Up @@ -64,7 +64,7 @@ export default function registry(opts: RegistryOptions = {}): RegistryCli {
}
);
},
get(callback: Callback<string[], string>) {
get(callback: (err: string | null, data: string[]) => void) {
if (opts.registry) {
return callback(null, [opts.registry]);
}
Expand All @@ -79,7 +79,7 @@ export default function registry(opts: RegistryOptions = {}): RegistryCli {
},
getApiComponentByHref(
href: string,
callback: Callback<unknown, Error | number>
callback: (err: Error | number | null, data: unknown) => void
) {
request(
{
Expand All @@ -92,7 +92,7 @@ export default function registry(opts: RegistryOptions = {}): RegistryCli {
},
getComponentPreviewUrlByUrl(
componentHref: string,
callback: Callback<string, Error | number>
callback: (err: Error | number | null, data: string) => void
) {
request(
{
Expand Down Expand Up @@ -120,7 +120,7 @@ export default function registry(opts: RegistryOptions = {}): RegistryCli {
route: string;
path: string;
},
callback: Callback<unknown, string>
callback: (err: string | null, data: unknown) => void
) {
if (!!options.username && !!options.password) {
requestsHeaders = Object.assign(requestsHeaders, {
Expand Down Expand Up @@ -157,7 +157,7 @@ export default function registry(opts: RegistryOptions = {}): RegistryCli {
callback(err, res);
});
},
remove(registry: string, callback: Callback) {
remove(registry: string, callback: (err: Error | null) => void) {
if (registry.slice(registry.length - 1) !== '/') {
registry += '/';
}
Expand Down
2 changes: 1 addition & 1 deletion src/cli/facade/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const dev =
verbose?: boolean;
production?: boolean;
},
callback: Callback<Registry, Error | string>
callback: (err: Error | string | null, data: Registry) => void
): void => {
const componentsDir = opts.dirPath;
const port = opts.port || 3000;
Expand Down
2 changes: 1 addition & 1 deletion src/cli/facade/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const init =
componentPath: string;
templateType: string;
},
callback: Callback<string>
callback: (err: Error | null, data: string) => void
): void => {
const templateType =
typeof opts.templateType === 'undefined'
Expand Down
2 changes: 1 addition & 1 deletion src/cli/facade/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const cliPackage =
useComponentDependencies?: boolean;
compress?: boolean;
},
callback: Callback<Component, string>
callback: (err: string | null, data: Component) => void
): void => {
const componentPath = opts.componentPath;
const useComponentDependencies = opts.useComponentDependencies;
Expand Down
2 changes: 1 addition & 1 deletion src/cli/facade/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const preview =
({ logger, registry }: { logger: Logger; registry: RegistryCli }) =>
(
opts: { componentHref: string },
callback: Callback<string, string>
callback: (err: string | null, data: string) => void
): void => {
registry.getComponentPreviewUrlByUrl(opts.componentHref, (err, href) => {
if (err) {
Expand Down
16 changes: 11 additions & 5 deletions src/cli/facade/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,15 @@ const publish =

let errorMessage;

const readPackageJson = (cb: Callback<Component>) =>
fs.readJson(path.join(packageDir, 'package.json'), cb);
const readPackageJson = (
cb: (err: Error | null, data: Component) => void
) => fs.readJson(path.join(packageDir, 'package.json'), cb);

const getCredentials = (
cb: Callback<{ username: string; password: string }>
cb: (
err: Error | null,
data: { username: string; password: string }
) => void
) => {
if (opts.username && opts.password) {
logger.ok(strings.messages.cli.USING_CREDS);
Expand All @@ -62,7 +66,9 @@ const publish =
local.compress(packageDir, compressedPackagePath, cb);
};

const packageAndCompress = (cb: Callback<Component, Error | string>) => {
const packageAndCompress = (
cb: (err: Error | string | null, data: Component) => void
) => {
logger.warn(strings.messages.cli.PACKAGING(packageDir));
const packageOptions = {
production: true,
Expand Down Expand Up @@ -92,7 +98,7 @@ const publish =
username?: string;
password?: string;
},
cb: Callback<'ok', string>
cb: (err: string | null, data: 'ok') => void
) => {
logger.warn(strings.messages.cli.PUBLISHING(options.route));

Expand Down
5 changes: 4 additions & 1 deletion src/cli/facade/registry-add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { Logger } from '../logger';

const registryAdd =
({ registry, logger }: { logger: Logger; registry: RegistryCli }) =>
(opts: { registryUrl: string }, callback: Callback<string, string>): void => {
(
opts: { registryUrl: string },
callback: (err: string | null, data: string) => void
): void => {
registry.add(opts.registryUrl, err => {
if (err) {
logger.err(err);
Expand Down
5 changes: 4 additions & 1 deletion src/cli/facade/registry-ls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { Logger } from '../logger';

const registryLs =
({ registry, logger }: { logger: Logger; registry: RegistryCli }) =>
(_opts: unknown, callback: Callback<string[], string>): void => {
(
_opts: unknown,
callback: (err: string | null, data: string[]) => void
): void => {
registry.get((err, registries) => {
if (err) {
logger.err(strings.errors.generic(err));
Expand Down
Loading