diff --git a/.prettierrc b/.prettierrc index 9b0bb43..c7100c7 100644 --- a/.prettierrc +++ b/.prettierrc @@ -1,4 +1,5 @@ { "tabWidth": 4, - "singleQuote": true + "singleQuote": true, + "printWidth": 120 } diff --git a/package.json b/package.json index 104d6b5..cd0029e 100644 --- a/package.json +++ b/package.json @@ -72,6 +72,7 @@ "semantic-release": "^15.13.24", "semantic-release-vsce": "^2.2.8", "ts-loader": "^6.1.0", + "tslint-plugin-prettier": "^2.0.1", "typescript": "^3.3.1", "vscode": "^1.1.36", "vscode-nls-dev": "^3.3.1", @@ -102,10 +103,8 @@ "items": { "type": "string" }, - "default": [ - "~/**/*" - ], - "description": "Paths of directories when `.code-workspace` files can be saved and then read from. You do not need to use `**/*` at the end." + "default": [], + "description": "Paths of directories where `.code-workspace` files can be saved and then read from. You do not need to use `**/*` at the end." }, "workspace-manager.openInNewWindowWhenClickingInStatusBar": { "type": "boolean", diff --git a/src/cache/cacheWorkspaces.ts b/src/cache/cacheWorkspaces.ts index ead06c5..dab1660 100644 --- a/src/cache/cacheWorkspaces.ts +++ b/src/cache/cacheWorkspaces.ts @@ -17,15 +17,9 @@ export class CacheWorkspaces { return; } } catch (err) { - statusBarCache.notify( - 'alert', - 'Failed to cache the workspace entries (click for another attempt)' - ); + statusBarCache.notify('alert', 'Failed to cache the workspace entries (click for another attempt)'); } finally { - statusBarCache.notify( - 'sync', - 'Workspace entries cached (click to cache again)' - ); + statusBarCache.notify('sync', 'Workspace entries cached (click to cache again)'); statusBarWorkspace.notify(); diff --git a/src/util/getWorkspaces.ts b/src/util/getWorkspaces.ts index 9d6fc93..20eca19 100644 --- a/src/util/getWorkspaces.ts +++ b/src/util/getWorkspaces.ts @@ -1,42 +1,26 @@ +import { Commands } from './../commands/common'; import * as glob from 'fast-glob'; import { join } from 'path'; import * as VError from 'verror'; import * as vscode from 'vscode'; import { Cache } from '../cache/cache'; import { configuration } from '../configuration'; -import { - CommandContext, - extensionId, - extensionOutputChannelName, - setCommandContext -} from '../constants'; +import { CommandContext, extensionId, extensionOutputChannelName, setCommandContext } from '../constants'; import { Logger } from '../logger'; import { IWorkspace, Workspace } from '../model/workspace'; import { Container } from './../container'; import { getWorkspacesDirectories } from './getWorkspacesDirectories'; import { statusBarCache } from './statusBar/cache'; -export async function getWorkspaces( - fromCache: boolean = true -): Promise { +export async function getWorkspaces(fromCache: boolean = true): Promise { const cache = new Cache(extensionId); const cacheKey = 'workspaces'; - const cachedWorkspaces: IWorkspace[] | undefined = cache.get( - cacheKey, - [] - ); + const cachedWorkspaces: IWorkspace[] | undefined = cache.get(cacheKey, []); - if ( - fromCache && - cachedWorkspaces && - cachedWorkspaces.length && - !cache.isExpired(cacheKey) - ) { + if (fromCache && cachedWorkspaces && cachedWorkspaces.length && !cache.isExpired(cacheKey)) { setCommandContext(CommandContext.WorkspacesEmpty, false); - return await cachedWorkspaces.map( - workspace => new Workspace(workspace) - ); + return cachedWorkspaces.map(workspace => new Workspace(workspace)); } const excludeGlobPattern: string[] = configuration.get( @@ -44,13 +28,43 @@ export async function getWorkspaces( null, [] ); - const deep: number = configuration.get( - configuration.name('advanced')('deep').value, - null, - 5 - ); + const deep: number = configuration.get(configuration.name('advanced')('deep').value, null, 5); const directoryPaths = getWorkspacesDirectories(); + if (!directoryPaths.length) { + // no directories set up + const actions: vscode.MessageItem[] = [{ title: 'Setup Paths' }]; + + const result = await vscode.window.showInformationMessage( + "Workspace Manager doesn't have set up paths of directories where `.code-workspace` can be saved and then read from? Without that, the extension can't search for workspace configuration files.", + ...actions + ); + + if (result != null) { + if (result === actions[0]) { + const userHome = process.env[process.platform == 'win32' ? 'USERPROFILE' : 'HOME'] || '~'; + + const paths = await vscode.window.showInputBox({ + placeHolder: `${userHome}/.vscode/, ${userHome}/projects/`, + value: undefined, + prompt: + 'Paths of directories where `.code-workspace` files can be saved and then read from. You do not need to use `**/*` at the end. Use comma-separated directories. Try to not use root directories with too many files.' + }); + + if (paths && paths.length) { + await configuration.updateEffective( + 'includeGlobPattern', + paths.split(',').map(path => path.trim()) + ); + + setTimeout(() => vscode.commands.executeCommand(Commands.CacheWorkspace), 500); + } + } + } + + return; + } + const directories = directoryPaths .map(dir => join(dir, '**/*.code-workspace')) .filter((path, index, arr) => arr.indexOf(path) == index); @@ -61,9 +75,7 @@ export async function getWorkspaces( const addPath = async (path: string) => { if (path) { - const workspaceConfiguration = await configuration.getWorkspaceConfiguration( - path - ); + const workspaceConfiguration = await configuration.getWorkspaceConfiguration(path); if (workspaceConfiguration) { const rootPath = workspaceConfiguration.folders[0].path; @@ -85,11 +97,7 @@ export async function getWorkspaces( filesParsed++; - statusBarCache.notify( - 'eye', - `Looking for workspaces... [${filesParsed}]`, - false - ); + statusBarCache.notify('eye', `Looking for workspaces... [${filesParsed}]`, false); } } }; @@ -102,11 +110,7 @@ export async function getWorkspaces( }, async progress => { try { - statusBarCache.notify( - 'eye', - 'Looking for workspaces...', - false - ); + statusBarCache.notify('eye', 'Looking for workspaces...', false); const stream = glob.stream(directories, { ignore: ['**/node_modules/**', ...excludeGlobPattern], @@ -114,75 +118,41 @@ export async function getWorkspaces( onlyFiles: true }); - const onEnd = () => { - Logger.info( - `All the workspece files in the directories [${directories.join( - ', ' - )}] has been found [${workspaces.length}]` + timeoutId = setTimeout(() => { + stream.pause(); + Logger.info('Reading stream has been paused after 10s.'); + vscode.window.showInformationMessage( + `${extensionOutputChannelName}\nReading stream has been paused after 10s.` ); + }, 10000); - workspaces.sort((a, b) => a.name.localeCompare(b.name)); + for await (const entry of stream) { + if (entry && typeof entry === 'string') { + addPath(entry); + } + } - cache.update(cacheKey, workspaces); + Logger.info( + `All the workspece files in the directories [${directories.join(', ')}] has been found [${ + workspaces.length + }]` + ); - if (timeoutId) { - clearTimeout(timeoutId); - } + workspaces.sort((a, b) => a.name.localeCompare(b.name)); - statusBarCache.notify( - 'sync', - 'Workspaces cached (click to cache again)' - ); + cache.update(cacheKey, workspaces); - setCommandContext( - CommandContext.WorkspacesEmpty, - !!!workspaces.length - ); + if (timeoutId) { + clearTimeout(timeoutId); + } - Container.refreshViews(); + statusBarCache.notify('sync', 'Workspaces cached (click to cache again)'); - return workspaces.map( - workspace => new Workspace(workspace) - ); - }; + setCommandContext(CommandContext.WorkspacesEmpty, !!!workspaces.length); - stream - .on('data', (path: string) => { - statusBarCache.notify( - 'eye', - `Looking for workspaces... [${workspaces.length}]`, - false - ); - - progress.report({ - message: `Looking for workspaces... [${workspaces.length}]` - }); - - addPath(path); - }) - .on('error', err => { - err = new VError( - err, - '${extensionOutputChannelName}\nReading stream error' - ); - vscode.window.showInformationMessage(err); - throw err; - }) - .on('pause', onEnd) - .on('end', onEnd) - .on('close', () => { - Logger.info( - `Stream has been destroyed and file has been closed` - ); - }); + Container.refreshViews(); - timeoutId = setTimeout(() => { - stream.pause(); - Logger.info('Reading stream has been paused after 10s.'); - vscode.window.showInformationMessage( - `${extensionOutputChannelName}\nReading stream has been paused after 10s.` - ); - }, 10000); + return workspaces.map(workspace => new Workspace(workspace)); } catch (err) { err = new VError(err, 'Reading stream has been destroyed'); throw err;