Skip to content

Commit

Permalink
feat: complete migrate to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
AdoKevin committed Jul 24, 2024
1 parent f6d3b48 commit 6e0d893
Show file tree
Hide file tree
Showing 8 changed files with 521 additions and 3 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@
"dependencies": {
"compare-versions": "^6.1.1",
"find-up": "5.0.0",
"glob": "^11.0.0",
"load-json-file": "^7.0.1",
"radash": "^12.1.0",
"read-pkg": "^5.2.0"
}
Expand Down
59 changes: 59 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

176 changes: 176 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
import vscode from 'vscode';
import fs from 'fs';
import path from 'path';
import { findParentModules } from './find-parent-modules';
import { findChildPackages } from './find-child-packages';
import { showError } from './utils';
import { sortFiles } from './sort-files';

let lastFolder = '';
let lastWorkspaceName = '';
let lastWorkspaceRoot = '';

const nodeModules = 'node_modules';

exports.activate = (context) => {

Check failure on line 15 in src/extension.ts

View workflow job for this annotation

GitHub Actions / typecheck (18.x)

Parameter 'context' implicitly has an 'any' type.
const searchNodeModules = vscode.commands.registerCommand('extension.search', () => {
const preferences = vscode.workspace.getConfiguration('search-node-modules');

const useLastFolder = preferences.get('useLastFolder', false);
const nodeModulesPath = preferences.get('path', nodeModules);
const searchParentModules = preferences.get('searchParentModules', true);
const orderPriority = preferences.get('orderPriority', []);

const searchPath = (workspaceName, workspaceRoot, folderPath) => {

Check failure on line 24 in src/extension.ts

View workflow job for this annotation

GitHub Actions / typecheck (18.x)

Parameter 'workspaceName' implicitly has an 'any' type.

Check failure on line 24 in src/extension.ts

View workflow job for this annotation

GitHub Actions / typecheck (18.x)

Parameter 'workspaceRoot' implicitly has an 'any' type.

Check failure on line 24 in src/extension.ts

View workflow job for this annotation

GitHub Actions / typecheck (18.x)

Parameter 'folderPath' implicitly has an 'any' type.
// Path to node_modules in this workspace folder
const workspaceNodeModules = path.join(workspaceName, nodeModulesPath);

// Reset last folder
lastFolder = '';
lastWorkspaceName = '';
lastWorkspaceRoot = '';

// Path to current folder
const folderFullPath = path.join(workspaceRoot, folderPath);

// Read folder, built quick pick with files/folder (and shortcuts)
fs.readdir(folderFullPath, async (readErr, files) => {
if (readErr) {
if (folderPath === nodeModulesPath) {
return showError('No node_modules folder in this workspace.');
}

return showError(`Unable to open folder ${folderPath}`);
}

const isParentFolder = folderPath.includes('..');
const options = sortFiles(files, orderPriority);

// If searching in root node_modules, also include modules from parent folders, that are outside of the workspace
if (folderPath === nodeModulesPath) {
if (searchParentModules) {
const parentModules = await findParentModules(workspaceRoot, nodeModulesPath);
options.push(...parentModules);
}
} else {
// Otherwise, show option to move back to root
options.push('');
options.push(workspaceNodeModules);

// If current folder is not outside of the workspace, also add option to move a step back
if (!isParentFolder) {
options.push('..');
}
}

vscode.window
.showQuickPick(options, {
placeHolder: path.format({ dir: workspaceName, base: folderPath }),
})
.then((selected) => {
// node_modules shortcut selected
if (selected === workspaceNodeModules) {
searchPath(workspaceName, workspaceRoot, nodeModulesPath);
} else {
const selectedPath = path.join(folderPath, selected);

Check failure on line 75 in src/extension.ts

View workflow job for this annotation

GitHub Actions / typecheck (18.x)

Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
const selectedFullPath = path.join(workspaceRoot, selectedPath);

// If selected is a folder, traverse it,
// otherwise open file.
fs.stat(selectedFullPath, (statErr, stats) => {
if (stats.isDirectory()) {
searchPath(workspaceName, workspaceRoot, selectedPath);
} else {
lastWorkspaceName = workspaceName;
lastWorkspaceRoot = workspaceRoot;
lastFolder = folderPath;

vscode.workspace
.openTextDocument(selectedFullPath, selectedPath)

Check failure on line 89 in src/extension.ts

View workflow job for this annotation

GitHub Actions / typecheck (18.x)

Expected 0-1 arguments, but got 2.
.then(vscode.window.showTextDocument);
}
});
}
});
});
};

const getProjectFolder = async (workspaceFolder) => {

Check failure on line 98 in src/extension.ts

View workflow job for this annotation

GitHub Actions / typecheck (18.x)

Parameter 'workspaceFolder' implicitly has an 'any' type.
const packages = await findChildPackages(workspaceFolder.uri.fsPath);
// If in a lerna/yarn monorepo, prompt user to select which project to traverse
if (packages.length > 0) {
const selected = await vscode.window.showQuickPick(
[
{ label: workspaceFolder.name, packageDir: '' }, // First option is the root dir
...packages.map((packageDir) => ({
label: path.join(workspaceFolder.name, packageDir),
packageDir,
})),
],
{ placeHolder: 'Select Project' }
);
if (!selected) {
return;
}

return {
name: selected.label,
path: path.join(workspaceFolder.uri.fsPath, selected.packageDir),
};
}

// Otherwise, use the root folder
return {
name: workspaceFolder.name,
path: workspaceFolder.uri.fsPath,
};
};

const getWorkspaceFolder = async () => {
// If in a multifolder workspace, prompt user to select which one to traverse.
if (vscode.workspace.workspaceFolders.length > 1) {

Check failure on line 131 in src/extension.ts

View workflow job for this annotation

GitHub Actions / typecheck (18.x)

'vscode.workspace.workspaceFolders' is possibly 'undefined'.
const selected = await vscode.window.showQuickPick(
vscode.workspace.workspaceFolders.map((folder) => ({

Check failure on line 133 in src/extension.ts

View workflow job for this annotation

GitHub Actions / typecheck (18.x)

'vscode.workspace.workspaceFolders' is possibly 'undefined'.
label: folder.name,
folder,
})),
{
placeHolder: 'Select workspace folder',
}
);

if (!selected) {
return;
}

return selected.folder;
}

// Otherwise, use the first one
const folder = vscode.workspace.workspaceFolders[0];

Check failure on line 150 in src/extension.ts

View workflow job for this annotation

GitHub Actions / typecheck (18.x)

'vscode.workspace.workspaceFolders' is possibly 'undefined'.
return folder;
};

// Open last folder if there is one
if (useLastFolder && lastFolder) {
return searchPath(lastWorkspaceName, lastWorkspaceRoot, lastFolder);
}

// Must have at least one workspace folder
if (!vscode.workspace.workspaceFolders.length) {
return showError('You must have a workspace opened.');
}

getWorkspaceFolder()
.then((folder) => folder && getProjectFolder(folder))
.then((folder) => {
if (folder) {
searchPath(folder.name, folder.path, nodeModulesPath);
}
});
});

context.subscriptions.push(searchNodeModules);
};

exports.deactivate = () => {};
Loading

0 comments on commit 6e0d893

Please sign in to comment.