Skip to content

Commit

Permalink
fix(cli:hosting): fixing bug with hosting sync
Browse files Browse the repository at this point in the history
  • Loading branch information
mkucharz committed Feb 16, 2018
1 parent 85a10c7 commit 2029cd3
Showing 1 changed file with 37 additions and 24 deletions.
61 changes: 37 additions & 24 deletions packages/cli/src/utils/hosting/hosting.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const { debug } = logger('utils-hosting')

class HostingFile {
loadRemote (fileRemoteData) {
debug('loadRemote')
this.id = fileRemoteData.id
this.instanceName = fileRemoteData.instanceName
this.path = decodeURIComponent(fileRemoteData.path)
Expand All @@ -25,8 +26,9 @@ class HostingFile {
return this
}
loadLocal (fileLocalData) {
this.localPath = fileLocalData.path
this.path = path.basename(this.localPath)
debug('loadLocal')
this.localPath = fileLocalData.localPath
this.path = fileLocalData.path
this.checksum = md5(fs.readFileSync(this.localPath))
this.size = fs.statSync(this.localPath).size
return this
Expand Down Expand Up @@ -276,20 +278,21 @@ class Hosting {
return `https://${this.name}--${session.project.instance}.${this.hostingHost}`
}

getLocalFilePath (file) {
debug('getLocalFilePath')
if (!file) return null
return file.localPath.replace(this.path, '')
encodePath (pathToEncode) {
return pathToEncode.split(path.sep).map(part => encodeURIComponent(part)).join(path.sep)
}

decodePath (pathToEncode) {
return pathToEncode.split('/').map(part => decodeURIComponent(part)).join('/')
}

// Verify local file if it should be created or updated
async getFilesToUpload (file, remoteFiles) {
debug('getFilesToUpload')
const localHostingFilePath = this.getLocalFilePath(file)
const fileToUpdate = _.find(remoteFiles, { path: localHostingFilePath })
const fileToUpdate = _.find(remoteFiles, { path: file.path })
const payload = new FormData()
payload.append('file', fs.createReadStream(file.localPath))
payload.append('path', encodeURIComponent(localHostingFilePath))
payload.append('path', this.encodePath(file.path))

let singleFile = null

Expand All @@ -301,24 +304,24 @@ class Hosting {
if (remoteChecksum === localChecksum) {
try {
singleFile = await session.connection.hosting.getFile(this.name, fileToUpdate.id)
echo(6)(`${format.green('✓')} File skipped: ${format.dim(localHostingFilePath)}`)
echo(6)(`${format.green('✓')} File skipped: ${format.dim(file.path)}`)
} catch (err) {
error(err)
}
} else {
try {
singleFile = await session.connection.hosting.updateFile(this.name, fileToUpdate.id, payload)
echo(6)(`${format.green('✓')} File updated: ${format.dim(localHostingFilePath)}`)
echo(6)(`${format.green('✓')} File updated: ${format.dim(file.path)}`)
} catch (err) {
echo(`Error while syncing (updating) ${localHostingFilePath}`)
echo(`Error while syncing (updating) ${file.path}`)
debug(err.response.data)
}
}
} else {
// Adding (first upload) file
try {
singleFile = await session.connection.hosting.uploadFile(this.name, payload)
echo(6)(`${format.green('✓')} File added: ${format.dim(localHostingFilePath)}`)
echo(6)(`${format.green('✓')} File added: ${format.dim(file.path)}`)
} catch (err) {
echo(`Error while syncing (creating) ${file.path}`)
debug(err.response.data)
Expand Down Expand Up @@ -364,18 +367,19 @@ class Hosting {
promises.push(this.getFilesToUpload(file, files))
})

if (params.delete) {
// promises for deleting files
promises = promises.concat(this.getFilesToDelete(files, localFiles))
}

const values = await Promise.all(promises)
uploadedFilesCount = 0
uploadedSize = 0
values.forEach(upload => {
uploadedFilesCount += 1
uploadedSize += upload ? upload.size : 0
})

if (params.delete) {
// promises for deleting files
await Promise.all(this.getFilesToDelete(files, localFiles))
}

return { uploadedFilesCount, uploadedSize }
}

Expand All @@ -402,7 +406,7 @@ class Hosting {
}

const localChecksums = await this.listLocalFiles().map(localFile => ({
filePath: this.getLocalFilePath(localFile),
filePath: localFile.path,
checksum: localFile.checksum
}))

Expand All @@ -420,7 +424,7 @@ class Hosting {
const files = await session.connection.hosting.listFiles(this.name)
return Promise.all(files.map(async file => {
// TODO: Maybe it should be somehow done in the library not here
file.path = decodeURIComponent(file.path)
file.path = this.decodePath(file.path)

const hostingFile = new HostingFile(file)
return hostingFile.loadRemote(file)
Expand All @@ -433,17 +437,26 @@ class Hosting {
const localHostingFiles = this.path ? await getFiles(this.path) : []
if (!Array.isArray(localHostingFiles)) return localHostingFiles

return localHostingFiles ? localHostingFiles.map(file => new HostingFile().loadLocal({ path: file })) : []
const getLocalPath = (file) => {
if (!file) return null
return file.replace(this.path, '')
}

return localHostingFiles ? localHostingFiles.map(file => {
return new HostingFile().loadLocal({
localPath: file,
path: getLocalPath(file)
})
}) : []
}

async listFiles () {
const remoteFiles = await this.listRemoteFiles()
const listLocalFiles = await this.listLocalFiles()

const files = []
listLocalFiles.forEach(localFile => {
const file = localFile
const remoteCopy = _.find(remoteFiles, { path: this.getLocalFilePath(file) })
listLocalFiles.forEach(file => {
const remoteCopy = _.find(remoteFiles, { path: file.path })

if (remoteCopy) {
file.isUpToDate = file.checksum === remoteCopy.checksum
Expand Down

0 comments on commit 2029cd3

Please sign in to comment.