Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add uninstall hooks #57 #58

Merged
merged 1 commit into from
Sep 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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