Skip to content

Commit

Permalink
fix: using glob to find the import file
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenngoclongdev authored Oct 9, 2024
1 parent 8945265 commit ed29746
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/twelve-suits-bathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"terminal-keeper": patch
---

fix: using glob to find the import file
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"gitlens.hovers.currentLine.over": "line",
"gitlens.codeLens.enabled": false,
"cSpell.words": [
"akefile",
"bombadil",
"callhierarchy",
"gradlew",
Expand All @@ -36,13 +37,15 @@
"nmake",
"nodir",
"NOTPARALLEL",
"NSIS",
"ONESHELL",
"outdir",
"outfile",
"pipenv",
"pipfile",
"posttest",
"pree",
"publishrc",
"registertask",
"SECONDEXPANSION",
"sgarciac",
Expand Down
46 changes: 29 additions & 17 deletions src/commands/importAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,37 @@ import { extractPipenvCommands } from './modules/pipenvParse';

export type ImportFileType = 'npm' | 'composer' | 'make' | 'gradle' | 'pipenv' | 'ant' | 'grunt' | 'gulp';

const getFilenames = (fileType: ImportFileType): Array<string> | undefined => {
const getGlobFiles = (fileType: ImportFileType): Array<string> | undefined => {
switch (fileType) {
case 'npm':
return ['package.json'];
return ['**/package.json'];
case 'composer':
return ['composer.json'];
return ['**/composer.json'];
case 'make':
return ['Makefile', 'makefile'];
return ['**/[Mm]akefile'];
case 'gradle':
return ['build.gradle', 'test.gradle'];
return ['**/*.[Gg][Rr][Aa][Dd][Ll][Ee]'];
case 'pipenv':
return ['Pipfile', 'pipfile'];
return ['**/[Pp][Ii][Pp][Ff][Ii][Ll][Ee]'];
case 'ant':
return ['build.xml'];
return ['**/[Bb][Uu][Ii][Ll][Dd].[Xx][Mm][Ll]'];
case 'grunt':
return ['GRUNTFILE.JS'];
return ['**/[Gg][Rr][Uu][Nn][Tt][Ff][Ii][Ll][Ee].[Jj][Ss]'];
case 'gulp':
return ['GULPFILE.js', 'GULPFILE.mjs', 'gulpfile.js', 'gulpfile.mjs'];
return [
'**/[Gg][Uu][Ll][Pp][Ff][Ii][Ll][Ee].{[Jj][Ss],[Tt][Ss],[Mm][Jj][Ss],[Bb][Aa][Bb][Ee][Ll].[Jj][Ss]}'
];
default:
// AppPublisher: **/.publishrc*
// Maven: **/pom.xml
// Bash: **/*.[Ss][Hh]
// Batch: **/*.{[Bb][Aa][Tt],[Cc][Mm][Dd]}
// NSIS: **/*.[Nn][Ss][Ii]
// Perl: **/*.[Pp][Ll]
// PowerShell: **/*.[Pp][Ss]1
// Python: **/*.[Pp][Yy]
// Ruby: **/*.rb
// tsc: **/tsconfig.{json,*.json}
return undefined;
}
};
Expand Down Expand Up @@ -62,13 +74,13 @@ const getCommands = async (fileType: ImportFileType, filePath: string): Promise<
}
};

const getFilePaths = async (workspaceFolders: readonly WorkspaceFolder[], filenames: string[]): Promise<string[]> => {
const getFilePaths = async (workspaceFolders: readonly WorkspaceFolder[], globFiles: string[]): Promise<string[]> => {
const filePaths: string[] = [];
for (let i = 0; i < workspaceFolders.length; i++) {
const wsFolder = workspaceFolders[i];
for (let j = 0; j < filenames.length; j++) {
const filename = filenames[j];
const files = await glob(`**/${filename}`, {
for (let j = 0; j < globFiles.length; j++) {
const globFile = globFiles[j];
const files = await glob(globFile, {
cwd: wsFolder.uri.fsPath,
nodir: true,
absolute: true,
Expand Down Expand Up @@ -155,18 +167,18 @@ export const importAsync = async (fileType: ImportFileType): Promise<void> => {
}

// Get filename by fileType
const filenames = getFilenames(fileType);
if (!filenames || filenames.length <= 0) {
const globFiles = getGlobFiles(fileType);
if (!globFiles || globFiles.length <= 0) {
window.showWarningMessage(constants.notSupportFileType.replace('{fileType}', fileType));
return;
}

// Get all filepaths
const filePaths = await getFilePaths(workspaceFolders, filenames);
const filePaths = await getFilePaths(workspaceFolders, globFiles);
if (!filePaths || filePaths.length <= 0) {
window.showWarningMessage(
constants.notExistImportFile
.replace('{filename}', filenames.join(', '))
.replace('{fileType}', fileType)
.replace('{workspace}', workspaceFolders.map((w) => w.uri.fsPath).join(', '))
);
return;
Expand Down
2 changes: 1 addition & 1 deletion src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export const constants = {
// Import commands
importFileFailed: 'Failed to import the command from file type {fileType}.',
notSupportFileType: 'The file type {fileType} is not supported!',
notExistImportFile: 'The file {filename} does not exist in any workspace {workspace}.',
notExistImportFile: 'The file type {fileType} does not exist in any workspace {workspace}.',
notExistAnyCommands: 'There are no definition commands in the {filePath}.',

// Migrate the configuration file
Expand Down

0 comments on commit ed29746

Please sign in to comment.