2016-02-25 20:40:35 -08:00
|
|
|
import yaml from 'js-yaml';
|
|
|
|
import moment from 'moment';
|
2017-01-10 22:23:22 -02:00
|
|
|
import AssetProxy from '../valueObjects/AssetProxy';
|
2016-02-25 20:40:35 -08:00
|
|
|
|
|
|
|
const MomentType = new yaml.Type('date', {
|
|
|
|
kind: 'scalar',
|
2017-01-10 22:23:22 -02:00
|
|
|
predicate(value) {
|
2016-02-25 20:40:35 -08:00
|
|
|
return moment.isMoment(value);
|
|
|
|
},
|
2017-01-10 22:23:22 -02:00
|
|
|
represent(value) {
|
2016-02-25 20:40:35 -08:00
|
|
|
return value.format(value._f);
|
|
|
|
},
|
2017-01-10 22:23:22 -02:00
|
|
|
resolve(value) {
|
2016-02-25 20:40:35 -08:00
|
|
|
return moment.isMoment(value) && value._f;
|
2017-01-10 22:23:22 -02:00
|
|
|
},
|
2016-02-25 20:40:35 -08:00
|
|
|
});
|
|
|
|
|
2016-06-06 21:53:22 -03:00
|
|
|
const ImageType = new yaml.Type('image', {
|
|
|
|
kind: 'scalar',
|
2017-01-10 22:23:22 -02:00
|
|
|
instanceOf: AssetProxy,
|
|
|
|
represent(value) {
|
|
|
|
return `${ value.path }`;
|
2016-06-06 21:53:22 -03:00
|
|
|
},
|
2017-01-10 22:23:22 -02:00
|
|
|
resolve(value) {
|
2016-06-06 21:53:22 -03:00
|
|
|
if (value === null) return false;
|
2017-01-10 22:23:22 -02:00
|
|
|
if (value instanceof AssetProxy) return true;
|
2016-06-06 21:53:22 -03:00
|
|
|
return false;
|
2017-01-10 22:23:22 -02:00
|
|
|
},
|
2016-06-06 21:53:22 -03:00
|
|
|
});
|
|
|
|
|
|
|
|
|
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),
|
2017-01-10 22:23:22 -02:00
|
|
|
explicit: yaml.DEFAULT_SAFE_SCHEMA.explicit,
|
2016-02-25 20:40:35 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
export default class YAML {
|
|
|
|
fromFile(content) {
|
|
|
|
return yaml.safeLoad(content);
|
|
|
|
}
|
|
|
|
|
|
|
|
toFile(data) {
|
2016-09-13 15:30:58 +02:00
|
|
|
return yaml.safeDump(data, { schema: OutputSchema });
|
2016-02-25 20:40:35 -08:00
|
|
|
}
|
|
|
|
}
|