Skip to content

Commit

Permalink
fix: improve io (#96)
Browse files Browse the repository at this point in the history
* fix: improve io

* chore: nit
  • Loading branch information
hongaar authored Dec 15, 2022
1 parent 5e7c5d9 commit 03b6c1a
Show file tree
Hide file tree
Showing 20 changed files with 2,145 additions and 99 deletions.
19 changes: 10 additions & 9 deletions packages/cli/moker.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
#!/usr/bin/env node

import { writeError } from "@mokr/core";
import { hasEncounteredErrors, writeError } from "@mokr/core";
import cli from "./dist/cli.js";

function errorHandler(err) {
if (process.env["DEBUG"]) {
console.debug(err);
} else {
writeError(`\n${String(err)}`);
}

writeError(err);
process.exit(1);
}

cli.runOrRepl().catch(errorHandler);

// Compat layer for Node < 16
process.on("unhandledRejection", errorHandler);

// Run application
await cli.runOrRepl().catch(errorHandler);

// If tasks had errors, exit with non-zero code
if (hasEncounteredErrors()) {
process.exit(1);
}
4 changes: 2 additions & 2 deletions packages/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const cli = program().prompt("moker > ");
Object.values(commands).forEach((command) => cli.add(command));

// Some assertions we always need
assertYarnVersion(3);
assertNodeVersion(14);
await assertYarnVersion(2);
await assertNodeVersion(14);

export default cli;
10 changes: 7 additions & 3 deletions packages/cli/src/commands/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { addWorkspace, applyTemplate, isMonorepo, task } from "@mokr/core";
import { command } from "bandersnatch";
import { resolve } from "node:path";
import {
addPlugin,
format,
installPlugin,
loadPlugins,
updateDependencies,
} from "../tasks.js";
Expand Down Expand Up @@ -37,19 +37,23 @@ export const add = command("add")
}

for (const workspaceName of name) {
const workspaceDirectory = await task(
const [workspaceDirectory, error] = await task(
`Add workspace ${workspaceName}`,
() => addWorkspace({ directory, name: workspaceName })
);

if (error) {
throw error;
}

for (const name of template) {
await task(`Apply template ${name}`, () =>
applyTemplate({ directory: workspaceDirectory, name })
);
}

for (const name of plugin) {
await addPlugin({ directory: workspaceDirectory, name });
await installPlugin({ directory: workspaceDirectory, name });
}

await loadPlugins({ directory: workspaceDirectory });
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/commands/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import {
import { command } from "bandersnatch";
import { resolve } from "node:path";
import {
addPlugin,
format,
installPlugin,
loadPlugins,
updateDependencies,
} from "../tasks.js";
Expand Down Expand Up @@ -93,7 +93,7 @@ export const create = command("create")
}

for (const name of plugin) {
await addPlugin({ directory, name });
await installPlugin({ directory, name });
}

await loadPlugins({ directory });
Expand Down
8 changes: 5 additions & 3 deletions packages/cli/src/tasks.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
exec,
hasPlugin,
installPlugin,
installPlugin as coreInstallPlugin,
loadAllPlugins,
runDependencyQueues,
task,
Expand All @@ -19,11 +19,13 @@ export async function format({ directory }: DirOption) {
}
}

export async function addPlugin({
export async function installPlugin({
directory,
name,
}: DirOption & { name: string }) {
await task(`Add plugin ${name}`, () => installPlugin({ directory, name }));
await task(`Install plugin ${name}`, () =>
coreInstallPlugin({ directory, name })
);
}

export async function loadPlugins({ directory }: DirOption) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"parent": "9d05f215-841d-4c98-ab0b-63a8d0c938cf",
"pid": 15752,
"argv": [
"/home/hongaar/.nvm/versions/node/v18.12.1/bin/node",
"/home/hongaar/Code/hongaar/moker/packages/core/test/json.test.ts"
],
"execArgv": [],
"cwd": "/home/hongaar/Code/hongaar/moker/packages/core",
"time": 1671097032296,
"ppid": 15725,
"coverageFilename": "/home/hongaar/Code/hongaar/moker/packages/core/.nyc_output/4f1e7034-ddce-495f-b447-e58daf3c0760.json",
"externalId": "test/json.test.ts",
"uuid": "4f1e7034-ddce-495f-b447-e58daf3c0760",
"files": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"parent": "9d05f215-841d-4c98-ab0b-63a8d0c938cf",
"pid": 15751,
"argv": [
"/home/hongaar/.nvm/versions/node/v18.12.1/bin/node",
"/home/hongaar/Code/hongaar/moker/packages/core/test/io.test.ts"
],
"execArgv": [],
"cwd": "/home/hongaar/Code/hongaar/moker/packages/core",
"time": 1671097032296,
"ppid": 15725,
"coverageFilename": "/home/hongaar/Code/hongaar/moker/packages/core/.nyc_output/837d69e9-01b2-4a68-8ecf-0699afb3bddb.json",
"externalId": "test/io.test.ts",
"uuid": "837d69e9-01b2-4a68-8ecf-0699afb3bddb",
"files": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"parent": null,
"pid": 15725,
"argv": [
"/home/hongaar/.nvm/versions/node/v18.12.1/bin/node",
"/home/hongaar/Code/hongaar/moker/node_modules/tap/bin/run.js"
],
"execArgv": [],
"cwd": "/home/hongaar/Code/hongaar/moker/packages/core",
"time": 1671097031352,
"ppid": 15681,
"coverageFilename": "/home/hongaar/Code/hongaar/moker/packages/core/.nyc_output/9d05f215-841d-4c98-ab0b-63a8d0c938cf.json",
"externalId": "",
"uuid": "9d05f215-841d-4c98-ab0b-63a8d0c938cf",
"files": []
}
32 changes: 32 additions & 0 deletions packages/core/.nyc_output/processinfo/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"processes": {
"4f1e7034-ddce-495f-b447-e58daf3c0760": {
"parent": "9d05f215-841d-4c98-ab0b-63a8d0c938cf",
"externalId": "test/json.test.ts",
"children": []
},
"837d69e9-01b2-4a68-8ecf-0699afb3bddb": {
"parent": "9d05f215-841d-4c98-ab0b-63a8d0c938cf",
"externalId": "test/io.test.ts",
"children": []
},
"9d05f215-841d-4c98-ab0b-63a8d0c938cf": {
"parent": null,
"children": [
"4f1e7034-ddce-495f-b447-e58daf3c0760",
"837d69e9-01b2-4a68-8ecf-0699afb3bddb"
]
}
},
"files": {},
"externalIds": {
"test/json.test.ts": {
"root": "4f1e7034-ddce-495f-b447-e58daf3c0760",
"children": []
},
"test/io.test.ts": {
"root": "837d69e9-01b2-4a68-8ecf-0699afb3bddb",
"children": []
}
}
}
10 changes: 5 additions & 5 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
"build:watch": "tsc --watch",
"clean": "rm -rf dist && rm -rf types",
"prepublish": "yarn build",
"test": "xv --loader=ts-node/esm test",
"test:watch": "jest --watch"
"test": "NODE_OPTIONS='--loader=ts-node/esm --no-warnings' node--test test/*.test.ts | NODE_OPTIONS='--loader=ts-node/esm --no-warnings' tap --no-coverage"
},
"dependencies": {
"chalk": "5.2.0",
Expand All @@ -33,18 +32,19 @@
"@types/hosted-git-info": "3.0.2",
"@types/node": "18.11.13",
"sort-package-json": "2.1.0",
"tap": "16.3.2",
"tempy": "3.0.0",
"test": "3.2.1",
"ts-node": "10.9.1",
"typescript": "4.9.4",
"xv": "2.0.0"
"typescript": "4.9.4"
},
"publishConfig": {
"access": "public"
},
"moker": {
"plugins": [
"typescript",
"xv"
"test"
]
}
}
66 changes: 50 additions & 16 deletions packages/core/src/io.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
import chalk from "chalk";
import ora from "ora";

type Log = {
message: string;
type: "debug" | "info" | "warning";
};
type Log =
| {
message: string;
type: "debug" | "info" | "warning";
}
| {
message: string | Error;
type: "error";
};

let messages: Log[];
let encounteredErrors: boolean;

function resetLog() {
messages = [];
}

resetLog();
resetState();

export function log(message: string, type: Log["type"] = "info") {
export function log(
message: string,
type: "debug" | "info" | "warning" = "info"
) {
messages.push({ message, type });
}

export function logError(error: string | Error) {
messages.push({ message: error, type: "error" });
encounteredErrors = true;
}

export function debug(message: string) {
if (process.env["DEBUG"]) {
log(message, "debug");
Expand All @@ -44,8 +54,25 @@ export function writeWarning(message: string) {
return console.warn(chalk.bgYellow.black(message));
}

export function writeError(message: string) {
return console.error(chalk.bgRed.white(message));
export function writeError(err: string | Error) {
if (process.env["DEBUG"]) {
console.debug(err);
} else {
console.error(chalk.bgRed.white(String(err)));
}
}

export function resetMessages() {
messages = [];
}

export function resetEncounteredErrors() {
encounteredErrors = false;
}

export function resetState() {
resetMessages();
resetEncounteredErrors();
}

export async function flushLogs() {
Expand All @@ -54,9 +81,11 @@ export async function flushLogs() {
? writeWarning(message)
: type === "debug"
? writeDebug(message)
: type === "error"
? writeError(message)
: writeInfo(message);
}
resetLog();
resetMessages();
}

export async function task<T>(title: string, callback: () => Promise<T>) {
Expand All @@ -65,10 +94,11 @@ export async function task<T>(title: string, callback: () => Promise<T>) {

try {
result = await callback();
} catch (error) {
} catch (error: any) {
spinner.fail();
logError(error);
flushLogs();
throw error;
return [null, error as Error] as const;
}

if (messages.filter(({ type }) => type === "warning").length > 0) {
Expand All @@ -79,5 +109,9 @@ export async function task<T>(title: string, callback: () => Promise<T>) {

flushLogs();

return result;
return [result, null] as const;
}

export function hasEncounteredErrors() {
return encounteredErrors;
}
1 change: 1 addition & 0 deletions packages/core/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const CORE_PLUGINS = [
"doctoc",
"esbuild",
"xv",
"test",
];

export function isPlugin(plugin: unknown): plugin is Plugin {
Expand Down
Loading

0 comments on commit 03b6c1a

Please sign in to comment.