figma-styles-to-ts
Version:
Generate typescript files with colors, fonts and shadows from figma styles. Generate icons and images from figma file.
218 lines • 10.7 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
var ast_1 = require("../types/ast");
var fs = require("fs");
var ImageTemplate = /** @class */ (function () {
function ImageTemplate(config, api) {
this._api = api;
this._config = config;
}
ImageTemplate.prototype.init = function () {
return this._getImages();
};
ImageTemplate.prototype._normalizeImageName = function (name) {
name = name.trim();
var regex = /(-|\/|\s)/g;
if (regex.test(name)) {
name = name.toLocaleLowerCase();
name = name
.split(regex)
.map(function (word) {
return regex.test(word)
? ""
: word[0].toUpperCase() + word.slice(1, word.length);
})
.join("");
}
else {
name = name[0].toUpperCase() + name.slice(1, name.length);
}
return name;
};
ImageTemplate.prototype._searchRecursively = function (ids, container) {
var _this = this;
var _a;
(_a = container === null || container === void 0 ? void 0 : container.children) === null || _a === void 0 ? void 0 : _a.forEach(function (node) {
var _a, _b;
if (node.type === "RECTANGLE" &&
((_a = node.fills) === null || _a === void 0 ? void 0 : _a.length) &&
node.fills[0].type === ast_1.PaintType.IMAGE &&
node.fills[0].blendMode ===
ast_1.BlendMode.NORMAL &&
node.fills[0].scaleMode ===
ast_1.PaintSolidScaleMode.FILL &&
((_b = node.exportSettings) === null || _b === void 0 ? void 0 : _b.length)) {
node = node;
ids[node.id] = {
name: _this._normalizeImageName(node.name),
export: node.exportSettings.map(function (e) {
var scale = e.constraint.type === ast_1.ConstrainType.SCALE
? e.constraint.value
: 1;
return {
suffix: e.suffix,
format: e.format,
scale: scale,
};
}),
};
}
else {
_this._searchRecursively(ids, node);
}
});
};
ImageTemplate.prototype._getImageIds = function () {
var _a;
return __awaiter(this, void 0, void 0, function () {
var pageName, page, _b, container, ids;
var _this = this;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
pageName = this._config.image.page;
if (!(pageName === "")) return [3 /*break*/, 2];
return [4 /*yield*/, this._api.getFirstPage()];
case 1:
_b = _c.sent();
return [3 /*break*/, 4];
case 2: return [4 /*yield*/, this._api.getPageByName(pageName)];
case 3:
_b = _c.sent();
_c.label = 4;
case 4:
page = _b;
if (!page) {
throw new Error("No page could be found !\n");
}
container = (_a = page.children.find(function (doc) { return doc.name === _this._config.image.container; })) !== null && _a !== void 0 ? _a : page;
if (!container) {
throw new Error("The container (" + this._config.image.container + ") couldn't be found !\n");
}
ids = {};
this._searchRecursively(ids, container);
ids = Object.entries(ids).map(function (_a) {
var id = _a[0], image = _a[1];
return { id: id, name: image.name, export: image.export };
});
if (!ids || !ids.length) {
throw new Error("Couldn't get any images from api.\n");
}
return [2 /*return*/, ids];
}
});
});
};
ImageTemplate.prototype._getImages = function () {
return __awaiter(this, void 0, void 0, function () {
var image_ids, image_chunks, promises, _a;
var _this = this;
return __generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, this._getImageIds()];
case 1:
image_ids = _b.sent();
image_chunks = {};
image_ids === null || image_ids === void 0 ? void 0 : image_ids.forEach(function (image) {
image.export.forEach(function (e) {
if (!image_chunks[e.format + "_" + e.scale]) {
image_chunks[e.format + "_" + e.scale] = [];
}
image_chunks[e.format + "_" + e.scale].push({
id: image.id,
name: image.name,
suffix: e.suffix,
format: e.format,
scale: e.scale,
});
});
});
promises = Object.values(image_chunks).map(function (images) {
return _this._api.getImagesWithExportSettings(images, images[0].format, images[0].scale);
});
_a = this;
return [4 /*yield*/, Promise.all(promises)];
case 2:
_a._images = (_b.sent()).flat(1);
return [2 /*return*/];
}
});
});
};
ImageTemplate.prototype._generateFiles = function () {
return __awaiter(this, void 0, void 0, function () {
var promises;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!this._images) {
throw new Error("No images fetched from api.\n");
}
promises = this._images.map(function (image) {
return new Promise(function (resolve, reject) {
if (!image.data) {
reject(new Error("Couldn't fetch data from Image (" + image.name + ") !\n"));
return;
}
var file_name = "" + image.name + image.suffix + "." + image.format.toLocaleLowerCase();
image.data
.pipe(fs.createWriteStream(_this._config.image.outDir + "/" + file_name))
.on("finish", function () {
console.log(file_name + " created successfully !");
resolve();
})
.on("error", function (e) {
console.error(e);
resolve();
});
});
});
return [4 /*yield*/, Promise.all(promises)];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
};
return ImageTemplate;
}());
exports.default = ImageTemplate;
//# sourceMappingURL=ImageTemplate.js.map