模仿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命令行
關鍵代碼
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: input 17 }) 18 } 19 20 // -l 指令 21 const returnLinesNum = (fileName) => { 22 const rl = readLineHandle(fileName) 23 let lines = 0 24 // 逐行加一 25 rl.on(‘line‘, (line) => { 26 lines += 1 27 }) 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 : currentLineArr 41 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 : currentLineArr 56 words = [...words, ...currentLine] 57 }) 58 rl.on(‘close‘, () => { 59 // 逐行讀取結束時,對單詞棧的逐個單詞長度累加,得字符數 60 const wordsNum = words.reduce((acc, val) => { 61 return acc + val.length 62 }, 0) 63 console.log(`${fileName}文件的字母數為: ${wordsNum}`) 64 }) 65 } 66 67 exports = module.exports = { 68 returnLinesNum, 69 returnWordsNum, 70 returnLettersNum 71 }
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.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 |
模仿WC.exe的功能實現--node.js