mirror of
https://github.com/vrtmrz/obsidian-livesync.git
synced 2026-06-11 08:50:17 +00:00
(chore): split utils
This commit is contained in:
@@ -0,0 +1,186 @@
|
||||
// Delete references to utils.ts and replace them with new imports based on the importMap.
|
||||
// Use this script by running `deno run --allow-read --allow-write --allow-run refactor-import-utils.ts` from the utilsdeno directory.
|
||||
import { Project } from "npm:ts-morph";
|
||||
|
||||
const isDryRun = !Deno.args.includes("--run");
|
||||
|
||||
if (isDryRun) {
|
||||
console.log("=== DRY RUN MODE ===");
|
||||
console.log(
|
||||
"To apply changes, run with: deno run --allow-read --allow-write --allow-run refactor-import-utils.ts --run\n"
|
||||
);
|
||||
}
|
||||
|
||||
const project = new Project({ tsConfigFilePath: "../tsconfig.json" });
|
||||
|
||||
const importMap = new Map<string, string>();
|
||||
|
||||
const targetFiles = [
|
||||
"utils.concurrency.ts",
|
||||
"utils.timer.ts",
|
||||
"utils.notations.ts",
|
||||
"utils.database.ts",
|
||||
"utils.regexp.ts",
|
||||
"utils.settings.ts",
|
||||
"utils.patch.ts",
|
||||
"utils.misc.ts",
|
||||
];
|
||||
|
||||
// 1. Map exports from our newly created subfiles
|
||||
for (const sourceFile of project.getSourceFiles()) {
|
||||
const filePath = sourceFile.getFilePath();
|
||||
const fileName = sourceFile.getBaseName();
|
||||
if (filePath.includes("src/lib/src/common/") && targetFiles.includes(fileName)) {
|
||||
const exports = sourceFile.getExportedDeclarations();
|
||||
for (const [name] of exports) {
|
||||
const relativePath = filePath.split("src/lib/src/")[1].replace(/\.ts$/, "");
|
||||
importMap.set(name, `@lib/${relativePath}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Map exports/imports of octagonal-wheels in utils.ts
|
||||
const utilsFile = project.getSourceFile("src/lib/src/common/utils.ts");
|
||||
if (utilsFile) {
|
||||
// Parse imports from octagonal-wheels
|
||||
for (const imp of utilsFile.getImportDeclarations()) {
|
||||
const moduleSpec = imp.getModuleSpecifierValue();
|
||||
if (moduleSpec.startsWith("octagonal-wheels")) {
|
||||
for (const namedImport of imp.getNamedImports()) {
|
||||
importMap.set(namedImport.getName(), moduleSpec);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Parse export declarations from octagonal-wheels
|
||||
for (const exp of utilsFile.getExportDeclarations()) {
|
||||
const moduleSpec = exp.getModuleSpecifierValue();
|
||||
if (moduleSpec && moduleSpec.startsWith("octagonal-wheels")) {
|
||||
for (const namedExport of exp.getNamedExports()) {
|
||||
importMap.set(namedExport.getName(), moduleSpec);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`Built importMap with ${importMap.size} mappings.\n`);
|
||||
|
||||
let modifiedFilesCount = 0;
|
||||
|
||||
// 3. Loop through all source files and replace imports
|
||||
for (const sourceFile of project.getSourceFiles()) {
|
||||
let fileModified = false;
|
||||
const imports = sourceFile.getImportDeclarations();
|
||||
|
||||
for (const imp of imports) {
|
||||
const moduleSpec = imp.getModuleSpecifierValue();
|
||||
const isUtilsImport =
|
||||
moduleSpec === "@lib/common/utils" ||
|
||||
moduleSpec === "@lib/common/utils.ts" ||
|
||||
moduleSpec.endsWith("/common/utils") ||
|
||||
moduleSpec.endsWith("/common/utils.ts");
|
||||
|
||||
if (isUtilsImport) {
|
||||
const namedImports = imp.getNamedImports();
|
||||
const defaultImport = imp.getDefaultImport();
|
||||
|
||||
const importsToReplace: Record<string, { name: string; newPath: string; isTypeOnly: boolean }[]> = {};
|
||||
for (const namedImport of namedImports) {
|
||||
const name = namedImport.getName();
|
||||
let newPath = importMap.get(name);
|
||||
if (newPath) {
|
||||
// If original ended with .ts and the new path starts with @lib, keep .ts
|
||||
if (moduleSpec.endsWith(".ts") && newPath.startsWith("@lib/")) {
|
||||
newPath = newPath + ".ts";
|
||||
}
|
||||
if (!importsToReplace[newPath]) {
|
||||
importsToReplace[newPath] = [];
|
||||
}
|
||||
importsToReplace[newPath].push({
|
||||
name,
|
||||
newPath,
|
||||
isTypeOnly: namedImport.isTypeOnly() || imp.isTypeOnly(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (Object.keys(importsToReplace).length > 0 || (defaultImport && importMap.has(defaultImport.getText()))) {
|
||||
fileModified = true;
|
||||
|
||||
console.log(`File: ${sourceFile.getFilePath().split("obsidian-livesync/")[1]}`);
|
||||
console.log(` Old: ${imp.getText()}`);
|
||||
}
|
||||
|
||||
if (!isDryRun) {
|
||||
// Apply replacements
|
||||
for (const newPath in importsToReplace) {
|
||||
const isTypeOnly = importsToReplace[newPath].filter((i) => i.isTypeOnly);
|
||||
if (isTypeOnly.length > 0) {
|
||||
sourceFile.insertImportDeclaration(imp.getChildIndex(), {
|
||||
namedImports: isTypeOnly.map((i) => i.name),
|
||||
moduleSpecifier: newPath,
|
||||
isTypeOnly: true,
|
||||
});
|
||||
}
|
||||
const isValueImport = importsToReplace[newPath].filter((i) => !i.isTypeOnly);
|
||||
if (isValueImport.length > 0) {
|
||||
sourceFile.insertImportDeclaration(imp.getChildIndex(), {
|
||||
namedImports: isValueImport.map((i) => i.name),
|
||||
moduleSpecifier: newPath,
|
||||
isTypeOnly: false,
|
||||
});
|
||||
}
|
||||
for (const { name } of importsToReplace[newPath]) {
|
||||
const namedImport = imp.getNamedImports().find((ni) => ni.getName() === name);
|
||||
if (namedImport) {
|
||||
namedImport.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// In dry run, just print what it would do
|
||||
for (const newPath in importsToReplace) {
|
||||
const names = importsToReplace[newPath].map((i) => i.name).join(", ");
|
||||
console.log(` -> Would import { ${names} } from "${newPath}"`);
|
||||
}
|
||||
}
|
||||
|
||||
if (defaultImport) {
|
||||
const name = defaultImport.getText();
|
||||
let newPath = importMap.get(name);
|
||||
if (newPath) {
|
||||
if (moduleSpec.endsWith(".ts") && newPath.startsWith("@lib/")) {
|
||||
newPath = newPath + ".ts";
|
||||
}
|
||||
if (!isDryRun) {
|
||||
sourceFile.insertImportDeclaration(imp.getChildIndex(), {
|
||||
defaultImport: name,
|
||||
moduleSpecifier: newPath,
|
||||
isTypeOnly: imp.isTypeOnly(),
|
||||
});
|
||||
imp.removeDefaultImport();
|
||||
} else {
|
||||
console.log(` -> Would import default ${name} from "${newPath}"`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!isDryRun) {
|
||||
if (imp.getNamedImports().length === 0 && !imp.getDefaultImport()) {
|
||||
imp.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (fileModified) {
|
||||
modifiedFilesCount++;
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`\nTotal files to modify: ${modifiedFilesCount}`);
|
||||
|
||||
if (!isDryRun) {
|
||||
project.saveSync();
|
||||
console.log("All changes successfully saved.");
|
||||
} else {
|
||||
console.log("Dry run complete. No changes were written to files.");
|
||||
}
|
||||
+100
-94
@@ -8,23 +8,23 @@ const importMap = new Map<string, string>();
|
||||
// Build a map of types moved out of Models.
|
||||
// Under src/lib/src/common/models.
|
||||
for (const sourceFile of project.getSourceFiles()) {
|
||||
if (sourceFile.getFilePath().includes("src/lib/src/common/models")) {
|
||||
const exports = sourceFile.getExportedDeclarations();
|
||||
for (const [name, declarations] of exports) {
|
||||
for (const declaration of declarations) {
|
||||
if (
|
||||
// declaration.getKindName() === "TypeAliasDeclaration" ||
|
||||
// declaration.getKindName() === "InterfaceDeclaration" ||
|
||||
// declaration.getKindName() === "EnumDeclaration" ||
|
||||
true
|
||||
) {
|
||||
console.log(`Found type export in ${sourceFile.getFilePath()}:`, name);
|
||||
const relativePath = sourceFile.getFilePath().split("src/lib/src/")[1].replace(/\.ts$/, "");
|
||||
importMap.set(name, `@lib/${relativePath}`);
|
||||
if (sourceFile.getFilePath().includes("src/lib/src/common/models")) {
|
||||
const exports = sourceFile.getExportedDeclarations();
|
||||
for (const [name, declarations] of exports) {
|
||||
for (const declaration of declarations) {
|
||||
if (
|
||||
// declaration.getKindName() === "TypeAliasDeclaration" ||
|
||||
// declaration.getKindName() === "InterfaceDeclaration" ||
|
||||
// declaration.getKindName() === "EnumDeclaration" ||
|
||||
true
|
||||
) {
|
||||
console.log(`Found type export in ${sourceFile.getFilePath()}:`, name);
|
||||
const relativePath = sourceFile.getFilePath().split("src/lib/src/")[1].replace(/\.ts$/, "");
|
||||
importMap.set(name, `@lib/${relativePath}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Extras
|
||||
|
||||
@@ -40,91 +40,97 @@ console.log("Import map:", importMap);
|
||||
|
||||
// Loop through all files that import from types.ts.
|
||||
for (const sourceFile of project.getSourceFiles()) {
|
||||
const imports = sourceFile.getImportDeclarations();
|
||||
const imports = sourceFile.getImportDeclarations();
|
||||
|
||||
for (const imp of imports) {
|
||||
if (imp.getModuleSpecifierValue().includes("types.ts") && imp.getModuleSpecifierValue().startsWith("@lib/common/")) {
|
||||
// Collect imports from types.ts.
|
||||
const namedImports = imp.getNamedImports();
|
||||
const defaultImport = imp.getDefaultImport();
|
||||
console.log(`Found import in ${sourceFile.getFilePath()}:`, {
|
||||
namedImports: namedImports.map((ni) => ni.getText()),
|
||||
defaultImport: defaultImport ? defaultImport.getText() : null,
|
||||
});
|
||||
// Group imports by their names and generate new import paths based on the importMap
|
||||
const importsToReplace: Record<string, { name: string; newPath: string; isTypeOnly: boolean }[]> = {};
|
||||
for (const namedImport of namedImports) {
|
||||
const name = namedImport.getName();
|
||||
const newPath = importMap.get(name);
|
||||
if (newPath) {
|
||||
console.log(
|
||||
`Will replace import of ${name} in ${sourceFile.getFilePath()} with new path:`,
|
||||
newPath
|
||||
);
|
||||
if (!importsToReplace[newPath]) {
|
||||
importsToReplace[newPath] = [];
|
||||
}
|
||||
importsToReplace[newPath].push({ name, newPath, isTypeOnly: namedImport.isTypeOnly() || imp.isTypeOnly() });
|
||||
}
|
||||
}
|
||||
for (const imp of imports) {
|
||||
if (
|
||||
imp.getModuleSpecifierValue().includes("types.ts") &&
|
||||
imp.getModuleSpecifierValue().startsWith("@lib/common/")
|
||||
) {
|
||||
// Collect imports from types.ts.
|
||||
const namedImports = imp.getNamedImports();
|
||||
const defaultImport = imp.getDefaultImport();
|
||||
console.log(`Found import in ${sourceFile.getFilePath()}:`, {
|
||||
namedImports: namedImports.map((ni) => ni.getText()),
|
||||
defaultImport: defaultImport ? defaultImport.getText() : null,
|
||||
});
|
||||
// Group imports by their names and generate new import paths based on the importMap
|
||||
const importsToReplace: Record<string, { name: string; newPath: string; isTypeOnly: boolean }[]> = {};
|
||||
for (const namedImport of namedImports) {
|
||||
const name = namedImport.getName();
|
||||
const newPath = importMap.get(name);
|
||||
if (newPath) {
|
||||
console.log(
|
||||
`Will replace import of ${name} in ${sourceFile.getFilePath()} with new path:`,
|
||||
newPath
|
||||
);
|
||||
if (!importsToReplace[newPath]) {
|
||||
importsToReplace[newPath] = [];
|
||||
}
|
||||
importsToReplace[newPath].push({
|
||||
name,
|
||||
newPath,
|
||||
isTypeOnly: namedImport.isTypeOnly() || imp.isTypeOnly(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// For each import, generate a new path from importMap and replace it.
|
||||
// Split the import when it needs to become multiple imports.
|
||||
// For each import, generate a new path from importMap and replace it.
|
||||
// Split the import when it needs to become multiple imports.
|
||||
|
||||
for (const newPath in importsToReplace) {
|
||||
// First, handle type-only imports.
|
||||
const isTypeOnly = importsToReplace[newPath].filter((i) => i.isTypeOnly);
|
||||
if (isTypeOnly.length > 0) {
|
||||
sourceFile.insertImportDeclaration(imp.getChildIndex(), {
|
||||
namedImports: isTypeOnly.map((i) => i.name),
|
||||
moduleSpecifier: newPath,
|
||||
isTypeOnly: true,
|
||||
});
|
||||
}
|
||||
// Then, handle non-type-only imports.
|
||||
const isValueImport = importsToReplace[newPath].filter((i) => !i.isTypeOnly);
|
||||
if (isValueImport.length > 0) {
|
||||
for (const newPath in importsToReplace) {
|
||||
// First, handle type-only imports.
|
||||
const isTypeOnly = importsToReplace[newPath].filter((i) => i.isTypeOnly);
|
||||
if (isTypeOnly.length > 0) {
|
||||
sourceFile.insertImportDeclaration(imp.getChildIndex(), {
|
||||
namedImports: isTypeOnly.map((i) => i.name),
|
||||
moduleSpecifier: newPath,
|
||||
isTypeOnly: true,
|
||||
});
|
||||
}
|
||||
// Then, handle non-type-only imports.
|
||||
const isValueImport = importsToReplace[newPath].filter((i) => !i.isTypeOnly);
|
||||
if (isValueImport.length > 0) {
|
||||
sourceFile.insertImportDeclaration(imp.getChildIndex(), {
|
||||
namedImports: isValueImport.map((i) => i.name),
|
||||
moduleSpecifier: newPath,
|
||||
isTypeOnly: false,
|
||||
});
|
||||
}
|
||||
// Remove the replaced named imports from the old import.
|
||||
for (const { name } of importsToReplace[newPath]) {
|
||||
const namedImport = imp.getNamedImports().find((ni) => ni.getName() === name);
|
||||
if (namedImport) {
|
||||
namedImport.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
// If there is also a default import and it exists in importMap, replace it too.
|
||||
if (defaultImport) {
|
||||
const name = defaultImport.getText();
|
||||
const newPath = importMap.get(name);
|
||||
|
||||
sourceFile.insertImportDeclaration(imp.getChildIndex(), {
|
||||
namedImports: isValueImport.map((i) => i.name),
|
||||
moduleSpecifier: newPath,
|
||||
isTypeOnly: false,
|
||||
});
|
||||
if (newPath) {
|
||||
console.log(
|
||||
`Replacing default import of ${name} in ${sourceFile.getFilePath()} with new path:`,
|
||||
newPath
|
||||
);
|
||||
// Add the new import statement.
|
||||
sourceFile.insertImportDeclaration(imp.getChildIndex(), {
|
||||
defaultImport: name,
|
||||
moduleSpecifier: newPath,
|
||||
isTypeOnly: imp.isTypeOnly(),
|
||||
});
|
||||
// Remove the default import from the old import.
|
||||
imp.removeDefaultImport();
|
||||
}
|
||||
}
|
||||
if (imp.getNamedImports().length === 0 && !imp.getDefaultImport()) {
|
||||
// Delete the entire import statement if nothing remains.
|
||||
imp.remove();
|
||||
}
|
||||
}
|
||||
// Remove the replaced named imports from the old import.
|
||||
for (const { name } of importsToReplace[newPath]) {
|
||||
const namedImport = imp.getNamedImports().find((ni) => ni.getName() === name);
|
||||
if (namedImport) {
|
||||
namedImport.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
// If there is also a default import and it exists in importMap, replace it too.
|
||||
if (defaultImport) {
|
||||
const name = defaultImport.getText();
|
||||
const newPath = importMap.get(name);
|
||||
|
||||
if (newPath) {
|
||||
console.log(
|
||||
`Replacing default import of ${name} in ${sourceFile.getFilePath()} with new path:`,
|
||||
newPath
|
||||
);
|
||||
// Add the new import statement.
|
||||
sourceFile.insertImportDeclaration(imp.getChildIndex(), {
|
||||
defaultImport: name,
|
||||
moduleSpecifier: newPath,
|
||||
isTypeOnly: imp.isTypeOnly(),
|
||||
});
|
||||
// Remove the default import from the old import.
|
||||
imp.removeDefaultImport();
|
||||
}
|
||||
}
|
||||
if (imp.getNamedImports().length === 0 && !imp.getDefaultImport()) {
|
||||
// Delete the entire import statement if nothing remains.
|
||||
imp.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Save everything at the end.
|
||||
|
||||
Reference in New Issue
Block a user