mirror of
https://github.com/morten-olsen/mini-loader.git
synced 2026-02-08 01:36:26 +01:00
init
This commit is contained in:
3
packages/cli/.gitignore
vendored
Normal file
3
packages/cli/.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
/dist/
|
||||
/node_modules/
|
||||
/coverage/
|
||||
4
packages/cli/bin/index.mjs
Executable file
4
packages/cli/bin/index.mjs
Executable file
@@ -0,0 +1,4 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import 'source-map-support/register.js';
|
||||
import '../dist/esm/index.js';
|
||||
41
packages/cli/package.json
Normal file
41
packages/cli/package.json
Normal file
@@ -0,0 +1,41 @@
|
||||
{
|
||||
"name": "@morten-olsen/mini-loader-cli",
|
||||
"version": "1.0.0",
|
||||
"main": "./dist/esm/index.js",
|
||||
"types": "./dist/esm/index.d.ts",
|
||||
"bin": {
|
||||
"mini-loader": "./bin/index.mjs"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "tsc --build"
|
||||
},
|
||||
"type": "module",
|
||||
"files": [
|
||||
"./dist"
|
||||
],
|
||||
"exports": {
|
||||
".": {
|
||||
"import": "./dist/esm/index.js"
|
||||
}
|
||||
},
|
||||
"dependencies": {
|
||||
"@rollup/plugin-auto-install": "^3.0.5",
|
||||
"@rollup/plugin-commonjs": "^25.0.7",
|
||||
"@rollup/plugin-json": "^6.1.0",
|
||||
"@rollup/plugin-node-resolve": "^15.2.3",
|
||||
"@rollup/plugin-replace": "^5.0.5",
|
||||
"@rollup/plugin-sucrase": "^5.0.2",
|
||||
"@trpc/client": "^10.45.0",
|
||||
"commander": "^11.1.0",
|
||||
"rollup": "^4.9.4",
|
||||
"rollup-plugin-node-polyfills": "^0.2.1",
|
||||
"source-map-support": "^0.5.21",
|
||||
"superjson": "^2.2.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@morten-olsen/mini-loader-configs": "workspace:^",
|
||||
"@morten-olsen/mini-loader-runner": "workspace:^",
|
||||
"@morten-olsen/mini-loader-server": "workspace:^",
|
||||
"typescript": "^5.3.3"
|
||||
}
|
||||
}
|
||||
42
packages/cli/src/bundler/bundler.ts
Normal file
42
packages/cli/src/bundler/bundler.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { rollup } from 'rollup';
|
||||
import sucrase from '@rollup/plugin-sucrase';
|
||||
import { nodeResolve } from '@rollup/plugin-node-resolve';
|
||||
import commonjs from '@rollup/plugin-commonjs';
|
||||
import json from '@rollup/plugin-json';
|
||||
import auto from '@rollup/plugin-auto-install';
|
||||
import { resolve } from 'path';
|
||||
|
||||
const fix = <T extends { default: any }>(f: T) => f as T['default'];
|
||||
|
||||
type BundleOptions = {
|
||||
entry: string;
|
||||
autoInstall?: boolean;
|
||||
};
|
||||
|
||||
const bundle = async ({ entry, autoInstall }: BundleOptions) => {
|
||||
const entryFile = resolve(entry);
|
||||
const codeBundler = await rollup({
|
||||
plugins: [
|
||||
fix(sucrase)({
|
||||
transforms: ['typescript', 'jsx'],
|
||||
}),
|
||||
...[autoInstall ? fix(auto) : []],
|
||||
nodeResolve({ extensions: ['.js', '.jsx', '.ts', '.tsx'] }),
|
||||
fix(json)(),
|
||||
fix(commonjs)({ include: /node_modules/ }),
|
||||
],
|
||||
input: entryFile,
|
||||
});
|
||||
const { output: codeOutput } = await codeBundler.generate({
|
||||
format: 'cjs',
|
||||
});
|
||||
if (codeOutput.length > 1) {
|
||||
throw new Error('Multiple outputs are not supported');
|
||||
}
|
||||
const [codeResult] = codeOutput;
|
||||
|
||||
const { code } = codeResult;
|
||||
return code;
|
||||
};
|
||||
|
||||
export { bundle };
|
||||
20
packages/cli/src/client/client.ts
Normal file
20
packages/cli/src/client/client.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { createTRPCProxyClient, httpBatchLink } from '@trpc/client';
|
||||
import superjson from 'superjson';
|
||||
import type { Runtime } from '@morten-olsen/mini-loader-server';
|
||||
import type { RootRouter } from '@morten-olsen/mini-loader-server';
|
||||
|
||||
const createClient = () => {
|
||||
return createTRPCProxyClient<RootRouter>({
|
||||
transformer: superjson,
|
||||
links: [
|
||||
httpBatchLink({
|
||||
url: 'http://localhost:4500',
|
||||
}),
|
||||
],
|
||||
});
|
||||
};
|
||||
|
||||
type Client = ReturnType<typeof createClient>;
|
||||
|
||||
export type { Client, Runtime };
|
||||
export { createClient };
|
||||
32
packages/cli/src/commands/artifacts/artifacts.list.ts
Normal file
32
packages/cli/src/commands/artifacts/artifacts.list.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import { Command } from 'commander';
|
||||
import { createClient } from '../../client/client.js';
|
||||
|
||||
const list = new Command('list');
|
||||
|
||||
const toInt = (value?: string) => {
|
||||
if (!value) {
|
||||
return undefined;
|
||||
}
|
||||
return parseInt(value, 10);
|
||||
};
|
||||
|
||||
list
|
||||
.alias('ls')
|
||||
.description('List logs')
|
||||
.option('-r, --run-id <runId>', 'Run ID')
|
||||
.option('-l, --load-id <loadId>', 'Load ID')
|
||||
.option('-o, --offset <offset>', 'Offset')
|
||||
.option('-a, --limit <limit>', 'Limit', '1000')
|
||||
.action(async () => {
|
||||
const { runId, loadId, offset, limit } = list.opts();
|
||||
const client = await createClient();
|
||||
const artifacts = await client.artifacts.find.query({
|
||||
runId,
|
||||
loadId,
|
||||
offset: toInt(offset),
|
||||
limit: toInt(limit),
|
||||
});
|
||||
console.table(artifacts);
|
||||
});
|
||||
|
||||
export { list };
|
||||
7
packages/cli/src/commands/artifacts/artifacts.ts
Normal file
7
packages/cli/src/commands/artifacts/artifacts.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { Command } from 'commander';
|
||||
import { list } from './artifacts.list.js';
|
||||
|
||||
const artifacts = new Command('artifacts');
|
||||
artifacts.addCommand(list);
|
||||
|
||||
export { artifacts };
|
||||
15
packages/cli/src/commands/loads/loads.list.ts
Normal file
15
packages/cli/src/commands/loads/loads.list.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { Command } from 'commander';
|
||||
import { createClient } from '../../client/client.js';
|
||||
|
||||
const list = new Command('list');
|
||||
|
||||
list
|
||||
.alias('ls')
|
||||
.description('List loads')
|
||||
.action(async () => {
|
||||
const client = await createClient();
|
||||
const loads = await client.loads.find.query({});
|
||||
console.table(loads);
|
||||
});
|
||||
|
||||
export { list };
|
||||
27
packages/cli/src/commands/loads/loads.local.ts
Normal file
27
packages/cli/src/commands/loads/loads.local.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { Command } from 'commander';
|
||||
import { resolve } from 'path';
|
||||
import { run } from '@morten-olsen/mini-loader-runner';
|
||||
import { bundle } from '../../bundler/bundler.js';
|
||||
|
||||
const local = new Command('local');
|
||||
|
||||
local.argument('script').action(async (script) => {
|
||||
const location = resolve(script);
|
||||
const code = await bundle({ entry: location });
|
||||
const { promise, emitter } = await run({
|
||||
script: code,
|
||||
});
|
||||
emitter.addListener('message', (message) => {
|
||||
switch (message.type) {
|
||||
case 'log':
|
||||
console.log(message.payload);
|
||||
break;
|
||||
case 'artifact:create':
|
||||
console.log('artifact:create', message.payload.name);
|
||||
break;
|
||||
}
|
||||
});
|
||||
await promise;
|
||||
});
|
||||
|
||||
export { local };
|
||||
30
packages/cli/src/commands/loads/loads.push.ts
Normal file
30
packages/cli/src/commands/loads/loads.push.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { Command } from 'commander';
|
||||
import { resolve } from 'path';
|
||||
import { createClient } from '../../client/client.js';
|
||||
import { bundle } from '../../bundler/bundler.js';
|
||||
|
||||
const push = new Command('push');
|
||||
|
||||
push
|
||||
.argument('script')
|
||||
.option('-i, --id <id>', 'Load id')
|
||||
.option('-n, --name <name>')
|
||||
.option('-r, --run', 'Run the load')
|
||||
.option('-ai, --auto-install', 'Auto install dependencies', false)
|
||||
.action(async (script) => {
|
||||
const opts = push.opts();
|
||||
const location = resolve(script);
|
||||
const client = await createClient();
|
||||
const code = await bundle({ entry: location, autoInstall: opts.autoInstall });
|
||||
const id = await client.loads.set.mutate({
|
||||
id: opts.id,
|
||||
name: opts.name,
|
||||
script: code,
|
||||
});
|
||||
console.log('created load with id', id);
|
||||
if (opts.run) {
|
||||
client.runs.create.mutate({ loadId: id });
|
||||
}
|
||||
});
|
||||
|
||||
export { push };
|
||||
12
packages/cli/src/commands/loads/loads.ts
Normal file
12
packages/cli/src/commands/loads/loads.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { Command } from 'commander';
|
||||
import { push } from './loads.push.js';
|
||||
import { list } from './loads.list.js';
|
||||
import { local } from './loads.local.js';
|
||||
|
||||
const loads = new Command('loads');
|
||||
|
||||
loads.addCommand(push);
|
||||
loads.addCommand(list);
|
||||
loads.addCommand(local);
|
||||
|
||||
export { loads };
|
||||
36
packages/cli/src/commands/logs/logs.list.ts
Normal file
36
packages/cli/src/commands/logs/logs.list.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { Command } from 'commander';
|
||||
import { createClient } from '../../client/client.js';
|
||||
|
||||
const list = new Command('list');
|
||||
|
||||
const toInt = (value?: string) => {
|
||||
if (!value) {
|
||||
return undefined;
|
||||
}
|
||||
return parseInt(value, 10);
|
||||
};
|
||||
|
||||
list
|
||||
.alias('ls')
|
||||
.description('List logs')
|
||||
.option('-r, --run-id <runId>', 'Run ID')
|
||||
.option('-l, --load-id <loadId>', 'Load ID')
|
||||
.option('--severities <severities...>', 'Severities')
|
||||
.option('-o, --offset <offset>', 'Offset')
|
||||
.option('-a, --limit <limit>', 'Limit', '1000')
|
||||
.option('-s, --sort <order>', 'Sort', 'desc')
|
||||
.action(async () => {
|
||||
const { runId, loadId, severities, offset, limit, order } = list.opts();
|
||||
const client = await createClient();
|
||||
const logs = await client.logs.find.query({
|
||||
runId,
|
||||
loadId,
|
||||
severities,
|
||||
offset: toInt(offset),
|
||||
limit: toInt(limit),
|
||||
order,
|
||||
});
|
||||
console.table(logs.reverse());
|
||||
});
|
||||
|
||||
export { list };
|
||||
7
packages/cli/src/commands/logs/logs.ts
Normal file
7
packages/cli/src/commands/logs/logs.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { Command } from 'commander';
|
||||
import { list } from './logs.list.js';
|
||||
|
||||
const logs = new Command('logs');
|
||||
logs.addCommand(list);
|
||||
|
||||
export { logs };
|
||||
14
packages/cli/src/commands/runs/runs.create.ts
Normal file
14
packages/cli/src/commands/runs/runs.create.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { Command } from 'commander';
|
||||
import { createClient } from '../../client/client.js';
|
||||
|
||||
const create = new Command('create');
|
||||
|
||||
create
|
||||
.description('Create a new run')
|
||||
.argument('load-id', 'Load ID')
|
||||
.action(async (loadId) => {
|
||||
const client = await createClient();
|
||||
await client.runs.create.mutate({ loadId });
|
||||
});
|
||||
|
||||
export { create };
|
||||
16
packages/cli/src/commands/runs/runs.list.ts
Normal file
16
packages/cli/src/commands/runs/runs.list.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { Command } from 'commander';
|
||||
import { createClient } from '../../client/client.js';
|
||||
|
||||
const list = new Command('create');
|
||||
|
||||
list
|
||||
.alias('ls')
|
||||
.description('Find a run')
|
||||
.argument('[load-id]', 'Load ID')
|
||||
.action(async (loadId) => {
|
||||
const client = await createClient();
|
||||
const runs = await client.runs.find.query({ loadId });
|
||||
console.table(runs);
|
||||
});
|
||||
|
||||
export { list };
|
||||
8
packages/cli/src/commands/runs/runs.ts
Normal file
8
packages/cli/src/commands/runs/runs.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { Command } from 'commander';
|
||||
import { create } from './runs.create.js';
|
||||
import { list } from './runs.list.js';
|
||||
|
||||
const runs = new Command('runs');
|
||||
runs.description('Manage runs').addCommand(create).addCommand(list);
|
||||
|
||||
export { runs };
|
||||
12
packages/cli/src/index.ts
Normal file
12
packages/cli/src/index.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { program } from 'commander';
|
||||
import { loads } from './commands/loads/loads.js';
|
||||
import { runs } from './commands/runs/runs.js';
|
||||
import { logs } from './commands/logs/logs.js';
|
||||
import { artifacts } from './commands/artifacts/artifacts.js';
|
||||
|
||||
program.addCommand(loads);
|
||||
program.addCommand(runs);
|
||||
program.addCommand(logs);
|
||||
program.addCommand(artifacts);
|
||||
|
||||
await program.parseAsync();
|
||||
9
packages/cli/tsconfig.json
Normal file
9
packages/cli/tsconfig.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"extends": "@morten-olsen/mini-loader-configs/tsconfig.esm.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "dist/esm",
|
||||
},
|
||||
"include": [
|
||||
"./src/**/*.ts"
|
||||
],
|
||||
}
|
||||
Reference in New Issue
Block a user