分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

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

发布时间:2023-09-06 02:14责任编辑:傅花花关键词: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命令行

关键代码

 1 // handler.js 基本操作指令 2 ?3 const fs = require("fs") 4 const readline = require(‘readline‘) 5 const path = require(‘path‘) 6 ?7 /* 8 ?* 逐行读取文件,待下一步操作 9 ?* input: fileName:文件相对路径10 ?* return:rl:逐行读取的文件内容11 ?*/12 const readLineHandle = (fileName) => {13 ??let filepath = path.join(__dirname, fileName)14 ??let input = fs.createReadStream(filepath)15 ??return readline.createInterface({16 ????input: input17 ??})18 }19 20 // -l 指令 21 const returnLinesNum = (fileName) => {22 ??const rl = readLineHandle(fileName)23 ??let lines = 024 ??// 逐行加一25 ??rl.on(‘line‘, (line) => {26 ????lines += 127 ??})28 ??rl.on(‘close‘, () => {29 ????console.log(`${fileName}文件的行数为: ${lines}`)30 ??})31 }32 33 // -s 指令34 const returnWordsNum = (fileName) => {35 ??const rl = readLineHandle(fileName)36 ??let words = []37 ??// 对逐行的内容操作,以空格为分界计算单词数,压入单词栈38 ??rl.on(‘line‘, (line) => {39 ????const currentLineArr = line.trim().split(‘ ‘)40 ????const currentLine = currentLineArr.length === 0 ? line : currentLineArr41 ????words = [...words, ...currentLine]42 ??})43 ??rl.on(‘close‘, () => {44 ????console.log(`${fileName}文件的单词数为: ${words.length}`)45 ??})46 }47 48 // -c 指令49 const returnLettersNum = (fileName) => {50 ??const rl = readLineHandle(fileName)51 ??let words = []52 ??// 对逐行的内容操作,以空格为分界计算单词数,压入单词栈53 ??rl.on(‘line‘, (line) => {54 ????const currentLineArr = line.trim().split(‘ ‘)55 ????const currentLine = currentLineArr.length === 0 ? line : currentLineArr56 ????words = [...words, ...currentLine]57 ??})58 ??rl.on(‘close‘, () => {59 ????// 逐行读取结束时,对单词栈的逐个单词长度累加,得字符数60 ????const wordsNum = words.reduce((acc, val) => {61 ??????return acc + val.length62 ????}, 0)63 ????console.log(`${fileName}文件的字母数为: ${wordsNum}`)64 ??})65 }66 67 exports = module.exports = {68 ??returnLinesNum,69 ??returnWordsNum,70 ??returnLettersNum71 }
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是获取命令行指令操作)

代码运行测试

在 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 返回文件里的通配符

 

总结:

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

PSP

PSP2.1Personal 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报告 6060
· Test Report· 测试报告 60 30
· Size Measurement· 计算工作量 30 30
· Postmortem & Process Improvement Plan· 事后总结, 并提出过程改进计划 30 30
 合计 870 630

 

 

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

原文地址:https://www.cnblogs.com/102menmin/p/9648746.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved