Skip to content

Commit

Permalink
feat: add should support change directory (#39)
Browse files Browse the repository at this point in the history
还是旧版的 CommandBin,有空得找时间重构下
  • Loading branch information
atian25 authored and popomore committed Jan 2, 2019
1 parent 61a1d28 commit 1771ca5
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 35 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2017 popomore
Copyright (c) 2017-present popomore

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ install:
test_script:
- node --version
- npm --version
- npm run ci
- npm run test

build: off
12 changes: 12 additions & 0 deletions lib/command/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const path = require('path');
const fs = require('mz/fs');
const chalk = require('chalk');
const clipboardy = require('clipboardy');
const utils = require('../utils');
const BaseCommand = require('../base_command');

class AddCommand extends BaseCommand {
Expand All @@ -19,6 +20,17 @@ class AddCommand extends BaseCommand {

yield this.addRepo(repo, key);

if (this.config.change_directory) {
/* istanbul ignore next */
if (process.platform === 'darwin') {
const script = utils.generateAppleScript(targetPath);
this.logger.info(`Change directory to ${targetPath}`);
yield this.runScript(script);
return;
}
this.logger.error('Change directory only supported in darwin');
}

try {
yield clipboardy.write(`cd ${targetPath}`);
this.logger.info(chalk.green('📋 Copied to clipboard') + ', just use Ctrl+V');
Expand Down
21 changes: 2 additions & 19 deletions lib/command/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const path = require('path');
const chalk = require('chalk');
const inquirer = require('inquirer');
const clipboardy = require('clipboardy');
const utils = require('../utils');
const BaseCommand = require('../base_command');

class FindCommand extends BaseCommand {
Expand Down Expand Up @@ -33,7 +34,7 @@ class FindCommand extends BaseCommand {
if (this.config.change_directory) {
/* istanbul ignore next */
if (process.platform === 'darwin') {
const script = this.generateAppleScript(dir);
const script = utils.generateAppleScript(dir);
this.logger.info(`Change directory to ${dir}`);
yield this.runScript(script);
return;
Expand All @@ -51,24 +52,6 @@ class FindCommand extends BaseCommand {
choices,
});
}
/* istanbul ignore next */
generateAppleScript(dir) {
const terminalCommand = `tell application "Terminal"
do script "cd ${dir}" in front window
end tell`.split('\n').map(line => (` -e '${line.trim()}'`)).join('');

const iTermCommand = `tell application "iTerm"
tell current session of current window
write text "cd ${dir}"
end tell
end tell`.split('\n').map(line => (` -e '${line.trim()}'`)).join('');

const currentApp = `tell application "System Events"
set activeApp to name of first application process whose frontmost is true
end tell`.split('\n').map(line => (` -e '${line.trim()}'`)).join('');

return `[ \`osascript ${currentApp}\` = "Terminal" ] && osascript ${terminalCommand} >/dev/null || osascript ${iTermCommand}`;
}

* copyPath(repo, dir) {
try {
Expand Down
19 changes: 19 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

exports.generateAppleScript = dir => {
const terminalCommand = `tell application "Terminal"
do script "cd ${dir}" in front window
end tell`.split('\n').map(line => (` -e '${line.trim()}'`)).join('');

const iTermCommand = `tell application "iTerm"
tell current session of current window
write text "cd ${dir}"
end tell
end tell`.split('\n').map(line => (` -e '${line.trim()}'`)).join('');

const currentApp = `tell application "System Events"
set activeApp to name of first application process whose frontmost is true
end tell`.split('\n').map(line => (` -e '${line.trim()}'`)).join('');

return `[ \`osascript ${currentApp}\` = "Terminal" ] && osascript ${terminalCommand} >/dev/null || osascript ${iTermCommand}`;
};
4 changes: 4 additions & 0 deletions test/fixtures/add-change-directory/.projj/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"base": "../../tmp",
"change_directory": true
}
10 changes: 0 additions & 10 deletions test/fixtures/find-change-directory/mock_darwin.js

This file was deleted.

10 changes: 10 additions & 0 deletions test/fixtures/mock_darwin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

const mm = require('mm');
const BaseCommand = require('../../lib/base_command');

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

mm(BaseCommand.prototype, 'runScript', function* (cmd) {
console.log(cmd);
});
File renamed without changes.
33 changes: 33 additions & 0 deletions test/projj_add.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,39 @@ describe('test/projj_add.test.js', () => {
.end(done);
});

it('should run script when changeDirectory is true and platform is darwin', done => {
const home = path.join(fixtures, 'add-change-directory');
const repo = 'https://github.com/popomore/projj.git';
const target = path.join(tmp, 'github.com/popomore/projj');
mm(process.env, 'HOME', home);

coffee.fork(binfile, [ 'add', repo ])
.debug()
.beforeScript(path.join(__dirname, 'fixtures/mock_darwin.js'))
.expect('stdout', new RegExp(`Start adding repository ${repo}`))
.expect('stdout', new RegExp(`Cloning into ${target}`))
.expect('stdout', new RegExp(`Change directory to ${target}`))
.notExpect('stdout', /Copied to clipboard/)
.expect('code', 0)
.end(done);
});

it('should run script when changeDirectory is true and platform is not darwin', done => {
const home = path.join(fixtures, 'add-change-directory');
const repo = 'https://github.com/popomore/projj.git';
const target = path.join(tmp, 'github.com/popomore/projj');
mm(process.env, 'HOME', home);

coffee.fork(binfile, [ 'add', repo ])
.debug()
.beforeScript(path.join(__dirname, 'fixtures/mock_not_darwin.js'))
.expect('stdout', new RegExp(`Start adding repository ${repo}`))
.expect('stdout', new RegExp(`Cloning into ${target}`))
.expect('stdout', /Copied to clipboard/)
.expect('stderr', new RegExp('Change directory only supported in darwin'))
.expect('code', 0)
.end(done);
});
});

function mkdir(file) {
Expand Down
6 changes: 2 additions & 4 deletions test/projj_find.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ describe('test/projj_find.test.js', () => {

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)
.beforeScript(path.join(__dirname, 'fixtures/mock_darwin.js'))
.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)
Expand All @@ -29,10 +28,9 @@ describe('test/projj_find.test.js', () => {

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)
.beforeScript(path.join(__dirname, 'fixtures/mock_not_darwin.js'))
.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)
Expand Down

0 comments on commit 1771ca5

Please sign in to comment.