UNPKG

cerevox

Version:

TypeScript SDK for browser automation and secure command execution in highly available and scalable micro computer environments

99 lines 4.12 kB
"use strict"; /** * 代码类型检测工具 * 用于检测 JavaScript/TypeScript/Python 代码类型 * 支持识别 ES 模块、CommonJS 模块和 Python 代码 */ Object.defineProperty(exports, "__esModule", { value: true }); exports.detectCodeType = detectCodeType; exports.isESModule = isESModule; exports.isCommonJS = isCommonJS; exports.isPython = isPython; /** * 检测代码类型(ES模块 vs CommonJS vs Python) * @param code - 要检测的代码字符串 * @returns 'esm' 表示ES模块,'cjs' 表示CommonJS,'py' 表示Python, 'sh' 表示Shell脚本 */ function detectCodeType(code) { // 检查是否包含 Shell 脚本特征 if (code.trim().startsWith('#!/bin/bash')) { return 'sh'; } // 移除注释和字符串字面量,避免误判 const cleanCode = code .replace(/\/\*[\s\S]*?\*\//g, '') // 移除块注释 .replace(/\/\/.*$/gm, '') // 移除行注释 .replace(/#.*$/gm, ''); // 移除Python行注释 // .replace(/['"``][^'"``]*['"``]/g, ''); // 移除字符串字面量 // 检查是否包含 ES 模块特征(优先检查,避免与Python import混淆) const hasJSImport = /\bimport\s+[\w{},\s*]+\s+from\s+['"]/.test(cleanCode); const hasJSExport = /\bexport\s+(default\s+|const\s+|function\s+|class\s+|\{)/.test(cleanCode); const hasImportMeta = /\bimport\.meta\b/.test(cleanCode); const hasTopLevelAwait = /^\s*.*\bawait\s+/m.test(cleanCode); // console.log(cleanCode, hasJSImport); // 检查是否包含 CommonJS 特征 const hasRequire = /\brequire\s*\(/.test(cleanCode); const hasModuleExports = /\bmodule\.exports\s*=/.test(cleanCode); const hasExportsAssignment = /\bexports\.[\w$]+\s*=/.test(cleanCode); // 检查是否包含 Python 特征 const hasPythonImport = /\bfrom\s+\w+\s+import\b|\bimport\s+\w+(?!\s+from)/.test(cleanCode); const hasPythonDef = /\bdef\s+\w+\s*\(/.test(cleanCode); const hasPythonClass = /\bclass\s+\w+\s*[(:]/.test(cleanCode); const hasPythonPrint = /\bprint\s*\(/.test(cleanCode); const hasPythonIfMain = /\bif\s+__name__\s*==\s*['"]__main__['"]/.test(cleanCode); const hasPythonIndentation = /^\s{4,}\S/m.test(cleanCode); // 检查4个或更多空格的缩进 const hasPythonKeywords = /\b(elif|except|finally|with|as|yield|lambda|pass|break|continue|global|nonlocal)\b/.test(cleanCode); // 如果包含 ES 模块特征(import/export/import.meta),识别为 ES 模块 if (hasJSImport || hasJSExport || hasImportMeta) { return 'esm'; } // 如果包含 CommonJS 特征,即使有顶级 await 也保持为 CommonJS // 这样可以避免 require() 在 ES 模块中的兼容性问题 if (hasRequire || hasModuleExports || hasExportsAssignment) { return 'cjs'; } // 如果包含明显的 Python 特征,识别为 Python if (hasPythonImport || hasPythonDef || hasPythonClass || hasPythonIfMain || hasPythonKeywords) { return 'py'; } // 只有在没有 CommonJS 特征时,才将顶级 await 识别为 ES 模块 if (hasTopLevelAwait) { return 'esm'; } // 如果只有 print() 和缩进,但没有 JavaScript 特征,也可能是 Python if ((hasPythonPrint || hasPythonIndentation) && !/\bfunction\s+\w+\s*\(|\bconst\s+\w+|\blet\s+\w+|\bvar\s+\w+/.test(cleanCode)) { return 'py'; } // 默认为 CommonJS(更兼容) return 'cjs'; } /** * 检查代码是否为 ES 模块 * @param code - 要检测的代码字符串 * @returns 是否为 ES 模块 */ function isESModule(code) { return detectCodeType(code) === 'esm'; } /** * 检查代码是否为 CommonJS 模块 * @param code - 要检测的代码字符串 * @returns 是否为 CommonJS 模块 */ function isCommonJS(code) { return detectCodeType(code) === 'cjs'; } /** * 检查代码是否为 Python * @param code - 要检测的代码字符串 * @returns 是否为 Python 代码 */ function isPython(code) { return detectCodeType(code) === 'py'; } //# sourceMappingURL=detect-code.js.map