nx/packages/vue/src/utils/ast-utils.ts
Jack Hsu 769974b45a
feat(vue): init, app, component and lib generators (#19130)
Co-authored-by: Katerina Skroumpelou <sk.katherine@gmail.com>
2023-09-13 15:05:10 -04:00

36 lines
854 B
TypeScript

import type * as ts from 'typescript';
import { findNodes } from '@nx/js';
import { ChangeType, StringChange } from '@nx/devkit';
import { ensureTypescript } from '@nx/js/src/utils/typescript/ensure-typescript';
let tsModule: typeof import('typescript');
export function addImport(
source: ts.SourceFile,
statement: string
): StringChange[] {
if (!tsModule) {
tsModule = ensureTypescript();
}
const allImports = findNodes(source, tsModule.SyntaxKind.ImportDeclaration);
if (allImports.length > 0) {
const lastImport = allImports[allImports.length - 1];
return [
{
type: ChangeType.Insert,
index: lastImport.end + 1,
text: `\n${statement}\n`,
},
];
} else {
return [
{
type: ChangeType.Insert,
index: 0,
text: `\n${statement}\n`,
},
];
}
}