Last active
January 12, 2019 00:54
-
-
Save dsherret/d70c7137ac69258b2a973f07d064a8ae to your computer and use it in GitHub Desktop.
Converts all module specifiers within a directory to relative paths.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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