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

Add support for file types from CLI #9

Closed
wants to merge 3 commits into from
Closed
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
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 'consola';

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();
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"dev": "bun run --watch index.ts"
},
"dependencies": {
"zx": "7.2.3"
"zx": "7.2.3",
"consola": "latest"
},
"devDependencies": {
"@skypack/package-check": "0.2.2",
Expand Down
Loading