typescript-assistant
Version:
Combines and integrates professional Typescript tools into your project
58 lines • 2.95 kB
JavaScript
;
// pre-commit makes sure the changed code is formatted and linted.
//
// Rationale:
// - Fixing formatting and linting errors are always fast and easy to fix.
// - This prevents over-complicated merge conflicts
// - This prevents small formatting/linting fix commits
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
/* tslint:disable no-null-keyword */
const tsfmt = require("typescript-formatter");
const util_1 = require("../util");
exports.createPreCommitCommand = (deps) => {
let { logger, linter, git } = deps;
return {
execute: () => __awaiter(this, void 0, void 0, function* () {
let files = (yield git.findChangedFiles()).filter(util_1.isTypescriptFile);
let lintFiles = () => {
return linter.lintOnce(false, files);
};
return tsfmt.processFiles(files, {
verify: true, replace: false, verbose: false, baseDir: process.cwd(), editorconfig: true, tslint: true, tsfmt: true, tsconfig: true,
tsconfigFile: null, tslintFile: null, tsfmtFile: null, vscode: false, vscodeFile: null
}).then((resultList) => __awaiter(this, void 0, void 0, function* () {
let unformattedFiles = [];
Object.keys(resultList).forEach((key) => {
let result = resultList[key];
if (result.error) {
unformattedFiles.push(result.fileName);
}
});
if (unformattedFiles.length === 0) {
logger.log('hooks', `All ${files.length} files were formatted`);
if (yield lintFiles()) {
logger.log('hooks', `All ${files.length} files were linted`);
process.exit(0);
}
else {
logger.log('hooks', `There were linting errors`);
process.exit(1);
}
}
else {
logger.error('hooks', `The following files were not formatted:\r\n ${unformattedFiles.join('\r\n ')}\r\nHint: this can be fixed by running 'npm run fix'`);
process.exit(1);
}
}));
})
};
};
//# sourceMappingURL=pre-commit.js.map