refactor: 'coding

This commit is contained in:
cha0s 2021-01-28 00:14:13 -06:00
parent f77bd8b209
commit 0121059f90
4 changed files with 20 additions and 12 deletions

View File

@ -28,7 +28,10 @@ const projectResources = async (uuid, resourcePaths, latus) => {
const {encode} = latus.get('%resource-controllers')(path); const {encode} = latus.get('%resource-controllers')(path);
return ({ return ({
...(await r), ...(await r),
[join(uuid, path)]: encode(await readFile(join(process.cwd(), 'projects', uuid, path))), [join(uuid, path)]: await encode(
await readFile(join(process.cwd(), 'projects', uuid, path)),
latus,
),
}); });
}, {}); }, {});
}; };

View File

@ -69,6 +69,16 @@ export default class EntityController extends JsonResourceController {
); );
} }
static decode(encoded, latus) {
const {Entity} = latus.get('%resources');
return super.decode(Entity.withoutDefaults(encoded));
}
static encode(buffer, latus) {
const {Entity} = latus.get('%resources');
return Entity.withDefaults(super.encode(buffer));
}
static get matcher() { static get matcher() {
return /\.entity\.json$/; return /\.entity\.json$/;
} }

View File

@ -1,6 +1,5 @@
import {join} from 'path'; import {join} from 'path';
import {Trait as BaseTrait} from '@avocado/traits';
import { import {
PropTypes, PropTypes,
React, React,
@ -65,18 +64,13 @@ const Traits = ({
); );
const TraitRenderers = latus.get('%trait-renderers'); const TraitRenderers = latus.get('%trait-renderers');
const tabPanels = types.map((type) => { const tabPanels = types.map((type) => {
const Trait = Traits[type] || BaseTrait;
const jsonWithDefaults = {
params: Trait.defaultParamsWith(json[type].params || {}),
state: Trait.defaultStateWith(json[type].state || {}),
};
const TraitRenderer = TraitRenderers[type] const TraitRenderer = TraitRenderers[type]
? TraitRenderers[type] ? TraitRenderers[type]
: NoTraitRenderer; : NoTraitRenderer;
return ( return (
<TabPanel key={type}> <TabPanel key={type}>
<TraitRenderer <TraitRenderer
json={jsonWithDefaults} json={json[type]}
path={join(path, type)} path={join(path, type)}
/> />
</TabPanel> </TabPanel>
@ -122,7 +116,7 @@ const Traits = ({
setIsSelecting(false); setIsSelecting(false);
patch({ patch({
path: join(path, type), path: join(path, type),
value: {}, value: Traits[type].withDefaults({}),
}); });
}} }}
types={suggestible} types={suggestible}

View File

@ -9,7 +9,7 @@ import {patchJsonResource} from '../../../state/json';
const readFile = promisify(fs.readFile); const readFile = promisify(fs.readFile);
const writeFile = promisify(fs.writeFile); const writeFile = promisify(fs.writeFile);
export default (Action) => class ProjectAction extends Action { export default (Action, latus) => class ProjectAction extends Action {
static async respond(packet, socket) { static async respond(packet, socket) {
const {data: {type, payload}} = packet; const {data: {type, payload}} = packet;
@ -18,9 +18,10 @@ export default (Action) => class ProjectAction extends Action {
const {patch, project, uri} = payload; const {patch, project, uri} = payload;
const path = join(process.cwd(), 'projects', project, uri); const path = join(process.cwd(), 'projects', project, uri);
const buffer = await readFile(path); const buffer = await readFile(path);
const json = JSON.parse(buffer.toString('utf8')); const {decode, encode} = latus.get('%resource-controllers')(uri);
const json = encode(buffer, latus);
applyPatch(json, patch); applyPatch(json, patch);
writeFile(path, JSON.stringify(json, null, 2)); writeFile(path, decode(json, latus));
break; break;
} }
default: default: