diff --git a/packages/sgmoudle-tools/CHANGELOG.md b/packages/sgmoudle-tools/CHANGELOG.md deleted file mode 100644 index 2d407c7..0000000 --- a/packages/sgmoudle-tools/CHANGELOG.md +++ /dev/null @@ -1,48 +0,0 @@ -# @iringo/sgmoudle-tools - -## 2.0.0 - -### Major Changes - -- d2754aa: first version - -## 1.2.2 - -### Patch Changes - -- Updated dependencies [0ef69e3] - - @iringo/utils@1.0.1 - -## 1.2.1 - -### Patch Changes - -- 793e54e: 修复新增 script 路由不生效的问题 - -## 1.2.0 - -### Minor Changes - -- 78c8e7c: 新增监听配置文件变动功能 - -### Patch Changes - -- c33466c: 修复模版错误 - -## 1.1.1 - -### Patch Changes - -- 74081f0: bump version - -## 1.1.0 - -### Minor Changes - -- 112f686: 重构实现 - -## 1.0.0 - -### Major Changes - -- b5d0a59: 初始化 diff --git a/packages/sgmoudle-tools/bin/sgmoudle.js b/packages/sgmoudle-tools/bin/sgmoudle.js deleted file mode 100644 index 7b620ed..0000000 --- a/packages/sgmoudle-tools/bin/sgmoudle.js +++ /dev/null @@ -1,4 +0,0 @@ -#!/usr/bin/env node -const { initCommand } = require('../dist/index.js'); - -initCommand(); diff --git a/packages/sgmoudle-tools/modern.config.ts b/packages/sgmoudle-tools/modern.config.ts deleted file mode 100644 index 4be36d6..0000000 --- a/packages/sgmoudle-tools/modern.config.ts +++ /dev/null @@ -1,20 +0,0 @@ -import fs from 'node:fs'; -import { type PartialBaseBuildConfig, defineConfig, moduleTools } from '@modern-js/module-tools'; - -const shared: PartialBaseBuildConfig = {}; - -export default defineConfig({ - plugins: [moduleTools()], - buildConfig: [ - { - buildType: 'bundleless', - format: 'cjs', - target: 'es2019', - outDir: './dist', - externalHelpers: true, - define: { - 'process.env.MODULE_TEMP': fs.readFileSync('./sgmodule.handlebars', 'utf-8'), - }, - }, - ], -}); diff --git a/packages/sgmoudle-tools/package.json b/packages/sgmoudle-tools/package.json deleted file mode 100644 index 94841fb..0000000 --- a/packages/sgmoudle-tools/package.json +++ /dev/null @@ -1,35 +0,0 @@ -{ - "name": "@iringo/sgmoudle-tools", - "version": "1.2.2", - "main": "dist/index.js", - "bin": { - "sgmoudle": "./bin/sgmoudle.js" - }, - "scripts": { - "dev": "modern build -w", - "build": "modern build" - }, - "dependencies": { - "@iringo/utils": "workspace:^", - "@swc/helpers": "^0.5.13", - "express": "^4.21.1", - "handlebars": "^4.7.8", - "lodash": "^4.17.21", - "qrcode-terminal": "^0.12.0", - "rslog": "^1.2.3" - }, - "devDependencies": { - "@modern-js/module-tools": "^2.60.2", - "@types/express": "^5.0.0", - "@types/lodash": "^4.17.10", - "@types/node": "^20.0.0", - "@types/qrcode-terminal": "^0.12.2", - "typescript": "^5.6.2" - }, - "files": ["dist", "bin", "CHANGELOG.md"], - "repository": { - "type": "git", - "url": "https://github.com/NSRingo/engineering-solutions", - "directory": "packages/sgmoudle-tools" - } -} diff --git a/packages/sgmoudle-tools/sgmodule.handlebars b/packages/sgmoudle-tools/sgmodule.handlebars deleted file mode 100644 index 3ec85ab..0000000 --- a/packages/sgmoudle-tools/sgmodule.handlebars +++ /dev/null @@ -1,68 +0,0 @@ -#!name = {{name}} -#!desc = 重新编译后更新模块即可生效 - -{{#if module.general}} -[General] -{{#each module.general}} -{{@key}} = {{this}} -{{/each}} -{{/if}} - -{{#if module.host}} -[Host] -{{#each module.host}} -{{@key}} = {{this}} -{{/each}} -{{/if}} - -{{#if module.rule}} -[Rule] -{{#each module.rule}} -{{this}} -{{/each}} -{{/if}} - -{{#if module.urlRewrite}} -[URL Rewrite] -{{#each module.urlRewrite}} -{{this}} -{{/each}} -{{/if}} - -{{#if module.headerRewrite}} -[Header Rewrite] -{{#each module.headerRewrite}} -{{this}} -{{/each}} -{{/if}} - -{{#if module.bodyRewrite}} -[Body Rewrite] -{{#each module.bodyRewrite}} -{{this}} -{{/each}} -{{/if}} - -{{#if module.mapLocal}} -[Map Local] -{{#each module.mapLocal}} -{{this}} -{{/each}} -{{/if}} - -{{#if module.script}} -[Script] -{{#each module.script}} -{{this.name}} = type={{this.type}}{{#if this.pattern}}, pattern={{this.pattern}}{{/if}}, requires-body={{#if this.requiresBody}}true{{else}}false{{/if}}, binary-body-mode={{#if this.binaryBodyMode}}true{{else}}false{{/if}}, script-path={{this.scriptPath}}, debug=true{{#if this.argument}}, argument={{{this.argument}}}{{/if}} -{{/each}} -{{/if}} - - - -{{#if module.mitm}} -[Mitm] -{{#if module.mitm.hostname}} -hostname = %APPEND% {{module.mitm.hostname}} -{{/if}} -{{/if}} - diff --git a/packages/sgmoudle-tools/src/config.ts b/packages/sgmoudle-tools/src/config.ts deleted file mode 100644 index add169d..0000000 --- a/packages/sgmoudle-tools/src/config.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { loadConfigFile } from '@iringo/utils'; -import type { SgModuleToolsOptions } from './types'; - -export const loadConfig = async ({ - cwd = process.cwd(), - path: configPath, -}: { - cwd?: string; - path?: string; -} = {}) => { - return loadConfigFile({ - configPath, - baseConfigName: 'sgmodule-tools.config', - cwd, - }); -}; diff --git a/packages/sgmoudle-tools/src/index.ts b/packages/sgmoudle-tools/src/index.ts deleted file mode 100644 index 9e2244c..0000000 --- a/packages/sgmoudle-tools/src/index.ts +++ /dev/null @@ -1,31 +0,0 @@ -import { commander } from '@iringo/utils'; -import { loadConfig } from './config'; -import { startServerFactory } from './server'; -import type { SgModuleToolsOptions } from './types'; - -export function initCommand() { - const { program } = commander; - program - .command('dev') - .description('Start a development sgmodule') - .option('-p, --port ', 'specify the port') - .option('-c, --config ', 'specify the configuration file') - .action(async (option) => { - const { config, configFilePath } = (await loadConfig({ path: option.config })) ?? {}; - if (!config || !configFilePath) { - return; - } - if (option.port) { - config.port = option.port; - } - config.port ??= 0; - const startServer = startServerFactory(config, configFilePath); - startServer(); - }); - - program.parse(process.argv); -} - -export function defineConfig(config: SgModuleToolsOptions) { - return config; -} diff --git a/packages/sgmoudle-tools/src/server.ts b/packages/sgmoudle-tools/src/server.ts deleted file mode 100644 index 261adc5..0000000 --- a/packages/sgmoudle-tools/src/server.ts +++ /dev/null @@ -1,71 +0,0 @@ -import fs from 'node:fs'; -import type { AddressInfo } from 'node:net'; -import path from 'node:path'; -import { address } from '@iringo/utils'; -import express from 'express'; -import { cloneDeep } from 'lodash'; -import qrcode from 'qrcode-terminal'; -import { logger } from 'rslog'; -import { loadConfig } from './config'; -import type { SgModuleToolsOptions } from './types'; -import { generateSgModule } from './utils'; - -export function startServerFactory(config: SgModuleToolsOptions, configFilePath: string) { - const configFileDir = path.dirname(configFilePath); - const packageJson = JSON.parse(fs.readFileSync(path.resolve(configFileDir, 'package.json'), 'utf-8')); - const name = packageJson.name ?? 'sgmodule'; - const modulePath = `/${name}.sgmodule`; - let module = cloneDeep(config.module); - - fs.watch(configFilePath, async (eventType) => { - if (eventType === 'change') { - logger.start('配置文件发生变化,重新加载配置...'); - const newConfig = await loadConfig({ path: configFilePath }); - if (newConfig) { - config.module = newConfig.config.module; - module = cloneDeep(config.module); - logger.ready('重新加载配置成功'); - } - } - }); - - return (differentPort = true) => { - const app = express(); - - config.module?.script?.forEach((script, index) => { - const scriptPath = path.resolve(configFileDir, script.scriptPath); - const routePath = `/${name}/${path.relative(configFileDir, scriptPath)}`; - app.get(routePath, (req, res) => { - res.sendFile(scriptPath); - }); - if (module?.script) { - module.script[index].scriptPath = routePath; - } - }); - - app.get(modulePath, (req, res) => { - const protocol = req.protocol; - const host = req.get('host'); - module?.script?.forEach((script) => { - script.scriptPath = `${protocol}://${host}${script.scriptPath}?${Date.now()}`; - }); - res.send( - generateSgModule({ - name, - module, - }), - ); - }); - - const server = app.listen(config.port, () => { - if (differentPort) { - config.port = (server.address() as AddressInfo)?.port; - const moduleRemoteUrl = `http://${address.ip()}:${config!.port}${modulePath}`; - qrcode.generate(`surge:///install-module?url=${encodeURIComponent(moduleRemoteUrl)}`, { small: true }); - logger.ready('扫码安装模块,或手动导入:', moduleRemoteUrl); - } - }); - - return server; - }; -} diff --git a/packages/sgmoudle-tools/src/types.ts b/packages/sgmoudle-tools/src/types.ts deleted file mode 100644 index 48cb554..0000000 --- a/packages/sgmoudle-tools/src/types.ts +++ /dev/null @@ -1,28 +0,0 @@ -export interface SurgeScript { - name: string; - type: 'http-request' | 'http-response' | 'cron' | 'event' | 'dns' | 'rule' | 'generic'; - scriptPath: string; - // timeout?: number; - argument?: string; - engine?: 'auto' | 'jsc' | 'webview'; - pattern?: string; - requiresBody?: boolean; - binaryBodyMode?: boolean; -} - -export interface SgModuleToolsOptions { - port?: number; - module?: { - general?: Record; - host?: Record; - rule?: string[]; - script?: SurgeScript[]; - mitm?: { - hostname?: string[]; - }; - urlRewrite?: string[]; - headerRewrite?: string[]; - bodyRewrite?: string[]; - mapLocal?: string[]; - }; -} diff --git a/packages/sgmoudle-tools/src/utils.ts b/packages/sgmoudle-tools/src/utils.ts deleted file mode 100644 index 9f5639b..0000000 --- a/packages/sgmoudle-tools/src/utils.ts +++ /dev/null @@ -1,10 +0,0 @@ -import handlebars from 'handlebars'; -import type { SgModuleToolsOptions } from './types'; - -export const generateSgModule = (options: { - name: string; - module: SgModuleToolsOptions['module']; -}) => { - const template = handlebars.compile(process.env.MODULE_TEMP); - return template(options); -}; diff --git a/packages/sgmoudle-tools/tsconfig.json b/packages/sgmoudle-tools/tsconfig.json deleted file mode 100644 index 44d04fa..0000000 --- a/packages/sgmoudle-tools/tsconfig.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "compilerOptions": { - "lib": ["ESNext"], - "strict": true, - "skipLibCheck": true, - "baseUrl": ".", - "module": "ESNext", - "moduleResolution": "Node", - "allowSyntheticDefaultImports": true - }, - "include": ["src/**/*"] -}