Skip to content

Commit

Permalink
feat: Updated index.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
sweep-ai[bot] authored Oct 15, 2023
1 parent 9452b67 commit 965e4dd
Showing 1 changed file with 65 additions and 30 deletions.
95 changes: 65 additions & 30 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@

import { $ } from "zx";
import { exec } from "child_process";

import { writeFile } from "./lib";
import { consola } from "./utils";

let isInstalled = false;
try {
await $`cspell --version`;
isInstalled = true;
} catch (error) {
console.error(
"cSpell is not installed. Please install with your package manager.",
);
// Print hint to console with cmd: npm install -g cspell@latest
console.info("Hint: npm install -g cspell@latest");
async function main() {
let isInstalled = false;
try {
await $`cspell --version`;
isInstalled = true;
} catch (error) {
console.error(
"cSpell is not installed. Please install with your package manager.",
);
// Print hint to console with cmd: npm install -g cspell@latest
console.info("Hint: npm install -g cspell@latest");
}
}

// Extract project name from current directory
Expand All @@ -41,27 +43,60 @@ const cSpellContent = {

// Create a project-name.txt file with the project name
writeFile(`./${projectName}.txt`, "");
writeFile("./cspell.json", JSON.stringify(cSpellContent, null, 2));
const projectName = await consola.prompt("What is your project name?", {
placeholder: "Not sure",
initial: "java",
});

const confirmed = await consola.prompt("Do you want to continue?", {
type: "confirm",
});

if (!confirmed) {
process.exit(0);
}

const projectType = await consola.prompt("Pick a project type.", {
type: "select",
options: [
"md",
"ts",
"json",
],
});

const tools = await consola.prompt("Select additional tools.", {
type: "multiselect",
required: false,
options: [
{ value: "eslint", label: "ESLint", hint: "recommended" },
{ value: "prettier", label: "Prettier" },
{ value: "gh-action", label: "GitHub Action" },
],
});

// TODO: Support other file types
// Run cspell on Markdown files to get unknown words
const cmd = `${
isInstalled ? "" : "npx "
}cspell --words-only --unique --no-progress --show-context "**/**/*.md" "**/**/*.ts" "**/**/*.json"`;
const unknownWords = await new Promise<string[]>((resolve) => {
exec(cmd, (error: any, stdout: string) => {
if (error) {
console.error(`Error running cspell: ${error}`);
}
consola.start("Creating project...");

const words = stdout ? stdout.split("\n").filter((word: any) => word) : [];
resolve(words);
const cmd = `${
isInstalled ? "" : "npx "
}cspell --words-only --unique --no-progress --show-context "**/**/*.${projectType}"`;
const unknownWords = await new Promise<string[]>((resolve) => {
exec(cmd, (error: any, stdout: string) => {
if (error) {
console.error(`Error running cspell: ${error}`);
}

const words = stdout ? stdout.split("\n").filter((word: any) => word) : [];
resolve(words);
});
});
});

console.log(`Found ${unknownWords.length} unknown words.`);
console.log(`Found ${unknownWords.length} unknown words.`);

// Save unknown words in project-name.txt
writeFile(`./${projectName}.txt`, unknownWords.join("\n"));
console.log("cSpell setup completed.");
process.exit(0);
}

// Save unknown words in project-name.txt
writeFile(`./${projectName}.txt`, unknownWords.join("\n"));
console.log("cSpell setup completed.");
process.exit(0);
main();

0 comments on commit 965e4dd

Please sign in to comment.