Skip to content

Commit b4b9fee

Browse files
committed
Added CommandBase with checkForOpenWorkspace method to show error the user tries to activate an AngularTools command before opening a folder.
1 parent 2fd4235 commit b4b9fee

9 files changed

+37
-17
lines changed

src/commands/commandBase.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import * as vscode from 'vscode';
2+
3+
export class CommandBase {
4+
protected checkForOpenWorkspace() {
5+
if (!vscode.workspace.workspaceFolders) {
6+
vscode.window.showErrorMessage('AngularTools: Must be in an open WorkSpace. Choose menu File -> Open Folder... to open a folder.');
7+
return;
8+
}
9+
}
10+
11+
protected isTypescriptFile(filename: string): boolean {
12+
return filename.endsWith('.ts') && !filename.endsWith('index.ts');
13+
}
14+
}

src/commands/componentHierarchyDgml.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1+
import { CommandBase } from '@commands';
12
import { Component, ComponentManager, Config, FileSystemUtils } from '@src';
23
import * as fs from 'fs';
34
import * as path from 'path';
45
import * as vscode from 'vscode';
56
import * as xmldom from 'xmldom';
67

7-
88
const prettifyXml = require('prettify-xml');
99
const xmlSerializer = require('xmlserializer');
1010

11-
export class ComponentHierarchyDgml {
11+
export class ComponentHierarchyDgml extends CommandBase {
1212
private config = new Config();
1313
public static get commandName(): string { return 'componentHierarchyDgml'; }
1414

1515
public execute() {
16+
this.checkForOpenWorkspace();
1617
const fsUtils = new FileSystemUtils();
1718
var directoryPath: string = fsUtils.getWorkspaceFolder();
1819
const components = ComponentManager.findComponents(directoryPath);

src/commands/componentHierarchyMarkdown.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
import { CommandBase } from '@commands';
12
import { Config, Component, ComponentManager, FileSystemUtils } from '@src';
23
import * as path from 'path';
34

4-
export class ComponentHierarchyMarkdown {
5+
export class ComponentHierarchyMarkdown extends CommandBase {
56
private config = new Config();
67
public static get commandName(): string { return 'componentHierarchyMarkdown'; }
78

89
public execute() {
10+
this.checkForOpenWorkspace();
911
const fsUtils = new FileSystemUtils();
1012
var workspaceDirectory: string = fsUtils.getWorkspaceFolder();
1113
const components = ComponentManager.findComponents(workspaceDirectory);

src/commands/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export * from './commandBase';
12
export * from './componentHierarchyDgml';
23
export * from './listAllImports';
34
export * from './modulesToMarkdown';

src/commands/listAllImports.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,20 @@ import * as fs from 'fs';
22
import * as vscode from 'vscode';
33

44
import { ArrayUtils, Config, FileSystemUtils } from '@src';
5+
import { CommandBase } from '@commands';
56

6-
export class ListAllImports {
7+
export class ListAllImports extends CommandBase {
78
private config = new Config();
89
public static get commandName(): string { return 'listAllImports'; }
910

1011
public execute() {
12+
this.checkForOpenWorkspace();
1113
const fsUtils = new FileSystemUtils();
1214
var directoryPath: string = fsUtils.getWorkspaceFolder();
1315
const files = fsUtils.listFiles(directoryPath, this.config.excludeDirectories, this.isTypescriptFile);
1416
this.writeResult(directoryPath, files);
1517
}
1618

17-
private isTypescriptFile(filename: string): boolean {
18-
return filename.endsWith('.ts') && !filename.endsWith('index.ts');
19-
}
20-
2119
private writeResult(workspaceDirectory: string, results: string[]) {
2220
const imports: { [module: string]: number } = {};
2321
if (!results) { return; }

src/commands/modulesToMarkdown.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as fs from 'fs';
22
import * as vscode from 'vscode';
33
import * as path from 'path';
44
import { ArrayUtils, Config, FileSystemUtils } from '@src';
5+
import { CommandBase } from '@commands';
56

67
export interface INgModule {
78
imports: string[];
@@ -34,11 +35,12 @@ export class NgModule implements INgModule {
3435
}
3536
}
3637

37-
export class ModulesToMarkdown {
38+
export class ModulesToMarkdown extends CommandBase {
3839
private config = new Config();
3940
public static get commandName(): string { return 'modulesToMarkdown'; }
4041

4142
public execute() {
43+
this.checkForOpenWorkspace();
4244
const fsUtils = new FileSystemUtils();
4345
var workspaceDirectory: string = fsUtils.getWorkspaceFolder();
4446
const filenames = fsUtils.listFiles(workspaceDirectory, this.config.excludeDirectories, this.isTypescriptFile);
@@ -67,10 +69,6 @@ export class ModulesToMarkdown {
6769
fsUtils.writeFileAndOpen(path.join(workspaceDirectory, this.config.modulesToMarkdownFilename), markdownContent);
6870
}
6971

70-
private isTypescriptFile(filename: string): boolean {
71-
return filename.endsWith('.ts') && !filename.endsWith('index.ts');
72-
}
73-
7472
private readModule(filename: string, errors: string[]): NgModule | undefined {
7573
const fileContents = fs.readFileSync(filename);
7674
const regex: RegExp = /@NgModule\s*\(\s*(\{.+?\})\s*\)\s*export\s+class\s+(\w+)\s*\{/ims;

src/commands/packageJsonToMarkdown.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ import * as fs from 'fs';
22
import * as path from 'path';
33

44
import { ArrayUtils, Config, FileSystemUtils } from '@src';
5+
import { CommandBase } from '@commands';
56

67
const fetch = require('npm-registry-fetch');
78

8-
export class PackageJsonToMarkdown {
9+
export class PackageJsonToMarkdown extends CommandBase {
910
private config = new Config();
1011
public static get commandName(): string { return 'packageJsonToMarkdown'; }
1112

1213
public execute() {
14+
this.checkForOpenWorkspace();
1315
const fsUtils = new FileSystemUtils();
1416
const directoryPath: string = fsUtils.getWorkspaceFolder();
1517
const isPackageJson = (filename: string): boolean => filename.toLowerCase().endsWith('package.json');

src/commands/projectDirectoryStructure.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { Config, FileSystemUtils } from '@src';
2+
import { CommandBase } from '@commands';
23
import * as vscode from 'vscode';
34

4-
export class ProjectDirectoryStructure {
5+
export class ProjectDirectoryStructure extends CommandBase {
56
private config = new Config();
67
public static get commandName(): string { return 'projectDirectoryStructure'; }
78

89
public execute() {
10+
this.checkForOpenWorkspace();
911
const fsUtils = new FileSystemUtils();
1012
var workspaceDirectory: string = fsUtils.getWorkspaceFolder();
1113
const directories: string[] = fsUtils.listDirectories(workspaceDirectory, this.config.excludeDirectories);

src/commands/showComponentHierarchy.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { CommandBase } from '@commands';
12
import { Component, ComponentManager, Config, FileSystemUtils } from '@src';
23
import * as fs from 'fs';
34
import { Base64 } from 'js-base64';
@@ -34,18 +35,19 @@ class Edge {
3435
}
3536
}
3637

37-
export class ShowComponentHierarchy {
38+
export class ShowComponentHierarchy extends CommandBase {
3839
private config = new Config();
3940
private extensionContext: vscode.ExtensionContext;
4041
private fsUtils = new FileSystemUtils();
4142
private static readonly Name = 'showComponentHierarchy';
4243
constructor(context: vscode.ExtensionContext) {
44+
super();
4345
this.extensionContext = context;
4446
}
4547
public static get commandName(): string { return ShowComponentHierarchy.Name; }
4648

4749
public execute(webview: vscode.Webview) {
48-
50+
this.checkForOpenWorkspace();
4951
webview.onDidReceiveMessage(
5052
message => {
5153
switch (message.command) {

0 commit comments

Comments
 (0)