Skip to content

Commit

Permalink
#296: first draft of the cli with build command
Browse files Browse the repository at this point in the history
  • Loading branch information
petermasking committed Aug 27, 2024
1 parent c8e1446 commit 26c05f8
Show file tree
Hide file tree
Showing 21 changed files with 264 additions and 49 deletions.
18 changes: 18 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions packages/cli/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

# Changelog

This package doesn't keep a changelog. See the changelog in the [github repository](https://github.com/MaskingTechnology/jitar/blob/main/CHANGELOG.md)
9 changes: 9 additions & 0 deletions packages/cli/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

# Jitar Caching

This package contains the components for creating the runtime cache for [Jitar](https://jitar.dev) applications.

For more information about Jitar:

* [Visit our website](https://jitar.dev)
* [Read the documentation](https://docs.jitar.dev).
45 changes: 45 additions & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"name": "@jitar/cli",
"version": "0.7.4",
"description": "JavaScript CLI library for the Jitar runtime.",
"author": "Masking Technology <[email protected]> (https://jitar.dev)",
"license": "MIT",
"type": "module",
"types": "dist/lib.d.ts",
"exports": {
".": "./dist/lib.js"
},
"files": [
"CHANGELOG.md",
"README.md",
"dist"
],
"publishConfig": {
"access": "public"
},
"scripts": {
"test": "vitest run",
"test-coverage": "vitest run --coverage",
"lint": "eslint . --ext .ts",
"build": "tsc -p tsconfig.json",
"clean": "rm -rf dist",
"prepublishOnly": "npm run clean && npm run build"
},
"dependencies": {
"@jitar/caching": "*",
"@jitar/server-nodejs": "*"
},
"repository": {
"type": "git",
"url": "git+https://github.com/MaskingTechnology/jitar.git"
},
"bugs": {
"url": "https://github.com/MaskingTechnology/jitar/issues"
},
"homepage": "https://jitar.dev",
"keywords": [
"javascript",
"cli",
"jitar"
]
}
23 changes: 23 additions & 0 deletions packages/cli/src/CliManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

import { ArgumentManager } from './arguments';
import { CommandManager } from './commands';

export default class CliManager
{
#argumentManager: ArgumentManager;
#commandManager: CommandManager;

constructor()
{
this.#argumentManager = new ArgumentManager(process.argv);
this.#commandManager = new CommandManager();
}

manage(): Promise<void>
{
const command = this.#argumentManager.getCommand();
const args = this.#argumentManager.getArguments();

return this.#commandManager.execute(command, args);
}
}
33 changes: 33 additions & 0 deletions packages/cli/src/arguments/ArgumentManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

const COMMAND_INDEX = 2;

export default class ArgumentManager
{
#args: string[];

constructor(args: string[])
{
this.#args = args;
}

getCommand(): string
{
return this.#args[COMMAND_INDEX];
}

getArguments(): Map<string, string>
{
const args = this.#args.slice(COMMAND_INDEX + 1);

const map = new Map<string, string>();

args.forEach((arg) =>
{
const [key, value] = arg.split('=');

map.set(key.trim(), value.trim());
});

return map;
}
}
2 changes: 2 additions & 0 deletions packages/cli/src/arguments/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

export { default as ArgumentManager } from './ArgumentManager';
26 changes: 26 additions & 0 deletions packages/cli/src/commands/CommandManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

import Command from './interfaces/Command';

import BuildCache from './implementations/BuildCache';

export default class CommandManager
{
#commands: Map<string, Command> = new Map<string, Command>();

constructor()
{
this.#commands.set('build', new BuildCache());
}

execute(name: string, args: Map<string, string>): Promise<void>
{
const command = this.#commands.get(name);

if (command === undefined)
{
throw new Error(`Command ${name} not found`);
}

return command.execute(args);
}
}
17 changes: 17 additions & 0 deletions packages/cli/src/commands/implementations/BuildCache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

import { LocalFileManager } from '@jitar/server-nodejs';
import { CacheManager } from '@jitar/caching';

import Command from '../interfaces/Command';

export default class BuildCache implements Command
{
async execute(args: Map<string, string>): Promise<void>
{
const projectFileManager = new LocalFileManager('./');
const appFileManager = new LocalFileManager('./dist');

const cacheManager = new CacheManager(projectFileManager, appFileManager);
cacheManager.build();
}
}
2 changes: 2 additions & 0 deletions packages/cli/src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

export { default as CommandManager } from './CommandManager';
5 changes: 5 additions & 0 deletions packages/cli/src/commands/interfaces/Command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

export default interface Command
{
execute(args: Map<string, string>): Promise<void>;
}
2 changes: 2 additions & 0 deletions packages/cli/src/lib.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

export { default as CliManager } from './CliManager';
21 changes: 21 additions & 0 deletions packages/cli/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"compilerOptions": {
"target": "es2022",
"module": "es2022",
"rootDir": "./src/",
"moduleResolution": "node",
"declaration": true,
"outDir": "./dist",
"removeComments": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
},
"exclude": [
"vite.config.ts",
"node_modules",
"dist",
"test"
]
}
10 changes: 10 additions & 0 deletions packages/cli/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// vite.config.ts
import { defineConfig } from 'vitest/config';

export default defineConfig({
test: {
coverage: {
provider: 'v8'
},
},
});
5 changes: 4 additions & 1 deletion packages/jitar/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
"dist",
"!dist/types"
],
"bin": "./dist/cli.js",
"scripts": {
"lint": "eslint . --ext .ts",
"validate": "tsc -p tsconfig.json --noEmit",
"build": "npm run clean && rollup -c",
"build": "npm run clean && rollup -c && chmod +x dist/cli.js",
"clean": "rm -rf dist",
"prepublishOnly": "npm run clean && npm run build"
},
Expand All @@ -30,12 +31,14 @@
"express-http-proxy": "^2.0.0",
"fs-extra": "^11.2.0",
"glob-promise": "6.0.5",
"jitar": "file:",
"mime-types": "^2.1.35",
"tslog": "^4.9.2",
"yargs": "^17.7.2",
"zod": "^3.22.4"
},
"devDependencies": {
"@jitar/cli": "*",
"@jitar/runtime": "*",
"@jitar/server-nodejs": "*"
},
Expand Down
16 changes: 16 additions & 0 deletions packages/jitar/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@ export default [
typescript()
]
},
{
external: SERVER_EXTERNALS,
input: 'src/cli.ts',
output: {
file: 'dist/cli.js',
format: 'module',
plugins: [terser({
module: true,
mangle: false
})]
},
plugins: [
typescript(),
nodeResolve()
]
},
{
input: './dist/types/lib.d.ts',
output: [{ file: 'dist/lib.d.ts', format: 'module' }],
Expand Down
6 changes: 6 additions & 0 deletions packages/jitar/src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env node

import { CliManager } from '@jitar/cli';

const cliManager = new CliManager();
cliManager.manage();
52 changes: 19 additions & 33 deletions packages/plugin-vite/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,6 @@ function formatPath(path: string)
return path;
}

function createServerConfig(jitarUrl: string)
{
return {
build:
{
target: 'esnext'
},
server: {
proxy: {
'/rpc': jitarUrl
}
}
};
}

function createBootstrapCode(middlewares: string[]): string
{
const jitarImport = `import { startClient } from "jitar/client";`;
const middlewareImports = middlewares.map((middleware, index) => `import { default as $${index} } from "${middleware}";`).join('');

const importFunction = `const importFunction = (specifier) => import(specifier);`;
const middlewareArray = `const middlewares = [${middlewares.map((_, index) => `$${index}`).join(', ')}];`;

const startClient = `startClient(document.location.origin, importFunction, [], middlewares);`;

return `${jitarImport}\n${middlewareImports}\n${importFunction}\n${middlewareArray}\n${startClient}`;
}

export default function viteJitar(sourcePath: string, jitarPath: string, jitarUrl: string, segments: string[] = [], middlewares: string[] = []): PluginOption
{
sourcePath = formatPath(sourcePath);
Expand All @@ -66,7 +38,10 @@ export default function viteJitar(sourcePath: string, jitarPath: string, jitarUr

config()
{
return createServerConfig(jitarUrl);
return {
build: { target: 'esnext' },
server: { proxy: { '/rpc': jitarUrl }}
};
},

configResolved(resolvedConfig: ResolvedConfig)
Expand Down Expand Up @@ -113,7 +88,7 @@ export default function viteJitar(sourcePath: string, jitarPath: string, jitarUr
return null;
}

const cacheId = resolution.id.replace('/src/', '/.jitar/');
const cacheId = resolution.id.replace('/src/', '/dist/');

for (const scope of scopes)
{
Expand All @@ -131,9 +106,20 @@ export default function viteJitar(sourcePath: string, jitarPath: string, jitarUr

load(id)
{
return id === BOOTSTRAPPING_ID
? createBootstrapCode(middlewares)
: null;
if (id !== BOOTSTRAPPING_ID)
{
return null;
}

const jitarImport = `import { startClient } from "jitar/client";`;
const middlewareImports = middlewares.map((middleware, index) => `import { default as $${index} } from "${middleware}";`).join('');

const importFunction = `const importFunction = (specifier) => import(specifier);`;
const middlewareArray = `const middlewares = [${middlewares.map((_, index) => `$${index}`).join(', ')}];`;

const startClient = `startClient(document.location.origin, importFunction, [], middlewares);`;

return `${jitarImport}\n${middlewareImports}\n${importFunction}\n${middlewareArray}\n${startClient}`;
}

} as PluginOption;
Expand Down
Loading

0 comments on commit 26c05f8

Please sign in to comment.