Saving cg defs to filesystem upon change, updating calls to save defs to use fs
This commit is contained in:
parent
cea41c8247
commit
e0cd7f2bae
1 changed files with 68 additions and 13 deletions
|
|
@ -129,20 +129,38 @@ export const _rt = {
|
||||||
return { _tag: 'Some', _0: moduleDefs.map(d => prettyPrint(d)).join('\n\n') };
|
return { _tag: 'Some', _0: moduleDefs.map(d => prettyPrint(d)).join('\n\n') };
|
||||||
},
|
},
|
||||||
"saveImage!": () => {
|
"saveImage!": () => {
|
||||||
const saved: Record<string, string> = {};
|
const modules = new Map<string, Definition[]>();
|
||||||
for (const [name, ast] of definitions) {
|
const standalone: Definition[] = [];
|
||||||
const source = prettyPrint({ kind: 'definition', name, body: ast });
|
|
||||||
saved[name] = source;
|
for (const [_name, def] of definitions) {
|
||||||
|
if (def.module) {
|
||||||
|
if (!modules.has(def.module)) modules.set(def.module, []);
|
||||||
|
modules.get(def.module)!.push(def);
|
||||||
|
} else {
|
||||||
|
standalone.push(def);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save modules
|
||||||
|
for (const [moduleName, defs] of modules) {
|
||||||
|
const content = '@' + moduleName + '\n\n' +
|
||||||
|
defs.map(d => prettyPrint(d)).join('\n\n') + '\n\n@\n';
|
||||||
|
fetch('/api/save', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ filename: moduleName, content })
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save standalone defs
|
||||||
|
for (const def of definitions) {
|
||||||
|
const content = prettyPrint(def) + '\n';
|
||||||
|
fetch('/api/save', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ filename: def.name, content })
|
||||||
|
});
|
||||||
}
|
}
|
||||||
const content = Object.values(saved).join('\n\n');
|
|
||||||
const blob = new Blob([content], { type: 'text/plain' });
|
|
||||||
const url = URL.createObjectURL(blob);
|
|
||||||
const a = document.createElement('a');
|
|
||||||
a.href = url;
|
|
||||||
a.download = `cg-image-${new Date().toISOString().slice(0, 19).replace(/[T:]/g, '-')}.cg`;
|
|
||||||
a.click();
|
|
||||||
URL.revokeObjectURL(url);
|
|
||||||
return { _tag: 'Ok' };
|
|
||||||
},
|
},
|
||||||
"saveModule!": (moduleName: string) => {
|
"saveModule!": (moduleName: string) => {
|
||||||
const moduleDefs = [...definitions.values()]
|
const moduleDefs = [...definitions.values()]
|
||||||
|
|
@ -160,6 +178,18 @@ export const _rt = {
|
||||||
|
|
||||||
return { _tag: 'Defined', _0: moduleName };
|
return { _tag: 'Defined', _0: moduleName };
|
||||||
},
|
},
|
||||||
|
"saveDef!": (name: string) => {
|
||||||
|
const def = definitions.get(name);
|
||||||
|
if (!def) return { _tag: 'Err', _0: 'Unknown: ' + name };
|
||||||
|
const content = prettyPrint(def) + '\n';
|
||||||
|
fetch('/api/save', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ filename: name, content })
|
||||||
|
});
|
||||||
|
|
||||||
|
return { _tag: 'Defined', _0: name };
|
||||||
|
},
|
||||||
rebind: (name: string, pathOrValue: any, maybeValue?: any) => {
|
rebind: (name: string, pathOrValue: any, maybeValue?: any) => {
|
||||||
if (maybeValue === undefined) {
|
if (maybeValue === undefined) {
|
||||||
store[name] = pathOrValue;
|
store[name] = pathOrValue;
|
||||||
|
|
@ -240,6 +270,7 @@ export const _rt = {
|
||||||
const source = prettyPrint(def);
|
const source = prettyPrint(def);
|
||||||
appendChangeLog(def.name, source);
|
appendChangeLog(def.name, source);
|
||||||
saveDefinitions();
|
saveDefinitions();
|
||||||
|
syncToFilesystem(def.name);
|
||||||
return { _tag: 'Defined', _0: def.name };
|
return { _tag: 'Defined', _0: def.name };
|
||||||
}
|
}
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
|
|
@ -428,3 +459,27 @@ function appendChangeLog(name: string, source: string) {
|
||||||
console.error('Failed to append changelog:', e);
|
console.error('Failed to append changelog:', e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function syncToFilesystem(name: string) {
|
||||||
|
const def = definitions.get(name);
|
||||||
|
if (!def) return;
|
||||||
|
|
||||||
|
if (def.module) {
|
||||||
|
const moduleDefs = [...definitions.values()]
|
||||||
|
.filter(d => d.module === def.module);
|
||||||
|
const content = '@' + def.module + '\n\n' +
|
||||||
|
moduleDefs.map(d => prettyPrint(d)).join('\n\n') + '\n\n@\n';
|
||||||
|
fetch('/api/save', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ filename: def.module, content })
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
const content = prettyPrint(def) + '\n';
|
||||||
|
fetch('/api/save', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ filename: name, content })
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue