diff --git a/commands/export.ts b/commands/export.ts index 132f9b0d..9820007c 100644 --- a/commands/export.ts +++ b/commands/export.ts @@ -41,8 +41,56 @@ export async function exportFile(name: string, fileName: string): Promise { throw new Error('Something wrong happened'); } const content = data.result.content; - fs.writeFileSync(fileName, (content || []).join('\n')); - log('Success'); + const { noStorage, dontExportIfNoChanges } = config().get('export'); + + const promise = new Promise((resolve, reject) => { + if(noStorage) { + // get only the storage xml for the doc. + api.getDoc(name + '?storageOnly=1').then(storageData => { + if (!storageData || !storageData.result) { + reject(new Error('Something wrong happened fetching the storage data')); + } + const storageContent = storageData.result.content; + + if (storageContent.length>1 && storageContent[0]) { + const storageContentString = storageContent.join("\n"); + const contentString = content.join("\n"); + + // find and replace the docs storage section with '' + resolve({'found': contentString.indexOf(storageContentString) >= 0, 'content': contentString.replace(storageContentString, '')}); + } else { + resolve({'found': false}); + } + }); + }else{ + resolve({'found': false}); + } + }); + + promise.then((res:any) => { + let joinedContent = (content || []).join("\n").toString('utf8'); + let isSkipped = ''; + + if(res.found) { + joinedContent = res.content.toString('utf8'); + } + + if (dontExportIfNoChanges && fs.existsSync(fileName)) { + const existingContent = fs.readFileSync(fileName, "utf8"); + // stringify to harmonise the text encoding. + if (JSON.stringify(joinedContent) != JSON.stringify(existingContent)) { + fs.writeFileSync(fileName, joinedContent); + } else { + isSkipped = ' => skipped - no changes.'; + } + } else { + fs.writeFileSync(fileName, joinedContent); + } + + log(`Success ${isSkipped}`); + }).catch(error => { + throw error; + }); }); }) .catch(error => { diff --git a/package.json b/package.json index 76b467c7..cb3314d3 100644 --- a/package.json +++ b/package.json @@ -417,6 +417,16 @@ "type": "boolean", "default": false, "description": "Automatically Preview XML Export files as UDL" + }, + "objectscript.export.noStorage": { + "description": "Strip the storage xml on export. (Useful for multiple systems)", + "type": "boolean", + "default": false + }, + "objectscript.export.dontExportIfNoChanges": { + "description": "Don't update the local file on export if the content is identical to the server code", + "type": "boolean", + "default": false } } },