extress.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #!/usr/bin/env node
  2. import * as fs from 'fs';
  3. import * as path from 'path';
  4. import { ServerProperties } from '../lib';
  5. class Extress {
  6. private static Init: string = 'init';
  7. private static args: string[] = process.argv.slice(2);
  8. private static command: string | undefined = Extress.args[0];
  9. private static cwd: string = process.cwd();
  10. private static configPath: string = path.join(Extress.cwd, 'config.json');
  11. private static srcPath: string = path.join(Extress.cwd, 'src');
  12. private static middlewaresPath: string = path.join(Extress.srcPath, 'middlewares');
  13. private static routesPath: string = path.join(Extress.srcPath, 'routes');
  14. private static exitWithError(message: string): never {
  15. return console.error(message), process.exit(1);
  16. }
  17. private static loadConfig(): ServerProperties {
  18. if(!fs.existsSync(Extress.configPath))
  19. Extress.exitWithError('config.json not found in current directory');
  20. try {
  21. const content: string = fs.readFileSync(Extress.configPath, 'utf-8');
  22. return JSON.parse(content) as ServerProperties;
  23. } catch (error) {
  24. const message = error instanceof Error ? error.message : String(error);
  25. Extress.exitWithError(`Failed to read config.json: ${message}`);
  26. }
  27. }
  28. private static getDefaultConfig(): ServerProperties {
  29. return {
  30. port: 3000,
  31. middlewaresPath: './src/middlewares',
  32. routesPath: './src/routes',
  33. json: false,
  34. urlencoded: false,
  35. sessions: false
  36. };
  37. }
  38. private static init(): void {
  39. if(fs.existsSync(Extress.configPath))
  40. return console.log('config.json already exists');
  41. const serverCode =
  42. 'import { Server as ExtressServer } from \'extress\';\n\n' +
  43. 'class Server {\n' +
  44. '\tpublic static async start(): Promise<ExtressServer> {\n' +
  45. '\t\treturn new ExtressServer().init().then((server: Server) => {\n' +
  46. '\t\t\treturn server.start(), server;\n' +
  47. '\t\t});\n' +
  48. '\t}\n' +
  49. '}\n\n' +
  50. 'export default Server;';
  51. const indexCode =
  52. 'import Server from \'./Server\';\n\n' +
  53. 'Server.start();';
  54. const exampleMiddleware =
  55. 'import { Middleware, NextFunction, Request, Response } from \'extress\';\n\n' +
  56. 'class DefaultMiddleware extends Middleware {\n' +
  57. '\tprotected action = (req: Request, res: Response, next: NextFunction) => {\n' +
  58. '\t\tnext();\n' +
  59. '\t};\n' +
  60. '\tprotected order: number = 0;\n' +
  61. '\tprotected route: string | null = null;\n' +
  62. '}\n\n' +
  63. 'export default DefaultMiddleware;';
  64. const exampleRoute =
  65. 'import { Route, Request, Response, HttpMethod } from \'extress\';\n\n' +
  66. 'class GetIndex extends Route {\n' +
  67. '\tprotected action = (req: Request, res: Response) => {\n' +
  68. '\t\tres.status(200).send(\'Welcome to Extress!\');\n' +
  69. '\t};\n' +
  70. '\tprotected route: string | null = \'/\';\n' +
  71. '\tprotected method: HttpMethod = HttpMethod.GET;\n' +
  72. '}\n\n' +
  73. 'export default GetIndex;';
  74. fs.mkdirSync(Extress.srcPath);
  75. fs.mkdirSync(Extress.middlewaresPath);
  76. fs.mkdirSync(Extress.routesPath);
  77. fs.writeFileSync(path.join(Extress.middlewaresPath, 'DefaultMiddleware.ts'), exampleMiddleware);
  78. fs.writeFileSync(path.join(Extress.routesPath, 'GetIndex.ts'), exampleRoute);
  79. fs.writeFileSync(path.join(Extress.srcPath, 'Server.ts'), serverCode);
  80. fs.writeFileSync(path.join(Extress.srcPath, 'index.ts'), indexCode);
  81. fs.writeFileSync(Extress.configPath, JSON.stringify(Extress.getDefaultConfig(), null, 2), 'utf-8');
  82. console.log('Project initialized successfully.');
  83. }
  84. private static printUsage(): void {
  85. console.log('Usage:\nextress init');
  86. }
  87. public static run(): void {
  88. switch (Extress.command) {
  89. case Extress.Init: return Extress.init();
  90. default: return Extress.printUsage();
  91. }
  92. }
  93. }
  94. Extress.run();