UNPKG

typescript-assistant

Version:

Combines and integrates professional Typescript tools into your project

80 lines 2.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /* Runs in a separate process and communicates using process.on('message', ...) */ /* This is because tslint is implemented synchronously */ const fs_1 = require("fs"); const tslint_1 = require("tslint"); const glob = require("glob"); let { rules, rulesDirectory } = tslint_1.Linter.loadConfigurationFromPath(`${process.cwd()}/tslint.json`); let configuration = { rules: rules, rulesDirectory: rulesDirectory, jsRules: new Map(), defaultSeverity: 'error', extends: [] }; const options = { fix: false, formatter: 'prose' }; const fixOptions = { fix: true, formatter: 'prose' }; process.on('message', (msg) => { let success = true; let configPaths = glob.sync('**/tsconfig.json', { ignore: '**/node_modules/**' }) .sort((a, b) => b.split('/').length - a.split('/').length); let configs = configPaths.map(path => ({ match: new RegExp(`^${path.replace('tsconfig.json', '.*')}`), config: `${process.cwd()}/${path}` })); let currentConfig; let currentProgram; msg.filesToLint.forEach((fileName) => { let config = configs.find(({ match }) => match.test(fileName)); if (!config) { process.send({ error: { message: `Could not find tsconfig.json for file ${fileName}` } }); return; } if (config !== currentConfig) { currentConfig = config; // Programs can't be stored in the config, it will cause an out of memory error when keeping all the programs. currentProgram = tslint_1.Linter.createProgram(config.config); } let linter = new tslint_1.Linter(msg.fix ? fixOptions : options, currentProgram); let contents = fs_1.readFileSync(fileName, 'utf8'); try { linter.lint(fileName, contents, configuration); let results = linter.getResult(); results.failures.forEach((failure) => { success = false; let line = failure.getStartPosition().getLineAndCharacter().line; let column = failure.getStartPosition().getLineAndCharacter().character; let response = { violation: { fileName, line: line + 1, column: column, message: failure.getFailure(), hasFix: failure.hasFix() } }; process.send(response); }); } catch (e) { process.send({ error: { message: e.message } }); } }); process.send({ finished: { success } }); }); //# sourceMappingURL=linter-process.js.map