cr-react-cli
Version:
Create react files with a single command
64 lines (63 loc) • 1.89 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createAction = void 0;
// @ts-ignore
const indent_js_1 = __importDefault(require("indent.js"));
var ActionType;
(function (ActionType) {
ActionType["Insert"] = "insert";
ActionType["Overide"] = "overide";
ActionType["NewLine"] = "newLine";
})(ActionType || (ActionType = {}));
const createAction = () => (type) => {
return (target, propertyName, descriptor) => {
const originalFn = descriptor.value;
descriptor.value = function () {
// @ts-ignore
this.actions[this.nextId++] = {
type,
method: originalFn.bind(this),
args: arguments,
};
return this;
};
};
};
exports.createAction = createAction;
class TemplateBuilder {
constructor(fileType) {
this.fileType = fileType;
this.template = '';
this.nextId = 0;
this.actions = {};
}
indent(code) {
return indent_js_1.default[this.fileType](code, { tabString: '\t' });
}
_insert(content) {
this.template = this.template + content;
return this;
}
insert(content) {
return this.insertAction({
type: ActionType.Insert,
method: this._insert.bind(this),
args: [content],
});
}
insertAction(options) {
this.actions[this.nextId++] = options;
return this;
}
toString() {
Object.values(this.actions).forEach((action) => {
action.method(...action.args);
});
const template = this.template;
this.template = '';
return this.indent(template);
}
}
exports.default = TemplateBuilder;