This repository was archived by the owner on Oct 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathcli.ts
270 lines (256 loc) · 6.12 KB
/
cli.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import * as fileType from 'file-type';
import * as fs from 'fs';
import * as excludeGitignore from 'gulp-exclude-gitignore';
import * as filter from 'gulp-filter';
import * as reporter from 'gulp-reporter';
import * as tap from 'gulp-tap';
import * as _ from 'lodash';
import * as minimatch from 'minimatch';
import * as path from 'path';
import * as Stream from 'stream';
import * as through from 'through2';
import * as vfs from 'vinyl-fs';
import * as yargs from 'yargs';
import * as eclint from './eclint';
import i18n = require('./i18n');
/* tslint:disable:no-var-requires */
const pkg = require('../package.json');
/* tslint:enable:no-var-requires */
function gitignore(): Stream {
try {
return excludeGitignore();
} catch (ex) {
if (ex.code === 'ENOENT') {
return through.obj();
} else {
throw ex;
}
}
}
interface IArgv extends yargs.Argv {
globs: string[];
dest?: string;
stream?: Stream;
}
function excludeBinaryFile(file: eclint.IEditorConfigLintFile) {
return !(file && file.isBuffer() && fileType(file.contents));
}
function builder(argv: yargs.Argv): yargs.Argv {
return argv.option('indent_style', {
alias: 'i',
choices: [
'tab',
'space',
undefined,
],
describe: i18n('Indentation Style'),
requiresArg: false,
})
.option('indent_size', {
alias: 's',
describe: i18n('Indentation Size (in single-spaced characters)'),
type: 'number',
})
.option('tab_width', {
alias: 't',
describe: i18n('Width of a single tabstop character'),
type: 'number',
})
.option('end_of_line', {
alias: 'e',
choices: [
'lf',
'crlf',
'cr',
undefined,
],
describe: i18n('Line ending file format (Unix, DOS, Mac)'),
})
.option('charset', {
alias: 'c',
choices: [
'latin1',
'utf-8',
'utf-8-bom',
'utf-16le',
'utf-16be',
undefined,
],
describe: i18n('File character encoding'),
})
.option('trim_trailing_whitespace', {
alias: 'w',
default: undefined,
describe: i18n('Denotes whether whitespace is allowed at the end of lines'),
type: 'boolean',
})
.option('insert_final_newline', {
alias: 'n',
default: undefined,
describe: i18n('Denotes whether file should end with a newline'),
type: 'boolean',
})
.option('max_line_length', {
alias: 'm',
default: undefined,
describe: i18n('Forces hard line wrapping after the amount of characters specified'),
type: 'number',
})
.option('block_comment_start', {
default: undefined,
describe: i18n('Block comments start with'),
type: 'string',
})
.option('block_comment', {
default: undefined,
describe: i18n('Lines in block comment start with'),
type: 'string',
})
.option('block_comment_end', {
default: undefined,
describe: i18n('Block comments end with'),
type: 'string',
});
}
function inferBuilder(argv: yargs.Argv): yargs.Argv {
return argv
.option('score', {
alias: 's',
describe: i18n('Shows the tallied score for each setting'),
type: 'boolean',
})
.option('ini', {
alias: 'i',
describe: i18n('Exports file as ini file type'),
type: 'boolean',
})
.option('root', {
alias: 'r',
describe: i18n('Adds root = true to your ini file, if any'),
type: 'boolean',
});
}
function handler(argv: IArgv): Stream.Transform {
let ignore = [
// # Repository
// Git
'!.git/**/*',
// Subversion
'!.svn/**/*',
// Mercurial
'!.hg/**/*',
// # Dependency directories
'!node_modules/**/*',
'!bower_components/**/*',
// # macOS
// Stores custom folder attributes
'!**/.DS_Store',
// Stores additional file resources
'!**/.AppleDouble',
// Contains the absolute path to the app to be used
'!**/.LSOverride',
// Resource fork
'!**/__MACOSX/**/*',
// # Windows
// Image file cache
'!**/Thumbs.db',
// Folder config file
'!**/ehthumbs.db',
];
let globs = argv.globs;
if (globs && globs.length) {
globs = globs.map((file) => {
let stat;
try {
stat = fs.statSync(file);
} catch (e) {
//
}
if (stat) {
if (stat.isDirectory()) {
return path.join(file, '**/*');
} else {
ignore = ignore.filter((glob) => (
!minimatch(file, glob.slice(1), {
dot: true,
})
));
}
}
return file;
});
} else {
globs = ['**/*'];
}
globs = globs.concat(ignore);
argv.globs = globs;
return vfs.src(globs, {
dot: true,
// stripBOM: false,
removeBOM: false,
})
.pipe(filter(excludeBinaryFile))
.pipe(gitignore());
}
function pickSettings(argv: yargs.Argv): eclint.ICommandOptions {
const settings = _.pickBy(_.pick(argv, eclint.ruleNames));
return {
settings: settings as eclint.ISettings,
};
}
function check(argv: IArgv): Stream.Transform {
return argv.stream = handler(argv)
.pipe(eclint.check(pickSettings(argv)))
.pipe(reporter({
blame: false,
output: console.error,
}))
.on('error', (error) => {
if (error.plugin !== 'gulp-reporter') {
console.error(error);
}
process.exitCode = -1;
}).resume();
}
function fix(argv: IArgv): Stream {
return argv.stream = handler(argv)
.pipe(eclint.fix(pickSettings(argv)))
.pipe(argv.dest ? vfs.dest(argv.dest) : vfs.dest((file) => file.base));
}
function infer(argv: IArgv): Stream {
return argv.stream = handler(argv)
.pipe(eclint.infer(_.pickBy(argv) as eclint.InferOptions))
.pipe(tap((file) => {
console.log(file.contents + '');
}));
}
export = (argv) => yargs(argv)
.usage(i18n('Usage: $0 <command> [globs...] [<options>]'))
.command({
builder,
command: 'check [globs...]',
describe: i18n('Validate that file(s) adhere to .editorconfig settings'),
handler: check,
})
.command({
builder: (fixArgv) => (
builder(fixArgv).option('dest', {
alias: 'd',
describe: i18n('Destination folder to pipe source files'),
type: 'string',
})
),
command: 'fix [globs...]',
describe: i18n('Fix formatting errors that disobey .editorconfig settings'),
handler: fix,
})
.command({
builder: inferBuilder,
command: 'infer [globs...]',
describe: i18n('Infer .editorconfig settings from one or more files'),
handler: infer,
})
.demandCommand(1, 1, i18n('CommandError: Missing required sub-command.'))
.help()
.version(pkg.version)
.argv;