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

Utils package #61

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Particular codechecks can be distributed as **npm** packages and can be setup in
| Package | Version | Description |
| ---------------------------------------- | --------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------- |
| [`@codechecks/client`](/packages/client) | [![npm](https://img.shields.io/npm/v/@codechecks/client.svg)](https://www.npmjs.com/package/@codechecks/client) | TypeScript/JavaScript client used for executing codechecks |
| [`@codechecks/client`](/packages/utils) | [![npm](https://img.shields.io/npm/v/@codechecks/utils.svg)](https://www.npmjs.com/package/@codechecks/utils) | Utils to simplify codechecks plugins development |

Note: Currently we are in the process of open sourcing whole platform.

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "MIT",
"scripts": {
"prepare": "patch-package",
"build": "rm -rf ./dist && tsc -p ./tsconfig.prod.json && (cd packages/client/ && yarn build)",
"build": "rm -rf ./dist && tsc -p ./tsconfig.prod.json && (cd packages/client/ && yarn build) && (cd packages/utils/ && yarn build)",
"tsc": "tsc -p . --noEmit && tsc -p ./tsconfig.prod.json --noEmit",
"tslint": "tslint -p ./tsconfig.json -e 'node_modules/**/*' -e '**/node_modules/**/*' 'packages/**/*.ts'",
"tslint:fix": "tslint --fix --format stylish -p ./tsconfig.json -e 'node_modules/**/*' -e '**/node_modules/**/*' 'packages/**/*.ts'",
Expand All @@ -30,7 +30,7 @@
"ts-node": "^7.0.1",
"tsconfig-paths": "^3.7.0",
"tslint": "^5.11.0",
"typescript": "^3.1.6",
"typescript": "^3.6.3",
"typestrict": "^1.0.1",
"wsrun": "^5.0.0"
},
Expand Down
4 changes: 2 additions & 2 deletions packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"types": "dist/index.d.ts",
"license": "MIT",
"scripts": {
"build": "rm -rf ./dist && cp -R '../../dist/client/' ./dist/ && cp -R ./src/js ./dist/js && chmod +x ./dist/runner.js",
"build": "rm -rf ./dist && cp -R '../../dist/client/src/' ./dist/ && cp -R ./src/js ./dist/js && chmod +x ./dist/runner.js",
"prepublishOnly": "cd ../../ && yarn build"
},
"dependencies": {
Expand All @@ -42,7 +42,7 @@
"promise": "^8.0.2",
"request": "^2.88.0",
"request-promise": "^4.2.2",
"ts-essentials": "^1.0.2",
"ts-essentials": "^3.0.2",
"ts-node": "^8.0.2",
"url-join": "^4.0.0"
},
Expand Down
3 changes: 1 addition & 2 deletions packages/client/src/file-handling/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { join, extname } from "path";
import { existsSync } from "fs";
import { loadJson } from "../file-executors/executeJson";
import { loadYaml } from "../file-executors/executeYaml";
import { DeepPartial } from "ts-essentials";
import { crash } from "../utils/errors";

const CODECHECKS_SETTINGS_FILES_NAMES = ["codechecks.yml", "codechecks.yaml", "codechecks.json"];
Expand Down Expand Up @@ -37,7 +36,7 @@ function loadSettingsFromFile(filePath: string): CodeChecksSettings | undefined
}
}

function normalizeSettings(userProvidedSettings: DeepPartial<CodeChecksSettings> = {}): CodeChecksSettings {
function normalizeSettings(userProvidedSettings: Partial<CodeChecksSettings> = {}): CodeChecksSettings {
return {
speculativeBranchSelection: userProvidedSettings.speculativeBranchSelection === false ? false : true,
branches: userProvidedSettings.branches || ["master"],
Expand Down
13 changes: 13 additions & 0 deletions packages/utils/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<p align="center">
<a href="https://codechecks.io"><img src="./images/codechecks.png" width="600" alt="codechecks.io"></a>
<h1 align="center">Code Checks Utils</h1>
<p align="center">
Utils to simplify codechecks plugins development
</p>
<p align="center">
<a href="https://circleci.com/gh/codechecks/monorepo"><img alt="Build Status" src="https://circleci.com/gh/codechecks/monorepo/tree/master.svg?style=svg"></a>
<a href="/package.json"><img alt="Software License" src="https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square"></a>
<img src="https://img.shields.io/badge/all_contributors-7-orange.svg?style=flat-square" alt="All Contributors">
<a href="https://codechecks.io"><img src="https://raw.githubusercontent.com/codechecks/docs/master/images/badges/badge-default.svg?sanitize=true" alt="codechecks.io"></a>
</p>
</p>
46 changes: 46 additions & 0 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "@codechecks/utils",
"description": "Utils to simplify codechecks plugins development",
"keywords": [
"codechecks",
"utils"
],
"repository": "codechecks/monorepo",
"author": "Chris Kaczor <chris@kaczor.io>",
"version": "0.1.2",
"main": "dist/index.js",
"bin": {
"codechecks": "dist/runner.js"
},
"types": "dist/index.d.ts",
"license": "MIT",
"scripts": {
"build": "rm -rf ./dist && cp -R '../../dist/utils/src/' ./dist/",
"prepublishOnly": "cd ../../ && yarn build"
},
"dependencies": {
"@types/debug": "^4.1.5",
"debug": "^4.1.1",
"execa": "^2.0.4",
"lodash": "^4.17.15",
"promise": "^8.0.2",
"promise-all": "^1.0.0",
"tmp-promise": "^2.0.2",
"ts-essentials": "^3.0.2",
"ts-node": "^8.0.2",
"url-join": "^4.0.0"
},
"devDependencies": {
"@types/lodash": "^4.14.138",
"@types/tmp": "^0.1.0"
},
"files": [
"dist/**/*"
],
"publishConfig": {
"access": "public"
},
"engines": {
"node": ">=6"
}
}
5 changes: 5 additions & 0 deletions packages/utils/src/createDebug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as debugLib from "debug";

export function createDebug(name: string): debugLib.Debugger {
return debugLib(`codechecks:${name}`);
}
42 changes: 42 additions & 0 deletions packages/utils/src/execute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import * as execa from "execa";
import { Readable } from "stream";
import { createDebug } from "./createDebug";
import { Debugger } from "debug";

interface SharedOptions extends execa.CommonOptions<string> {}

interface ExecuteOptions extends SharedOptions {
file: string;
args?: string[];
}
interface ExecuteCommandOptions extends SharedOptions {
cmd: string;
}

type ExecuteResult = execa.ExecaChildProcess;

export function execute(options: ExecuteOptions): ExecuteResult {
const executionResult = execa(options.file, options.args, options);
logResult(executionResult, "execute");
return executionResult;
}

export function executeCommand(options: ExecuteCommandOptions): ExecuteResult {
const executionResult = (execa as any).command(options.cmd, options);
logResult(executionResult, "executeCommand");
return executionResult;
}

function logResult(result: ExecuteResult, namespace: string): void {
function logStream(stream: Readable, debug: Debugger): void {
stream.on("close", () => debug("close"));
stream.on("error", (e: any) => debug("error", e));
stream.on("end", () => debug("end"));
stream.on("data", (data: any) => debug(data.toString()));
}

const outDebug = createDebug(`utils:${namespace}:out`);
logStream(result.stdout, outDebug);
const errDebug = createDebug(`utils:${namespace}:err`);
logStream(result.stderr, errDebug);
}
4 changes: 4 additions & 0 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from "./createDebug";
export * from "./using";
export * from "./tmpDir";
export * from "./execute";
27 changes: 27 additions & 0 deletions packages/utils/src/tmpDir.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { dir } from "tmp-promise";
import { DisposableResource } from "./using";
import { createDebug } from "./createDebug";

const SHARED_PREFIX = "codechecks";

const debug = createDebug("utils/tmp-dir");

export async function createTmpDir(): Promise<string> {
const { path: tmpPathDir } = await dir({ prefix: SHARED_PREFIX });
debug(`Created tmp dir:`, tmpPathDir);

return tmpPathDir;
}

export async function disposableTmpDir(): Promise<DisposableResource<string>> {
const tmpDir = await dir({ prefix: SHARED_PREFIX });
debug(`Created tmp dir:`, tmpDir.path);

return {
resource: tmpDir.path,
dispose: async () => {
debug(`Deleting tmp dir:`, tmpDir.path);
await tmpDir.cleanup();
},
};
}
26 changes: 26 additions & 0 deletions packages/utils/src/using.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { AsyncOrSync } from "ts-essentials";
import { mapValues } from "lodash";
import { Dictionary } from "ts-essentials";
const promiseAll = require("promise-all");

export type DisposableResource<T> = {
resource: T;
dispose: () => AsyncOrSync<any>;
};

declare type DisposableResourceMap<T> = {
[P in keyof T]: T[P] extends () => AsyncOrSync<DisposableResource<infer R>> ? R : never
};

export async function using<T extends Dictionary<() => AsyncOrSync<DisposableResource<any>>>>(
disposable: T,
fn: (resources: DisposableResourceMap<T>) => any,
): Promise<void> {
const allDisposablePromises = mapValues(disposable, d => d());
const resources = await promiseAll(allDisposablePromises);

await fn(resources);

const disposeAllResources = mapValues(resources, r => r.dispose());
await promiseAll(disposeAllResources);
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"lib": ["es2015", "esnext.asynciterable", "dom"],
"sourceMap": true,
"declaration": true,
"outDir": "dist/client",
"outDir": "./dist",
"baseUrl": "packages",
"paths": {
"codechecks": ["client/src"]
Expand Down
Loading