Skip to content

Instantly share code, notes, and snippets.

@dsherret
Last active January 12, 2019 00:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dsherret/d70c7137ac69258b2a973f07d064a8ae to your computer and use it in GitHub Desktop.
Save dsherret/d70c7137ac69258b2a973f07d064a8ae to your computer and use it in GitHub Desktop.
Converts all module specifiers within a directory to relative paths.
// untested...
import { Project, SyntaxKind } from "ts-simple-ast";
const project = new Project({ tsConfigFilePath: "tsconfig.json" });
const srcDir = project.getDirectoryOrThrow("./src");
for (const sourceFile of project.getSourceFiles().filter(s => srcDir.isAncestorOf(s))) {
for (const dec of [...sourceFile.getImportDeclarations(), ...sourceFile.getExportDeclarations()]) {
const moduleSpecifierSourceFile = dec.getModuleSpecifierSourceFile();
if (moduleSpecifierSourceFile == null || !srcDir.isAncestorOf(moduleSpecifierSourceFile))
continue;
const newSpecifierValue = sourceFile.getRelativePathAsModuleSpecifierTo(moduleSpecifierSourceFile);
if (newSpecifierValue === dev.getModuleSpecifierValue())
continue;
console.log(`Updating ${dev.getModuleSpecifierValue()} to ${newSpecifierValue}`);
dec.setModuleSpecifier(sourceFile.getRelativePathAsModuleSpecifierTo(moduleSpecifierSourceFile));
}
}
project.save().then(() => console.log("done"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment