Skip to content
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
/.claude/settings.local.json
tmpclaude*
108 changes: 78 additions & 30 deletions dist/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function _interopNamespaceDefault(e) {
const fs__namespace = /* @__PURE__ */ _interopNamespaceDefault(fs);
const glob__namespace = /* @__PURE__ */ _interopNamespaceDefault(glob);
const path__namespace = /* @__PURE__ */ _interopNamespaceDefault(path);
async function formatFiles(targetDir, config, dryRun) {
async function formatDirectory(targetDir, config, dryRun) {
const container = new Container.Container();
ServiceRegistration.ServiceRegistration.registerServices(container, config);
const include = config.sorting?.include || ["**/*.{ts,tsx,js,jsx}"];
Expand Down Expand Up @@ -68,54 +68,102 @@ async function formatFiles(targetDir, config, dryRun) {
console.info(`Formatted ${formattedCount} of ${files.length} files.`);
}
}
async function formatSingleFile(filePath, config, dryRun) {
const container = new Container.Container();
ServiceRegistration.ServiceRegistration.registerServices(container, config);
const pipeline = container.resolve("FormatterPipeline");
try {
const context = await pipeline.formatFile(filePath, dryRun);
if (context.changed) {
if (dryRun) {
console.info(`Would format: ${filePath}`);
} else {
console.log(`📊 Formatted: ${filePath}`);
}
} else {
console.info(`No changes needed: ${filePath}`);
}
} catch (error) {
console.error(`Error formatting file ${filePath}:`, error.message);
}
}
function isSupportedFile(filePath) {
const supportedExtensions = [".ts", ".tsx", ".js", ".jsx"];
return supportedExtensions.some((ext) => filePath.endsWith(ext));
}
async function main() {
const args = process.argv.slice(2);
let targetDir = process.cwd();
let target = process.cwd();
let dryRun = false;
for (let i = 0; i < args.length; i++) {
const arg = args[i];
if (arg === "--dry") {
dryRun = true;
} else if (!arg.startsWith("-")) {
targetDir = path__namespace.resolve(arg);
target = path__namespace.resolve(arg);
} else {
console.error(`Error: Unsupported option "${arg}". Only --dry is supported.`);
process.exit(1);
}
}
try {
const config = ConfigLoader.ConfigLoader.loadConfig(targetDir);
if (ConfigLoader.ConfigLoader.hasConfigFile(targetDir)) {
const targetStat = fs__namespace.existsSync(target) ? fs__namespace.statSync(target) : null;
const isFile = targetStat?.isFile() ?? false;
const isDirectory = targetStat?.isDirectory() ?? false;
if (!targetStat) {
console.error(`Error: Target "${target}" does not exist.`);
process.exit(1);
}
const configDir = isFile ? path__namespace.dirname(target) : target;
const config = ConfigLoader.ConfigLoader.loadConfig(configDir);
if (ConfigLoader.ConfigLoader.hasConfigFile(configDir)) {
console.log("Using custom configuration from tsfmt.config.ts");
}
if (config.packageJson?.enabled) {
const packagePath = path__namespace.join(targetDir, "package.json");
if (fs__namespace.existsSync(packagePath)) {
console.log(`📦 Processing ${packagePath}...`);
sortPackage.sortPackageFile(packagePath, {
customSortOrder: config.packageJson.customSortOrder,
indentation: config.packageJson.indentation,
dryRun
});
if (isFile) {
if (!isSupportedFile(target)) {
console.error(`Error: Unsupported file type. Supported: .ts, .tsx, .js, .jsx`);
process.exit(1);
}
}
if (config.tsConfig?.enabled) {
const tsconfigPath = path__namespace.join(targetDir, "tsconfig.json");
if (fs__namespace.existsSync(tsconfigPath)) {
console.log(`🔧 Processing ${tsconfigPath}...`);
sortTSConfig.sortTsConfigFile(tsconfigPath, {
indentation: config.tsConfig.indentation,
dryRun
});
if (config.codeStyle?.enabled || config.imports?.enabled || config.sorting?.enabled || config.spacing?.enabled) {
await formatSingleFile(target, config, dryRun);
}
if (dryRun) {
console.info("Dry run completed. No files were modified.");
} else {
console.info("Formatting completed successfully.");
}
return;
}
if (config.codeStyle?.enabled || config.imports?.enabled || config.sorting?.enabled || config.spacing?.enabled) {
await formatFiles(targetDir, config, dryRun);
}
if (dryRun) {
console.info("Dry run completed. No files were modified.");
} else {
console.info("Formatting completed successfully.");
if (isDirectory) {
if (config.packageJson?.enabled) {
const packagePath = path__namespace.join(target, "package.json");
if (fs__namespace.existsSync(packagePath)) {
console.log(`📦 Processing ${packagePath}...`);
sortPackage.sortPackageFile(packagePath, {
customSortOrder: config.packageJson.customSortOrder,
indentation: config.packageJson.indentation,
dryRun
});
}
}
if (config.tsConfig?.enabled) {
const tsconfigPath = path__namespace.join(target, "tsconfig.json");
if (fs__namespace.existsSync(tsconfigPath)) {
console.log(`🔧 Processing ${tsconfigPath}...`);
sortTSConfig.sortTsConfigFile(tsconfigPath, {
indentation: config.tsConfig.indentation,
dryRun
});
}
}
if (config.codeStyle?.enabled || config.imports?.enabled || config.sorting?.enabled || config.spacing?.enabled) {
await formatDirectory(target, config, dryRun);
}
if (dryRun) {
console.info("Dry run completed. No files were modified.");
} else {
console.info("Formatting completed successfully.");
}
}
} catch (error) {
console.error("Error during formatting:", error.message);
Expand Down
4 changes: 2 additions & 2 deletions dist/core/config/ConfigLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ const config: CoreConfig = {
semicolons: "always",
indentWidth: 4,
lineWidth: 120,
},
},

// Import organization
imports: {
Expand All @@ -106,7 +106,7 @@ const config: CoreConfig = {
// JSON file sorting
packageJson: { enabled: true },
tsConfig: { enabled: true },
};
};

export default config;
`;
Expand Down
2 changes: 2 additions & 0 deletions dist/core/di/ServiceRegistration.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const DocBlockCommentRule = require("../formatters/rules/style/DocBlockCommentRu
const IndentationRule = require("../formatters/rules/style/IndentationRule.js");
const QuoteStyleRule = require("../formatters/rules/style/QuoteStyleRule.js");
const SemicolonRule = require("../formatters/rules/style/SemicolonRule.js");
const StructuralIndentationRule = require("../formatters/rules/style/StructuralIndentationRule.js");
const FormatterPipeline = require("../pipeline/FormatterPipeline.js");
class ServiceRegistration {
static registerServices(container, config) {
Expand All @@ -21,6 +22,7 @@ class ServiceRegistration {
container.singleton(SemicolonRule.SemicolonRule);
container.singleton(BracketSpacingRule.BracketSpacingRule);
container.singleton(IndentationRule.IndentationRule);
container.singleton(StructuralIndentationRule.StructuralIndentationRule);
container.singleton(BlockSpacingRule.BlockSpacingRule);
container.singleton(DocBlockCommentRule.DocBlockCommentRule);
container.singleton(ImportOrganizationRule.ImportOrganizationRule);
Expand Down
Loading