UNPKG

@dx-box/common

Version:

(DX) by providing common configurations for TypeScript, ESLint, Prettier, and automated Git hooks for formatting and linting.

2 lines (1 loc) 909 B
import*as S from"fs";import*as e from"path";import{Project as b}from"ts-morph";const j=async g=>{const r=e.resolve(g),h=e.dirname(r),c=JSON.parse(S.readFileSync(r,"utf-8")).compilerOptions||{},m=e.resolve(h,c.baseUrl||"."),u=c.paths,P=new b({tsConfigFilePath:r});for(const s of P.getSourceFiles()){let n=!1;s.getImportDeclarations().forEach(o=>{const l=o.getModuleSpecifierValue();if(!l.startsWith("."))return;const d=e.resolve(e.dirname(s.getFilePath()),l),t=e.relative(m,d).replace(/\\/g,"/"),p=Object.entries(u).find(([f,a])=>{const i=a[0].replace(/\*$/,"").replace(/\\/g,"/");return t.startsWith(i)});if(p){const[f,a]=p,i=f.replace(/\*$/,""),F=a[0].replace(/\*$/,"").replace(/\\/g,"/"),v=t.slice(F.length);o.setModuleSpecifier(i+v)}else o.setModuleSpecifier(t.startsWith(".")?t:"./"+t);n=!0}),n&&(await s.save(),console.log(`\u2705 Updated imports in: ${s.getFilePath()}`))}};export{j as convertImports};