Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

organize-imports-cli

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

organize-imports-cli - npm Package Compare versions

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.
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc