模仿WC.exe的功能实现--node.js

时间:2023-03-08 18:13:37
模仿WC.exe的功能实现--node.js

Github项目地址:https://github.com/102derLinmenmin/myWc

WC 项目要求

wc.exe 是一个常见的工具,它能统计文本文件的字符数、单词数和行数。这个项目要求写一个命令行程序,模仿已有wc.exe 的功能,并加以扩充,给出某程序设计语言源文件的字符数、单词数和行数。

实现一个统计程序,它能正确统计程序文件中的字符数、单词数、行数,以及还具备其他扩展功能,并能够快速地处理多个文件。
具体功能要求:
程序处理用户需求的模式为:

wc.exe [parameter] [file_name]


遇到的困难及解决方法

  • 遇到的困难:

1.命令行自定义不了,抛出了与win10不兼容的错误

2.程序跑不了,发现node.js内置的npm在git中无法安装

  • 做的尝试:

1.在package.json中添加配置{"bin":{"mywc":"./index.js"}}自定义配置了mywc命令

2.通过clone在文件里装上npm发现还是无法使用,询问别人后用cmd敲入命令行

  • 是否解决

  • 收获

1.有一些与win10不兼容的错误可以使用添加配置解决

2.很多软件使用时安装成功但是打开失败可以使用cmd命令行

关键代码

 // handler.js 基本操作指令

 const fs = require("fs")
const readline = require('readline')
const path = require('path') /*
* 逐行读取文件,待下一步操作
* input: fileName:文件相对路径
* return:rl:逐行读取的文件内容
*/
const readLineHandle = (fileName) => {
let filepath = path.join(__dirname, fileName)
let input = fs.createReadStream(filepath)
return readline.createInterface({
input: input
})
} // -l 指令
const returnLinesNum = (fileName) => {
const rl = readLineHandle(fileName)
let lines = 0
// 逐行加一
rl.on('line', (line) => {
lines += 1
})
rl.on('close', () => {
console.log(`${fileName}文件的行数为: ${lines}`)
})
} // -s 指令
const returnWordsNum = (fileName) => {
const rl = readLineHandle(fileName)
let words = []
// 对逐行的内容操作,以空格为分界计算单词数,压入单词栈
rl.on('line', (line) => {
const currentLineArr = line.trim().split(' ')
const currentLine = currentLineArr.length === 0 ? line : currentLineArr
words = [...words, ...currentLine]
})
rl.on('close', () => {
console.log(`${fileName}文件的单词数为: ${words.length}`)
})
} // -c 指令
const returnLettersNum = (fileName) => {
const rl = readLineHandle(fileName)
let words = []
// 对逐行的内容操作,以空格为分界计算单词数,压入单词栈
rl.on('line', (line) => {
const currentLineArr = line.trim().split(' ')
const currentLine = currentLineArr.length === 0 ? line : currentLineArr
words = [...words, ...currentLine]
})
rl.on('close', () => {
// 逐行读取结束时,对单词栈的逐个单词长度累加,得字符数
const wordsNum = words.reduce((acc, val) => {
return acc + val.length
}, 0)
console.log(`${fileName}文件的字母数为: ${wordsNum}`)
})
} exports = module.exports = {
returnLinesNum,
returnWordsNum,
returnLettersNum
}
const fs = require("fs")
const path = require('path')
const commonHandle = require('./constant') module.exports = (filePath, commands) => {
try {
commands.forEach((command) => {
//根据文件路径读取文件,返回文件列表
fs.readdir(filePath, (err, files) => {
if (err) {
console.log('如果使用 -s 指令请选择一个文件夹')
console.log('正则表达式不需要使用 -s 操作')
throw new Error('unexpected command')
} else {
//遍历读取到的文件列表
files.forEach((filename) => {
//获取当前文件的绝对路径
const filedir = path.join(filePath, filename)
//根据文件路径获取文件信息,返回一个fs.Stats对象
fs.stat(filedir, (error, stats) => {
if (error) {
console.warn('获取文件stats失败')
throw new Error('unexpected command')
} else {
const isFile = stats.isFile() //是文件
const isDir = stats.isDirectory() //是文件夹
if (isFile) {
commonHandle[command].call(this, filedir, command)
}
if (isDir) {
fileDisplay(filedir) //递归,如果是文件夹,就继续遍历该文件夹下面的文件
}
}
})
})
}
})
})
} catch (e) {
console.log(e.message)
}
}

  解题思路:使用node.js中的fs和process.argv (fs是读取文件操作指令集  process.argv是获取命令行指令操作)

模仿WC.exe的功能实现--node.js

代码运行测试

在 git bash 中运行以下命令,file 可以相应替换成测试文件

bash mywc -c ./test/*.txt //返回文件的字符数

mywc -w ./test/*.txt //返回文件的词数

mywc -l ./test/*.txt //返回文件的行数

mywc -s -l -w -c test 递归文件夹test里的所有文件 

mywc -l -w -c ./test/*.txt 返回文件里的通配符

 

模仿WC.exe的功能实现--node.js

模仿WC.exe的功能实现--node.js

模仿WC.exe的功能实现--node.js

总结:

第一次运用JavaScript写课设并不是非常熟练,只写了基础的功能和拓展-s ,也是第一次写博客,尝试就会有收获,还是需要不断学习。

PSP

PSP2.1 Personal Software Process Stages 预估耗时(分钟) 实际耗时(分钟)
Planning 计划  30  30
· Estimate · 估计这个任务需要多少时间  870  630
Development 开发  810  600
· Analysis · 需求分析 (包括学习新技术)  90  30
· Design Spec · 生成设计文档  30  30
· Design Review · 设计复审 (和同事审核设计文档)  30  30
· Coding Standard · 代码规范 (为目前的开发制定合适的规范)  30  30
· Design · 具体设计  120  60
· Coding · 具体编码  240  180
· Code Review · 代码复审  30  60
· Test · 测试(自我测试,修改代码,提交修改)  60  30
Reporting 报告  60 60
· Test Report · 测试报告  60  30
· Size Measurement · 计算工作量  30  30
· Postmortem & Process Improvement Plan · 事后总结, 并提出过程改进计划  30  30
  合计  870  630