mp-lens
Version:
微信小程序分析工具 (Unused Code, Dependencies, Visualization)
130 lines • 5.83 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.findAppJsonConfig = findAppJsonConfig;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const debug_logger_1 = require("./debug-logger");
// Basic validation for app.json content
function isValidAppJson(content) {
return typeof content === 'object' && content !== null && Array.isArray(content.pages);
}
// Common directories to exclude during search
const DEFAULT_EXCLUDE_DIRS = [
'node_modules',
'.git',
'dist',
'build',
'out',
'coverage',
'.vscode',
'.idea',
'miniprogram_npm', // Often contains copies
];
/**
* Searches for a valid app.json within a project directory to automatically determine
* miniappRoot and appJsonPath.
*
* @param projectRoot The absolute path to the project's root directory.
* @param excludeDirs Optional array of directory names to exclude from search.
* @returns An object with absolute paths for appJsonPath and miniappRoot, 'ambiguous' if multiple found, or null if none found.
*/
function findAppJsonConfig(projectRoot, excludeDirs = DEFAULT_EXCLUDE_DIRS) {
debug_logger_1.logger.debug('Attempting to auto-detect app.json...');
const foundAppJsons = [];
const visitedDirs = new Set(); // Avoid infinite loops with symlinks if any
function searchDir(currentDir) {
if (visitedDirs.has(currentDir)) {
return;
}
visitedDirs.add(currentDir);
try {
const entries = fs.readdirSync(currentDir, { withFileTypes: true });
for (const entry of entries) {
const fullPath = path.resolve(currentDir, entry.name);
const relativePath = path.relative(projectRoot, fullPath); // For logging/debugging
if (entry.isDirectory()) {
// Check if directory should be excluded
if (excludeDirs.includes(entry.name)) {
debug_logger_1.logger.trace(`Skipping excluded directory: ${relativePath}`);
continue;
}
// Recurse into subdirectory
searchDir(fullPath);
}
else if (entry.isFile() && entry.name === 'app.json') {
debug_logger_1.logger.trace(`Found potential app.json: ${relativePath}`);
try {
const contentStr = fs.readFileSync(fullPath, 'utf-8');
const content = JSON.parse(contentStr);
if (isValidAppJson(content)) {
debug_logger_1.logger.debug(`Found valid app.json at: ${relativePath}`);
foundAppJsons.push({
appJsonPath: fullPath, // Absolute path
miniappRoot: path.dirname(fullPath), // Absolute path
});
}
else {
debug_logger_1.logger.trace(`Skipping invalid app.json (missing 'pages' array?): ${relativePath}`);
}
}
catch (error) {
debug_logger_1.logger.trace(`Error reading/parsing app.json at ${relativePath}: ${error.message}`);
}
}
}
}
catch (error) {
debug_logger_1.logger.warn(`读取目录 ${currentDir} 出错: ${error.message}`);
}
}
// Start search from project root
searchDir(projectRoot);
if (foundAppJsons.length === 1) {
debug_logger_1.logger.info(`自动检测到入口文件: ${path.relative(projectRoot, foundAppJsons[0].appJsonPath)}`);
debug_logger_1.logger.info(`自动检测到小程序根目录: ${path.relative(projectRoot, foundAppJsons[0].miniappRoot)}`);
return foundAppJsons[0];
}
else if (foundAppJsons.length > 1) {
debug_logger_1.logger.warn(`发现多个有效的 app.json 文件。无法自动检测配置。请指定 --miniapp-root 和 --entry-file。发现的位置:`);
foundAppJsons.forEach((f) => debug_logger_1.logger.warn(` - ${path.relative(projectRoot, f.appJsonPath)}`));
return 'ambiguous';
}
else {
debug_logger_1.logger.debug('No valid app.json found for auto-detection.');
return null;
}
}
//# sourceMappingURL=fs-finder.js.map