Skip to content

Commit 8f0ceec

Browse files
committed
chore: update import paths and enhance insertImport function for better handling
1 parent 2ce56a5 commit 8f0ceec

File tree

3 files changed

+10
-8
lines changed

3 files changed

+10
-8
lines changed

packages/create-vuestic/src/steps/2.addVuestic/insert-vuestic-compiler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { insertImport } from "./insert-import";
1+
import { insertImport } from "../../utils/insert-import";
22

33
import { usePackageJson } from '../../composables/usePackageJson';
44
import { versions } from '../../versions';

packages/create-vuestic/src/utils/add-vite-plugin.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ export async function addVitePlugin(viteConfigSource: string, plugin: {
1111
const lines = viteConfigSource.split('\n');
1212
const pluginsLineIndex = lines.findIndex(line => line.includes('plugins:'));
1313
if (pluginsLineIndex === -1) {
14+
// TODO: Handle case when plugins array is not found. For now, we expect create-vue adds vue plugin with plugins array.
1415
return viteConfigSource;
1516
}
17+
1618
let insertIndex = -1;
1719
let bracketBalance = 0;
1820

@@ -27,7 +29,8 @@ export async function addVitePlugin(viteConfigSource: string, plugin: {
2729
}
2830
}
2931

30-
const intent = lines[pluginsLineIndex + 1].match(/^\s*/)?.[0] || '';
32+
const indentSourceLine = lines[pluginsLineIndex + 1] ?? lines[pluginsLineIndex];
33+
const intent = indentSourceLine.match(/^\s*/)?.[0] || '';
3134

3235
if (insertIndex !== -1) {
3336
lines.splice(insertIndex, 0, `${intent}${plugin.name}(),`);

packages/create-vuestic/src/utils/insert-import.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ export const insertImport = (source: string, imports: string[]) => {
22
const lines = source.split('\n')
33
const importString = imports.join('\n')
44

5-
const lastImportIndex = lines.length - lines.reverse().findIndex(line => line.match(/import/))
5+
const lastImportIndex = lines.findLastIndex(line => line.match(/^import/))
66

7-
if (lastImportIndex > lines.length - 1) {
8-
lines.splice(lastImportIndex, 0, importString)
9-
10-
return lines.join('\n')
7+
if (lastImportIndex === -1) {
8+
return importString + '\n' + source
119
}
1210

13-
return importString + '\n' + source
11+
const insertionIndex = lastImportIndex + 1
12+
return [...lines.slice(0, insertionIndex), importString, ...lines.slice(insertionIndex)].join('\n')
1413
}

0 commit comments

Comments
 (0)