UNPKG

@sprucelabs/spruce-cli

Version:

Command line interface for building Spruce skills.

89 lines 3.22 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const schema_1 = require("@sprucelabs/schema"); // import { ESLint } from 'eslint' // import fs from 'fs-extra' const SpruceError_1 = __importDefault(require("../errors/SpruceError")); class LintService { cwd; getCommand; static ignorePatterns = [ 'valueType.tmp', '.md', '.js', '.gitignore', '.mjs', '.spruce', '.nvmrc', 'tsconfig.json', '.npmignore', ]; static isLintingEnabled = true; static disableLinting() { this.isLintingEnabled = false; } static enableLinting() { this.isLintingEnabled = true; } static shouldIgnorePattern(pattern) { return !!LintService.ignorePatterns.find((p) => p.includes(pattern) || pattern.includes(p)); } constructor(cwd, commandServiceFactory) { this.cwd = cwd; this.getCommand = commandServiceFactory; } fix = async (pattern) => { if (!pattern) { throw new schema_1.SchemaError({ code: 'MISSING_PARAMETERS', parameters: ['pattern'], }); } if (!LintService.isLintingEnabled || LintService.shouldIgnorePattern(pattern)) { return []; } // let fixedFiles: any = {} const fixedPaths = []; try { // const cli = new ESLint({ fix: true, cwd: this.cwd, cache: true }) // fixedFiles = await cli.lintFiles([pattern]) const script = `"(async function lint() { const { ESLint } = require('eslint'); const cli = new ESLint({ fix: true, cwd: '${this.cwd}', cache: true }); const result = await cli.lintFiles(['${pattern}']); ESLint.outputFixes(result); })()"`; await this.getCommand().execute('node', { args: ['-e', script], }); // JSON.parse(stdout) } catch (err) { throw new SpruceError_1.default({ code: 'LINT_FAILED', pattern, originalError: err, }); } // if (fixedFiles) { // for (const fixedFile of fixedFiles) { // if (fixedFile?.output) { // await fs.writeFile(fixedFile.filePath, fixedFile.output) // fixedPaths.push(fixedFile.filePath) // } else if (fixedFile?.messages && fixedFile?.errorCount > 0) { // throw new SpruceError({ // code: 'LINT_FAILED', // pattern, // friendlyMessage: `Lint error with '${ // fixedFile.filePath // }':\n\n${fixedFile.messages // .map((m: any) => m?.message) // .join('\n')}`, // }) // } // } // } return fixedPaths; }; } exports.default = LintService; //# sourceMappingURL=LintService.js.map