Skip to content

Commit

Permalink
fix: allow to import the array commands
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenngoclongdev authored Oct 14, 2024
1 parent f41eb01 commit bfe1708
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 38 deletions.
5 changes: 5 additions & 0 deletions .changeset/famous-ants-agree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"terminal-keeper": patch
---

fix: allow to import the array commands
4 changes: 2 additions & 2 deletions package-lock.json

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

13 changes: 6 additions & 7 deletions src/commands/importAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ const getGlobFiles = (fileType: ImportFileType): Array<string> | undefined => {
}
};

const getCommands = async (fileType: ImportFileType, filePath: string): Promise<Record<string, string> | undefined> => {
const getCommands = async (
fileType: ImportFileType,
filePath: string
): Promise<Record<string, string[]> | undefined> => {
switch (fileType) {
case 'npm':
return extractJsonScriptCommands(filePath);
Expand Down Expand Up @@ -200,12 +203,8 @@ export const importAsync = async (fileType: ImportFileType): Promise<void> => {

// Parse to terminal item from scripts
const cwd = path.dirname(selectedFilePath);
const terminalItems = Object.entries(scripts).map(([key, value]) => {
const item: TerminalItem = {
name: key,
cwd,
commands: [value]
};
const terminalItems = Object.entries(scripts).map(([name, commands]) => {
const item: TerminalItem = { name, cwd, commands };
return item;
});

Expand Down
9 changes: 5 additions & 4 deletions src/commands/modules/antParse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,24 @@ const getCommand = (): string => {
return ant;
};

const buildCommands = async (contents: string): Promise<Record<string, string>> => {
const scripts: Record<string, string> = {};
const buildCommands = async (contents: string): Promise<Record<string, string[]>> => {
const scripts: Record<string, string[]> = {};
const cmd = getCommand();
const text = await parseStringPromise(contents);
if (text && text.project && text.project.target) {
const defaultTask = text.project.$.default;
const targets = text.project.target;
for (const tgt of targets) {
if (tgt.$ && tgt.$.name) {
scripts[defaultTask === tgt.$.name ? tgt.$.name + ' - Default' : tgt.$.name] = `${cmd} ${tgt.$.name}`;
const name = defaultTask === tgt.$.name ? tgt.$.name + ' - Default' : tgt.$.name;
scripts[name] = [`${cmd} ${tgt.$.name}`];
}
}
}
return scripts;
};

export const extractAntCommands = async (filePath: string): Promise<Record<string, string> | undefined> => {
export const extractAntCommands = async (filePath: string): Promise<Record<string, string[]> | undefined> => {
const content = await getFileContent(filePath);
return await buildCommands(content);
};
8 changes: 4 additions & 4 deletions src/commands/modules/gradleParse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ const getCommand = (): string => {
return gradle;
};

const buildCommands = (contents: string): Record<string, string> => {
const scripts: Record<string, string> = {};
const buildCommands = (contents: string): Record<string, string[]> => {
const scripts: Record<string, string[]> = {};
const cmd = getCommand();
let idx = 0;
let eol = contents.indexOf('\n', 0);
Expand All @@ -31,7 +31,7 @@ const buildCommands = (contents: string): Record<string, string> => {
if (idx2 !== -1) {
const tgtName = line.substring(idx1, idx2).trim();
if (tgtName) {
scripts[tgtName] = `${cmd} ${tgtName}`;
scripts[tgtName] = [`${cmd} ${tgtName}`];
}
}
}
Expand All @@ -42,7 +42,7 @@ const buildCommands = (contents: string): Record<string, string> => {
return scripts;
};

export const extractGradleCommands = async (filePath: string): Promise<Record<string, string> | undefined> => {
export const extractGradleCommands = async (filePath: string): Promise<Record<string, string[]> | undefined> => {
const content = await getFileContent(filePath);
return buildCommands(content);
};
8 changes: 4 additions & 4 deletions src/commands/modules/gruntParse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ const getCommand = (): string => {
return 'npm grunt';
};

const buildCommands = (contents: string): Record<string, string> => {
const scripts: Record<string, string> = {};
const buildCommands = (contents: string): Record<string, string[]> => {
const scripts: Record<string, string[]> = {};
const cmd = getCommand();
let idx = 0;
let eol = contents.indexOf('\n', 0);
Expand Down Expand Up @@ -47,7 +47,7 @@ const buildCommands = (contents: string): Record<string, string> => {
if (idx2 !== -1) {
const tgtName = line.substring(idx1, idx2).trim();
if (tgtName) {
scripts[tgtName] = `${cmd} ${tgtName}`;
scripts[tgtName] = [`${cmd} ${tgtName}`];
}
}
}
Expand All @@ -59,7 +59,7 @@ const buildCommands = (contents: string): Record<string, string> => {
return scripts;
};

export const extractGruntCommands = async (filePath: string): Promise<Record<string, string> | undefined> => {
export const extractGruntCommands = async (filePath: string): Promise<Record<string, string[]> | undefined> => {
const content = await getFileContent(filePath);
return buildCommands(content);
};
12 changes: 6 additions & 6 deletions src/commands/modules/gulpParse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const parseGulpExport = (line: string) => {
let idx1: number, idx2: number;
let tgtName: string | undefined;

if (line.toLowerCase().trimLeft().startsWith('exports.')) {
if (line.toLowerCase().trimStart().startsWith('exports.')) {
idx1 = line.indexOf('.') + 1;
idx2 = line.indexOf(' ', idx1);
/* istanbul ignore if */
Expand All @@ -24,7 +24,7 @@ const parseGulpExport = (line: string) => {
if (idx1 !== -1) {
tgtName = line.substring(idx1, idx2).trim();
}
} else if (line.toLowerCase().trimLeft().startsWith('exports[')) {
} else if (line.toLowerCase().trimStart().startsWith('exports[')) {
/* istanbul ignore else */
idx1 = line.indexOf('[') + 2; // skip past [ and '/"
idx2 = line.indexOf(']', idx1) - 1; // move up to "/'
Expand Down Expand Up @@ -80,8 +80,8 @@ const parseGulpTask = (line: string, contents: string, eol: number) => {
return tgtName;
};

const buildCommands = (contents: string): Record<string, string> => {
const scripts: Record<string, string> = {};
const buildCommands = (contents: string): Record<string, string[]> => {
const scripts: Record<string, string[]> = {};
const cmd = getCommand();
let idx = 0;
let eol = contents.indexOf('\n', 0);
Expand All @@ -96,7 +96,7 @@ const buildCommands = (contents: string): Record<string, string> => {
tgtName = parseGulpTask(line, contents, eol);
}
if (tgtName) {
scripts[tgtName] = `${cmd} ${tgtName}`;
scripts[tgtName] = [`${cmd} ${tgtName}`];
}
}
idx = eol + 1;
Expand All @@ -105,7 +105,7 @@ const buildCommands = (contents: string): Record<string, string> => {
return scripts;
};

export const extractGulpCommands = async (filePath: string): Promise<Record<string, string> | undefined> => {
export const extractGulpCommands = async (filePath: string): Promise<Record<string, string[]> | undefined> => {
const content = await getFileContent(filePath);
return buildCommands(content);
};
9 changes: 6 additions & 3 deletions src/commands/modules/jsonScriptParse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ const getFileContent = async (filePath: string): Promise<string> => {
return await fs.readFileAsync(filePath);
};

const buildCommands = (contents: string): Record<string, string> => {
const buildCommands = (contents: string): Record<string, string[]> => {
const scripts: Record<string, string[]> = {};
const packageJson = JSON.parse(contents);
const { scripts } = packageJson || {};
Object.entries(packageJson?.scripts ?? {}).forEach(([name, command]) => {
scripts[name] = Array.isArray(command) ? command : [command];
});
return scripts;
};

export const extractJsonScriptCommands = async (filePath: string): Promise<Record<string, string> | undefined> => {
export const extractJsonScriptCommands = async (filePath: string): Promise<Record<string, string[]> | undefined> => {
const content = await getFileContent(filePath);
return buildCommands(content);
};
8 changes: 4 additions & 4 deletions src/commands/modules/makeParse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ const getCommand = (): string => {
return make;
};

const buildCommands = (contents: string): Record<string, string> => {
const scripts: Record<string, string> = {};
const buildCommands = (contents: string): Record<string, string[]> => {
const scripts: Record<string, string[]> = {};
const cmd = getCommand();
let match;
while ((match = ruleTargetExp.exec(contents))) {
Expand All @@ -59,13 +59,13 @@ const buildCommands = (contents: string): Record<string, string> => {
continue;
}
if (isNormalTarget(tgtName)) {
scripts[tgtName] = `${cmd} ${tgtName}`;
scripts[tgtName] = [`${cmd} ${tgtName}`];
}
}
return scripts;
};

export const extractMakeCommands = async (filePath: string): Promise<Record<string, string> | undefined> => {
export const extractMakeCommands = async (filePath: string): Promise<Record<string, string[]> | undefined> => {
const content = await getFileContent(filePath);
return buildCommands(content);
};
8 changes: 4 additions & 4 deletions src/commands/modules/pipenvParse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ const getCommand = (): string => {
return gradle;
};

const buildCommands = (contents: string): Record<string, string> => {
const scripts: Record<string, string> = {};
const buildCommands = (contents: string): Record<string, string[]> => {
const scripts: Record<string, string[]> = {};
const cmd = getCommand();
const pipfile = new TomlReader();
pipfile.readToml(contents);
Object.entries(pipfile.result?.scripts ?? {}).forEach(([scriptName, _scriptCmd]) => {
scripts[scriptName] = `${cmd} ${scriptName}`;
scripts[scriptName] = [`${cmd} ${scriptName}`];
});
return scripts;
};

export const extractPipenvCommands = async (filePath: string): Promise<Record<string, string> | undefined> => {
export const extractPipenvCommands = async (filePath: string): Promise<Record<string, string[]> | undefined> => {
const content = await getFileContent(filePath);
return buildCommands(content);
};

0 comments on commit bfe1708

Please sign in to comment.