Skip to content

Commit

Permalink
feat: change dir when config change_directory true and platform is da…
Browse files Browse the repository at this point in the history
…rwin (#34)

因为老版本 coffee 不支持插入 script,所以就升级到了最新版。
升级 coffee 后测试覆盖率无法收集,所以升级了 egg-bin。
  • Loading branch information
DiamondYuan authored and popomore committed Dec 18, 2018
1 parent 0e199fb commit 444dee9
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/node_modules/
coverage/
cache.json
.nyc_output/
!test/fixtures/hook/.projj/cache.json
!test/fixtures/find/.projj/cache.json
!test/fixtures/find-change-directory/.projj/cache.json
44 changes: 37 additions & 7 deletions lib/command/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,25 @@ class FindCommand extends BaseCommand {
this.logger.error('Can not find repo %s', chalk.yellow(repo));
return;
}
let key;
if (matched.length === 1) {
yield this.copyPath(repo, matched[0]);
return;
key = matched[0];
} else {
const res = yield this.choose(matched);
key = res.key;
}
const dir = path.join(this.config.base, key);
if (this.config.change_directory) {
/* istanbul ignore next */
if (process.platform === 'darwin') {
const script = this.generateAppleScript(dir);
this.logger.info(`Change directory to ${dir}`);
yield this.runScript(script);
return;
}
this.logger.error('Change directory only supported in darwin');
}
// multi
const res = yield this.choose(matched);
yield this.copyPath(repo, res.key);
yield this.copyPath(repo, dir);
}

* choose(choices) {
Expand All @@ -39,9 +51,27 @@ class FindCommand extends BaseCommand {
choices,
});
}
/* istanbul ignore next */
generateAppleScript(dir) {
const command =
`tell application "System Events"
set activeApp to name of first application process whose frontmost is true
if "iTerm" is in activeApp then
tell application "iTerm"
tell current session of current window
write text "cd ${dir}"
end tell
end tell
else if "Terminal" is in activeApp then
tell application "Terminal"
do script "cd ${dir}" in front window
end tell
end if
end tell`.split('\n').map(line => (` -e '${line.trim()}'`));
return `osascript ${command.join('')}`;
}

* copyPath(repo, key) {
const dir = path.join(this.config.base, key);
* copyPath(repo, dir) {
try {
this.logger.info('find repo %s\'s location: %s', repo, dir);
yield clipboardy.write(`cd ${dir}`);
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
},
"devDependencies": {
"autod": "^2.7.1",
"coffee": "^3.3.0",
"egg-bin": "^2.2.3",
"coffee": "^5.1.0",
"egg-bin": "^4.9.0",
"egg-ci": "^1.2.0",
"eslint": "^3.16.1",
"eslint-config-egg": "^3.2.0",
Expand Down
14 changes: 14 additions & 0 deletions test/fixtures/find-change-directory/.projj/cache.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"github.com/eggjs/egg": {
"repo": "[email protected]:eggjs/egg.git"
},
"github.com/eggjs/egg-core": {
"repo": "[email protected]:eggjs/egg-core.git"
},
"gitlab.com/eggjs/egg-core": {
"repo": "[email protected]:eggjs/egg-core.git"
},
"gitlab.com/eggjs/autod-egg": {
"repo": "[email protected]:eggjs/autod-egg.git"
}
}
5 changes: 5 additions & 0 deletions test/fixtures/find-change-directory/.projj/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"base": "..",
"run_config": true,
"change_directory":true
}
10 changes: 10 additions & 0 deletions test/fixtures/find-change-directory/mock_darwin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

const mm = require('mm');
const FindCommand = require('../../../lib/command/find');

mm(process, 'platform', 'darwin');

mm(FindCommand.prototype, 'runScript', function* (cmd) {
console.log(cmd);
});
10 changes: 10 additions & 0 deletions test/fixtures/find-change-directory/mock_not_darwin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

const mm = require('mm');
const clipboardy = require('clipboardy');


mm(process, 'platform', 'not_darwin');
mm(clipboardy, 'write', function* () {
return;
});
23 changes: 23 additions & 0 deletions test/projj_find.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,29 @@ describe('test/projj_find.test.js', () => {
afterEach(mm.restore);
afterEach(() => rimraf(tmp));

it('should run script when changeDirectory is true and platform is darwin', done => {
const home = path.join(fixtures, 'find-change-directory');
const mockDarwin = path.join(home, 'mock_darwin.js');
mm(process.env, 'HOME', home);
coffee.fork(binfile, [ 'find', 'egg' ])
.beforeScript(mockDarwin)
.expect('stdout', new RegExp(`Change directory to ${home}/github.com/eggjs/egg`))
.expect('stdout', new RegExp(`cd ${home}/github.com/eggjs/egg`))
.expect('code', 0)
.end(done);
});

it('should show warn when changeDirectory is true and platform is not darwin', done => {
const home = path.join(fixtures, 'find-change-directory');
const mockNotDarwin = path.join(home, 'mock_not_darwin.js');
mm(process.env, 'HOME', home);
coffee.fork(binfile, [ 'find', 'egg' ])
.beforeScript(mockNotDarwin)
.expect('stderr', new RegExp('Change directory only supported in darwin'))
.expect('stdout', new RegExp(`find repo egg's location: ${home}/github.com/eggjs/egg`))
.expect('code', 0)
.end(done);
});

it('should to prompt if the input is empty', done => {
const home = path.join(fixtures, 'find');
Expand Down
2 changes: 1 addition & 1 deletion test/projj_import.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ describe('test/projj_import.test.js', () => {

it('should import from it', function* () {
yield coffee.fork(binfile, [ 'import', '--cache' ])
.debug()
// .debug()
.expect('stdout', /importing repository https:\/\/github.com\/popomore\/projj.git/)
.expect('stdout', new RegExp(`Cloning into ${target}`))
.expect('stdout', /preadd/)
Expand Down

0 comments on commit 444dee9

Please sign in to comment.