Skip to content

Commit

Permalink
Merge pull request #58 from js-spider/master
Browse files Browse the repository at this point in the history
add uninstall hooks #57
  • Loading branch information
EmilyMew authored Sep 4, 2019
2 parents b41ff9b + 4be2895 commit 87b5c4e
Show file tree
Hide file tree
Showing 4 changed files with 3,071 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ lib-cov
*.out
*.pid
*.gz

.idea/**
pids
logs
results
Expand Down
14 changes: 13 additions & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ program

program
.command('help', { isDefault: true })
.description('Print this help')
.description('Print this help \n if you want to clear the NRM configuration when uninstall you can execute "npm uninstall nrm -g -C or npm uninstall nrm -g --clean"')
.action(function() {
program.outputHelp();
});
Expand Down Expand Up @@ -555,3 +555,15 @@ function equalsIgnoreCase(str1, str2) {
return !str1 && !str2;
}
}

function cleanRegistry(){
setCustomRegistry('',function(err){
if (err) exit(err);
onUse('npm')
})
}

module.exports = {
cleanRegistry,
errExit: exit
}
45 changes: 45 additions & 0 deletions hooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env node

const { cleanRegistry } = require('./cli')

function preuninstall(){
if(ifNeedClean()){
console.log('preuninstall run success : clean .nrmrc info')
return cleanRegistry();
};
console.log('preuninstall run success : keep .nrmrc info')
process.exit();
}

function ifNeedClean(){
if(process.env.npm_config_clean){
return true
}else{
const envArgv = process.env.npm_config_argv || '{"original":[]}';
const envOriginal = JSON.parse(envArgv).original;
return envOriginal.includes('-C')
}
}

function isLocalDebug(argv){
let debugTarget = null;
argv.forEach(item=>{
// use node hooks.js --LocalDebugging=preuninstall
if(item.startsWith('--LocalDebugging=')){
debugTarget = item.split('=')[1]
}
})
return debugTarget;
}


function run (argv) {
const target = isLocalDebug(argv) || process.env.npm_lifecycle_event;
switch(target){
case 'preuninstall':
preuninstall(argv);break;
default: process.exit(0)
}
}

run(process.argv.slice(2));  
Loading

0 comments on commit 87b5c4e

Please sign in to comment.