extress.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435
  1. #!/usr/bin/env node
  2. "use strict";var l=Object.create;var c=Object.defineProperty;var p=Object.getOwnPropertyDescriptor;var u=Object.getOwnPropertyNames;var f=Object.getPrototypeOf,g=Object.prototype.hasOwnProperty;var h=(t,e,s,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of u(e))!g.call(t,i)&&i!==s&&c(t,i,{get:()=>e[i],enumerable:!(o=p(e,i))||o.enumerable});return t};var d=(t,e,s)=>(s=t!=null?l(f(t)):{},h(e||!t||!t.__esModule?c(s,"default",{value:t,enumerable:!0}):s,t));var r=d(require("fs")),n=d(require("path")),a=class t{static{this.Init="init"}static{this.args=process.argv.slice(2)}static{this.command=t.args[0]}static{this.cwd=process.cwd()}static{this.configPath=n.join(t.cwd,"config.json")}static{this.srcPath=n.join(t.cwd,"src")}static{this.middlewaresPath=n.join(t.srcPath,"middlewares")}static{this.routesPath=n.join(t.srcPath,"routes")}static exitWithError(e){return console.error(e),process.exit(1)}static loadConfig(){r.existsSync(t.configPath)||t.exitWithError("config.json not found in current directory");try{let e=r.readFileSync(t.configPath,"utf-8");return JSON.parse(e)}catch(e){let s=e instanceof Error?e.message:String(e);t.exitWithError(`Failed to read config.json: ${s}`)}}static getDefaultConfig(){return{port:3e3,middlewaresPath:"./src/middlewares",routesPath:"./src/routes",json:!1,urlencoded:!1,sessions:!1}}static init(){if(r.existsSync(t.configPath))return console.log("config.json already exists");let e=`import { Server as ExtressServer } from 'extress';
  3. class Server {
  4. public static async start(): Promise<ExtressServer> {
  5. return new ExtressServer().init().then((server: Server) => {
  6. return server.start(), server;
  7. });
  8. }
  9. }
  10. export default Server;`,s=`import Server from './Server';
  11. Server.start();`,o=`import { Middleware, NextFunction, Request, Response } from 'extress';
  12. class DefaultMiddleware extends Middleware {
  13. protected action = (req: Request, res: Response, next: NextFunction) => {
  14. next();
  15. };
  16. protected order: number = 0;
  17. protected route: string | null = null;
  18. }
  19. export default DefaultMiddleware;`,i=`import { Route, Request, Response, HttpMethod } from 'extress';
  20. class GetIndex extends Route {
  21. protected action = (req: Request, res: Response) => {
  22. res.status(200).send('Welcome to Extress!');
  23. };
  24. protected route: string | null = '/';
  25. protected method: HttpMethod = HttpMethod.GET;
  26. }
  27. export default GetIndex;`;r.mkdirSync(t.srcPath),r.mkdirSync(t.middlewaresPath),r.mkdirSync(t.routesPath),r.writeFileSync(n.join(t.middlewaresPath,"DefaultMiddleware.ts"),o),r.writeFileSync(n.join(t.routesPath,"GetIndex.ts"),i),r.writeFileSync(n.join(t.srcPath,"Server.ts"),e),r.writeFileSync(n.join(t.srcPath,"index.ts"),s),r.writeFileSync(t.configPath,JSON.stringify(t.getDefaultConfig(),null,2),"utf-8"),console.log("Project initialized successfully.")}static printUsage(){console.log(`Usage:
  28. extress init`)}static run(){return t.command===t.Init?t.init():t.printUsage()}};a.run();