Skip to content

Commit

Permalink
fix(package): remove default include directories (#39)
Browse files Browse the repository at this point in the history
Disable workspaces search on extension install. Remove default Include Glob Pattern. Add prompt for update Include Glob Pattern if not set up.

Closes #39
  • Loading branch information
tomandco-rafalwolak committed Nov 26, 2019
1 parent cf45342 commit e675e22
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 112 deletions.
3 changes: 2 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"tabWidth": 4,
"singleQuote": true
"singleQuote": true,
"printWidth": 120
}
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
10 changes: 2 additions & 8 deletions src/cache/cacheWorkspaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
168 changes: 69 additions & 99 deletions src/util/getWorkspaces.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,70 @@
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<Workspace[] | undefined> {
export async function getWorkspaces(fromCache: boolean = true): Promise<Workspace[] | undefined> {
const cache = new Cache(extensionId);
const cacheKey = 'workspaces';
const cachedWorkspaces: IWorkspace[] | undefined = cache.get<IWorkspace[]>(
cacheKey,
[]
);
const cachedWorkspaces: IWorkspace[] | undefined = cache.get<IWorkspace[]>(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<string[]>(
configuration.name('advanced')('excludeGlobPattern').value,
null,
[]
);
const deep: number = configuration.get<number>(
configuration.name('advanced')('deep').value,
null,
5
);
const deep: number = configuration.get<number>(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);
Expand All @@ -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;
Expand All @@ -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);
}
}
};
Expand All @@ -102,87 +110,49 @@ 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],
deep: deep,
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;
Expand Down

0 comments on commit e675e22

Please sign in to comment.