Last active
January 31, 2023 18:14
-
-
Save travist/22dfa9202bfbda9e97f24e21e0e244ad to your computer and use it in GitHub Desktop.
Manually Migrate forms in a project
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fetch = require('@formio/node-fetch-http-proxy'); | |
const shell = require('@travist/async-shell'); | |
const fs = require('fs'); | |
const config = require('./migrate.json'); | |
async function migrateForm(src, project) { | |
let resp; | |
let originalForm; | |
try { | |
resp = await fetch(src, { | |
rejectUnauthorized: false | |
}); | |
originalForm = await resp.json(); | |
} | |
catch (err) { | |
console.log(err); | |
return; | |
} | |
let currentResp; | |
try { | |
currentResp = await fetch(`${config.dest}/form?name=${originalForm.name}`, { | |
rejectUnauthorized: false, | |
headers: { | |
'Content-Type': 'application/json', | |
'x-token': config.destKey | |
} | |
}); | |
} | |
catch (err) { | |
console.log(err); | |
return; | |
} | |
if (originalForm.settings && originalForm.settings.pdf) { | |
const srcPath = originalForm.settings.pdf.src.replace(/http(s)?:\/\/[^\/]+\//, ''); | |
const dstPath = `pdf/${project._id}/file/${originalForm.settings.pdf.id}`; | |
originalForm.settings.pdf.src = `${config.dstPdfUrl}/${dstPath}`; | |
const srcFilePath = srcPath.replace('/file', ''); | |
const dstFilePath = dstPath.replace('/file', ''); | |
try { | |
console.log(await shell(`sshpass -p '${config.srcPdfPass}' scp ${config.srcPdfSSH}:${config.srcPdfPath}/${srcFilePath}.html ./${dstFilePath}.html`)); | |
} | |
catch (err) { | |
console.log(err.message); | |
} | |
try { | |
console.log(await shell(`sshpass -p '${config.srcPdfPass}' scp ${config.srcPdfSSH}:${config.srcPdfPath}/${srcFilePath}.pdf ./${dstFilePath}.pdf`)); | |
} | |
catch (err) { | |
console.log(err.message); | |
} | |
try { | |
console.log(await shell(`sshpass -p '${config.srcPdfPass}' scp ${config.srcPdfSSH}:${config.srcPdfPath}/${srcFilePath}-proof.html ./${dstFilePath}-proof.html`)); | |
} | |
catch (err) { | |
console.log(err.message); | |
} | |
} | |
if (currentResp) { | |
const forms = await currentResp.json(); | |
if (forms.length) { | |
const currentForm = forms[0]; | |
console.log(`Updating ${originalForm.title}...`); | |
currentForm.title = originalForm.title; | |
currentForm.display = originalForm.display; | |
currentForm.type = originalForm.type; | |
currentForm.path = originalForm.path; | |
currentForm.components = originalForm.components; | |
currentForm.settings = originalForm.settings; | |
await fetch(`${config.dest}/form/${currentForm._id}`, { | |
rejectUnauthorized: false, | |
method: 'PUT', | |
body: JSON.stringify(currentForm), | |
headers: { | |
'Content-Type': 'application/json', | |
'x-token': config.destKey | |
} | |
}); | |
console.log(`Done updating ${originalForm.title}...`); | |
return; | |
} | |
} | |
console.log(`Creating ${originalForm.title}...`); | |
try { | |
await fetch(`${config.dest}/form`, { | |
rejectUnauthorized: false, | |
method: 'POST', | |
body: JSON.stringify({ | |
title: originalForm.title, | |
display: originalForm.display, | |
type: originalForm.type, | |
name: originalForm.name, | |
path: originalForm.path, | |
components: originalForm.components, | |
created: originalForm.created, | |
modified: originalForm.modified, | |
settings: originalForm.settings | |
}), | |
headers: { | |
'Content-Type': 'application/json', | |
'x-token': config.destKey | |
} | |
}); | |
} | |
catch (err) { | |
console.log(err); | |
} | |
console.log(`Done copying ${originalForm.title}...`); | |
} | |
async function getProjectInfo() { | |
resp = await fetch(config.dest, { | |
rejectUnauthorized: false, | |
headers: { | |
'Content-Type': 'application/json', | |
'x-token': config.destKey | |
} | |
}); | |
return await resp.json(); | |
} | |
async function migrateForms() { | |
const project = await getProjectInfo(); | |
try { | |
fs.mkdirSync('pdf'); | |
fs.mkdirSync(`pdf/${project._id}`); | |
} | |
catch (err) { | |
console.log(err.message); | |
} | |
for (let i = 0; i < config.forms.length; i++) { | |
await migrateForm(config.forms[i], project); | |
} | |
console.log(await shell(`sshpass -p '${config.dstPdfPass}' rsync -av ./pdf/${project._id}/ ${config.dstPdfSSH}:${config.dstPdfPath}/pdf/${project._id}`)); | |
} | |
migrateForms(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"srcPdfSSH": "oldpdf", | |
"srcPdfPass": "SOURCE_PDF_SERVER_PASSWORD", | |
"srcPdfPath": "/home/dockeruser/minio/data/formio", | |
"dstPdfSSH": "newpdf", | |
"dstPdfPass": "DESTINATION_PDF_SERVER_SSH_PASSWORD", | |
"dstPdfPath": "/home/dockeruser/minio/data/formio", | |
"dstPdfUrl": "https://forms.yoursite.com/pdf", | |
"dest": "https://forms.yoursite.com/yourproject", | |
"destKey": "", | |
"forms": [ | |
"" | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment