Find all books in workspace with multiple folders (#6613)

* find all books in workspace with multiple flolders

* fixed optional parameter
This commit is contained in:
Lucy Zhang
2019-08-06 10:20:57 -07:00
committed by GitHub
parent c3b7485e3f
commit a0163c8625
2 changed files with 23 additions and 22 deletions

View File

@@ -24,27 +24,29 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
private _throttleTimer: any;
private _resource: string;
constructor(private workspaceRoot: string, extensionContext: vscode.ExtensionContext) {
if (workspaceRoot !== '') {
this._tableOfContentsPath = this.getTocFiles(this.workspaceRoot);
let bookOpened: boolean = this._tableOfContentsPath && this._tableOfContentsPath.length > 0;
vscode.commands.executeCommand('setContext', 'bookOpened', bookOpened);
}
constructor(workspaceFolders: vscode.WorkspaceFolder[], extensionContext: vscode.ExtensionContext) {
let workspacePaths: string[] = workspaceFolders.map(a => a.uri.fsPath);
this._tableOfContentsPath = this.getTableOfContentFiles(workspacePaths);
let bookOpened: boolean = this._tableOfContentsPath && this._tableOfContentsPath.length > 0;
vscode.commands.executeCommand('setContext', 'bookOpened', bookOpened);
this._extensionContext = extensionContext;
}
private getTocFiles(dir: string): string[] {
let allFiles: string[] = [];
let files = fs.readdirSync(dir);
for (let i in files) {
let name = path.join(dir, files[i]);
if (fs.statSync(name).isDirectory()) {
allFiles = allFiles.concat(this.getTocFiles(name));
} else if (files[i] === 'toc.yml') {
allFiles.push(name);
}
}
return allFiles;
private getTableOfContentFiles(directories: string[]): string[] {
let tableOfContentPaths: string[] = [];
let paths: string[];
directories.forEach(dir => {
paths = fs.readdirSync(dir);
paths.forEach(filename => {
let fullPath = path.join(dir, filename);
if (fs.statSync(fullPath).isDirectory()) {
tableOfContentPaths = tableOfContentPaths.concat(this.getTableOfContentFiles([fullPath]));
} else if (filename === 'toc.yml') {
tableOfContentPaths.push(fullPath);
}
});
});
return tableOfContentPaths;
}
async openNotebook(resource: string): Promise<void> {
@@ -231,4 +233,4 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
return Promise.resolve(result);
}
}
}

View File

@@ -27,8 +27,7 @@ let controller: JupyterController;
type ChooseCellType = { label: string, id: CellType };
export async function activate(extensionContext: vscode.ExtensionContext): Promise<IExtensionApi> {
const bookTreeViewProvider = new BookTreeViewProvider(vscode.workspace.rootPath || '', extensionContext);
const bookTreeViewProvider = new BookTreeViewProvider(vscode.workspace.workspaceFolders || [], extensionContext);
extensionContext.subscriptions.push(vscode.window.registerTreeDataProvider('bookTreeView', bookTreeViewProvider));
extensionContext.subscriptions.push(azdata.nb.registerNavigationProvider(bookTreeViewProvider));
extensionContext.subscriptions.push(vscode.commands.registerCommand('bookTreeView.openNotebook', (resource) => bookTreeViewProvider.openNotebook(resource)));
@@ -248,4 +247,4 @@ export function deactivate() {
if (controller) {
controller.deactivate();
}
}
}