You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
constfs=require('fs');constpath=require('path');functioncountLinesInDirectory(dirPath,fileExtension){lettotalLines=0;functioncountLinesInFile(filePath){constcontent=fs.readFileSync(filePath,'utf-8');constlines=content.split('\n');totalLines+=lines.length;}functionprocessDirectory(directoryPath){constfiles=fs.readdirSync(directoryPath);files.forEach((file)=>{constfilePath=path.join(directoryPath,file);conststats=fs.statSync(filePath);if(stats.isFile()&&path.extname(file)===fileExtension){countLinesInFile(filePath);}elseif(stats.isDirectory()){processDirectory(filePath);}});}processDirectory(dirPath);returntotalLines;}// 命令行参数,第一个参数是目录路径,第二个参数是文件扩展名const[_,__,dirPath,fileExtension]=process.argv;constlinesCount=countLinesInDirectory(dirPath,fileExtension);console.log(`Total lines of ${fileExtension} files in ${dirPath}: ${linesCount}`);
关键词:统计指定目录下代码行数
要实现一个命令行工具来统计输入目录下指定代码的行数,你可以使用Node.js的
fs
模块来读取文件内容并进行行数统计。以下是一个简单的实现示例:你可以将上述代码保存为一个JavaScript文件,比如
line-counter.js
。然后,在终端中运行以下命令:其中
/path/to/directory
是你要统计的目录路径,.js
是你要统计的文件扩展名。运行命令后,程序将会输出指定文件类型在指定目录中的总行数。你可以根据需要自定义输出格式、文件过滤规则等。此示例只是一个基本的实现,你可以根据具体需求进行扩展和优化。
The text was updated successfully, but these errors were encountered: