Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clean (delete contents) folder with npm [closed]

Tags:

npm

deployment

I am trying to deploy a web project but before build it, I need to clear/delete the dist folder.

I could add a script that would run rm -rf dist/* but that would not work on Windows system.

Is there some npm package u other solution that allows deleting a folder with a command that works in every OS?

like image 743
Pablo Avatar asked Nov 18 '16 17:11

Pablo


People also ask

What does npm run clean do?

The npm clean-install command (or npm ci for short) is an in-place replacement for npm install with two major differences: It does a clean install: if the node_modules folder exists, npm deletes it and installs a fresh one. It checks for consistency: if package-lock.

How do I empty a directory in node JS?

To remove all files from a directory, first you need to list all files in the directory using fs. readdir , then you can use fs. unlink to remove each file.

How do you delete all files in node JS?

If you want to delete a folder and all files and subfolders inside it, you can use fsPromises. rmdir() with the recursive: true option, like this: import fsPromises from "fs/promises"; /* Other code go here */ fsPromises. rmdir(dirPath, { recursive: true });


2 Answers

You can use rimraf: https://github.com/isaacs/rimraf.

Note that if you are using globs containing the globstar (**), you must double-quote them. Unix systems don't all support the globstar by default, but rimraf will expand them for you. Windows doesn't support single-quotes, so those can't be used. Remember that double-quotes must be escaped in JSON with a \.

like image 152
RyanZim Avatar answered Oct 13 '22 22:10

RyanZim


Recently I was faced with the same challenge as you, and just like user Kevin Brown stated in a comment above, I was interested in a solution which wasn't just "use this npm package". So I'm throwing this out here hoping someone will find it useful.

So, what I did was taking some code I found in StackOverflow, putting it on a .js file, and binding it to an npm script in my package.json. In my case, the goal was to delete the "/out" folder, where .ts scripts were compiling to, but the possibilities are endless!

With this solution, you only need to start your app with "npm run cleanstart".

package.json

{     "name": "my_app",     "version": "1.0.0",     "main": "main.js",     "scripts": {         "tsc": "tsc",         "precleanstart": "node clean.js",         "cleanstart": "npm start",         "prestart": "npm run tsc",         "start": "node out/main.js"     },     "dependencies": {         "@babel/runtime": "^7.0.0-beta.54",         "babel-runtime": "^6.26.0",         "body-parser": "^1.18.3",         "express": "^4.16.3",         "typescript": "^3.0.3"     } } 

clean.js

var fs = require('fs');  function deleteFolderRecursive(path) {   if (fs.existsSync(path) && fs.lstatSync(path).isDirectory()) {     fs.readdirSync(path).forEach(function(file, index){       var curPath = path + "/" + file;        if (fs.lstatSync(curPath).isDirectory()) { // recurse         deleteFolderRecursive(curPath);       } else { // delete file         fs.unlinkSync(curPath);       }     });      console.log(`Deleting directory "${path}"...`);     fs.rmdirSync(path);   } };  console.log("Cleaning working tree...");  deleteFolderRecursive("./out");  console.log("Successfully cleaned working tree!"); 
like image 26
Tharkius Avatar answered Oct 13 '22 22:10

Tharkius