Skip to content

Commit b63d4f6

Browse files
committed
Converted extension to Typescript.
1 parent 309f381 commit b63d4f6

11 files changed

Lines changed: 618 additions & 220 deletions

File tree

.vscode/launch.json

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
// A launch configuration that launches the extension inside a new window
22
{
3-
"version": "0.1.0",
4-
"configurations": [
5-
{
6-
"name": "Launch Extension",
7-
"type": "extensionHost",
8-
"request": "launch",
9-
"runtimeExecutable": "${execPath}",
10-
"args": ["--extensionDevelopmentPath=${workspaceRoot}" ],
11-
"stopOnEntry": false
12-
},
13-
{
14-
"name": "Launch Tests",
15-
"type": "extensionHost",
16-
"request": "launch",
17-
"runtimeExecutable": "${execPath}",
18-
"args": ["--extensionDevelopmentPath=${workspaceRoot}", "--extensionTestsPath=${workspaceRoot}/test" ],
19-
"stopOnEntry": false
20-
}
21-
]
3+
"version": "0.1.0",
4+
"configurations": [
5+
{
6+
"name": "Launch Extension",
7+
"type": "extensionHost",
8+
"request": "launch",
9+
"runtimeExecutable": "${execPath}",
10+
"args": [
11+
"--extensionDevelopmentPath=${workspaceRoot}"
12+
],
13+
"stopOnEntry": true,
14+
"sourceMaps": true,
15+
"outFiles": [
16+
"${workspaceRoot}/out/**/*.js",
17+
"${workspaceRoot}/node_modules/**/*.js",
18+
"!${workspaceRoot}/out/tests/**/*.js"
19+
]
20+
}
21+
]
2222
}

.vscodeignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
.vscode/**
22
typings/**
33
test/**
4+
out/**
45
.gitignore
56
jsconfig.json

extension.js renamed to extension.ts

Lines changed: 76 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,47 @@
1-
"use strict";
2-
// The module 'vscode' contains the VS Code extensibility API
3-
// Import the module and reference it with the alias vscode in your code below
4-
const vscode = require('vscode');
5-
const path = require('path');
1+
import * as vscode from 'vscode';
2+
import * as path from 'path';
63
const parseConfig = require('parse-git-config');
74
const gitBranch = require('git-branch');
85
const githubUrlFromGit = require('github-url-from-git');
9-
const copyPaste = require("copy-paste");
10-
const fs = require("fs");
6+
import * as copyPaste from 'copy-paste'
7+
import * as fs from 'fs';
118

12-
const extensionName = 'Github-File-Url';
9+
type Uri = vscode.Uri;
1310

11+
const extensionName = 'Github-File-Url';
12+
namespace TYPE {
13+
export type config = '.git/config';
14+
export type modules = '.gitmodules';
15+
}
1416
const TYPE = {
15-
git_config: '.git/config',
16-
git_modules: '.gitmodules',
17-
};
17+
config: '.git/config' as TYPE.config,
18+
modules: '.gitmodules' as TYPE.modules,
19+
}
1820

19-
// This method is called when your extension is activated
20-
// Your extension is activated the very first time the command is executed
21-
function activate(context) {
21+
export function activate(context: vscode.ExtensionContext) {
2222

2323
// The command has been defined in the package.json file
2424
{
2525
const commandName = 'extension.github-file-folder-url.copyGithubUrlWithSelection';
26-
context.subscriptions.push(vscode.commands.registerCommand(commandName, (fileUri) => {
26+
context.subscriptions.push(vscode.commands.registerCommand(commandName, (fileUri: Uri) => {
2727
executeCommand1(commandName, fileUri, true, false);
2828
}));
2929
}
3030
{
3131
const commandName = 'extension.github-file-folder-url.copyGithubUrlWithSelection-simple';
32-
context.subscriptions.push(vscode.commands.registerCommand(commandName, (fileUri) => {
32+
context.subscriptions.push(vscode.commands.registerCommand(commandName, (fileUri: Uri) => {
3333
executeCommand1(commandName, fileUri, true, true);
3434
}));
3535
}
3636
{
3737
const commandName = 'extension.github-file-folder-url.copyGithubUrl';
38-
context.subscriptions.push(vscode.commands.registerCommand(commandName, (fileUri) => {
38+
context.subscriptions.push(vscode.commands.registerCommand(commandName, (fileUri: Uri) => {
3939
executeCommand1(commandName, fileUri, false, false);
4040
}));
4141
}
4242
{
4343
const commandName = 'extension.github-file-folder-url.copyGithubUrl-simple';
44-
context.subscriptions.push(vscode.commands.registerCommand(commandName, (fileUri) => {
44+
context.subscriptions.push(vscode.commands.registerCommand(commandName, (fileUri: Uri) => {
4545
executeCommand1(commandName, fileUri, false, true);
4646
}));
4747
}
@@ -59,11 +59,11 @@ function activate(context) {
5959
}
6060
}
6161

62-
function executeCommandAllTextEditors(commandName, simpleFormat) {
62+
function executeCommandAllTextEditors(commandName: string, simpleFormat: boolean) {
6363

6464
try {
6565
const workspaceRootPath = vscode.workspace.rootPath;
66-
const uniquePaths = {};
66+
const uniquePaths: { [key: string]: vscode.TextDocument } = {};
6767
const textEditors = vscode.workspace.textDocuments.filter(p => path.isAbsolute(p.fileName));
6868
if (textEditors.length === 0) {
6969
let errorMessage = 'No open text editors';
@@ -124,25 +124,30 @@ function executeCommandAllTextEditors(commandName, simpleFormat) {
124124
console.log(combinedMessage);
125125
vscode.window.showErrorMessage(combinedMessage);
126126
}
127+
if (allWarnings.length === 0) {
128+
const combinedMessage = `The following ${allWarnings.length} warnings occured:\n\n${allWarnings.join('\n\n')}`;
129+
console.log(combinedMessage);
130+
vscode.window.showErrorMessage(combinedMessage);
131+
}
127132

128133
if (allPaths.length > 0) {
129134
const combindedPaths = allPaths.join('\n');
130135
copyPaste.copy(combindedPaths);
131136
return;
132137
}
133138

134-
if (allErrors.length === 0) {
135-
const errorMessage = `${extensionName} extension failed to run command '${commandName}'.
139+
if (allWarnings.length === 0 && allErrors.length === 0) {
140+
const message = `${extensionName} extension failed to run command '${commandName}'.
136141
Is this a Github repository?
137142
138143
Workspace Root: ${workspaceRootPath}`;
139-
console.log(errorMessage);
140-
vscode.window.showErrorMessage(errorMessage);
144+
console.log(message);
145+
vscode.window.showErrorMessage(message);
141146
return;
142147
}
143148
}
144149
catch (e) {
145-
const errorMessage = `${extensionName} extension failed to execute command '${commandName}'. See debug console for details.`;
150+
let errorMessage = `${extensionName} extension failed to execute command '${commandName}'. See debug console for details.`;
146151
if (e) {
147152
errorMessage += `\n\nErr: ${e}`;
148153
}
@@ -152,10 +157,12 @@ Workspace Root: ${workspaceRootPath}`;
152157
}
153158

154159
}
155-
function executeCommand1(commandName, fileUri, pullLines, simpleFormat) {
160+
161+
type ILineInfo = { start: number, end: number };
162+
function executeCommand1(commandName: string, fileUri: Uri, pullLines: boolean, simpleFormat: boolean) {
156163
try {
157164
const workspaceRootPath = vscode.workspace.rootPath;
158-
let lineInfo = null;
165+
let lineInfo : ILineInfo = null;
159166
if (pullLines) {
160167
let editor = vscode.window.activeTextEditor;
161168
if (editor) {
@@ -182,6 +189,7 @@ function executeCommand1(commandName, fileUri, pullLines, simpleFormat) {
182189
filePath = editor.document.fileName;
183190
}
184191

192+
const allWarnings = [];
185193
if (!fs.existsSync(filePath)) {
186194
// we generate a warning but still generate the url
187195
const errorMessage = `The file '${filePath}' does not exist locally, so no url was generated.`;
@@ -222,7 +230,7 @@ Workspace Root: ${workspaceRootPath}`;
222230
}
223231
}
224232
catch (e) {
225-
const errorMessage = `${extensionName} extension failed to execute command '${commandName}'. See debug console for details.`;
233+
let errorMessage = `${extensionName} extension failed to execute command '${commandName}'. See debug console for details.`;
226234
if (e) {
227235
errorMessage += `\n\nErr: ${e}`;
228236
}
@@ -233,7 +241,7 @@ Workspace Root: ${workspaceRootPath}`;
233241

234242
}
235243

236-
function generateGithubUrl(commandName, workspaceRootPath, filePath, lineSelection) {
244+
function generateGithubUrl(commandName: string, workspaceRootPath: string, filePath: string, lineSelection: ILineInfo) {
237245
try {
238246
workspaceRootPath = workspaceRootPath.replace(/\\/g, '/'); // Flip subdir slashes on Windows
239247
filePath = filePath.replace(/\\/g, '/'); // Flip subdir slashes on Windows
@@ -245,7 +253,7 @@ Is this a Github repository?
245253
Workspace Root: ${workspaceRootPath}
246254
Filepath: ${filePath}`;
247255
return {
248-
type: 'error',
256+
type: 'error' as 'error',
249257
errorMessage,
250258
};
251259
}
@@ -288,7 +296,7 @@ Filepath: ${filePath}`;
288296
}
289297
}
290298
return {
291-
type: 'success',
299+
type: 'success' as 'success',
292300
url,
293301
relativeFilePath,
294302
relativePathFromGitRoot,
@@ -300,7 +308,7 @@ Is this a Github repository?
300308
Workspace Root: ${workspaceRootPath}
301309
Filepath: ${filePath}`;
302310
return {
303-
type: 'error',
311+
type: 'error' as 'error',
304312
errorMessage,
305313
};
306314
}
@@ -311,23 +319,23 @@ Filepath: ${filePath}`;
311319
errorMessage += `\n\nErr: ${e}`;
312320
}
313321
return {
314-
type: 'error',
322+
type: 'error' as 'error',
315323
errorMessage,
316324
};
317325
}
318326
}
319327

320328
const reSubModulePuller = /"([^"]*)"/g;
321329

322-
function findAndParseConfig(rootPath) {
323-
function intParseGitConfig(cwd) {
324-
let ret = parseConfig.sync({ cwd: cwd, path: TYPE.git_config });
330+
function findAndParseConfig(rootPath: string) {
331+
function intParseGitConfig(cwd: string) {
332+
let ret = parseConfig.sync({ cwd: cwd, path: TYPE.config });
325333
if (!(Object.keys(ret).length === 0 && ret.constructor === Object)) {
326334
return ret;
327335
}
328336
}
329-
function intParseModuleConfig(cwd) {
330-
let ret = parseConfig.sync({ cwd: cwd, path: TYPE.git_modules });
337+
function intParseModuleConfig(cwd: string) {
338+
let ret = parseConfig.sync({ cwd: cwd, path: TYPE.modules });
331339
if (!(Object.keys(ret).length === 0 && ret.constructor === Object)) {
332340
return ret;
333341
}
@@ -336,12 +344,12 @@ function findAndParseConfig(rootPath) {
336344
rootPath = rootPath.replace(/\\/g, '/'); // Flip subdir slashes on Windows
337345
const pathParts = rootPath.split('/');
338346
let subModuleConfigs = [];
339-
let subModules = [];
347+
let subModules: IModuleInfo[] = [];
340348
let stepsUp = 0;
341349
while (pathParts.length > 0) {
342350
let currentPath = pathParts.join('/');
343351
let gitConfig = intParseGitConfig(currentPath);
344-
function pullSubModules(subMeta) {
352+
function pullSubModules(subMeta: IRootOrModuleMeta): IModuleInfo[] {
345353
const config = subMeta.config;
346354
const ret = [];
347355
for (const key in config) {
@@ -375,8 +383,8 @@ function findAndParseConfig(rootPath) {
375383
// pathMap
376384
}
377385

378-
const rootMeta = {
379-
type: TYPE.git_config,
386+
const rootMeta: IRootMeta = {
387+
type: TYPE.config,
380388
rootPath: currentPath,
381389
path: currentPath,
382390
config: gitConfig,
@@ -391,8 +399,8 @@ function findAndParseConfig(rootPath) {
391399
} else {
392400
let moduleConfig = intParseModuleConfig(currentPath);
393401
if (moduleConfig) {
394-
const subMeta = {
395-
type: TYPE.git_modules,
402+
const subMeta: IModuleMeta = {
403+
type: TYPE.modules,
396404
path: currentPath,
397405
config: moduleConfig,
398406
stepsUp,
@@ -406,4 +414,27 @@ function findAndParseConfig(rootPath) {
406414
pathParts.pop();
407415
}
408416
}
409-
exports.activate = activate;
417+
418+
type IModuleInfo = {
419+
path: string,
420+
name: string,
421+
url: any,
422+
};
423+
type IModuleMeta = {
424+
type: TYPE.modules,
425+
path: string,
426+
config: any,
427+
subs?: IModuleInfo[],
428+
stepsUp: number,
429+
}
430+
type IRootMeta = {
431+
type: TYPE.config,
432+
rootPath: string,
433+
path: string,
434+
config: any,
435+
stepsUp: number,
436+
subs?: IModuleInfo[],
437+
subModules?: any,
438+
}
439+
440+
type IRootOrModuleMeta = IModuleMeta | IRootMeta;

jsconfig.json

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)