@shlroland/lint-cli
Version:
Setup Wizard for lint tools
173 lines (159 loc) • 4.68 kB
JavaScript
// src/answer/abstract/context.ts
import process3 from "node:process";
// src/utils/actions.ts
import { parseNi, parseNlx, run } from "@antfu/ni";
import * as p from "@clack/prompts";
import { x } from "tinyexec";
async function install(pendingPkgs) {
await run(parseNi, ["-D", ...pendingPkgs], { programmatic: true });
}
async function initGit() {
const spinner2 = p.spinner();
spinner2.start("Initializing git repository");
await x("git", ["init"]);
spinner2.stop("Git repository initialized");
}
async function initHusky() {
await run(parseNlx, ["husky", "init"], { programmatic: true });
}
// src/utils/cancel.ts
import process from "node:process";
import * as p2 from "@clack/prompts";
function onCancel() {
p2.cancel("Operation canceled");
process.exit(0);
}
// src/utils/detect.ts
import fs from "node:fs";
import fsP from "node:fs/promises";
import path from "node:path";
import process2 from "node:process";
import { cosmiconfig } from "cosmiconfig";
import c from "picocolors";
async function getModuleType(cwd) {
const packageJsonPath = path.join(cwd, "package.json");
const packageJson = JSON.parse(await fsP.readFile(packageJsonPath, "utf-8"));
return packageJson.type ?? "commonjs";
}
function isGitRepository(directory) {
const gitDir = path.join(directory, ".git");
return fs.existsSync(gitDir) && fs.lstatSync(gitDir).isDirectory();
}
function isHuskyInstalled(directory) {
const huskyDir = path.join(directory, ".husky");
return fs.existsSync(huskyDir) && fs.lstatSync(huskyDir).isDirectory();
}
var commonjsError = [
"Unexpected token 'export'",
"SyntaxError: Cannot use import statement outside a module"
];
var esmError = "module is not defined in ES module scope";
function isPackageModuleError(error) {
return commonjsError.some((msg) => error.message.includes(msg)) || error.message.includes(esmError);
}
async function ensureConfig(moduleName) {
try {
const explorer = cosmiconfig(moduleName);
const result = await explorer.search();
return result;
} catch (error) {
if (error instanceof Error) {
const isCjsOrEsmError = isPackageModuleError(error);
if (isCjsOrEsmError) {
console.log(`${c.redBright(`So bad`)} 😞!
You may have mismatched the value of the ${c.yellowBright(`"type"`)} field in ${c.greenBright("package.json")} and the export format of the ${c.blueBright(moduleName)} configuration file.
Please check your ${c.greenBright("package.json")} and ${c.blueBright(moduleName)} configuration file.
`);
process2.exit(10);
}
throw error;
}
}
}
// src/utils/F.ts
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
// src/utils/factory.ts
function esmConfigFactory(exportContent, importContent) {
return `
${importContent}
export default ${exportContent}
`;
}
function cjsConfigFactory(exportContent, importContent) {
return `
${importContent}
module.exports = ${exportContent}
`;
}
// src/utils/fs.ts
import fsP2 from "node:fs/promises";
function deleteFile(filePath) {
return fsP2.unlink(filePath);
}
function writeFile(filePath, content) {
return fsP2.writeFile(filePath, content);
}
// src/utils/prompts.ts
import * as p3 from "@clack/prompts";
import c2 from "picocolors";
function willInstallList(pendingPkgs) {
p3.note(`${c2.yellowBright("🛠️ will install packages")}:
${pendingPkgs.map((pkg) => c2.cyanBright(pkg)).join("\n")}
`);
}
async function shouldOverridePrompt(tool) {
const shouldOverride = await p3.confirm({
message: `${highlightPkg(tool)} configuration already exists, do you want to override it?`,
initialValue: true
});
return !!shouldOverride;
}
function highlightPkg(pkg) {
return c2.red(pkg);
}
async function shouldInitGitPrompt() {
return p3.confirm({
message: `Current directory is not a ${highlightPkg("git")} repository, do you want to initialize it?`,
initialValue: true
});
}
// src/answer/abstract/context.ts
var AnswerContext = class {
moduleType;
cwd;
constructor(context) {
this.cwd = context.cwd;
this.moduleType = context.moduleType;
}
static instance;
};
async function initAnswerContext() {
const cwd = process3.cwd();
const moduleType = await getModuleType(cwd);
const context = {
moduleType,
cwd
};
AnswerContext.instance = new AnswerContext(context);
}
export {
install,
initGit,
initHusky,
onCancel,
isGitRepository,
isHuskyInstalled,
ensureConfig,
sleep,
esmConfigFactory,
cjsConfigFactory,
deleteFile,
writeFile,
willInstallList,
shouldOverridePrompt,
shouldInitGitPrompt,
AnswerContext,
initAnswerContext
};