Skip to content

Commit

Permalink
Merge pull request #27 from imLymei/next
Browse files Browse the repository at this point in the history
Merge with next
  • Loading branch information
imLymei authored Sep 16, 2024
2 parents 5939d80 + 690d71e commit d0fae0b
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 18 deletions.
33 changes: 30 additions & 3 deletions package-lock.json

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

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "foji",
"version": "0.4.1",
"version": "0.0.0-development",
"main": "src/index.ts",
"bin": {
"foji": "./bin/index.js",
Expand All @@ -27,13 +27,15 @@
"license": "MIT",
"description": "Forge your code in a new way.",
"devDependencies": {
"@types/fast-levenshtein": "^0.0.4",
"@types/inquirer": "^9.0.7",
"@types/node": "^22.5.1",
"semantic-release": "^24.1.1",
"typescript": "^5.5.4"
},
"dependencies": {
"@inquirer/prompts": "^5.4.0",
"commander": "^12.1.0"
"commander": "^12.1.0",
"fast-levenshtein": "^3.0.0"
}
}
2 changes: 1 addition & 1 deletion release.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
* @type {import('semantic-release').GlobalConfig}
*/
export default {
branches: ['master', 'next'],
branches: ['master', 'next', { name: 'beta', prerelease: true }],
};
22 changes: 20 additions & 2 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import removeCommand from './removeCommand';
import openConfig from './openConfig';
import uploadConfig from './uploadConfig';
import downloadConfig from './downloadConfig';
import { getConfig, logList, runUserCommand } from '../lib/utils';
import {
getClosestWord,
getConfig,
logList,
runUserCommand,
} from '../lib/utils';
import syncConfig from './syncConfig';
import renameCommand from './renameCommand';

Expand Down Expand Up @@ -51,7 +56,20 @@ const program = new Command()
const command = configCommands[commandName];

if (!command) {
console.error(`command "${commandName}" not found`);
const closestWord = getClosestWord(
commandName,
Object.keys(configCommands)
);

let suggestion: string;

if (isFinite(closestWord.distance))
suggestion = `Did you mean: "${closestWord.word}"?`;

console.error(`command "${commandName}" not found.`);

if (suggestion) console.log(suggestion);

process.exit(1);
}

Expand Down
4 changes: 0 additions & 4 deletions src/commands/syncConfig.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import { Command } from 'commander';
import {
uploadConfiguration,
login,
updateCloudConfiguration,
hasGithubCli,
isGithubLogged,
getConfiguration,
basicGithubVerifications,
} from '../lib/github';
import { confirm } from '@inquirer/prompts';
import { changeGistUrl, createConfig, getConfig } from '../lib/utils';

const syncConfig = new Command('sync')
Expand Down
33 changes: 27 additions & 6 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { confirm } from '@inquirer/prompts';
import { spawn, exec } from 'node:child_process';
import levenshtein from 'fast-levenshtein';
import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
Expand Down Expand Up @@ -150,12 +151,6 @@ export function formatCommand(

args = args.map((arg) => (arg === '_' ? undefined : arg));

if (debug) {
console.log('Command:', command);
console.log('Command arguments:', commandArguments);
console.log('Received arguments:', args);
}

for (let index = 0; index < commandArguments.length; index++) {
const arg = commandArguments[index];
let argValue = arg.alternativeValue
Expand All @@ -167,6 +162,16 @@ export function formatCommand(
splitCommand[index * 2 + 1] = argValue;
}

if (debug) {
console.log('Command:', command);
console.log('Command arguments:', commandArguments);
console.log('Received arguments:', args);
console.log(`Formatted command: ${splitCommand.join('')}`);
console.log();
console.log('Running command...');
console.log();
}

return splitCommand.join('');
}

Expand Down Expand Up @@ -205,3 +210,19 @@ export function openDirectory(path: string) {

exec(`${command} "${path}"`);
}

export function getClosestWord(
searchWord: string,
words: string[],
defaultWord?: ''
): { distance: number; word: string } {
return words.reduce(
(data, word) => {
const distance = levenshtein.get(searchWord, word);

if (distance < data.distance) return { distance, word };
return data;
},
{ distance: Infinity, word: defaultWord }
);
}

0 comments on commit d0fae0b

Please sign in to comment.