-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b45883a
commit 76762aa
Showing
13 changed files
with
254 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
'use strict'; | ||
|
||
const path = require('path'); | ||
const chalk = require('chalk'); | ||
const BaseCommand = require('../base_command'); | ||
const fs = require('mz/fs'); | ||
const rimraf = require('mz-modules/rimraf'); | ||
|
||
class RemoveCommand extends BaseCommand { | ||
|
||
* _run(cwd, [ repo ]) { | ||
if (!repo) { | ||
this.logger.error('Please specify the repo name:'); | ||
this.childLogger.error(chalk.white('For example:'), chalk.green('projj remove', chalk.yellow('example'))); | ||
return; | ||
} | ||
const keys = Object.keys(yield this.cache.get()); | ||
let matched = keys.filter(key => key.endsWith(repo.replace(/^\/?/, '/'))); | ||
if (!matched.length) matched = keys.filter(key => key.indexOf(repo) >= 0); | ||
if (!matched.length) { | ||
this.logger.error('Can not find repo %s', chalk.yellow(repo)); | ||
return; | ||
} | ||
let key; | ||
if (matched.length === 1) { | ||
key = matched[0]; | ||
} else { | ||
const res = yield this.choose(matched); | ||
key = res.key; | ||
} | ||
this.logger.info('Do you want to remove the repository', chalk.green(key)); | ||
this.logger.info(chalk.red('Removed repository cannot be restored!')); | ||
const s = key.split('/'); | ||
const res = yield this.confirm(`${s[1]}/${s[2]}`); | ||
if (res) { | ||
const dir = path.join(this.config.base, key); | ||
yield rimraf(dir); | ||
const parent = path.dirname(dir); | ||
if ((yield fs.readdir(parent)).length === 0) { | ||
yield rimraf(parent); | ||
} | ||
yield this.cache.remove(key); | ||
yield this.cache.dump(); | ||
this.logger.info('Successfully remove repository', chalk.green(key)); | ||
} else { | ||
this.logger.info('Cancel remove repository ', chalk.green(key)); | ||
} | ||
} | ||
|
||
|
||
* confirm(repoName) { | ||
const res = yield this.prompt({ | ||
message: `Please type in the name of the repository to confirm. ${chalk.green(repoName)} \n`, | ||
name: 'userInput', | ||
}); | ||
if (res.userInput === repoName) { | ||
return true; | ||
} | ||
const continueRes = yield this.prompt({ | ||
type: 'confirm', | ||
message: 'Do you want to continue?', | ||
name: 'continueToEnter', | ||
default: false, | ||
}); | ||
if (continueRes.continueToEnter) { | ||
return yield this.confirm(repoName); | ||
} | ||
return false; | ||
} | ||
|
||
|
||
* choose(choices) { | ||
return yield this.prompt({ | ||
name: 'key', | ||
type: 'list', | ||
message: 'Please select the correct repo', | ||
choices, | ||
}); | ||
} | ||
|
||
help() { | ||
return 'Remove repository'; | ||
} | ||
|
||
} | ||
|
||
module.exports = RemoveCommand; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"base": "../temp" | ||
} |
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
'use strict'; | ||
|
||
const path = require('path'); | ||
const coffee = require('coffee'); | ||
const mm = require('mm'); | ||
const rimraf = require('mz-modules/rimraf'); | ||
const binfile = path.join(__dirname, '../bin/projj.js'); | ||
const fixtures = path.join(__dirname, 'fixtures'); | ||
const fs = require('mz/fs'); | ||
const home = path.join(fixtures, 'remove'); | ||
const runscript = require('runscript'); | ||
const assert = require('assert'); | ||
const projects = path.join(home, 'projects'); | ||
const tempProject = path.join(home, 'temp'); | ||
const catchPath = path.join(home, '.projj/cache.json'); | ||
|
||
describe('test/projj_remove.test.js', () => { | ||
beforeEach(function* () { | ||
mm(process.env, 'HOME', home); | ||
const content = JSON.stringify({ | ||
'github.com/popomore/projj': {}, | ||
'github.com/eggjs/egg': {}, | ||
'github.com/eggjs/egg-core': {}, | ||
'github.com/eggjs/autod-egg': {}, | ||
'gitlab.com/eggjs/egg': {}, | ||
'github.com/DiamondYuan/yuque': {}, | ||
}); | ||
yield runscript(`cp -r ${projects} ${tempProject}`); | ||
yield fs.writeFile(catchPath, content); | ||
}); | ||
|
||
afterEach(() => { | ||
afterEach(() => rimraf(tempProject)); | ||
afterEach(() => rimraf(catchPath)); | ||
}); | ||
|
||
|
||
it('should to prompt if the input is empty', done => { | ||
coffee.fork(binfile, [ 'remove', '' ]) | ||
.expect('stderr', new RegExp('Please specify the repo name:')) | ||
.expect('stderr', new RegExp('For example: projj remove example')) | ||
.expect('code', 0) | ||
.end(done); | ||
}); | ||
|
||
it('if there are other files in the folder, the folder will not be deleted.', done => { | ||
coffee.fork(binfile, [ 'remove', 'yuque' ]) | ||
.waitForPrompt() | ||
.expect('stdout', new RegExp('Do you want to remove the repository github.com/DiamondYuan/yuque')) | ||
.expect('stdout', new RegExp('Removed repository cannot be restored!')) | ||
.expect('stdout', new RegExp('Please type in the name of the repository to confirm. DiamondYuan/yuque')) | ||
.write('DiamondYuan/yuque\n') | ||
.expect('stdout', new RegExp('Successfully remove repository github.com/DiamondYuan/yuque')) | ||
.expect('code', 0) | ||
.end(err => { | ||
assert.ifError(err); | ||
assert(fs.existsSync(path.join(tempProject, 'github.com/DiamondYuan'))); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('if no other files are in the folder, the folder will be deleted.', done => { | ||
const folder = path.join(tempProject, 'github.com/popomore'); | ||
coffee.fork(binfile, [ 'remove', 'projj' ]) | ||
.expect('stdout', new RegExp('Do you want to remove the repository github.com/popomore/projj')) | ||
.expect('stdout', new RegExp('Removed repository cannot be restored!')) | ||
.expect('stdout', new RegExp('Please type in the name of the repository to confirm. popomore/projj')) | ||
.write('popomore/projj') | ||
.expect('code', 0) | ||
.end(err => { | ||
assert.ifError(err); | ||
assert(fs.existsSync(folder) === false); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should update cache that do not exist', done => { | ||
coffee.fork(binfile, [ 'remove', 'autod-egg' ]) | ||
.expect('stdout', new RegExp('Do you want to remove the repository github.com/eggjs/autod-egg')) | ||
.expect('stdout', new RegExp('Removed repository cannot be restored!')) | ||
.expect('stdout', new RegExp('Please type in the name of the repository to confirm. eggjs/autod-egg')) | ||
.write('eggjs/autod-egg') | ||
.expect('code', 0) | ||
.end(err => { | ||
assert.ifError(err); | ||
const cache = fs.readFileSync(catchPath); | ||
assert(JSON.parse(cache.toString())['github.com/eggjs/autod-egg'] === undefined); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('could retry if the input is incorrect', done => { | ||
coffee.fork(binfile, [ 'remove', 'autod-egg' ]) | ||
.waitForPrompt() | ||
.expect('stdout', new RegExp('Do you want to remove the repository github.com/eggjs/autod-egg')) | ||
.expect('stdout', new RegExp('Removed repository cannot be restored!')) | ||
.expect('stdout', new RegExp('Please type in the name of the repository to confirm. eggjs/autod-egg')) | ||
.write('eggjs/egg\n') | ||
.expect('stdout', new RegExp('Do you want to continue')) | ||
.write('Y\n') | ||
.write('eggjs/autod-egg') | ||
.expect('code', 0) | ||
.end(done); | ||
}); | ||
|
||
it('could cancel if the input is incorrect', done => { | ||
coffee.fork(binfile, [ 'remove', 'autod-egg' ]) | ||
.waitForPrompt() | ||
.expect('stdout', new RegExp('Do you want to remove the repository github.com/eggjs/autod-egg')) | ||
.expect('stdout', new RegExp('Removed repository cannot be restored!')) | ||
.expect('stdout', new RegExp('Please type in the name of the repository to confirm. eggjs/autod-egg')) | ||
.write('eggjs/egg\n') | ||
.expect('stdout', new RegExp('Do you want to continue')) | ||
.write('\n') | ||
.expect('stdout', new RegExp('Cancel remove repository {2}github.com/eggjs/autod-egg')) | ||
.expect('code', 0) | ||
.end(done); | ||
}); | ||
|
||
it('should find two matching file with egg', done => { | ||
coffee.fork(binfile, [ 'remove', 'egg' ]) | ||
.expect('stdout', new RegExp('Please select the correct repo')) | ||
.write('\n') | ||
.expect('stdout', new RegExp('Do you want to remove the repository github.com/eggjs/egg')) | ||
.expect('code', 0) | ||
.end(done); | ||
}); | ||
|
||
it('should find nothing with eggggg', done => { | ||
coffee.fork(binfile, [ 'remove', 'eggggg' ]) | ||
.expect('stderr', new RegExp('Can not find repo eggggg')) | ||
.expect('code', 0) | ||
.end(done); | ||
}); | ||
}); |