UNPKG

5.03 kBJavaScriptView Raw
1"use strict";
2const tslib_1 = require("tslib");
3/* istanbul ignore file */
4const fs = require("fs-extra");
5const path_1 = require("path");
6require("reflect-metadata");
7const xSpawn_1 = require("../fns/xSpawn");
8const spec = {
9 get bin() {
10 const jsonPath = require.resolve('tslint/package.json');
11 const rootDir = path_1.dirname(jsonPath);
12 const contents = JSON.parse(fs.readFileSync(jsonPath, 'utf8'));
13 const value = path_1.join(rootDir, contents.bin.tslint);
14 Object.defineProperty(spec, 'bin', { value });
15 return value;
16 },
17 get formattersDir() {
18 const root = path_1.dirname(require.resolve('custom-tslint-formatters/package.json'));
19 const value = path_1.join(root, 'formatters');
20 Object.defineProperty(spec, 'bin', { value });
21 return value;
22 }
23};
24const availableOpts = {};
25function setType(conf, type) {
26 return Object.assign({}, { type }, conf);
27}
28function Option(conf) {
29 return (target, name) => {
30 if (conf.overrideName) {
31 name = conf.overrideName;
32 conf = Object.assign({}, conf);
33 delete conf.overrideName;
34 }
35 switch (Reflect.getMetadata('design:type', target, name)) {
36 case Array:
37 conf = setType(conf, 'array');
38 break;
39 case Boolean:
40 conf = setType(conf, 'boolean');
41 break;
42 case Number:
43 conf = setType(conf, 'number');
44 break;
45 case String:
46 conf = setType(conf, 'string');
47 }
48 availableOpts[name] = conf;
49 };
50}
51class Conf {
52}
53tslib_1.__decorate([
54 Option({
55 alias: 'c',
56 describe: 'TSLint configuration file'
57 }),
58 tslib_1.__metadata("design:type", String)
59], Conf.prototype, "config", void 0);
60tslib_1.__decorate([
61 Option({
62 alias: 'e',
63 describe: 'Exclude globs from path expansion',
64 type: 'array'
65 }),
66 tslib_1.__metadata("design:type", Array)
67], Conf.prototype, "exclude", void 0);
68tslib_1.__decorate([
69 Option({
70 alias: 'x',
71 describe: 'Fixes linting errors for select rules (this may overwrite linted files)'
72 }),
73 tslib_1.__metadata("design:type", Boolean)
74], Conf.prototype, "fix", void 0);
75tslib_1.__decorate([
76 Option({ describe: 'Return status code 0 even if there are lint errors' }),
77 tslib_1.__metadata("design:type", Boolean)
78], Conf.prototype, "force", void 0);
79tslib_1.__decorate([
80 Option({
81 alias: 'i',
82 describe: 'Generate a tslint.json config file in the current working directory'
83 }),
84 tslib_1.__metadata("design:type", Boolean)
85], Conf.prototype, "init", void 0);
86tslib_1.__decorate([
87 Option({
88 alias: 'o',
89 describe: 'Output file'
90 }),
91 tslib_1.__metadata("design:type", String)
92], Conf.prototype, "out", void 0);
93tslib_1.__decorate([
94 Option({ describe: 'Whether or not outputted file paths are absolute' }),
95 tslib_1.__metadata("design:type", Boolean)
96], Conf.prototype, "outputAbsolutePaths", void 0);
97tslib_1.__decorate([
98 Option({
99 alias: 'p',
100 describe: 'tsconfig.json file'
101 }),
102 tslib_1.__metadata("design:type", String)
103], Conf.prototype, "project", void 0);
104tslib_1.__decorate([
105 Option({
106 alias: 'r',
107 describe: 'Rules directory',
108 overrideName: 'rules-dir'
109 }),
110 tslib_1.__metadata("design:type", String)
111], Conf.prototype, "rulesDir", void 0);
112tslib_1.__decorate([
113 Option({ describe: 'Test that tslint produces the correct output for the specified directory' }),
114 tslib_1.__metadata("design:type", Boolean)
115], Conf.prototype, "test", void 0);
116tslib_1.__decorate([
117 Option({
118 alias: 'tv',
119 describe: 'Output tslint version',
120 overrideName: 'tslint-version'
121 }),
122 tslib_1.__metadata("design:type", Boolean)
123], Conf.prototype, "tslintVersion", void 0);
124const cmd = {
125 builder(argv) {
126 return argv.options(availableOpts);
127 },
128 command: 'tslint',
129 describe: 'tslint files with a pretty formatter',
130 handler(c) {
131 const args = [
132 spec.bin,
133 '-s',
134 spec.formattersDir,
135 '-t',
136 'grouped'
137 ];
138 for (const optname of Object.keys(availableOpts)) {
139 if (!(optname in c)) {
140 continue;
141 }
142 if (optname === 'tslint-version') {
143 args.push('--version');
144 }
145 else {
146 args.push(`--${optname}`);
147 if (Array.isArray(c[optname])) {
148 args.push(...c[optname]);
149 }
150 else if (typeof c[optname] !== 'boolean') {
151 args.push(c[optname]);
152 }
153 }
154 }
155 const result = xSpawn_1.xSpawnSync(process.execPath, args, { stdio: 'inherit' });
156 if (result.status) {
157 process.exit(result.status);
158 }
159 }
160};
161module.exports = cmd;