2016-02-25 20:40:35 -08:00
|
|
|
import yaml from 'js-yaml';
|
|
|
|
import moment from 'moment';
|
2016-06-10 00:16:01 -03:00
|
|
|
import MediaProxy from '../valueObjects/MediaProxy';
|
2016-02-25 20:40:35 -08:00
|
|
|
|
|
|
|
const MomentType = new yaml.Type('date', {
|
|
|
|
kind: 'scalar',
|
|
|
|
predicate: function(value) {
|
|
|
|
return moment.isMoment(value);
|
|
|
|
},
|
|
|
|
represent: function(value) {
|
|
|
|
return value.format(value._f);
|
|
|
|
},
|
|
|
|
resolve: function(value) {
|
|
|
|
return moment.isMoment(value) && value._f;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-06-06 21:53:22 -03:00
|
|
|
const ImageType = new yaml.Type('image', {
|
|
|
|
kind: 'scalar',
|
2016-06-10 00:16:01 -03:00
|
|
|
instanceOf: MediaProxy,
|
2016-06-06 21:53:22 -03:00
|
|
|
represent: function(value) {
|
2016-07-19 17:11:22 -03:00
|
|
|
return `${value.path}`;
|
2016-06-06 21:53:22 -03:00
|
|
|
},
|
|
|
|
resolve: function(value) {
|
|
|
|
if (value === null) return false;
|
2016-06-10 00:16:01 -03:00
|
|
|
if (value instanceof MediaProxy) return true;
|
2016-06-06 21:53:22 -03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2016-02-25 20:40:35 -08:00
|
|
|
const OutputSchema = new yaml.Schema({
|
|
|
|
include: yaml.DEFAULT_SAFE_SCHEMA.include,
|
2016-06-06 21:53:22 -03:00
|
|
|
implicit: [MomentType, ImageType].concat(yaml.DEFAULT_SAFE_SCHEMA.implicit),
|
2016-02-25 20:40:35 -08:00
|
|
|
explicit: yaml.DEFAULT_SAFE_SCHEMA.explicit
|
|
|
|
});
|
|
|
|
|
|
|
|
export default class YAML {
|
|
|
|
fromFile(content) {
|
|
|
|
return yaml.safeLoad(content);
|
|
|
|
}
|
|
|
|
|
|
|
|
toFile(data) {
|
|
|
|
return yaml.safeDump(data, {schema: OutputSchema});
|
|
|
|
}
|
|
|
|
}
|