forked from unlight/prisma-nestjs-graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-import.ts
33 lines (30 loc) · 928 Bytes
/
generate-import.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import { SourceFile } from 'ts-morph';
export function generateImport(args: {
name: string;
sourceFile: SourceFile;
moduleSpecifier: string | undefined;
}) {
const { moduleSpecifier, name, sourceFile } = args;
if (!moduleSpecifier) {
return name;
}
let importDeclaration = sourceFile.getImportDeclaration(
importDeclaration =>
importDeclaration.getModuleSpecifier().getLiteralValue() ===
moduleSpecifier,
);
if (!importDeclaration) {
importDeclaration = sourceFile.addImportDeclaration({
moduleSpecifier,
});
}
let importSpecifier = importDeclaration
.getNamedImports()
.find(importSpecifier => importSpecifier.getName() === name);
if (!importSpecifier) {
importSpecifier = importDeclaration.addNamedImport({
name,
});
}
return importSpecifier.getName();
}