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 a bit better package jsons, component package jsons and author-p… #1243

Merged
merged 2 commits into from
Oct 13, 2021
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"simple-git": "2.46.0",
"sinon": "11.1.2",
"ts-node": "10.3.0",
"type-fest": "^2.5.0",
"typescript": "4.4.3"
},
"dependencies": {
Expand Down
3 changes: 2 additions & 1 deletion src/cli/domain/get-components-by-dir.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import fs from 'fs-extra';
import path from 'path';
import { Component } from '../../types';

export default function getComponentsByDir() {
return (componentsDir: string, callback: Callback<string[]>): void => {
const isOcComponent = function (file: string) {
const filePath = path.resolve(componentsDir, file),
packagePath = path.join(filePath, 'package.json');
let content;
let content: Component;

try {
content = fs.readJsonSync(packagePath);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import strings from '../../../resources';
import { Component } from '../../../types';

export default function ensureCompilerIsDeclaredAsDevDependency(
options: {
componentPath: string;
pkg: { devDependencies: Dictionary<string> };
pkg: Component;
template: string;
},
cb: Callback<string, string>
): void {
const { componentPath, pkg, template } = options;
const compilerDep = `${template}-compiler`;
const isOk = pkg.devDependencies[compilerDep];
const isOk = pkg.devDependencies?.[compilerDep];

const err = isOk
? null
Expand Down
3 changes: 1 addition & 2 deletions src/cli/domain/handle-dependencies/get-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default function getCompiler(
compilerDep: string;
componentPath: string;
logger: Logger;
pkg: { name: string; devDependencies: Dictionary<string> };
pkg: { devDependencies: Dictionary<string> };
pbazydlo marked this conversation as resolved.
Show resolved Hide resolved
},
cb: Callback<string, string | number>
): void {
Expand All @@ -28,7 +28,6 @@ export default function getCompiler(

const installOptions = {
compilerPath,
componentName: pkg.name,
componentPath,
dependency,
logger
Expand Down
29 changes: 18 additions & 11 deletions src/cli/domain/handle-dependencies/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import async from 'async';
import coreModules from 'builtin-modules';
import fs from 'fs-extra';
import path from 'path';
import _ from 'lodash';

import ensureCompilerIsDeclaredAsDevDependency from './ensure-compiler-is-declared-as-devDependency';
import getCompiler from './get-compiler';
Expand All @@ -13,8 +12,14 @@ import strings from '../../../resources';
import { Logger } from '../../logger';
import { Component } from '../../../types';

const getComponentPackageJson = (componentPath: string, cb: Callback<any>) =>
fs.readJson(path.join(componentPath, 'package.json'), cb);
const getComponentPackageJson = (
componentPath: string,
cb: Callback<Component>
) => fs.readJson(path.join(componentPath, 'package.json'), cb);

const union = (a: ReadonlyArray<string>, b: ReadonlyArray<string>) => [
...new Set([...a, ...b])
];

export default function handleDependencies(
options: {
Expand All @@ -33,10 +38,12 @@ export default function handleDependencies(
const { components, logger, useComponentDependencies } = options;

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

const templates: Dictionary<(...args: unknown[]) => unknown> = {};
const addTemplate = (
Expand Down Expand Up @@ -82,21 +89,21 @@ export default function handleDependencies(
cb: any
) =>
ensureCompilerIsDeclaredAsDevDependency(options, (err, compilerDep) =>
cb(err, _.extend(options, { compilerDep }))
cb(err, Object.assign(options, { compilerDep }))
),

(
options: {
componentPath: string;
logger: Logger;
pkg: Component;
pkg: Component & { devDependencies: Dictionary<string> };
template: string;
compilerDep: string;
},
cb: any
) =>
getCompiler(options, (err, compiler) =>
cb(err, _.extend(options, { compiler }))
cb(err, Object.assign(options, { compiler }))
),

(
Expand All @@ -121,7 +128,7 @@ export default function handleDependencies(
}

const result = {
modules: _.union(coreModules, Object.keys(dependencies)).sort(),
modules: union(coreModules, Object.keys(dependencies)).sort(),
templates: Object.values(templates)
};
const options = { dependencies, logger };
Expand Down
2 changes: 1 addition & 1 deletion src/cli/domain/package-components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const packageComponents =

fs.emptyDirSync(publishPath);

const componentPackage = fs.readJsonSync(componentPackagePath);
const componentPackage: Component = fs.readJsonSync(componentPackagePath);
const ocPackage = fs.readJsonSync(ocPackagePath);

if (!validator.validateComponentName(componentPackage.name)) {
Expand Down
3 changes: 2 additions & 1 deletion src/registry/domain/extract-package.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from 'path';
import targz from 'targz';
import { PackageJson } from 'type-fest';

import getPackageJsonFromTempDir from './get-package-json-from-temp-dir';

Expand All @@ -11,7 +12,7 @@ export default function extractPackage(
},
callback: Callback<{
outputFolder: string;
packageJson: any;
packageJson: PackageJson;
}>
): void {
const packageFile: Express.Multer.File = (files as any)[0];
Expand Down
3 changes: 2 additions & 1 deletion src/registry/domain/get-package-json-from-temp-dir.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import fs from 'fs-extra';
import path from 'path';
import { PackageJson } from 'type-fest';

export default function getPackageJsonFromTempDir(
tempDirPath: string,
callback: Callback<any>
callback: Callback<PackageJson>
): void {
return fs.readJson(path.join(tempDirPath, 'package.json'), callback);
}
11 changes: 6 additions & 5 deletions src/registry/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,21 @@ import getComponentsHistory from './helpers/get-components-history';
import getAvailableDependencies from './helpers/get-available-dependencies';
import indexView from '../views';
import urlBuilder = require('../domain/url-builder');
import { Author, Component, Repository } from '../../types';
import { Author, Component, ParsedComponent, Repository } from '../../types';
import { NextFunction, Request, Response } from 'express';
import { IncomingHttpHeaders } from 'http';
import { PackageJson } from 'type-fest';

const packageInfo = fs.readJsonSync(
const packageInfo: PackageJson = fs.readJsonSync(
path.join(__dirname, '..', '..', '..', 'package.json')
);

const getParsedAuthor = (author: Author | string): Author => {
const getParsedAuthor = (author?: Author | string): Author => {
author = author || {};
return typeof author === 'string' ? parseAuthor(author) : author;
};

const mapComponentDetails = (component: Component): Component =>
const mapComponentDetails = (component: Component): ParsedComponent =>
_.extend(component, { author: getParsedAuthor(component.author) });

const isHtmlRequest = (headers: IncomingHttpHeaders) =>
Expand All @@ -43,7 +44,7 @@ export default function (repository: Repository) {
};

if (isHtmlRequest(req.headers) && !!res.conf.discovery) {
let componentsInfo: Component[] = [];
let componentsInfo: ParsedComponent[] = [];
let componentsReleases = 0;
const stateCounts: { deprecated?: number; experimental?: number } = {};

Expand Down
4 changes: 2 additions & 2 deletions src/registry/views/partials/components-list.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, VM } from '../../../types';
import { ParsedComponent, VM } from '../../../types';
import getSelectedCheckbox from './selected-checkbox';

export default function componentsList(vm: VM): string {
Expand All @@ -9,7 +9,7 @@ export default function componentsList(vm: VM): string {
? '<div class="date">Updated</div><div class="activity">Activity</div>'
: '';

const componentRow = (component: Component) => {
const componentRow = (component: ParsedComponent) => {
const componentState = component.oc.state
? `<div class="state component-state-${component.oc.state.toLowerCase()}">${
component.oc.state
Expand Down
22 changes: 8 additions & 14 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import { NextFunction, Request, Response } from 'express';
import { Logger } from './cli/logger';

interface PackageJson {
name: string;
version: string;
dependencies?: Record<string, string>;
devDependencies?: Record<string, string>;
}
import { PackageJson } from 'type-fest';

export interface Author {
name?: string;
Expand Down Expand Up @@ -85,14 +79,14 @@ interface OcConfiguration {
}

export interface Component extends PackageJson {
name: string;
version: string;
allVersions: string[];
author: Author;
repository?: string;
dependencies: Record<string, string>;
description: string;
devDependencies: Record<string, string>;
oc: OcConfiguration;
scripts: Record<string, string>;
}

export interface ParsedComponent extends Omit<Component, 'author'> {
author: Author;
}

export interface VM {
Expand All @@ -103,7 +97,7 @@ export interface VM {
version: string;
link: string;
}>;
components: Component[];
components: ParsedComponent[];
componentsHistory?: ComponentHistory[];
componentsList: ComponentList[];
componentsReleases: number;
Expand Down