Skip to content

Commit 5ecfafc

Browse files
committed
added command decorators
1 parent 8acfc0e commit 5ecfafc

File tree

3 files changed

+85
-98
lines changed

3 files changed

+85
-98
lines changed

src/commands.ts

Lines changed: 29 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,25 @@ interface CommandOptions {
1818
repository?: boolean;
1919
}
2020

21+
interface Command {
22+
commandId: string;
23+
key: string;
24+
method: Function;
25+
options: CommandOptions;
26+
}
27+
28+
const Commands: Command[] = [];
29+
30+
function command(commandId: string, options: CommandOptions = {}): Function {
31+
return (target: any, key: string, descriptor: any) => {
32+
if (!(typeof descriptor.value === "function")) {
33+
throw new Error("not supported");
34+
}
35+
36+
Commands.push({ commandId, key, method: descriptor.value, options });
37+
};
38+
}
39+
2140
class CreateBranchItem implements QuickPickItem {
2241
constructor(private commands: SvnCommands) {}
2342

@@ -66,55 +85,7 @@ export class SvnCommands {
6685
private commands: any[] = [];
6786

6887
constructor(private model: Model) {
69-
this.commands = [
70-
{
71-
commandId: "svn.commitWithMessage",
72-
method: this.commitWithMessage,
73-
options: { repository: true }
74-
},
75-
{
76-
commandId: "svn.add",
77-
method: this.addFile,
78-
options: {}
79-
},
80-
{
81-
commandId: "svn.fileOpen",
82-
method: this.fileOpen,
83-
options: {}
84-
},
85-
{
86-
commandId: "svn.commit",
87-
method: this.commit,
88-
options: { repository: true }
89-
},
90-
{
91-
commandId: "svn.refresh",
92-
method: this.refresh,
93-
options: { repository: true }
94-
},
95-
{
96-
commandId: "svn.openDiffHead",
97-
method: this.openDiffHead,
98-
options: {}
99-
},
100-
{
101-
commandId: "svn.switchBranch",
102-
method: this.switchBranch,
103-
options: { repository: true }
104-
},
105-
{
106-
commandId: "svn.branch",
107-
method: this.branch,
108-
options: { repository: true }
109-
},
110-
{
111-
commandId: "svn.revert",
112-
method: this.revert,
113-
options: { repository: true }
114-
}
115-
];
116-
117-
this.commands.map(({ commandId, method, options }) => {
88+
Commands.map(({ commandId, method, options }) => {
11889
const command = this.createCommand(method, options);
11990
commands.registerCommand(commandId, command);
12091
});
@@ -158,10 +129,12 @@ export class SvnCommands {
158129
return result;
159130
}
160131

132+
@command("svn.fileOpen")
161133
fileOpen(resourceUri: Uri) {
162134
commands.executeCommand("vscode.open", resourceUri);
163135
}
164136

137+
@command("svn.commitWithMessage", { repository: true })
165138
async commitWithMessage(repository: Repository) {
166139
const message = repository.inputBox.value;
167140
const changes = repository.changes.resourceStates;
@@ -191,6 +164,7 @@ export class SvnCommands {
191164
}
192165
}
193166

167+
@command("svn.add")
194168
async addFile(resource: Resource) {
195169
const repository = this.model.getRepository(resource.resourceUri.fsPath);
196170

@@ -206,6 +180,7 @@ export class SvnCommands {
206180
}
207181
}
208182

183+
@command("svn.commit", { repository: true })
209184
async commit(repository: Repository, ...args: any[][]): Promise<void> {
210185
console.log(args);
211186
try {
@@ -227,6 +202,7 @@ export class SvnCommands {
227202
}
228203
}
229204

205+
@command("svn.refresh", { repository: true })
230206
refresh(repository: Repository) {
231207
repository.update();
232208
}
@@ -258,6 +234,7 @@ export class SvnCommands {
258234
return `${file} (${ref})`;
259235
}
260236

237+
@command("svn.openDiffHead")
261238
async openDiffHead(resource: Resource) {
262239
if (resource instanceof Resource) {
263240
this.openDiff(resource, "HEAD");
@@ -287,6 +264,7 @@ export class SvnCommands {
287264
return resource.resourceUri;
288265
}
289266

267+
@command("svn.switchBranch", { repository: true })
290268
async switchBranch(repository: Repository) {
291269
const branches = repository.branches.map(
292270
branch => new SwitchBranchItem(branch)
@@ -304,6 +282,7 @@ export class SvnCommands {
304282
await choice.run(repository);
305283
}
306284

285+
@command("svn.branch", { repository: true })
307286
async branch(repository: Repository): Promise<void> {
308287
const result = await window.showInputBox({
309288
prompt: "Please provide a branch name",
@@ -321,6 +300,7 @@ export class SvnCommands {
321300
await repository.branch(name);
322301
}
323302

303+
@command("svn.revert", { repository: true })
324304
async revert(repository: Repository, ...args: any[][]) {
325305
try {
326306
const paths = args[0].map(state => {

src/decorators.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,27 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
'use strict';
6+
"use strict";
77

8-
import { done } from './util';
8+
import { done } from "./util";
99

10-
function decorate(decorator: (fn: Function, key: string) => Function): Function {
10+
function decorate(
11+
decorator: (fn: Function, key: string) => Function
12+
): Function {
1113
return (target: any, key: string, descriptor: any) => {
1214
let fnKey: string | null = null;
1315
let fn: Function | null = null;
1416

15-
if (typeof descriptor.value === 'function') {
16-
fnKey = 'value';
17+
if (typeof descriptor.value === "function") {
18+
fnKey = "value";
1719
fn = descriptor.value;
18-
} else if (typeof descriptor.get === 'function') {
19-
fnKey = 'get';
20+
} else if (typeof descriptor.get === "function") {
21+
fnKey = "get";
2022
fn = descriptor.get;
2123
}
2224

2325
if (!fn || !fnKey) {
24-
throw new Error('not supported');
26+
throw new Error("not supported");
2527
}
2628

2729
descriptor[fnKey] = decorator(fn, key);
@@ -31,7 +33,7 @@ function decorate(decorator: (fn: Function, key: string) => Function): Function
3133
function _memoize(fn: Function, key: string): Function {
3234
const memoizeKey = `$memoize$${key}`;
3335

34-
return function (...args: any[]) {
36+
return function(...args: any[]) {
3537
if (!this.hasOwnProperty(memoizeKey)) {
3638
Object.defineProperty(this, memoizeKey, {
3739
configurable: false,
@@ -51,7 +53,7 @@ function _throttle<T>(fn: Function, key: string): Function {
5153
const currentKey = `$throttle$current$${key}`;
5254
const nextKey = `$throttle$next$${key}`;
5355

54-
const trigger = function (...args: any[]) {
56+
const trigger = function(...args: any[]) {
5557
if (this[nextKey]) {
5658
return this[nextKey];
5759
}
@@ -67,7 +69,7 @@ function _throttle<T>(fn: Function, key: string): Function {
6769

6870
this[currentKey] = fn.apply(this, args) as Promise<T>;
6971

70-
const clear = () => this[currentKey] = undefined;
72+
const clear = () => (this[currentKey] = undefined);
7173
done(this[currentKey]).then(clear, clear);
7274

7375
return this[currentKey];
@@ -81,8 +83,9 @@ export const throttle = decorate(_throttle);
8183
function _sequentialize<T>(fn: Function, key: string): Function {
8284
const currentKey = `__$sequence$${key}`;
8385

84-
return function (...args: any[]) {
85-
const currentPromise = this[currentKey] as Promise<any> || Promise.resolve(null);
86+
return function(...args: any[]) {
87+
const currentPromise =
88+
(this[currentKey] as Promise<any>) || Promise.resolve(null);
8689
const run = async () => await fn.apply(this, args);
8790
this[currentKey] = currentPromise.then(run, run);
8891
return this[currentKey];
@@ -95,9 +98,9 @@ export function debounce(delay: number): Function {
9598
return decorate((fn, key) => {
9699
const timerKey = `$debounce$${key}`;
97100

98-
return function (...args: any[]) {
101+
return function(...args: any[]) {
99102
clearTimeout(this[timerKey]);
100103
this[timerKey] = setTimeout(() => fn.apply(this, args), delay);
101104
};
102105
});
103-
}
106+
}

src/svnRepository.ts

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -83,47 +83,51 @@ export class Repository {
8383
async getBranches() {
8484
const repoUrl = await this.getRepoUrl();
8585

86-
let branches:string[] = [];
86+
let branches: string[] = [];
8787

8888
let promises = [];
8989

90-
promises.push(new Promise<string[]>(async resolve => {
91-
let trunkExists = await this.svn.exec("", [
92-
"ls",
93-
repoUrl + "/trunk",
94-
"--depth",
95-
"empty"
96-
]);
97-
98-
if (trunkExists.exitCode === 0) {
99-
resolve(["trunk"]);
100-
return;
101-
}
102-
resolve([]);
103-
}));
104-
105-
const trees = ["branches", "tags"];
106-
107-
for (let index in trees) {
108-
promises.push(new Promise<string[]>(async resolve => {
109-
const tree = trees[index];
110-
const branchUrl = repoUrl + "/" + tree;
111-
112-
const result = await this.svn.list(branchUrl);
113-
114-
if (result.exitCode !== 0) {
115-
resolve([]);
90+
promises.push(
91+
new Promise<string[]>(async resolve => {
92+
let trunkExists = await this.svn.exec("", [
93+
"ls",
94+
repoUrl + "/trunk",
95+
"--depth",
96+
"empty"
97+
]);
98+
99+
if (trunkExists.exitCode === 0) {
100+
resolve(["trunk"]);
116101
return;
117102
}
103+
resolve([]);
104+
})
105+
);
118106

119-
const list = result.stdout
120-
.trim()
121-
.replace(/\/|\\/g, "")
122-
.split(/[\r\n]+/)
123-
.map((i: string) => tree + "/" + i);
107+
const trees = ["branches", "tags"];
124108

125-
resolve(list);
126-
}));
109+
for (let index in trees) {
110+
promises.push(
111+
new Promise<string[]>(async resolve => {
112+
const tree = trees[index];
113+
const branchUrl = repoUrl + "/" + tree;
114+
115+
const result = await this.svn.list(branchUrl);
116+
117+
if (result.exitCode !== 0) {
118+
resolve([]);
119+
return;
120+
}
121+
122+
const list = result.stdout
123+
.trim()
124+
.replace(/\/|\\/g, "")
125+
.split(/[\r\n]+/)
126+
.map((i: string) => tree + "/" + i);
127+
128+
resolve(list);
129+
})
130+
);
127131
}
128132

129133
const all = await Promise.all<any>(promises);

0 commit comments

Comments
 (0)