Skip to content

Commit

Permalink
Extract out the stylesheet object to run the transformation on directly
Browse files Browse the repository at this point in the history
  • Loading branch information
theBGuy committed Jan 4, 2025
1 parent e482cd6 commit a0f7e49
Showing 1 changed file with 36 additions and 6 deletions.
42 changes: 36 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ const parseFile = (filePath: string) => {
}
ts.forEachChild(sourceFile, visitReferences);

const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed, removeComments: false });

const transformer = (context: ts.TransformationContext) => {
return (node: ts.Node): ts.Node => {
function visit(node: ts.Node): ts.Node {
Expand All @@ -73,7 +71,9 @@ const parseFile = (filePath: string) => {
ts.isVariableDeclaration(node) &&
node.name.getText() === "styles" &&
node.initializer &&
ts.isCallExpression(node.initializer)
ts.isCallExpression(node.initializer) &&
ts.isPropertyAccessExpression(node.initializer.expression) &&
node.initializer.expression.getText() === "StyleSheet.create"
) {
const arg = node.initializer.arguments[0];
if (ts.isObjectLiteralExpression(arg)) {
Expand Down Expand Up @@ -117,13 +117,19 @@ const parseFile = (filePath: string) => {

const result = ts.transform(sourceFile, [transformer]);
const transformedSourceFile = result.transformed[0] as ts.SourceFile;
const updatedCode = printer.printFile(transformedSourceFile);
const updatedCode = ts
.createPrinter({ newLine: ts.NewLineKind.LineFeed, removeComments: false })
.printFile(transformedSourceFile);

result.dispose();

if (hasChanges) {
const updatedSourceFile = ts.createSourceFile(filePath, updatedCode, ts.ScriptTarget.Latest, true, scriptKind);
const updatedStylesheet = extractStylesheet(updatedSourceFile);
const finalCode = replaceStylesheet(sourceCode, updatedStylesheet);

if (!isDryRun) {
fs.writeFileSync(filePath, updatedCode, "utf-8");
fs.writeFileSync(filePath, finalCode, "utf-8");
if (!noFormat) {
formatFile(filePath, formatter);
}
Expand All @@ -136,6 +142,30 @@ const parseFile = (filePath: string) => {
}
};

const extractStylesheet = (sourceFile: ts.SourceFile): string => {
let stylesheetCode = "";
function visit(node: ts.Node) {
if (
ts.isVariableDeclaration(node) &&
node.name.getText() === "styles" &&
node.initializer &&
ts.isCallExpression(node.initializer) &&
ts.isPropertyAccessExpression(node.initializer.expression) &&
node.initializer.expression.getText() === "StyleSheet.create"
) {
const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed, removeComments: false });
stylesheetCode = printer.printNode(ts.EmitHint.Unspecified, node.initializer, sourceFile);
}
ts.forEachChild(node, visit);
}
ts.forEachChild(sourceFile, visit);
return stylesheetCode;
};

const replaceStylesheet = (sourceCode: string, updatedStylesheet: string): string => {
return sourceCode.replace(/StyleSheet\.create\(\{[\s\S]*?\}\)/, updatedStylesheet);
};

const shouldIncludeFile = (filePath: string) => {
const relativePath = path.relative(process.cwd(), filePath); // Make paths relative
const includePatternsArray = includePatterns.trim().split(/\s+/); // Split by space
Expand All @@ -151,7 +181,7 @@ const shouldIncludeFile = (filePath: string) => {

const parseDirectory = (dirPath: string) => {
const files = fs.readdirSync(dirPath);
console.log(dirPath, files.length);
log(dirPath, files.length);
for (const file of files) {
const filePath = path.join(dirPath, file);
if (fs.statSync(filePath).isDirectory()) {
Expand Down

0 comments on commit a0f7e49

Please sign in to comment.