Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to new command api #2161

Merged
merged 4 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"source.fixAll.eslint": true
},
"editor.formatOnSave": true,
"deno.enablePaths": [".cicada"],
"deno.enablePaths": ["migrate.ts"],
"deno.enable": true
}
30 changes: 30 additions & 0 deletions migrate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { walkSync } from "https://deno.land/std/fs/walk.ts";
import { split } from "npm:shlex";

const walk = walkSync("src");

for (const entry of walk) {
// console.log(entry.path);

if (entry.isFile) {
const file = Deno.readTextFileSync(`${entry.path}`);

const re = /script:\s"([^"]*)"/g;
const newFile = file.replaceAll(re, (match) => {
const a = re.exec(match);
if (a && a[1]) {
console.log(a[1]);
console.log(split(a[1]));
if (confirm("abc")) {
return `script: ${JSON.stringify(split(a[1]))}`;
} else {
return match;
}
} else {
return match;
}
});

Deno.writeTextFileSync(entry.path, newFile);
}
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"@typescript-eslint/parser": "^6.7.3",
"@vitejs/plugin-react": "^4.1.0",
"@withfig/autocomplete-tools": "^2.7.10",
"@withfig/autocomplete-types": "^1.28.0",
"@withfig/autocomplete-types": "^1.29.0",
"autoprefixer": "^10.4.16",
"chalk": "^5.3.0",
"chokidar": "^3.5.3",
Expand All @@ -96,7 +96,7 @@
"vite-plugin-externals": "^0.6.2"
},
"dependencies": {
"@fig/autocomplete-generators": "^2.2.5",
"@fig/autocomplete-generators": "^2.3.0",
"@fig/autocomplete-helpers": "^1.0.7",
"@fig/autocomplete-hooks": "^1.0.2",
"@withfig/api-bindings": "^0.30.3",
Expand Down
7 changes: 6 additions & 1 deletion src/@usermn/sdc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ const versionFiles = ["0.0.0"];
export const getVersionCommand: Fig.GetVersionCommand = async (
executeShellCommand
) => {
return await executeShellCommand("npx @usermn/sdc --version");
return (
await executeShellCommand({
command: "npx",
args: ["@usermn/sdc", "--version"],
})
).stdout;
};
export default createVersionedSpec("@usermn/sdc", versionFiles);
2 changes: 1 addition & 1 deletion src/amplify.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const envNameGenerator: Fig.Generator = {
script: "amplify env list --json",
script: ["amplify", "env", "list", "--json"],
postProcess: function (out) {
const envContent = JSON.parse(out);
return envContent["envs"].map((env: string) => {
Expand Down
2 changes: 1 addition & 1 deletion src/ansible-doc.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const allPluginsGenerator: Fig.Generator = {
script: "ansible-doc --list --json 2>/dev/null",
script: ["ansible-doc", "--list", "--json"],
postProcess: function (output) {
const plugins = JSON.parse(output);
return Object.keys(plugins).map((key) => ({
Expand Down
2 changes: 1 addition & 1 deletion src/ant.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const tasksGenerator: Fig.Generator = {
script: "command ant -p | grep -i '^\\s' | tr -d ' '",
script: ["bash", "-c", "command ant -p | grep -i '^\\s' | tr -d ' '"],
postProcess: (out) =>
out.split("\n").map((task) => ({
name: task,
Expand Down
17 changes: 11 additions & 6 deletions src/apt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@ const packages: Fig.Generator = {
if (finalToken.length === 0) {
return [];
}
const { stdout } = await executeShellCommand({
command: "apt",
// eslint-disable-next-line @withfig/fig-linter/no-useless-arrays
args: ["list"],
});

// Only lines matching the first character, delete characters after '/'
const out = await executeShellCommand(
`apt list | grep '^${finalToken[0]}' | sed 's#/.*##g'`
);
return out
return stdout
.trim()
.split("\n")
.filter((name) => name.startsWith(finalToken))
.map((name) => name.replace(/\/.*/, ""))
.map((name) => ({
name,
description: "Package",
Expand All @@ -30,7 +35,7 @@ const packages: Fig.Generator = {
};

const installedPackages: Fig.Generator = {
script: "apt list --installed",
script: ["apt", "list", "--installed"],
postProcess: function (a) {
return a
.trim()
Expand All @@ -46,7 +51,7 @@ const installedPackages: Fig.Generator = {
};

const upgradablePackages: Fig.Generator = {
script: "apt list --upgradable",
script: ["apt", "list", "--upgradable"],
postProcess: function (a) {
return a
.trim()
Expand Down
39 changes: 26 additions & 13 deletions src/asdf.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
const PRIORITY_TOP_THRESHOLD = 76;
const LS_BIN = "/bin/ls";
const ASDF_DATA_DIR = "~/.asdf";
const HOUR_IN_MILLISECONDS = 3600000;

/*
Expand All @@ -9,7 +7,7 @@ const HOUR_IN_MILLISECONDS = 3600000;
const installedPluginNamesGenerator = (
suggestOptions?: Partial<Fig.Suggestion>
): Fig.Generator => ({
script: "asdf plugin-list",
script: ["asdf", "plugin-list"],
postProcess: (output) =>
output.split("\n").map((pluginName) => ({
name: `${pluginName}`,
Expand All @@ -24,15 +22,23 @@ const allPluginNamesGenerator = (
suggestOptions?: Partial<Fig.Suggestion>
): Fig.Generator => ({
// If use `asdf plugin-list-all`, it will time out, so use `ls`.
script: `${LS_BIN} -1 ${ASDF_DATA_DIR}/repository/plugins`,
postProcess: (output) =>
output.split("\n").map((pluginName) => ({
custom: async (_, executeCommand, generatorContext) => {
const { stdout } = await executeCommand({
command: "ls",
args: [
"-1",
`${generatorContext.environmentVariables["HOME"]}/.asdf/repository/plugins`,
],
});

return stdout.split("\n").map((pluginName) => ({
name: `${pluginName}`,
description: "Plugin name",
priority: PRIORITY_TOP_THRESHOLD,
icon: "fig://icon?type=package",
...suggestOptions,
})),
}));
},
});

const installedPluginVersionsGenerator = (
Expand All @@ -41,7 +47,7 @@ const installedPluginVersionsGenerator = (
): Fig.Generator => ({
script: (context) => {
const pluginName = context[context.length - 2];
return `asdf list ${pluginName}`;
return ["asdf", "list", pluginName];
},
postProcess: (output) =>
output
Expand All @@ -63,7 +69,7 @@ const allPluginVersionsGenerator = (
): Fig.Generator => ({
script: (context) => {
const pluginName = context[context.length - 2];
return `asdf list-all ${pluginName}`;
return ["asdf", "list-all", pluginName];
},
cache: {
ttl: HOUR_IN_MILLISECONDS,
Expand All @@ -86,15 +92,22 @@ const shimNamesGenerator = (
suggestOptions?: Partial<Fig.Suggestion>
): Fig.Generator => ({
// Use `ls` because there is no command to get shims in `asdf`.
script: `${LS_BIN} -1 ${ASDF_DATA_DIR}/shims`,
postProcess: (output) =>
output.split("\n").map((shimName) => ({
custom: async (_, executeCommand, generatorContext) => {
const { stdout } = await executeCommand({
command: "ls",
args: [
"-1",
`${generatorContext.environmentVariables["HOME"]}/.asdf/shims`,
],
});
return stdout.split("\n").map((shimName) => ({
name: `${shimName}`,
description: "Shim name",
priority: PRIORITY_TOP_THRESHOLD,
icon: "fig://icon?type=command",
...suggestOptions,
})),
}));
},
});

/*
Expand Down
4 changes: 2 additions & 2 deletions src/assimp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const commonOptions: Fig.Option[] = [
];

const importExtGenerator: Fig.Generator = {
script: "assimp listext",
script: ["assimp", "listext"],
postProcess: function (out) {
return out.split(";").map((ext) => {
return {
Expand All @@ -33,7 +33,7 @@ const importExtGenerator: Fig.Generator = {
};

const exportExtGenerator: Fig.Generator = {
script: "assimp listexport",
script: ["assimp", "listexport"],
postProcess: function (out) {
return out.split("\n").map((ext) => {
return {
Expand Down
2 changes: 1 addition & 1 deletion src/aws-vault.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const profilesGenerator: Fig.Generator = {
script: "aws-vault list --profiles",
script: ["aws-vault", "list", "--profiles"],
postProcess(out) {
return out.split("\n").map((name) => ({ name }));
},
Expand Down
11 changes: 6 additions & 5 deletions src/aws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export const awsProfileGenerator: Fig.Generator = {
strategy: "stale-while-revalidate",
cacheByDirectory: true,
},
script: "aws configure list-profiles",
script: ["aws", "configure", "list-profiles"],
postProcess: function (out) {
if (out.trim() == "") {
return [];
Expand All @@ -19,10 +19,11 @@ export const awsProfileGenerator: Fig.Generator = {
const completionSpec: Fig.Spec = {
name: "aws",
async generateSpec(_, executeShellCommand) {
const check = await executeShellCommand(
"ls ~/.aws/credentials && ls ~/.aws/config"
);
const prioritize = check.includes("No such file or directory");
const { stdout } = await executeShellCommand({
command: "bash",
args: ["-c", "ls ~/.aws/credentials && ls ~/.aws/config"],
});
const prioritize = stdout.includes("No such file or directory");
return {
name: "aws",
subcommands: [
Expand Down
12 changes: 6 additions & 6 deletions src/aws/acm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ const postPrecessGenerator = (
const _prefixFile = "file://";
const _prefixBlob = "fileb://";

const appendFolderPath = (tokens: string[], prefix: string): string => {
const baseLSCommand = "\\ls -1ApL ";
const appendFolderPath = (tokens: string[], prefix: string): string[] => {
const baseLsCommand = ["ls", "-1ApL"];
let whatHasUserTyped = tokens[tokens.length - 1];

if (!whatHasUserTyped.startsWith(prefix)) {
return `echo '${prefix}'`;
return ["echo", prefix];
}
whatHasUserTyped = whatHasUserTyped.slice(prefix.length);

Expand All @@ -51,7 +51,7 @@ const appendFolderPath = (tokens: string[], prefix: string): string => {
}
}

return baseLSCommand + folderPath;
return [...baseLsCommand, folderPath];
};

const postProcessFiles = (out: string, prefix: string): Fig.Suggestion[] => {
Expand Down Expand Up @@ -157,7 +157,7 @@ const generators: Record<string, Fig.Generator> = {
},

listCertificates: {
script: "aws acm list-certificates",
script: ["aws", "acm", "list-certificates"],
postProcess: (out) => {
return postPrecessGenerator(
out,
Expand All @@ -168,7 +168,7 @@ const generators: Record<string, Fig.Generator> = {
},

listCertificateAuthorities: {
script: "aws acm-pca list-certificate-authorities",
script: ["aws", "acm-pca", "list-certificate-authorities"],
postProcess: (out) => {
return postPrecessGenerator(out, "CertificateAuthorities", "Arn");
},
Expand Down
Loading