Skip to content

Commit

Permalink
Merge branch 'main' into 85-improve-performance
Browse files Browse the repository at this point in the history
  • Loading branch information
SellersEvan authored Apr 30, 2024
2 parents 6696c7b + fd81967 commit 54dcb7c
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 14 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,20 @@ sherpa build [options]
- `-h`, `--help` display help for command


<br>


### Start Command
Start SherpaJS Server Locally. Ensure you have created a [local build](#build-command).
```bash
sherpa start [options]
```

#### Options:
- `-i`, `--input <path>` path to SherpaJS server, defaults to current directory
- `-p`, `--port <number>` port number (default: "3000")
- `-h`, `--help` display help for command


<br>

Expand Down
30 changes: 29 additions & 1 deletion src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@
*/


import fs from "fs";
import { spawn } from "child_process";
import { Command, Option } from "commander";
import { Compiler, BundlerType } from "../compiler/index.js";
import { getEnvironmentFiles, getAbsolutePath, getKeyValuePairs, getVersion } from "./utilities.js";
import { Logger } from "../compiler/utilities/logger/index.js";
import { Path } from "../compiler/utilities/path/index.js";
import { Level } from "../compiler/utilities/logger/model.js";
let CLI = new Command();


Expand All @@ -39,7 +43,7 @@ CLI.command("build")
let variables = getKeyValuePairs(options.variable);

if (Logger.hasError(variables.logs)) {
Logger.format(variables.logs);
Logger.display(variables.logs);
Logger.exit();
}

Expand Down Expand Up @@ -70,6 +74,30 @@ CLI.command("clean")
});


CLI.command("start")
.description("Start SherpaJS Server Locally")
.option("-i, --input <path>", "path to SherpaJS build directories, defaults to current directory")
.option("-p, --port <number>", "port number", (3000).toString())
.action((options) => {
let directory = getAbsolutePath(options.input, process.cwd());
let filepath = Path.join(directory, "/.sherpa/index.js");

if (!fs.existsSync(filepath)) {
Logger.display({
level: Level.ERROR,
text: "SherpaJS build directory not found",
file: { filepath }
});
Logger.exit();
}

let server = spawn("node", [filepath, options.port]);
server.stdout.on("data", (data) => console.log(data.toString().replace("\n", "")));
server.stderr.on("data", (data) => console.log(data.toString().replace("\n", "")));
server.on("close", (data) => console.log(data.toString().replace("\n", "")));
});


CLI.parse();


Expand Down
16 changes: 13 additions & 3 deletions src/compiler/structure/config-module/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,7 @@ function lintPackageJSON(entry:string):Message[] {


function lintPackageExports(filepath:string, entry:string, packageJSON:Record<string, unknown>):Message[] {
let exportObject = packageJSON.exports;
let exportFilepaths = typeof exportObject === "string" ? [exportObject] : Array.isArray(exportObject) ? exportObject : [];
for (let exportFilepath of exportFilepaths) {
for (let exportFilepath of getAllExported(packageJSON.exports as string|string[]|Record<string, unknown>)) {
let expectedFilepath = Path.resolveExtension(entry, "sherpa.module", SUPPORTED_FILE_EXTENSIONS);
if (expectedFilepath == Path.join(entry, exportFilepath)) {
return [];
Expand All @@ -143,6 +141,18 @@ function lintPackageExports(filepath:string, entry:string, packageJSON:Record<st
}


function getAllExported(exports:string|string[]|Record<string, unknown>):string[] {
if (typeof exports === "string") {
return [exports];
} else if (Array.isArray(exports)) {
return exports;
} else if (typeof exports === "object") {
return Object.keys(exports).map(o => getAllExported(exports[o] as string|string[]|Record<string, unknown>)).flat(3);
}
return [];
}


// Whoever believes and is baptized will be saved, but whoever does not believe
// will be condemned.
// - Mark 16:16
28 changes: 25 additions & 3 deletions src/compiler/structure/config-server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@
*/


import { FILENAME_CONFIG_SERVER, SUPPORTED_FILE_EXTENSIONS, ServerConfig, ServerConfigFile } from "../../models.js";
import {
FILENAME_CONFIG_MODULE, FILENAME_CONFIG_SERVER,
SUPPORTED_FILE_EXTENSIONS, ServerConfig, ServerConfigFile
} from "../../models.js";
import { Path } from "../../utilities/path/index.js";
import { Tooling } from "../../utilities/tooling/index.js";
import { Level, Message } from "../../utilities/logger/model.js";
import { getModuleConfig } from "../config-module/index.js";


export async function getServerConfig(entry:string):Promise<{ logs:Message[], server?:ServerConfigFile }> {
Expand All @@ -28,14 +32,15 @@ export async function getServerConfig(entry:string):Promise<{ logs:Message[], se
logs.push(...logsInstance);
if (!instance) return { logs };

let errorsTypes = await Tooling.typeValidation(filepath, "Server config");
logs.push(...await verifyModuleConfig(entry));
logs.push(...await Tooling.typeCheck(filepath, "Server config"));

return {
server: {
filepath: filepath,
instance: instance
},
logs: errorsTypes
logs
}
}

Expand Down Expand Up @@ -84,6 +89,23 @@ async function getInstance(filepath:string):Promise<{ logs:Message[], instance?:
}


async function verifyModuleConfig(entry:string):Promise<Message[]> {
if (hasModuleConfig(entry)) {
return (await getModuleConfig(entry, undefined, undefined)).logs;
}
return [];
}


function hasModuleConfig(entry:string):boolean {
return Path.resolveExtension(
entry,
FILENAME_CONFIG_MODULE,
SUPPORTED_FILE_EXTENSIONS
) != undefined;
}


// Therefore I tell you, whatever you ask for in prayer, believe that you have
// received it, and it will be yours.
// - Mark 11:24
7 changes: 0 additions & 7 deletions src/compiler/utilities/logger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,6 @@ import { Message, Level, MessageFile } from "./model.js";
export class Logger {


public static format(messages:Message[]) {
for (let message of messages) {
this.display(message);
}
}


public static display(message:Message[]|Message) {
if (Array.isArray(message)) {
message.forEach((m) => this.display(m));
Expand Down
18 changes: 18 additions & 0 deletions tests/servers/pass-primary/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "sherpa-core",
"author": "Sellers Industries",
"version": "0.0.0",
"description": "Module and Reusable Microservice Platform. Build and modularize custom API endpoints, inspired by NextJS APIs. Export to Vercel and ExpressJS.",
"type": "module",
"exports": "./sherpa.module.ts",
"scripts": {

},
"devDependencies": {

},
"license": "ISC",
"dependencies": {

}
}
8 changes: 8 additions & 0 deletions tests/servers/pass-primary/sherpa.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// @SherpaJS IgnoreInvalidSource
import { CreateModuleInterface } from "../../../src/compiler/models";
import { SherpaJS } from "../../../index";

export default SherpaJS.New.module({
name: "pass-primary-2",
interface: CreateModuleInterface<{ test: boolean }>
});

0 comments on commit 54dcb7c

Please sign in to comment.