-
Notifications
You must be signed in to change notification settings - Fork 2
/
cli.mjs
executable file
·680 lines (565 loc) · 21.4 KB
/
cli.mjs
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
#!/usr/bin/env node
// ISC License
// Copyright © 2024, Chris Oakman
// https://github.com/oakmac/standard-clojure-style-js/
//
// The purpose of this file is to handle everything related to running
// Standard Clojure Style via the command line.
// node.js imports
import fs from 'fs-plus'
import path from 'path'
import { performance } from 'node:perf_hooks'
import process from 'process'
// npm imports
import { parseEDNString, toEDNStringFromSimpleObject } from 'edn-data'
import { globSync } from 'glob'
import yargs from 'yargs'
import { hideBin } from 'yargs/helpers'
import yocto from 'yoctocolors'
// import Standard Clojure Style
// NOTE: the line below (including the UUID) gets replaced by script/build-release.js
// script before publishing to npm
import standardClj from './lib/standard-clojure-style.js' // 7b323d1c-2984-4bd1-9304-d62d8dee9a1f
const scriptStartTime = performance.now()
// NOTE: the line below (including the UUID) gets replaced by script/build-release.js
// script before publishing to npm
const programVersion = '[dev]' // 6444ef98-c603-42ca-97e7-ebe5c60382de
const defaultFileExtensions = new Set(['.clj', '.cljs', '.cljc', '.edn'])
let logLevel = 'everything'
let atLeastOneFilePrinted = false
// this is the directory where the script is being called from
// in most cases, this will be a project root
const rootDir = process.cwd()
const defaultConfigJSONFile = path.join(rootDir, '.standard-clj.json')
const defaultConfigEDNFile = path.join(rootDir, '.standard-clj.edn')
const parseEDNOptions = {
keywordAs: 'string',
mapAs: 'object'
}
// returns a Set of files from the args passed to the "list", "check", or "fix" commands
function getFilesFromArgv (argv, cmd) {
// remove the first item, which is the command
argv._.shift()
const directArgs = argv._
const fileExtensionsSet = argv['file-ext']
let includeFiles = []
// process the direct arguments
directArgs.forEach(arg => {
let possibleFileOrDir = arg
if (!fs.isAbsolute(arg)) {
// if the argument is not an absolute path, assume it is relative to the
// directory where the script is being run from
possibleFileOrDir = path.join(rootDir, arg)
}
if (fs.isFileSync(possibleFileOrDir)) {
includeFiles.push(possibleFileOrDir)
} else if (fs.isDirectorySync(possibleFileOrDir)) {
fs.traverseTreeSync(possibleFileOrDir, (f) => {
const fileExt = path.extname(f)
if (fileExtensionsSet.has(fileExt)) {
includeFiles.push(f)
}
return true
}, alwaysTrue)
} else {
printToStderr(yocto.bold(yocto.yellow('WARN')) + ' Could not find a file or directory at "' + arg + '"')
}
})
// process the --include glob patterns
if (isArray(argv.include)) {
argv.include.forEach(includeStr => {
const filesFromGlob = globSync(includeStr)
includeFiles = includeFiles.concat(filesFromGlob)
})
}
// load --include files via config file if the user did not pass any direct arguments
const anyDirectArgsPassed = directArgs.length > 0
if (!anyDirectArgsPassed && argv._optionsLoadedViaConfigFile && isArray(argv.includeFromConfig)) {
argv.includeFromConfig.forEach(includeStr => {
const filesFromGlob = globSync(includeStr)
includeFiles = includeFiles.concat(filesFromGlob)
})
}
// exclude files if necessary
const ignoreFiles = []
let ignorePatterns = null
// use --ignore from CLI argument
if (isArray(argv.ignore)) {
ignorePatterns = argv.ignore
// or from config file if present
} else if (argv._optionsLoadedViaConfigFile && isArray(argv.ignoreFromConfig)) {
ignorePatterns = argv.ignoreFromConfig
}
if (ignorePatterns) {
ignorePatterns.forEach(ignoreStr => {
let possibleFileOrDir = ignoreStr
if (!fs.isAbsolute(ignoreStr)) {
// if the argument is not an absolute path, assume it is relative to the
// directory where the script is being run from
possibleFileOrDir = path.join(rootDir, ignoreStr)
}
if (fs.isFileSync(possibleFileOrDir)) {
ignoreFiles.push(possibleFileOrDir)
} else if (fs.isDirectorySync(possibleFileOrDir)) {
fs.traverseTreeSync(possibleFileOrDir, (f) => {
const fileExt = path.extname(f)
if (fileExtensionsSet.has(fileExt)) {
ignoreFiles.push(f)
}
return true
}, alwaysTrue)
} else {
printToStderr(yocto.bold(yocto.yellow('WARN')) + ' Could not find a file or directory to ignore at "' + ignoreStr + '"')
}
})
}
const includeFilesSet = new Set(includeFiles)
const ignoreFileSet = new Set(ignoreFiles)
return setDifference(includeFilesSet, ignoreFileSet)
}
function formatFileSync (formatResult, filename) {
const formatSingleFileStartTime = performance.now()
let fileTxt = null
try {
fileTxt = fs.readFileSync(filename, 'utf8')
} catch (e) {
// FIXME: this should match the error format below
printToStderr('Unable to read file: ' + filename)
atLeastOneFilePrinted = true
formatResult.filesWithErrors.push(filename)
return formatResult
}
const result = standardClj.format(fileTxt)
if (result && result.status === 'success') {
// add a single newline to the end of the file
// FIXME: should we do this here or upstream in the format() function?
// https://github.com/oakmac/standard-clojure-style-js/issues/154
const outTxtWithNewline = result.out + '\n'
// write the file to disk if necessary
let statusEmoji = '✓'
if (outTxtWithNewline !== fileTxt) {
fs.writeFileSync(filename, outTxtWithNewline)
formatResult.filesThatWereFormatted.push(filename)
statusEmoji = 'F'
} else {
formatResult.filesThatDidNotRequireFormatting.push(filename)
}
const formatSingleFileEndTime = performance.now()
const formatDurationMs = formatSingleFileEndTime - formatSingleFileStartTime
if (statusEmoji === 'F') {
printToStdout(yocto.green(statusEmoji) + ' ' + yocto.bold(relativeFilename(filename)) + ' ' + formatDuration(formatDurationMs))
atLeastOneFilePrinted = true
} else {
if (logLevel !== 'ignore-already-formatted') {
printToStdout(yocto.green(statusEmoji) + ' ' + yocto.bold(relativeFilename(filename)) + ' ' + formatDuration(formatDurationMs))
atLeastOneFilePrinted = true
}
}
return formatResult
} else {
formatResult.filesWithErrors.push(filename)
let errMsg = 'Unknown error! Please help the standard-clj project by opening an issue to report this 🙏'
if (result && result.status === 'error' && isString(result.reason)) {
errMsg = result.reason
}
const formatSingleFileEndTime = performance.now()
const formatDurationMs = formatSingleFileEndTime - formatSingleFileStartTime
printToStderr(yocto.red('E') + ' ' + yocto.bold(yocto.red(relativeFilename(filename))) + ' - ' + errMsg + ' ' + formatDuration(formatDurationMs))
atLeastOneFilePrinted = true
return formatResult
}
}
// FIXME: write an async version of this that returns a Promise
// function formatFileAsync (filename) {}
function relativeFilename (filename) {
return filename.replace(rootDir, '')
}
function formatDuration (durationMs) {
const roundedDuration = Math.round(durationMs * 100)
return yocto.dim('[' + (roundedDuration / 100) + 'ms]')
}
function checkFileSync (checkResult, filename) {
const checkStartTime = performance.now()
let fileTxt = null
try {
fileTxt = fs.readFileSync(filename, 'utf8')
} catch (e) {
// FIXME: this should match the error format below
printToStderr('Unable to read file: ' + filename)
atLeastOneFilePrinted = true
checkResult.filesWithErrors.push(filename)
return checkResult
}
const result = standardClj.format(fileTxt)
const checkEndTime = performance.now()
const durationMs = checkEndTime - checkStartTime
if (result && result.status === 'success') {
// FIXME: should we do this here or upstream in the format() function?
// https://github.com/oakmac/standard-clojure-style-js/issues/154
// add a single newline to the end of the file
const outTxtWithNewline = result.out + '\n'
if (isString(fileTxt) && fileTxt === outTxtWithNewline) {
if (logLevel !== 'ignore-already-formatted') {
printToStdout(yocto.green('✓') + ' ' + yocto.bold(relativeFilename(filename)) + ' ' + formatDuration(durationMs))
atLeastOneFilePrinted = true
}
checkResult.filesThatDidNotRequireFormatting.push(filename)
} else {
printToStderr(yocto.red('✗') + ' ' + yocto.bold(relativeFilename(filename)) + ' ' + formatDuration(durationMs))
atLeastOneFilePrinted = true
checkResult.filesThatDidRequireFormatting.push(filename)
}
} else if (result && result.status === 'error') {
const errMsg = 'Failed to format file ' + filename + ': ' + result.reason
printToStderr(errMsg)
atLeastOneFilePrinted = true
checkResult.filesWithErrors.push(filename)
} else {
printToStderr('Unknown error when formatting file ' + filename)
printToStderr('Please report this upstream to the standard-clj project:')
printToStderr(result)
atLeastOneFilePrinted = true
checkResult.filesWithErrors.push(filename)
}
return checkResult
}
function printProgramInfo (opts) {
printToStdout(yocto.bold('standard-clj ' + opts.command) + ' ' + yocto.dim(programVersion))
printToStdout('')
}
function setLogLevel (level) {
level = '' + level
if (level === 'ignore-already-formatted' || level === '1') logLevel = 'ignore-already-formatted'
else if (level === 'quiet' || level === '5') logLevel = 'quiet'
else logLevel = 'everything'
}
function injectConfigFile (argv) {
let config = null
const userPassedConfigArgument = isString(argv.config) && argv.config !== ''
if (userPassedConfigArgument) {
const isJSON = argv.config.endsWith('.json')
const isEDN = argv.config.endsWith('.edn')
if (isJSON) {
try {
config = JSON.parse(fs.readFileSync(argv.config, 'utf8'))
} catch (e) {}
} else if (isEDN) {
try {
config = parseEDNString(fs.readFileSync(argv.config, 'utf8'), parseEDNOptions)
} catch (e) {}
}
// exit if they passed a config file argument, but we are unable to read it
if (!config) {
printToStderr('Unable to load config file: ' + argv.config)
if (isJSON) {
printToStderr('Maybe the file is invalid JSON?')
} else if (isEDN) {
printToStderr('Maybe the file is invalid EDN?')
} else {
printToStderr('The filename does not end in .json or .edn. That is probably wrong.')
}
exitSad()
}
} else {
// try to load the default config file
try {
config = JSON.parse(fs.readFileSync(defaultConfigJSONFile, 'utf8'))
} catch (e) {}
try {
config = parseEDNString(fs.readFileSync(defaultConfigEDNFile, 'utf8'), parseEDNOptions)
} catch (e) {}
}
// apply the config options if found
if (config) {
argv._optionsLoadedViaConfigFile = true
// log level
if (!argv['log-level']) {
if (config['log-level']) {
argv['log-level'] = config['log-level']
}
}
// include
if (isString(config.include)) {
argv.includeFromConfig = [config.include]
} else if (isArray(config.include)) {
argv.includeFromConfig = config.include
}
// ignores
if (isString(config.ignore)) {
argv.ignoreFromConfig = [config.ignore]
} else if (isArray(config.ignore)) {
argv.ignoreFromConfig = config.ignore
}
}
return argv
}
// convert String arguments into Arrays
function convertStringsToArrays (argv) {
if (isString(argv.ignore)) {
argv.ignore = [argv.ignore]
}
if (isString(argv.include)) {
argv.include = [argv.include]
}
return argv
}
// convert a String --file-ext argument into a Set of extensions that start with a period
function convertFileExt (argv) {
if (isString(argv['file-ext']) && argv['file-ext'] !== '') {
let fileExtsArr = argv['file-ext'].split(',')
fileExtsArr = fileExtsArr.map(addPeriodPrefix)
argv['file-ext'] = new Set(fileExtsArr)
}
return argv
}
// -----------------------------------------------------------------------------
// yargs commands
function processCheckCmd (argv) {
setLogLevel(argv['log-level'])
printProgramInfo({ command: 'check' })
const filesToProcess = getFilesFromArgv(argv, 'check')
if (filesToProcess.size === 0) {
exitSad('No files were passed to the "check" command. Please pass a filename, directory, or --include glob pattern.')
} else {
const sortedFiles = setToArray(filesToProcess).sort()
const initialResult = {
filesThatDidNotRequireFormatting: [],
filesThatDidRequireFormatting: [],
filesWithErrors: [],
numFilesTotal: sortedFiles.length
}
const checkResult = sortedFiles.reduce(checkFileSync, initialResult)
// sanity-check the result
const numFilesProcessed = checkResult.filesThatDidRequireFormatting.length +
checkResult.filesThatDidNotRequireFormatting.length +
checkResult.filesWithErrors.length
console.assert(sortedFiles.length === numFilesProcessed, 'checkFileSync missed a file?')
const allFilesFormatted = checkResult.filesThatDidNotRequireFormatting.length === sortedFiles.length
const checkCommandEndTime = performance.now()
const scriptDurationMs = checkCommandEndTime - scriptStartTime
if (atLeastOneFilePrinted) printToStdout('')
if (allFilesFormatted) {
if (sortedFiles.length === 1) {
printToStdout(yocto.green('1 file formatted with Standard Clojure Style 👍') + ' ' + formatDuration(scriptDurationMs))
} else {
printToStdout(yocto.green('All ' + sortedFiles.length + ' files formatted with Standard Clojure Style 👍') + ' ' + formatDuration(scriptDurationMs))
}
} else {
printToStdout(yocto.green(checkResult.filesThatDidNotRequireFormatting.length + ' ' + fileStr(checkResult.filesThatDidNotRequireFormatting.length) + ' formatted with Standard Clojure Style'))
printToStdout(yocto.red(checkResult.filesThatDidRequireFormatting.length + ' ' + fileStr(checkResult.filesThatDidRequireFormatting.length) + ' require formatting'))
printToStdout('Checked ' + numFilesProcessed + ' ' + fileStr(numFilesProcessed) + '. ' + formatDuration(scriptDurationMs))
}
if (checkResult.filesThatDidNotRequireFormatting.length === sortedFiles.length) {
exitHappy()
} else {
exitSad()
}
}
}
// this is the fix command when not reading input from stdin
function processFixCmdNotStdin (argv) {
setLogLevel(argv['log-level'])
printProgramInfo({ command: 'fix' })
const filesToProcess = getFilesFromArgv(argv, 'fix')
if (filesToProcess.size === 0) {
exitSad('No files were passed to the "fix" command. Please pass a filename, directory, or --include glob pattern.')
} else {
const sortedFiles = setToArray(filesToProcess).sort()
const initialResult = {
filesThatDidNotRequireFormatting: [],
filesThatWereFormatted: [],
filesWithErrors: [],
numFilesTotal: sortedFiles.length
}
const formatResult = sortedFiles.reduce(formatFileSync, initialResult)
const numFormattedFiles = formatResult.filesThatDidNotRequireFormatting.length + formatResult.filesThatWereFormatted.length
const allFilesFormatted = numFormattedFiles === sortedFiles.length
const formatCommandEndTime = performance.now()
const scriptDurationMs = formatCommandEndTime - scriptStartTime
if (atLeastOneFilePrinted) printToStdout('')
if (allFilesFormatted) {
if (sortedFiles.length === 1) {
printToStdout(yocto.green('1 file formatted with Standard Clojure Style 👍') + ' ' + formatDuration(scriptDurationMs))
} else {
printToStdout(yocto.green('All ' + sortedFiles.length + ' files formatted with Standard Clojure Style 👍') + ' ' + formatDuration(scriptDurationMs))
}
} else {
printToStdout(yocto.green(numFormattedFiles + ' files formatted with Standard Clojure Style'))
printToStdout(yocto.red(formatResult.filesWithErrors.length + ' files with errors'))
printToStdout('Checked ' + sortedFiles.length + ' files. ' + formatDuration(scriptDurationMs))
}
if (allFilesFormatted) {
exitHappy()
} else {
exitSad()
}
}
}
// fix command when reading input from stdin
async function processFixCmdStdin (argv) {
const stdinStr = await readStream(process.stdin)
if (!isString(stdinStr) || stdinStr === '') {
exitSad('Nothing found on stdin. Please pipe some Clojure code to stdin when using "standard-clj fix -"')
} else {
let formatResult = null
try {
formatResult = standardClj.format(stdinStr)
} catch (e) {}
if (formatResult && formatResult.status === 'success') {
console.log(formatResult.out)
exitHappy()
} else if (formatResult && formatResult.status === 'error' && isString(formatResult.reason)) {
exitSad('Failed to format code: ' + formatResult.reason)
} else {
exitSad('Failed to format your code due to unknown error with the format() function. Please help the standard-clj project by opening an issue to report this 🙏')
}
}
}
function processFixCmd (argv) {
const lastArg = getLastItemInArray(argv._)
if (lastArg === '-') {
processFixCmdStdin(argv)
} else {
processFixCmdNotStdin(argv)
}
}
function processListCmd (argv) {
const filesSet = getFilesFromArgv(argv, 'list')
const sortedFiles = setToArray(filesSet).sort()
if (argv.output === 'json') {
printToStdout(JSON.stringify(sortedFiles))
} else if (argv.output === 'json-pretty') {
printToStdout(JSON.stringify(sortedFiles, null, 2))
} else if (argv.output === 'edn') {
printToStdout(toEDNStringFromSimpleObject(sortedFiles))
} else if (argv.output === 'edn-pretty') {
// NOTE: this is hacky, but it works 🤷♂️
const jsonOutput = JSON.stringify(sortedFiles)
printToStdout(jsonOutput.replaceAll(/","/g, '"\n "'))
} else {
sortedFiles.forEach(printToStdout)
}
exitHappy()
}
const yargsCheckCommand = {
command: 'check',
describe: 'Checks if files are formatted according to Standard Clojure Style. ' +
'This command does not modify files. ' +
'Returns exit code 0 if all files are formatted, 1 otherwise.',
handler: processCheckCmd
}
const yargsFixCommand = {
command: 'fix',
describe: 'Formats files according to Standard Clojure Style. ' +
'This command will modify your files on disk. ' +
'Returns exit code 0 if all files are formatted, 1 otherwise.',
handler: processFixCmd
}
const yargsListCommand = {
command: 'list',
describe: 'Prints a list of files that will be used by the "check" or "fix" commands. ' +
'Useful for debugging your .standard-clj.edn file or glob patterns.',
handler: processListCmd
}
yargs(hideBin(process.argv))
.scriptName('standard-clj')
.usage('$0 <cmd> [args]')
.command(yargsCheckCommand)
.command(yargsFixCommand)
.command(yargsListCommand)
.middleware([injectConfigFile, convertStringsToArrays, convertFileExt])
.alias('c', 'config')
.alias('ig', 'ignore')
.alias('in', 'include')
.alias('l', 'log-level')
.alias('v', 'version')
.alias('h', 'help')
.default('file-ext', defaultFileExtensions)
.demandCommand() // show them --help if they do not pass a valid command
.version(programVersion)
.example([
['$0 list src/', 'List files that will be formatted in the src/ directory (recursive)'],
['$0 check src/', 'Check if files are already formatted in src/'],
['$0 fix src/', 'Format all files in src/'],
['$0 fix -', 'Reads from stdin, prints a formatted file to stdout']
])
.describe('l', 'Set the logging level: "everything", "ignore-already-formatted", "quiet"')
.epilogue('For more information, see the README at https://github.com/oakmac/standard-clojure-style-js')
.wrap(100)
.help()
.parse()
// -----------------------------------------------------------------------------
// Util
function isString (s) {
return typeof s === 'string'
}
function isArray (a) {
return Array.isArray(a)
}
function setToArray (s) {
return Array.from(s)
}
// returns the last item in an Array, or null if the Array is empty
function getLastItemInArray (a) {
const size = a.length
if (size === 0) {
return null
} else {
return a[size - 1]
}
}
// some older versions of node.js do not have Set.difference
function setDifference (setA, setB) {
if (typeof Set.prototype.difference === 'function') {
return setA.difference(setB)
} else {
const returnSet = new Set()
setA.forEach(itm => {
if (!setB.has(itm)) {
returnSet.add(itm)
}
})
return returnSet
}
}
function printToStdout (s) {
if (logLevel !== 'quiet') {
console.log(s)
}
}
function printToStderr (s) {
if (logLevel !== 'quiet') {
console.error(s)
}
}
function exitHappy (s) {
if (isString(s)) {
printToStdout(s)
}
process.exit(0)
}
function exitSad (s) {
if (isString(s)) {
printToStderr(s)
}
process.exit(1)
}
function fileStr (numFiles) {
if (numFiles === 1) return 'file'
else return 'files'
}
// https://stackoverflow.com/a/54565854
async function readStream (stream) {
const chunks = []
for await (const chunk of stream) { chunks.push(chunk) }
return Buffer.concat(chunks).toString('utf8')
}
function alwaysTrue () {
return true
}
function addPeriodPrefix (f) {
if (isString(f) && !f.startsWith('.')) {
return '.' + f
}
return f
}