@drangis-tech/file-writer
Version:
TSX file writer utility using ts-morph
116 lines (114 loc) • 3.97 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var index_exports = {};
__export(index_exports, {
FileWriter: () => FileWriter
});
module.exports = __toCommonJS(index_exports);
// src/writer.ts
var import_ts_morph = require("ts-morph");
var FileWriter = class {
constructor() {
this.project = new import_ts_morph.Project({
skipFileDependencyResolution: true
});
}
/**
* Apply a change to a TSX file based on the design map.
*/
async applyChange(change, designMap, projectRoot) {
const entry = designMap[change.id];
if (!entry) {
throw new Error(`No mapping found for id: ${change.id}`);
}
const filePath = `${projectRoot}/${entry.file}`;
const sourceFile = this.project.addSourceFileAtPath(filePath);
const jsxElements = sourceFile.getDescendantsOfKind(
import_ts_morph.SyntaxKind.JsxSelfClosingElement
);
const jsxOpeningElements = sourceFile.getDescendantsOfKind(
import_ts_morph.SyntaxKind.JsxOpeningElement
);
let targetElement = null;
for (const el of jsxElements) {
const attr = el.getAttribute("data-edit-id");
if (attr && attr.getText().includes(`"${change.id}"`)) {
targetElement = el;
break;
}
}
if (!targetElement) {
for (const el of jsxOpeningElements) {
const attr = el.getAttribute("data-edit-id");
if (attr && attr.getText().includes(`"${change.id}"`)) {
targetElement = el;
break;
}
}
}
if (!targetElement) {
throw new Error(
`Could not find JSX element with data-edit-id="${change.id}" in ${entry.file}`
);
}
if (change.kind === "text") {
const parent = targetElement.getParent();
if (parent && parent.getKind() === import_ts_morph.SyntaxKind.JsxElement) {
const jsxElement = parent;
const children = jsxElement.getJsxChildren();
const textNode = children.find(
(child) => child.getKind() === import_ts_morph.SyntaxKind.JsxText
);
if (textNode) {
textNode.replaceWithText(change.value);
} else {
const openingElement = jsxElement.getOpeningElement();
openingElement.insertText(openingElement.getEnd(), change.value);
}
} else {
throw new Error(
`Element with data-edit-id="${change.id}" is self-closing and cannot contain text. Please use a non-self-closing element.`
);
}
} else if (change.kind === "classes") {
let classNameAttr = targetElement.getAttribute("className");
if (classNameAttr) {
const initializer = classNameAttr.getInitializer();
if (initializer) {
initializer.replaceWithText(`"${change.value}"`);
} else {
classNameAttr.setInitializer(`"${change.value}"`);
}
} else {
targetElement.addAttribute({
name: "className",
initializer: `"${change.value}"`
});
}
}
await sourceFile.save();
this.project.removeSourceFile(sourceFile);
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
FileWriter
});
//# sourceMappingURL=index.js.map