Skip to content

Commit

Permalink
Merge pull request #310 from dhis2/fix/clone-avoid-dupes
Browse files Browse the repository at this point in the history
fix: avoid creating duplicates when cloning
  • Loading branch information
Erik A authored Apr 27, 2020
2 parents 8764aa1 + bc72156 commit 4c1ba1d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
52 changes: 49 additions & 3 deletions tools/clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ const uploadImages = require('./helpers/uploadImages')

const errors = []

const findAppByName = (name, list) => {
for (let i = 0; i < list.length; ++i) {
const app = list[i]
if (app.name === name) {
return app
}
}
return null
}

async function main() {
const sourceUrl = 'https://play.dhis2.org/appstore/api/apps'
const targetUrl = 'http://localhost:3000/api'
Expand All @@ -22,21 +32,56 @@ async function main() {
const publishedApps = await request(sourceUrl)
const appsJson = JSON.parse(publishedApps)

let targetApps = await request(`${targetUrl}/apps`)
let existingAppsAtTarget = JSON.parse(targetApps)

//TODO: make configurable
const cleanTarget = true

if (cleanTarget) {
for (let i = 0; i < existingAppsAtTarget.length; ++i) {
const app = existingAppsAtTarget[i]
console.log(`Deleting app '${app.name}'`)
await request.delete({
url: `${targetUrl}/apps/${app.id}`,
headers: {
Authorization: 'Bearer ' + authToken,
},
})
}
//refresh existing apps
targetApps = await request(`${targetUrl}/apps`)
existingAppsAtTarget = JSON.parse(targetApps)
}

for (let i = 0; i < appsJson.length; ++i) {
const app = appsJson[i]

const existingApp = findAppByName(app.name, existingAppsAtTarget)

if (existingApp !== null) {
//app exists, but all versions?
console.log(
`App "${app.name}" already exists at target/destination, skipping it.`
)
continue
}

if (!fs.existsSync('./apps')) {
fs.mkdirSync('./apps')
}

const dir = './apps/' + app.name
const dir = './apps/' + app.name.replace('/', '-')
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir)
fs.mkdirSync(dir, { recursive: true, mode: '0755' })
}

console.log(`Downloading app: ${app.name}`)
console.log(`Downloading app: ${app.name} to ${dir}`)

if (!app.developer.email || app.developer.email == '') {
console.log(`Skipping app because no developer email is set`)

//also store this in an array so we can summarize errors after the run has completed
errors.push(`${app.name} | No developer email set. Skipping.`)
continue
}
Expand Down Expand Up @@ -66,6 +111,7 @@ async function main() {
//create app with first version
const appVersion = app.versions[0]
const appVersionFile = path.join(dir, appVersion.version + '.zip')

const form = {
app: JSON.stringify(appBase),
file: {
Expand Down
5 changes: 3 additions & 2 deletions tools/helpers/downloadImages.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ module.exports = async (dir, app) => {
const fileName = appImage.id + fileExtension

return new Promise((resolve, reject) => {
const imageFile = fs.createWriteStream(path.join(dir, fileName))
const target = path.join(dir, fileName)
const imageFile = fs.createWriteStream(target)
request(appImage.imageUrl)
.pipe(imageFile)
.on('finish', () => {
console.log(
`Successfully downloaded image: ${appImage.imageUrl} => ${fileName}`
`Successfully downloaded image: ${appImage.imageUrl} => ${target}`
)
resolve()
})
Expand Down

0 comments on commit 4c1ba1d

Please sign in to comment.