devsplain 2.2.0 → 2.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/cli.js +56 -11
- package/package.json +1 -1
package/bin/cli.js
CHANGED
|
@@ -10,7 +10,7 @@ const { execSync } = require('child_process');
|
|
|
10
10
|
let rl;
|
|
11
11
|
let askQuestion;
|
|
12
12
|
|
|
13
|
-
//
|
|
13
|
+
// Determines whether the current Git repository has uncommitted changes [ds]
|
|
14
14
|
function isGitDirty() {
|
|
15
15
|
try {
|
|
16
16
|
const gitDir = execSync('git rev-parse --is-inside-work-tree', { stdio: ['ignore', 'pipe', 'ignore'], encoding: 'utf8' }).trim();
|
|
@@ -23,7 +23,7 @@ function isGitDirty() {
|
|
|
23
23
|
return false;
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
// Checks
|
|
26
|
+
// Checks whether a given line of code is inside a string literal [ds]
|
|
27
27
|
function isLineInsideString(lines, targetLineIndex, ext = '') {
|
|
28
28
|
const isPython = ext.toLowerCase() === '.py';
|
|
29
29
|
const isHTML = ['.html', '.vue', '.svelte'].includes(ext.toLowerCase());
|
|
@@ -236,7 +236,7 @@ function isLineInsideString(lines, targetLineIndex, ext = '') {
|
|
|
236
236
|
return inBacktick || inTripleDouble || inTripleSingle || inSingle || inDouble || inCppRawString || inRegex;
|
|
237
237
|
}
|
|
238
238
|
|
|
239
|
-
// Analyzes comments
|
|
239
|
+
// Analyzes comments within a given list of code lines [ds]
|
|
240
240
|
function analyzeComments(lines, ext = '') {
|
|
241
241
|
const isPython = ext.toLowerCase() === '.py';
|
|
242
242
|
const isHTML = ['.html', '.vue', '.svelte'].includes(ext.toLowerCase());
|
|
@@ -477,7 +477,7 @@ function analyzeComments(lines, ext = '') {
|
|
|
477
477
|
return analysis;
|
|
478
478
|
}
|
|
479
479
|
|
|
480
|
-
//
|
|
480
|
+
// Inserts or removes comments from code based on the specified mode [ds]
|
|
481
481
|
function spliceComments(data, comments, mode = 'default', ext = '') {
|
|
482
482
|
const hasCRLF = data.includes('\r\n');
|
|
483
483
|
const lineEnding = hasCRLF ? '\r\n' : '\n';
|
|
@@ -530,7 +530,23 @@ function spliceComments(data, comments, mode = 'default', ext = '') {
|
|
|
530
530
|
if (lineAnalysis.isPureComment) {
|
|
531
531
|
finalDeletions.add(lineNum);
|
|
532
532
|
} else if (lineAnalysis.commentStartIndex !== -1) {
|
|
533
|
-
|
|
533
|
+
const idx = lineAnalysis.commentStartIndex;
|
|
534
|
+
const remainder = lineStr.slice(idx);
|
|
535
|
+
let newText = lineStr.slice(0, idx).trimEnd();
|
|
536
|
+
|
|
537
|
+
if (remainder.startsWith('/*')) {
|
|
538
|
+
const endIdx = remainder.indexOf('*/');
|
|
539
|
+
if (endIdx !== -1) {
|
|
540
|
+
newText = lineStr.slice(0, idx) + remainder.slice(endIdx + 2);
|
|
541
|
+
}
|
|
542
|
+
} else if (remainder.startsWith('<!--')) {
|
|
543
|
+
const endIdx = remainder.indexOf('-->');
|
|
544
|
+
if (endIdx !== -1) {
|
|
545
|
+
newText = lineStr.slice(0, idx) + remainder.slice(endIdx + 3);
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
annotated[i].text = newText.trimEnd();
|
|
534
550
|
}
|
|
535
551
|
} else if (mode === 'clean') {
|
|
536
552
|
const isDsBlockLine = dsBlocks.has(lineNum);
|
|
@@ -542,7 +558,23 @@ function spliceComments(data, comments, mode = 'default', ext = '') {
|
|
|
542
558
|
}
|
|
543
559
|
} else if (lineAnalysis.commentStartIndex !== -1) {
|
|
544
560
|
if (isDsBlockLine || hasDsInline) {
|
|
545
|
-
|
|
561
|
+
const idx = lineAnalysis.commentStartIndex;
|
|
562
|
+
const remainder = lineStr.slice(idx);
|
|
563
|
+
let newText = lineStr.slice(0, idx).trimEnd();
|
|
564
|
+
|
|
565
|
+
if (remainder.startsWith('/*')) {
|
|
566
|
+
const endIdx = remainder.indexOf('*/');
|
|
567
|
+
if (endIdx !== -1) {
|
|
568
|
+
newText = lineStr.slice(0, idx) + remainder.slice(endIdx + 2);
|
|
569
|
+
}
|
|
570
|
+
} else if (remainder.startsWith('<!--')) {
|
|
571
|
+
const endIdx = remainder.indexOf('-->');
|
|
572
|
+
if (endIdx !== -1) {
|
|
573
|
+
newText = lineStr.slice(0, idx) + remainder.slice(endIdx + 3);
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
annotated[i].text = newText.trimEnd();
|
|
546
578
|
}
|
|
547
579
|
}
|
|
548
580
|
}
|
|
@@ -643,7 +675,23 @@ function spliceComments(data, comments, mode = 'default', ext = '') {
|
|
|
643
675
|
const isDsBlockLine = dsBlocks.has(origIdx + 1);
|
|
644
676
|
const hasDsInline = originalLine.includes('[ds]');
|
|
645
677
|
if (mode === 'prune' || (mode === 'clean' && (hasDsInline || isDsBlockLine))) {
|
|
646
|
-
const
|
|
678
|
+
const idx = lineAnalysis.commentStartIndex;
|
|
679
|
+
const remainder = originalLine.slice(idx);
|
|
680
|
+
let expectedStripped = originalLine.slice(0, idx).trimEnd();
|
|
681
|
+
|
|
682
|
+
if (remainder.startsWith('/*')) {
|
|
683
|
+
const endIdx = remainder.indexOf('*/');
|
|
684
|
+
if (endIdx !== -1) {
|
|
685
|
+
expectedStripped = originalLine.slice(0, idx) + remainder.slice(endIdx + 2);
|
|
686
|
+
}
|
|
687
|
+
} else if (remainder.startsWith('<!--')) {
|
|
688
|
+
const endIdx = remainder.indexOf('-->');
|
|
689
|
+
if (endIdx !== -1) {
|
|
690
|
+
expectedStripped = originalLine.slice(0, idx) + remainder.slice(endIdx + 3);
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
expectedStripped = expectedStripped.trimEnd();
|
|
694
|
+
|
|
647
695
|
if (text === expectedStripped) {
|
|
648
696
|
return true;
|
|
649
697
|
}
|
|
@@ -669,7 +717,7 @@ function spliceComments(data, comments, mode = 'default', ext = '') {
|
|
|
669
717
|
return annotated.map(line => line.text).join(lineEnding);
|
|
670
718
|
}
|
|
671
719
|
|
|
672
|
-
// Runs the
|
|
720
|
+
// Runs the command-line interface for the application [ds]
|
|
673
721
|
async function runCLI() {
|
|
674
722
|
rl = readline.createInterface({ input: process.stdin, output: process.stdout });
|
|
675
723
|
askQuestion = (query) => new Promise((resolve) => rl.question(query, resolve));
|
|
@@ -823,7 +871,6 @@ Options:
|
|
|
823
871
|
return false;
|
|
824
872
|
}
|
|
825
873
|
|
|
826
|
-
// Processes a given file path, either a file or directory [ds]
|
|
827
874
|
async function processPath(targetPath) {
|
|
828
875
|
const stats = fs.statSync(targetPath);
|
|
829
876
|
|
|
@@ -881,7 +928,6 @@ Options:
|
|
|
881
928
|
fs.renameSync(tempPath, targetPath);
|
|
882
929
|
console.log(` Successfully saved ${targetPath}`);
|
|
883
930
|
} else {
|
|
884
|
-
// Asks the user if they want to save the commented code to a file [ds]
|
|
885
931
|
console.log(` Skipped ${targetPath}`);
|
|
886
932
|
}
|
|
887
933
|
} else {
|
|
@@ -914,7 +960,6 @@ Options:
|
|
|
914
960
|
rl.close();
|
|
915
961
|
}
|
|
916
962
|
|
|
917
|
-
// Starts the CLI application if this script is run directly [ds]
|
|
918
963
|
if (require.main === module) {
|
|
919
964
|
runCLI().catch(err => {
|
|
920
965
|
console.error(err);
|
package/package.json
CHANGED