organize-imports-cli
Advanced tools
Comparing version 0.4.0 to 0.5.0
97
cli.js
@@ -20,10 +20,33 @@ #!/usr/bin/env node | ||
} else { | ||
main(process.argv.slice(2)); | ||
main( | ||
process.argv.slice(2).filter(arg => arg !== "--list-different"), | ||
process.argv.includes("--list-different") | ||
); | ||
} | ||
/** | ||
* @param {string[]} filePaths | ||
* @param {boolean} listDifferent | ||
*/ | ||
function main(filePaths, listDifferent) { | ||
const logger = listDifferent | ||
? { | ||
write() {}, | ||
writeLine() {} | ||
} | ||
: { | ||
write: process.stdout.write.bind(process.stdout), | ||
writeLine: console.log.bind(console) | ||
}; | ||
logger.writeLine(chalk`{yellowBright Organizing imports...}`); | ||
function main(filePaths) { | ||
console.log(chalk`{yellowBright Organizing imports...}`); | ||
/** | ||
* @type {Record<string | number, { | ||
* filePaths: string[], | ||
* project: import('ts-morph').Project, | ||
* detectNewLineKind: boolean, | ||
* processAllFiles: boolean | ||
* }>} | ||
*/ | ||
const projects = {}; | ||
@@ -73,7 +96,7 @@ let adHocProjectCounter = 0; | ||
let changeCounter = 0, | ||
let differentFiles = [], | ||
crLfWeight = 0; | ||
for (const sourceFile of sourceFiles) { | ||
process.stdout.write(chalk`{gray ${sourceFile.getFilePath()}}`); | ||
logger.write(chalk`{gray ${sourceFile.getFilePath()}}`); | ||
@@ -83,3 +106,3 @@ const fullText = sourceFile.getFullText(); | ||
if (fullText.includes("// organize-imports-ignore")) { | ||
console.log(" (skipped)"); | ||
logger.writeLine(" (skipped)"); | ||
continue; | ||
@@ -92,25 +115,39 @@ } | ||
const importsBefore = listDifferent && serializeImports(sourceFile); | ||
sourceFile.organizeImports(); | ||
if (fullText === sourceFile.getFullText()) { | ||
console.log(""); | ||
if ( | ||
listDifferent | ||
? importsBefore === serializeImports(sourceFile) | ||
: fullText === sourceFile.getFullText() | ||
) { | ||
logger.writeLine(""); | ||
} else { | ||
changeCounter++; | ||
console.log(`\r${sourceFile.getFilePath()} (modified)`); | ||
differentFiles.push(sourceFile.getFilePath()); | ||
logger.writeLine(`\r${sourceFile.getFilePath()} (modified)`); | ||
} | ||
} | ||
if (changeCounter > 0) { | ||
if (crLfWeight !== 0) { | ||
project.manipulationSettings.set({ | ||
newLineKind: crLfWeight > 0 | ||
? NewLineKind.CarriageReturnLineFeed | ||
: NewLineKind.LineFeed | ||
}); | ||
if (differentFiles.length > 0) { | ||
if (listDifferent) { | ||
for (const filePath of differentFiles) { | ||
console.log(filePath); | ||
} | ||
process.exit(2); | ||
} else { | ||
if (crLfWeight !== 0) { | ||
project.manipulationSettings.set({ | ||
newLineKind: | ||
crLfWeight > 0 | ||
? NewLineKind.CarriageReturnLineFeed | ||
: NewLineKind.LineFeed | ||
}); | ||
} | ||
project.saveSync(); | ||
} | ||
project.saveSync(); | ||
} | ||
} | ||
console.log(chalk`{yellowBright Done!}`); | ||
logger.writeLine(chalk`{yellowBright Done!}`); | ||
} | ||
@@ -136,3 +173,3 @@ | ||
console.log(chalk` | ||
Usage: organize-imports-cli files... | ||
Usage: organize-imports-cli [--list-different] files... | ||
@@ -142,3 +179,19 @@ Files can be specific {yellow ts} and {yellow js} files or {yellow tsconfig.json}, in which case the whole project is processed. | ||
Files containing the substring "{yellow // organize-imports-ignore}" are skipped. | ||
The {yellow --list-different} flag prints a list of files with unorganized imports. No files are modified. | ||
`); | ||
} | ||
} | ||
/** | ||
* @param {import('ts-morph').SourceFile} sourceFile | ||
*/ | ||
function serializeImports(sourceFile) { | ||
return sourceFile | ||
.getImportDeclarations() | ||
.map(importDeclaration => importDeclaration.getText()) | ||
.join("") | ||
.replace(/'/g, '"') | ||
.replace(/\s+/g, "\t") | ||
.replace(/(\w)\t(\w)/g, "$1 $2") | ||
.replace(/\t/g, ""); | ||
} |
{ | ||
"name": "organize-imports-cli", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"description": "VS Code's 'Organize imports' executable from command line", | ||
@@ -13,7 +13,15 @@ "bin": "./cli.js", | ||
"dependencies": { | ||
"chalk": "^2.4.2", | ||
"chalk": "^3.0.0", | ||
"editorconfig": "^0.15.3", | ||
"ts-morph": "^3.0.0", | ||
"ts-morph": "^5.0.0", | ||
"tsconfig": "^7.0.0" | ||
}, | ||
"devDependencies": { | ||
"escape-string-regexp": "^2.0.0", | ||
"jest": "^24.9.0", | ||
"strip-ansi": "^6.0.0" | ||
}, | ||
"scripts": { | ||
"test": "jest" | ||
} | ||
} |
@@ -5,3 +5,3 @@ # organize-imports-cli | ||
Plays nicely with Prettier and [lint-staged](https://github.com/okonet/lint-staged): | ||
Plays nicely with [Prettier](https://prettier.io) and [lint-staged](https://github.com/okonet/lint-staged): | ||
@@ -21,3 +21,3 @@ ```json | ||
```console | ||
> organize-imports-cli files... | ||
> organize-imports-cli [--list-different] files... | ||
``` | ||
@@ -28,1 +28,3 @@ | ||
Files containing the substring `// organize-imports-ignore` are skipped. | ||
The `--list-different` flag prints a list of files with unorganized imports. No files are modified. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6470
167
28
3
+ Added@ts-morph/common@0.1.1(transitive)
+ Addedansi-styles@4.3.0(transitive)
+ Addedchalk@3.0.0(transitive)
+ Addedcode-block-writer@10.1.1(transitive)
+ Addedcolor-convert@2.0.1(transitive)
+ Addedcolor-name@1.1.4(transitive)
+ Addedhas-flag@4.0.0(transitive)
+ Addedsupports-color@7.2.0(transitive)
+ Addedts-morph@5.0.0(transitive)
+ Addedtypescript@3.7.7(transitive)
- Removedansi-styles@3.2.1(transitive)
- Removedchalk@2.4.2(transitive)
- Removedcode-block-writer@9.4.1(transitive)
- Removedcolor-convert@1.9.3(transitive)
- Removedcolor-name@1.1.3(transitive)
- Removedescape-string-regexp@1.0.5(transitive)
- Removedhas-flag@3.0.0(transitive)
- Removedsupports-color@5.5.0(transitive)
- Removedts-morph@3.1.3(transitive)
- Removedtypescript@3.9.10(transitive)
Updatedchalk@^3.0.0
Updatedts-morph@^5.0.0