Skip to content

Commit

Permalink
Stuff
Browse files Browse the repository at this point in the history
Hugos68 committed Jan 15, 2025
1 parent 6a5c22f commit 63c1db7
Showing 6 changed files with 33 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { transformClasses } from './transform-classes.js';
import { createSourceFile } from '../../../../../utility/create-source-file.js';
import { COMPONENT_MAPPINGS } from '../utility/component-mappings';
import { REMOVED_COMPONENTS } from '../utility/removed-components';
import { Node } from 'ts-morph';
import {addNamedImport} from "../../../../../utility/ts-morph/add-named-import";
import {parseSourceFile} from "../../../../../utility/ts-morph/parse-source-file";

function transformModule(code: string) {
const file = createSourceFile(code);
const file = parseSourceFile(code);
file.forEachDescendant((node) => {
if (Node.isImportDeclaration(node)) {
const moduleSpecifier = node.getModuleSpecifier();
@@ -16,7 +17,8 @@ function transformModule(code: string) {
if (Node.isImportSpecifier(node)) {
const name = node.getName();
if (name in COMPONENT_MAPPINGS) {
node.setName(COMPONENT_MAPPINGS[name]);
node.remove();
addNamedImport(file, '@skeletonlabs/skeleton-svelte', COMPONENT_MAPPINGS[name]);
}
if (REMOVED_COMPONENTS.includes(name)) {
const parent = node.getParent().getParent().getParent();
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { CallExpression, Node, ObjectLiteralExpression, SourceFile } from 'ts-morph';
import { getDefaultExportObject } from '../../../../../utility/get-default-export-object';
import { THEME_MAPPINGS } from '../utility/theme-mappings';
import { createSourceFile } from '../../../../../utility/create-source-file';
import { FALLBACK_THEME } from '../utility/constants';
import {addNamedImport} from "../../../../../utility/ts-morph/add-named-import";
import {getDefaultExportObject} from "../../../../../utility/ts-morph/get-default-export-object";
import {parseSourceFile} from "../../../../../utility/ts-morph/parse-source-file";

function isJoinCallExpression(node: Node): node is CallExpression {
if (!Node.isCallExpression(node)) {
@@ -82,15 +83,7 @@ function transformTailwindContentOption(file: SourceFile) {
}
joinCallExpression.replaceWithText('contentPath(import.meta.url, "svelte")');
file.getImportDeclaration('@skeletonlabs/tw-plugin')?.remove();
const skeletonPluginImportDeclaration = file.getImportDeclaration('@skeletonlabs/skeleton/plugin');
if (skeletonPluginImportDeclaration) {
skeletonPluginImportDeclaration.addNamedImport('contentPath');
} else {
file.addImportDeclaration({
moduleSpecifier: '@skeletonlabs/skeleton/plugin',
namedImports: ['contentPath']
});
}
addNamedImport(file, '@skeletonlabs/skeleton/plugin', 'contentPath');
}

function getThemeName(node: Node) {
@@ -179,15 +172,7 @@ function transformSkeletonThemesOption(file: SourceFile) {
};
}
file.getImportDeclaration('@skeletonlabs/tw-plugin')?.remove();
const skeletonPluginImportDeclaration = file.getImportDeclaration('@skeletonlabs/skeleton/plugin');
if (skeletonPluginImportDeclaration) {
skeletonPluginImportDeclaration.addNamedImport('skeleton');
} else {
file.addImportDeclaration({
moduleSpecifier: '@skeletonlabs/skeleton/plugin',
namedImports: ['skeleton']
});
}
addNamedImport(file, '@skeletonlabs/skeleton/plugin', 'skeleton');
const skeletonConfigObject = skeletonPluginCallExpression.getArguments().at(0);
if (!(skeletonConfigObject && Node.isObjectLiteralExpression(skeletonConfigObject))) {
return {
@@ -244,7 +229,7 @@ ${[...customThemes].map((theme) => ` * - ${theme}`).join('\n')}
}

function transformTailwindConfig(code: string) {
const file = createSourceFile(code);
const file = parseSourceFile(code);
transformTailwindContentOption(file);
const themes = transformSkeletonThemesOption(file);
file.fixUnusedIdentifiers();
Empty file.
20 changes: 20 additions & 0 deletions packages/skeleton-cli/src/utility/ts-morph/add-named-import.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { SourceFile } from "ts-morph";

function addNamedImport(file: SourceFile, specifier: string, name: string) {
const existingImportDeclaration = file.getImportDeclaration((importDeclaration) => {
const moduleSpecifier = importDeclaration.getModuleSpecifier().getLiteralText();
return moduleSpecifier === specifier;
});
if (existingImportDeclaration) {
if (!existingImportDeclaration.getNamedImports().some((namedImport) => namedImport.getName() === name)) {
existingImportDeclaration.addNamedImport(name);
}
} else {
file.addImportDeclaration({
moduleSpecifier: specifier,
namedImports: [name]
});
}
}

export { addNamedImport };
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Project } from 'ts-morph';

function createSourceFile(code: string) {
function parseSourceFile(code: string) {
const project = new Project({
useInMemoryFileSystem: true
});
return project.createSourceFile('virtual.ts', code);
}

export { createSourceFile };
export { parseSourceFile };

0 comments on commit 63c1db7

Please sign in to comment.