This commit is contained in:
Bret Comnes
2020-02-13 11:23:24 -07:00
parent 22ef26e851
commit ce8f0e70b0
10 changed files with 153 additions and 196 deletions

210
node_modules/async-neocities/index.js generated vendored
View File

@@ -12,6 +12,7 @@ const { neocitiesLocalDiff } = require('./lib/folder-diff')
const pkg = require('./package.json')
const SimpleTimer = require('./lib/timer')
const { getStreamLength, meterStream } = require('./lib/stream-meter')
const statsHandler = require('./lib/stats-handler')
const defaultURL = 'https://neocities.org'
@@ -19,6 +20,7 @@ const defaultURL = 'https://neocities.org'
const START = 'start'
const PROGRESS = 'progress' // progress updates
const STOP = 'stop'
const SKIP = 'skip'
// Progress stages
const INSPECTING = 'inspecting'
const DIFFING = 'diffing'
@@ -55,6 +57,8 @@ class NeocitiesAPIClient {
return fetch(url, opts)
}
static statsHanler (...args) { return statsHandler(...args) }
/**
* Create an async-neocities api client.
* @param {string} apiKey An apiKey to make requests with.
@@ -226,195 +230,73 @@ class NeocitiesAPIClient {
}
const statsCb = opts.statsCb
const startDeployTime = Date.now()
const totalTime = new SimpleTimer(startDeployTime)
const totalTime = new SimpleTimer(Date.now())
// Inspection stage stats initializer
const inspectionStats = {
stage: INSPECTING,
status: START,
timer: new SimpleTimer(startDeployTime),
totalTime,
tasks: {
localScan: {
numberOfFiles: 0,
totalSize: 0,
timer: new SimpleTimer(startDeployTime)
},
remoteScan: {
numberOfFiles: 0,
totalSize: 0,
timer: new SimpleTimer(startDeployTime)
}
}
}
const sendInspectionUpdate = (status) => {
if (status) inspectionStats.status = status
statsCb(inspectionStats)
}
sendInspectionUpdate(START)
// Remote scan timers
const remoteScanJob = this.list()
remoteScanJob.then(({ files }) => { // Comes in the form of a response object
const { tasks: { remoteScan } } = inspectionStats
remoteScan.numberOfFiles = files.length
remoteScan.totalSize = files.reduce((accum, cur) => {
return accum + cur.size || 0
}, 0)
remoteScan.timer.stop()
sendInspectionUpdate(PROGRESS)
})
// Local scan timers and progress accumulator
const localScanJob = progressAccum(
afw.asyncFolderWalker(directory, { shaper: f => f })
)
async function progressAccum (iterator) {
const localFiles = []
const { tasks: { localScan } } = inspectionStats
for await (const file of iterator) {
localFiles.push(file)
localScan.numberOfFiles += 1
localScan.totalSize += file.stat.size
sendInspectionUpdate(PROGRESS)
}
return localFiles
}
localScanJob.then(files => {
const { tasks: { localScan } } = inspectionStats
localScan.timer.stop()
sendInspectionUpdate(PROGRESS)
})
// Inspection stage finalizer
// INSPECTION STAGE
statsCb({ stage: INSPECTING, status: START })
const [localFiles, remoteFiles] = await Promise.all([
localScanJob,
remoteScanJob.then(res => res.files)
afw.allFiles(directory, { shaper: f => f }),
this.list().then(res => res.files)
])
inspectionStats.timer.stop()
sendInspectionUpdate(STOP)
statsCb({ stage: INSPECTING, status: STOP })
// DIFFING STAGE
const diffingStats = {
stage: DIFFING,
status: START,
timer: new SimpleTimer(Date.now()),
totalTime,
tasks: {
diffing: {
uploadCount: 0,
deleteCount: 0,
skipCount: 0
}
}
}
statsCb(diffingStats)
const { tasks: { diffing } } = diffingStats
statsCb({ stage: DIFFING, status: START })
const { filesToUpload, filesToDelete, filesSkipped } = await neocitiesLocalDiff(remoteFiles, localFiles)
statsCb({ stage: DIFFING, status: STOP })
diffingStats.timer.stop()
diffingStats.status = STOP
diffing.uploadCount = filesToUpload.length
diffing.deleteCount = filesToDelete.length
diffing.skipCount = filesSkipped.length
statsCb(diffingStats)
const applyingStartTime = Date.now()
const applyingStats = {
stage: APPLYING,
status: START,
timer: new SimpleTimer(applyingStartTime),
totalTime,
tasks: {
uploadFiles: {
timer: new SimpleTimer(applyingStartTime),
bytesWritten: 0,
totalBytes: 0,
get percent () {
return this.totalBytes === 0 ? 0 : this.bytesWritten / this.totalBytes
},
get speed () {
return this.bytesWritten / this.timer.elapsed
}
},
deleteFiles: {
timer: new SimpleTimer(applyingStartTime),
bytesWritten: 0,
totalBytes: 0,
get percent () {
return this.totalBytes === 0 ? 0 : this.bytesWritten / this.totalBytes
},
get speed () {
return this.bytesWritten / this.timer.elapsed
}
},
skippedFiles: {
count: filesSkipped.length,
size: filesSkipped.reduce((accum, file) => accum + file.stat.size, 0)
}
}
// APPLYING STAGE
if (filesToUpload.length === 0 && (!opts.cleanup || filesToDelete.length === 0)) {
statsCb({ stage: APPLYING, status: SKIP })
return stats()
}
const sendApplyingUpdate = (status) => {
if (status) applyingStats.status = status
statsCb(applyingStats)
}
sendApplyingUpdate(START)
statsCb({ stage: APPLYING, status: START })
const work = []
const { tasks: { uploadFiles, deleteFiles } } = applyingStats
if (filesToUpload.length > 0) {
const uploadJob = this.upload(filesToUpload, {
statsCb: ({ bytesWritten, totalBytes }) => {
uploadFiles.bytesWritten = bytesWritten
uploadFiles.totalBytes = totalBytes
sendApplyingUpdate(PROGRESS)
statsCb ({ totalBytes, bytesWritten }) {
statsCb({
stage: APPLYING,
status: PROGRESS,
complete: false,
totalBytes,
bytesWritten,
get progress () {
return (this.bytesWritten / this.totalBytes) || 0
}
})
}
}).then((_) => {
statsCb({
stage: APPLYING,
status: PROGRESS,
complete: true,
progress: 1.0
})
})
work.push(uploadJob)
uploadJob.then(res => {
uploadFiles.timer.stop()
sendApplyingUpdate(PROGRESS)
})
} else {
uploadFiles.timer.stop()
}
if (opts.cleanup && filesToDelete.length > 0) {
const deleteJob = this.delete(filesToDelete, {
statsCb: ({ bytesWritten, totalBytes }) => {
deleteFiles.bytesWritten = bytesWritten
deleteFiles.totalBytes = totalBytes
sendApplyingUpdate(PROGRESS)
}
})
work.push(deleteJob)
deleteJob.then(res => {
deleteFiles.timer.stop()
sendApplyingUpdate(PROGRESS)
})
} else {
deleteFiles.timer.stop()
work.push(this.delete(filesToDelete))
}
await Promise.all(work)
applyingStats.timer.stop()
sendApplyingUpdate(STOP)
statsCb({ stage: APPLYING, status: STOP })
totalTime.stop()
return stats()
const statsSummary = {
time: totalTime,
inspectionStats,
diffingStats,
applyingStats
function stats () {
totalTime.stop()
return {
time: totalTime.elapsed,
filesToUpload,
filesToDelete,
filesSkipped
}
}
return statsSummary
}
}