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

Refactor types #1299

Merged
merged 4 commits into from
Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 12 additions & 5 deletions src/cli/domain/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import mock from './mock';
import packageComponents from './package-components';
import strings from '../../resources';
import * as validator from '../../registry/domain/validators';
import { Local } from '../../types';
import { Logger } from '../logger';

export default function local(): Local {
export default function local() {
return {
clean,
cleanup(compressedPackagePath: string) {
cleanup(compressedPackagePath: string): Promise<void> {
return fs.unlink(compressedPackagePath);
},
compress(input, output) {
compress(input: string, output: string): Promise<void> {
return promisify(targz.compress)({
src: input,
dest: output,
Expand All @@ -32,7 +32,12 @@ export default function local(): Local {
});
},
getComponentsByDir: getComponentsByDir(),
async init(options) {
async init(options: {
componentName: string;
logger: Logger;
componentPath: string;
templateType: string;
}): Promise<void> {
const { componentName, logger } = options;
let { templateType } = options;
if (!validator.validateComponentName(componentName)) {
Expand Down Expand Up @@ -68,3 +73,5 @@ export default function local(): Local {
package: packageComponents()
};
}

export type Local = ReturnType<typeof local>;
2 changes: 1 addition & 1 deletion src/cli/domain/package-components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import requireTemplate from './handle-dependencies/require-template';
import * as validator from '../../registry/domain/validators';
import { Component } from '../../types';

interface PackageOptions {
export interface PackageOptions {
componentPath: string;
minify?: boolean;
verbose?: boolean;
Expand Down
18 changes: 10 additions & 8 deletions src/cli/domain/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import put from '../../utils/put';
import settings from '../../resources/settings';
import * as urlBuilder from '../../registry/domain/url-builder';
import * as urlParser from '../domain/url-parser';
import { RegistryCli } from '../../types';
import { Component } from '../../types';

const getOcVersion = (): string => {
const ocPackagePath = path.join(__dirname, '../../../package.json');
Expand All @@ -20,15 +20,15 @@ interface RegistryOptions {
registry?: string;
}

export default function registry(opts: RegistryOptions = {}): RegistryCli {
export default function registry(opts: RegistryOptions = {}) {
let requestsHeaders = {
'user-agent': `oc-cli-${getOcVersion()}/${process.version}-${
process.platform
}-${process.arch}`
};

return {
async add(registry: string) {
async add(registry: string): Promise<void> {
if (registry.slice(registry.length - 1) !== '/') {
registry += '/';
}
Expand Down Expand Up @@ -57,7 +57,7 @@ export default function registry(opts: RegistryOptions = {}): RegistryCli {
throw 'oc registry not available';
}
},
async get() {
async get(): Promise<string[]> {
if (opts.registry) {
return [opts.registry];
}
Expand All @@ -73,12 +73,12 @@ export default function registry(opts: RegistryOptions = {}): RegistryCli {
throw 'No oc registries';
}
},
getApiComponentByHref(href: string) {
getApiComponentByHref(href: string): Promise<Component> {
return got(href + settings.registry.componentInfoPath, {
headers: requestsHeaders
}).json();
},
async getComponentPreviewUrlByUrl(componentHref: string) {
async getComponentPreviewUrlByUrl(componentHref: string): Promise<string> {
const res: { requestVersion: string; href: string } = await got(
componentHref,
{ headers: requestsHeaders }
Expand All @@ -93,7 +93,7 @@ export default function registry(opts: RegistryOptions = {}): RegistryCli {
password?: string;
route: string;
path: string;
}) {
}): Promise<void> {
if (!!options.username && !!options.password) {
requestsHeaders = Object.assign(requestsHeaders, {
Authorization:
Expand Down Expand Up @@ -129,7 +129,7 @@ export default function registry(opts: RegistryOptions = {}): RegistryCli {
throw errMsg;
}
},
async remove(registry: string) {
async remove(registry: string): Promise<void> {
if (registry.slice(registry.length - 1) !== '/') {
registry += '/';
}
Expand All @@ -143,3 +143,5 @@ export default function registry(opts: RegistryOptions = {}): RegistryCli {
}
};
}

export type RegistryCli = ReturnType<typeof registry>;
2 changes: 1 addition & 1 deletion src/cli/facade/clean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import readCb from 'read';
import { fromPromise } from 'universalify';
import { promisify } from 'util';
import strings from '../../resources/index';
import { Local } from '../../types';
import type { Local } from '../domain/local';
import { Logger } from '../logger';

const read = promisify(readCb);
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 @@ -11,7 +11,7 @@ import * as oc from '../../index';
import strings from '../../resources/index';
import watch from '../domain/watch';
import { Logger } from '../logger';
import { Local } from '../../types';
import type { Local } from '../domain/local';

type Registry = ReturnType<typeof oc.Registry>;

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 @@ -2,7 +2,7 @@ import path from 'path';
import { fromPromise } from 'universalify';

import strings from '../../resources/index';
import { Local } from '../../types';
import type { Local } from '../domain/local';
import { Logger } from '../logger';

const init = ({ local, logger }: { local: Local; logger: Logger }) =>
Expand Down
2 changes: 1 addition & 1 deletion src/cli/facade/mock.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { fromPromise } from 'universalify';
import strings from '../../resources/index';
import { Local } from '../../types';
import type { Local } from '../domain/local';
import { Logger } from '../logger';

const mock = ({ local, logger }: { local: Local; logger: Logger }) =>
Expand Down
3 changes: 2 additions & 1 deletion src/cli/facade/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import path from 'path';
import { fromPromise } from 'universalify';
import handleDependencies from '../domain/handle-dependencies';
import { Logger } from '../logger';
import { Component, Local } from '../../types';
import type { Local } from '../domain/local';
import { Component } from '../../types';

const cliPackage = ({ local, logger }: { local: Local; logger: Logger }) =>
fromPromise(
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 @@ -2,7 +2,7 @@ import open from 'open';
import { fromPromise } from 'universalify';

import strings from '../../resources/index';
import { RegistryCli } from '../../types';
import { RegistryCli } from '../domain/registry';
import { Logger } from '../logger';

const preview = ({
Expand Down
4 changes: 3 additions & 1 deletion src/cli/facade/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import fs from 'fs-extra';
import readCb from 'read';
import { promisify } from 'util';
import { Logger } from '../logger';
import { Component, RegistryCli, Local } from '../../types';
import type { Local } from '../domain/local';
import { Component } from '../../types';
import { fromPromise } from 'universalify';

import handleDependencies from '../domain/handle-dependencies';
import strings from '../../resources/index';
import { RegistryCli } from '../domain/registry';

const read = promisify(readCb);

Expand Down
2 changes: 1 addition & 1 deletion src/cli/facade/registry-add.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { fromPromise } from 'universalify';
import strings from '../../resources/index';
import { RegistryCli } from '../../types';
import { RegistryCli } from '../domain/registry';
import { Logger } from '../logger';

const registryAdd = ({
Expand Down
2 changes: 1 addition & 1 deletion src/cli/facade/registry-ls.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { fromPromise } from 'universalify';

import strings from '../../resources/index';
import { RegistryCli } from '../../types';
import { RegistryCli } from '../domain/registry';
import { Logger } from '../logger';

const registryLs = ({
Expand Down
2 changes: 1 addition & 1 deletion src/cli/facade/registry-remove.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { fromPromise } from 'universalify';
import strings from '../../resources/index';
import { RegistryCli } from '../../types';
import { RegistryCli } from '../domain/registry';
import { Logger } from '../logger';

const registryRemove = ({
Expand Down
5 changes: 2 additions & 3 deletions src/cli/programmatic-api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import local from './domain/local';
import local, { Local } from './domain/local';
import { Logger } from './logger';
import Registry from './domain/registry';
import Registry, { RegistryCli } from './domain/registry';

import dev from './facade/dev';
import init from './facade/init';
Expand All @@ -11,7 +11,6 @@ import preview from './facade/preview';
import registryAdd from './facade/registry-add';
import registryLs from './facade/registry-ls';
import registryRemove from './facade/registry-remove';
import { Local, RegistryCli } from '../types';

type Options<T extends (...args: any) => any> = Parameters<ReturnType<T>>[0];
type Cb<T extends (...args: any) => any> = Parameters<ReturnType<T>>[1];
Expand Down
3 changes: 2 additions & 1 deletion src/registry/app-start.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import colors from 'colors/safe';
import path from 'path';
import fs from 'fs-extra';
import { Config, Repository } from '../types';
import type { Repository } from './domain/repository';
import { Config } from '../types';

const packageInfo = fs.readJsonSync(
path.join(
Expand Down
Loading