Comparing version 0.3.0 to 0.3.2
180
index.js
@@ -35,3 +35,29 @@ #!/usr/bin/env node | ||
var command = args._[0]; | ||
var uploadProcesses = {}; | ||
function getClosestSharedParent(dir0, dir1) { | ||
var dirs = {}; | ||
dirs[0] = dir0.split('/'); | ||
dirs[1] = dir1.split('/'); | ||
var ret = ''; | ||
dirs[0].unshift(); | ||
dirs[1].unshift(); | ||
for (var i = 0; i < dirs[0].length; i++) { | ||
if (i >= dirs[1].length) { | ||
break; | ||
} | ||
if (dirs[0][i] === dirs[1][i]) { | ||
ret += (ret ==='' ? dirs[0][i] : '/' + dirs[0][i]); | ||
} | ||
else { | ||
break; | ||
} | ||
} | ||
return '/' + ret; | ||
} | ||
function isGitFile(filePath) { | ||
@@ -138,2 +164,3 @@ return isFileOf(filePath, function(dir){ | ||
var i = 0; | ||
var safeFolderFilePath = '/'; | ||
@@ -146,7 +173,11 @@ function create(index) { | ||
if (isExist) { | ||
console.log('It does, move on'); | ||
console.log('It does, move on'); | ||
safeFolderFilePath = current; | ||
} | ||
else { | ||
console.log(`Creating ${current}...`); | ||
return getNodePrimaryType(current) | ||
console.log(`It doesn't, signal and creating ${current}...`); | ||
return signalPartialOff(aem, filePath, safeFolderFilePath) | ||
.then(function(){ | ||
return getNodePrimaryType(current); | ||
}) | ||
.then(function(primaryType){ | ||
@@ -176,2 +207,26 @@ console.log(`as ${primaryType}...`); | ||
const TYPE_NUMBERS = ['Long', 'Double', 'Decimal']; | ||
function convertType(value) { | ||
var ret = value; | ||
var found = false; | ||
for(var i = 0; i < TYPE_NUMBERS.length; i++) { | ||
let typeString = '{'+TYPE_NUMBERS[i]+'}'; | ||
if (ret.indexOf(typeString) !== 0) { | ||
continue; | ||
} | ||
ret = ret.replace(typeString, '') * 1; | ||
found = true; | ||
break; | ||
} | ||
if (found == false) { | ||
ret = ret.replace(/^\{.*?\}/, ''); | ||
} | ||
return ret; | ||
} | ||
function filterXMLAttributes(node){ | ||
@@ -191,3 +246,3 @@ var ret = {}; | ||
} | ||
ret[key] = node[key]; | ||
ret[key] = convertType(node[key]); | ||
} | ||
@@ -346,2 +401,111 @@ return ret; | ||
function waitUntilSafe(aem, filePath) { | ||
var sharedFolderFilePath; | ||
console.log('Check if it is safe to proceed ahead with filePath:', filePath); | ||
for(var key in uploadProcesses) { | ||
if (!uploadProcesses.hasOwnProperty(key)) { | ||
continue; | ||
} | ||
let processInfo = uploadProcesses[key]; | ||
sharedFolderFilePath = getClosestSharedParent(processInfo.filePath, filePath); | ||
if (sharedFolderFilePath) { | ||
let subscriber = { | ||
filePath: filePath, | ||
sharedFolderFilePath: sharedFolderFilePath, | ||
resolve: null | ||
}; | ||
let promise = new Promise(function(resolve){ | ||
subscriber.resolve = resolve; | ||
processInfo.promise | ||
.then(resolve); | ||
}); | ||
processInfo.subscribers.push(subscriber); | ||
console.log(`Not safe, process(${processInfo.filePath}) that...`); | ||
console.log(`shared same parent folder(${sharedFolderFilePath}) is found, waiting for safe signal...`); | ||
return promise | ||
.then(function(){ | ||
console.log(`safe signal received ${processInfo.filePath}, go ahead...`); | ||
return waitUntilSafe(aem, filePath); | ||
}); | ||
} | ||
} | ||
console.log('Safe, go ahead!'); | ||
return Promise.resolve(); | ||
} | ||
function signalOn(aem, filePath) { | ||
var signal = uploadProcesses[filePath]; | ||
if (!signal) { | ||
signal = { | ||
filePath: filePath, | ||
promise: null, | ||
resolve: null, | ||
subscribers: [] | ||
}; | ||
uploadProcesses[filePath] = signal; | ||
signal.promise = new Promise(function(resolve){ | ||
signal.resolve = resolve; | ||
}); | ||
} | ||
return Promise.resolve(); | ||
} | ||
function signalPartialOff(aem, filePath, safeFolderFilePath) { | ||
var signal = uploadProcesses[filePath]; | ||
if (!signal) { | ||
return Promise.reject('Something wrong, no signal found while tryinig to send safe signal...'); | ||
} | ||
for(var i = 0; i < signal.subscribers.length; i++) { | ||
let subscriber = signal.subscribers[i]; | ||
let sharedFolderFilePath = getClosestSharedParent(subscriber.sharedFolderFilePath, safeFolderFilePath); | ||
if (sharedFolderFilePath != null && sharedFolderFilePath.length >= subscriber.sharedFolderFilePath) { | ||
console.log(`Firing a partial safe signal for ${subscriber.filePath}`); | ||
subscriber.resolve(); | ||
} | ||
} | ||
} | ||
function signalOff(aem, filePath) { | ||
var signal = uploadProcesses[filePath]; | ||
if (signal) { | ||
delete uploadProcesses[filePath]; | ||
console.log(`Firing a complete safe signal for ${filePath}`); | ||
signal.resolve(); | ||
} | ||
return Promise.resolve(); | ||
} | ||
function runSafe(aem, filePath, callback) { | ||
return waitUntilSafe(aem, filePath) | ||
.then(function(){ | ||
return signalOn(aem, filePath); | ||
}) | ||
.then(function(){ | ||
return callback(aem, filePath); | ||
}) | ||
.then(function(){ | ||
return signalOff(aem, filePath); | ||
}, function(err){ | ||
return signalOff(aem, filePath) | ||
.then(function(){ | ||
Promise.reject(err); | ||
}); | ||
}); | ||
} | ||
function sync() { | ||
@@ -397,11 +561,11 @@ var renamedFiles = []; | ||
console.log('CQ File is detected.'); | ||
return createCQXMLTree(aem, filePath); | ||
return runSafe(aem, filePath, createCQXMLTree); | ||
} | ||
else if (basename === FILE_DOT_CONTENT_XML) { | ||
console.log('Property change is detected.'); | ||
return uploadPropertiesChange(aem, filePath); | ||
return runSafe(aem, filePath, uploadPropertiesChange); | ||
} | ||
else if (basename === 'dialog.xml') { | ||
console.log('Dialog box config change is detected.'); | ||
return createDialogBox(aem, filePath); | ||
return runSafe(aem, filePath, createDialogBox); | ||
} | ||
@@ -413,3 +577,3 @@ else if (basename.indexOf('.') === 0) { | ||
else { | ||
return uploadFile(aem, filePath); | ||
return runSafe(aem, filePath, uploadFile); | ||
} | ||
@@ -416,0 +580,0 @@ })() |
{ | ||
"name": "aem-watch", | ||
"version": "0.3.0", | ||
"version": "0.3.2", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -9,3 +9,3 @@ # aem-watch | ||
Interesting about it? Get it now by `npm install aem-watch` | ||
Interested about it? Get it now by `npm install aem-watch` | ||
@@ -20,4 +20,5 @@ ### Caveat: | ||
`npm install aem-watch -g` | ||
`npm install "git+https://github.com/normanzb/aem-watch.git#v0.3.0" -g` | ||
`npm install "git+https://github.com/normanzb/aem-watch.git#v0.3.2" -g` | ||
## Usage | ||
@@ -41,2 +42,7 @@ | ||
0.3.0 | ||
0.3.2 | ||
## Known issues | ||
1. Property type is omitted when updating property as underlying API doesn't support setting propoerty type. | ||
2. Conflict error will be thrown when setting existing node with a new cq:primaryType, you need to delete the node manually in order for aem-watch to recreate it with new primaryType. aem-watch will not delete the node automatically for you now as it requires re-uploading child nodes of newly created node which is un-intuitive. |
21429
542
45