There is a variable under env.process called npm_lifecycle_event that stores the key indicating the name of the script in package.json. So in your case, you can just check if process.env.npm_lifecycle_event === 'clean'.
For example, I run the following configuration:
{
"name": "npm-test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"pretest": "node test.js",
"test": "node index.js",
"posttest": "node test.js"
},
"author": "",
"license": "ISC"
}
test.js content:
console.log(process.env.npm_lifecycle_event)
and the output of the command npm test:
> [email protected] pretest /Users/bartlomiejgladys/Desktop/programming/npm-test
> node test.js
pretest
> [email protected] test /Users/bartlomiejgladys/Desktop/programming/npm-test
> node index.js
hi
> [email protected] posttest /Users/bartlomiejgladys/Desktop/programming/npm-test
> node test.js
posttest