用nodejs刪除檔案,資料夾(採用webpack打包時必用)

nt1979發表於2021-09-09
使用webpack打包,如果檔案內容有修改,那麼會出現些重複多餘的檔案。

遇到這樣的事,很多時候我們會採用gulp +webpack的方式處理,但是如果只是使用gulp的刪除檔案功能就採用gulp那真的是大炮打蚊子。前兩個有朋友問我怎麼做,我就把自己用node刪除檔案的方法共享給了他,稍微改造了下,覺得還行,比較好用,所以分享一下,有需要的朋友可以下載。

//del.js

/**
 * Created by lenovo on 2017/5/12.
 */
var fs = require('fs');

//獲取從命令列傳入的引數列表
function getParamList(val, config) {
    var valList = val.split('=');
    if (valList[0] === config) {
        return valList[1].split(',');
    } else {
        return [];
    }
}

//獲取從命令列傳入的引數列表(去除預設傳入的兩個)
var agrv = process.argv.slice(2);
if (agrv.length > 0) {
    agrv.forEach(function(val, index, array) {
        var list = getParamList(val, '--targets');
        console.log('list', list);
        list.forEach(function(ele, ind) {
            deleteTarget(ele || './dist');
        })
    });
} else {
    //如果從命令中沒有傳入引數,則直接預設刪除頂級目錄下的dist目錄。
    deleteTarget('./dist');
}

// 刪除目標資料夾或檔案
function deleteTarget(fileUrl) {
    // 如果當前url不存在,則退出
    if (!fs.existsSync(fileUrl)) return;
    // 當前檔案為資料夾時
    if (fs.statSync(fileUrl).isDirectory()) {
        var files = fs.readdirSync(fileUrl);
        var len = files.length,
            removeNumber = 0;
        if (len > 0) {
            files.forEach(function(file) {
                removeNumber ++;
                var stats = fs.statSync(fileUrl+'/'+file);
                var url = fileUrl + '/' + file;
                if (fs.statSync(url).isDirectory()) {
                    deleteTarget(url);
                } else {
                    fs.unlinkSync(url);
                }

            });
            if (removeNumber === len) {
                // 刪除當前資料夾下的所有檔案後,刪除當前空資料夾(注:所有的都採用同步刪除)
                fs.rmdirSync(fileUrl);
                console.log('刪除資料夾' + fileUrl + '成功');
            }
        } else {
            fs.rmdirSync(fileUrl)
        }
    } else {
        // 當前檔案為檔案時
        fs.unlinkSync(fileUrl);
        console.log('刪除檔案' + fileUrl + '成功');
    }
}

//package.json

{
  "name": "webpack-maoruibin2.0",
  "version": "1.0.1",
  "description": "",
  "author": {
    "name": "maoruibin",
    "email": "595123108@qq.com"
  },
  "main": "index.js",
  "scripts": {
    "test": "echo "Error: no test specified" && exit 1",
    "start": "webpack-dev-server --hot",
    **"webpack": "node server/delete.js && webpack",
    "webpack:p": "node server/delete.js --targets=./dist,test2 && webpack",**
    "del": "node server/delete.js",
    "build": "babel-node ./node_modules/webpack/bin/webpack"
  },
  "dependencies": {
    "vue": "^2.3.3"
  },
  "devDependencies": {
    "babel-core": "^6.24.1",
    "babel-loader": "^7.0.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-stage-0": "^6.24.1",
    "babel-preset-stage-1": "^6.24.1",
    "babel-preset-stage-2": "^6.24.1",
    "babel-preset-stage-3":"^6.24.1",
    "babel-plugin-transform-class-properties": "^6.24.1",
    "html-webpack-plugin": "^2.28.0",
    "webpack-dev-server": "^2.4.5",
    "webpack": "^2.5.1",
    "glob": "^7.1.1",
    "string-loader": "^0.0.1",
    "style-loader": "^0.17.0",
    "css-loader": "^0.28.1",
    "url-loader": "^0.5.8",
    "file-loader":"^0.11.1",
    "extract-text-webpack-plugin":"^2.1.0",
    "vue-loader":"^12.0.3",
    "vue-template-compiler":"^2.3.3",
    "webpack-chunk-hash": "^0.4.0",
    "inline-manifest-webpack-plugin":"^3.0.1"
  },
  "author": "",
  "license": "ISC"
}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/4822/viewspace-2798802/,如需轉載,請註明出處,否則將追究法律責任。

相關文章