|
|
@@ -153,7 +153,13 @@ var sync = function () {
|
|
|
services.forEach(initService);
|
|
|
}
|
|
|
function getFilename(uri) {
|
|
|
- return encodeURIComponent(uri) + '.user.js';
|
|
|
+ return 'vm-' + encodeURIComponent(uri);
|
|
|
+ }
|
|
|
+ function getURI(name) {
|
|
|
+ return decodeURIComponent(name.slice(3));
|
|
|
+ }
|
|
|
+ function isScriptFile(name) {
|
|
|
+ return /^vm-/.test(name);
|
|
|
}
|
|
|
function syncOne(service) {
|
|
|
if (!service.inst) return;
|
|
|
@@ -216,18 +222,37 @@ var sync = function () {
|
|
|
var promises = [].concat(
|
|
|
getRemote.map(function (item) {
|
|
|
console.log('Download script:', item.uri);
|
|
|
- return service.inst.get(getFilename(item.uri)).then(function (code) {
|
|
|
- return vmdb.parseScript({
|
|
|
- code: code,
|
|
|
- modified: item.modified,
|
|
|
- }).then(function (res) {
|
|
|
+ return service.inst.get(getFilename(item.uri)).then(function (raw) {
|
|
|
+ var data = {};
|
|
|
+ try {
|
|
|
+ var obj = JSON.parse(raw);
|
|
|
+ if (obj.version === 1) {
|
|
|
+ data.code = obj.code;
|
|
|
+ data.more = obj.more;
|
|
|
+ }
|
|
|
+ } catch (e) {
|
|
|
+ data.code = raw;
|
|
|
+ }
|
|
|
+ data.modified = item.modified;
|
|
|
+ return vmdb.parseScript(data)
|
|
|
+ .then(function (res) {
|
|
|
_.messenger.post(res);
|
|
|
});
|
|
|
});
|
|
|
}),
|
|
|
putRemote.map(function (item) {
|
|
|
console.log('Upload script:', item.uri);
|
|
|
- return service.inst.put(getFilename(item.uri), item.code).then(function (data) {
|
|
|
+ var data = JSON.stringify({
|
|
|
+ version: 1,
|
|
|
+ code: item.code,
|
|
|
+ more: {
|
|
|
+ custom: item.custom,
|
|
|
+ enabled: item.enabled,
|
|
|
+ update: item.update,
|
|
|
+ },
|
|
|
+ });
|
|
|
+ return service.inst.put(getFilename(item.uri), data)
|
|
|
+ .then(function (data) {
|
|
|
if (item.custom.modified !== data.modified) {
|
|
|
item.custom.modified = data.modified;
|
|
|
return vmdb.saveScript(item);
|
|
|
@@ -277,5 +302,10 @@ var sync = function () {
|
|
|
sync: sync,
|
|
|
service: service,
|
|
|
status: getStatuses,
|
|
|
+ utils: {
|
|
|
+ getFilename: getFilename,
|
|
|
+ isScriptFile: isScriptFile,
|
|
|
+ getURI: getURI,
|
|
|
+ },
|
|
|
};
|
|
|
}();
|