Skip to content

Instantly share code, notes, and snippets.

@Matthbo
Created January 6, 2021 16:19
Show Gist options
  • Save Matthbo/321d992ae46ddf0a2d97b904acdbb0bb to your computer and use it in GitHub Desktop.
Save Matthbo/321d992ae46ddf0a2d97b904acdbb0bb to your computer and use it in GitHub Desktop.
Stage portfolio code snippets
export async function generateModuleStore(moduleConfig: ConfigFile){
const project = new tsmorph.Project({
compilerOptions: tsmorphCompilerOptions
});
await Promise.all(moduleConfig.store!.map(async store => {
const storeFileName: string = path.basename(store.location, ".json"),
storeName = storeFileName.replace(/[^A-Za-z0-9_]/g, ""),
storeJSON = store.fileSource,
variableName = `INITIAL_${storeName.toUpperCase()}`;
let storeTS: tsmorph.SourceFile;
try{
try {
await fs.promises.unlink(path.join(
path.dirname(store.location), `${storeFileName}.ts`
));
} catch (Error) {
if (Error.code !== 'ENOENT') throw Error;
}
storeTS = project.createSourceFile(
path.join(
path.dirname(store.location), `${storeFileName}.ts`
), ""
);
storeTS.addVariableStatement({
declarationKind: tsmorph.VariableDeclarationKind.Const,
declarations: [{
name: variableName,
initializer: `${JSON.stringify(storeJSON, null, 2)}`
}]
});
storeTS.addExportDeclaration({
namedExports: [variableName],
});
storeTS.formatText(tsmorphTextFormat);
await storeTS.save();
} catch (Error) {
logger.error(Error);
}
}));
}
private setStyleFromProperties(View: View) {
if(View.properties != undefined){
const styleObj: { [index: string]: string; } = {};
/* Add previously assigned styles to styleObj */
if(View.properties.style !== undefined){
const props = (View.properties.style as string).split(";");
props.splice(-1,1); // Removes empty last item
props.forEach((prop) => {
const [key, value] = prop.split(":");
styleObj[key.trim()] = value.trim();
})
}
if (View.properties.width !== undefined) {
styleObj["width"] = `${View.properties.width}px`;
delete View.properties.width;
}
if (View.properties.minwidth !== undefined) {
styleObj["min-width"] = `${View.properties.minwidth}px`;
delete View.properties.minwidth;
}
if (View.properties.maxwidth !== undefined) {
styleObj["max-width"] = `${View.properties.maxwidth}px`;
delete View.properties.maxwidth;
}
if (View.properties.height !== undefined) {
styleObj["height"] = `${View.properties.height}px`;
delete View.properties.height;
}
if (View.properties.minheight !== undefined) {
styleObj["min-height"] = `${View.properties.minheight}px`;
delete View.properties.minheight;
}
if (View.properties.maxheight !== undefined) {
styleObj["max-height"] = `${View.properties.maxheight}px`;
delete View.properties.maxheight;
}
if (View.properties.flex !== undefined) {
if (View.properties.flex == "1")
styleObj["flex"] = "1 1 auto";
delete View.properties.flex;
}
if(View.properties.align !== undefined){
if(styleObj["display"] !== "flex")
styleObj["display"] = "flex";
styleObj["align-items"] = View.properties.align.replace("left", "start").replace("right", "end");
delete View.properties.align;
}
if(View.properties.pack !== undefined){
if (styleObj["display"] !== "flex")
styleObj["display"] = "flex";
styleObj["justify-content"] = View.properties.pack.replace("start", "flex-start").replace("end", "flex-end");
delete View.properties.pack;
}
if(View.properties.top !== undefined){
if(styleObj["postition"] !== "absolute")
styleObj["postition"] = "absolute";
styleObj["top"] = `${View.properties.top}px`;
delete View.properties.top;
}
if(View.properties.left !== undefined){
if(styleObj["postition"] !== "absolute")
styleObj["postition"] = "absolute";
styleObj["left"] = `${View.properties.left}px`;
delete View.properties.left;
}
if(View.properties.bottom !== undefined){
if(styleObj["postition"] !== "absolute")
styleObj["postition"] = "absolute";
styleObj["bottom"] = `${View.properties.bottom}px`;
delete View.properties.bottom;
}
if(View.properties.right !== undefined){
if(styleObj["postition"] !== "absolute")
styleObj["postition"] = "absolute";
styleObj["right"] = `${View.properties.right}px`;
delete View.properties.right;
}
if(View.properties.hidden !== undefined) {
styleObj["display"] = "none";
delete View.properties.hidden;
}
if (View.properties.tooltiptext !== undefined) {
View.properties.title = View.properties.tooltiptext;
delete View.properties.tooltiptext;
}
if (Object.keys(styleObj).length > 0){
View.properties.style = Object.entries(styleObj).reduce((acc, [prop, value]) => {
return acc.concat(`${prop}: ${value}; `);
}, "").trimEnd();
}
}
}
export function RelativePath(fromLocation: string, toLocation: string){
return path.isAbsolute(toLocation) ? path.relative(fromLocation, toLocation) : toLocation
}
/** inserts './' at the start if the path starts with a folder */
export function ImportablePath(path: string){
const ptrn = /^\.*\//
return ptrn.test(path) ? path : `./${path}`;
}
export function IsMeaningfulString(str: string){
return str !== undefined && str != "";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment