From ce8f0e70b0681c87e79343bf7e6353dd67fec1be Mon Sep 17 00:00:00 2001 From: Bret Comnes Date: Thu, 13 Feb 2020 11:23:24 -0700 Subject: [PATCH] 0.0.5 --- CHANGELOG.md | 6 + node_modules/async-neocities/CHANGELOG.md | 18 ++ node_modules/async-neocities/README.md | 4 +- node_modules/async-neocities/index.js | 210 ++++-------------- .../async-neocities/lib/stats-handler.js | 57 +++++ node_modules/async-neocities/package.json | 21 +- node_modules/async-neocities/test.js | 15 +- node_modules/pretty-bytes/package.json | 4 +- node_modules/pretty-time/package.json | 12 +- package.json | 2 +- 10 files changed, 153 insertions(+), 196 deletions(-) create mode 100644 node_modules/async-neocities/lib/stats-handler.js diff --git a/CHANGELOG.md b/CHANGELOG.md index d218770..90be64e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog). +## [v0.0.5](https://github.com/bcomnes/deploy-to-neocities/compare/v0.0.4...v0.0.5) - 2020-02-13 + +### Commits + +- bug: Use correct version of async-neocities [`22ef26e`](https://github.com/bcomnes/deploy-to-neocities/commit/22ef26e851efa57357d731a73a1f606dc212608a) + ## [v0.0.4](https://github.com/bcomnes/deploy-to-neocities/compare/v0.0.3...v0.0.4) - 2020-02-13 ### Commits diff --git a/node_modules/async-neocities/CHANGELOG.md b/node_modules/async-neocities/CHANGELOG.md index 06bc357..331c88b 100644 --- a/node_modules/async-neocities/CHANGELOG.md +++ b/node_modules/async-neocities/CHANGELOG.md @@ -7,6 +7,24 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog). +## [v1.1.1](https://github.com/bcomnes/async-neocities/compare/v1.1.0...v1.1.1) - 2020-02-13 + +### Commits + +- bug: Fix busted export of stats handler. [`510ae29`](https://github.com/bcomnes/async-neocities/commit/510ae293263955e0e34d3ab48df253fb6e093053) + +## [v1.1.0](https://github.com/bcomnes/async-neocities/compare/v1.0.2...v1.1.0) - 2020-02-13 + +### Commits + +- feat: Finish statsCb API and add a stats-handler.js function. [`c8e6483`](https://github.com/bcomnes/async-neocities/commit/c8e64835e594e68715ef71590b08baac374052bd) + +## [v1.0.2](https://github.com/bcomnes/async-neocities/compare/v1.0.1...v1.0.2) - 2020-02-13 + +### Commits + +- chore: remove total time from stats [`3c375ec`](https://github.com/bcomnes/async-neocities/commit/3c375ecf64ae8536a8e3ccce0a69cd93c8c6a306) + ## [v1.0.1](https://github.com/bcomnes/async-neocities/compare/v1.0.0...v1.0.1) - 2020-02-13 ### Commits diff --git a/node_modules/async-neocities/README.md b/node_modules/async-neocities/README.md index 02188d8..aa360b8 100644 --- a/node_modules/async-neocities/README.md +++ b/node_modules/async-neocities/README.md @@ -185,11 +185,11 @@ Deploy a path to a `directory`, efficiently only uploading missing and changed f ```js { cleanup: false // delete orphaned files on neocities that are not in the `directory` - statsCb: () => {} // WIP progress API + statsCb: (stats) => {} } ``` -The return value of this method is subject to change. +For an example of a stats handler, see [lib/stats-handler.js]('./lib/stats-handler.js'). ### `client.get(endpoint, [quieries], [opts])` diff --git a/node_modules/async-neocities/index.js b/node_modules/async-neocities/index.js index 6060d14..1fc4d69 100644 --- a/node_modules/async-neocities/index.js +++ b/node_modules/async-neocities/index.js @@ -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 } } diff --git a/node_modules/async-neocities/lib/stats-handler.js b/node_modules/async-neocities/lib/stats-handler.js new file mode 100644 index 0000000..ee670b8 --- /dev/null +++ b/node_modules/async-neocities/lib/stats-handler.js @@ -0,0 +1,57 @@ +const prettyBytes = require('pretty-bytes') + +// Progress API constants +const START = 'start' +const PROGRESS = 'progress' // progress updates +const STOP = 'stop' +const SKIP = 'skip' + +function statsHandler (opts = {}) { + let progressInterval + const lastStats = {} + + return (stats) => { + switch (stats.status) { + case START: { + console.log(`Starting ${stats.stage} stage...`) + break + } + case STOP: { + console.log(`Finished ${stats.stage} stage.`) + break + } + case SKIP: { + console.log(`Skipping ${stats.stage} stage.`) + break + } + case PROGRESS: { + progressHandler(stats) + break + } + default: { + } + } + } + + function progressHandler (stats) { + Object.assign(lastStats, stats) + if (!stats.complete) { + if (!progressInterval) { + progressInterval = setInterval(logProgress, 500, lastStats) + logProgress(lastStats) + } + } else { + if (progressInterval) clearInterval(progressInterval) + } + } + + function logProgress (stats) { + let logLine = `Stage ${stats.stage}: ${stats.progress * 100}%` + if (stats.bytesWritten != null && stats.totalBytes != null) { + logLine = logLine + ` (${prettyBytes(stats.bytesWritten)} / ${prettyBytes(stats.totalBytes)})` + } + console.log(logLine) + } +} + +module.exports = statsHandler diff --git a/node_modules/async-neocities/package.json b/node_modules/async-neocities/package.json index 531e190..667bbcc 100644 --- a/node_modules/async-neocities/package.json +++ b/node_modules/async-neocities/package.json @@ -1,26 +1,26 @@ { - "_from": "async-neocities@1.0.1", - "_id": "async-neocities@1.0.1", + "_from": "async-neocities@1.1.1", + "_id": "async-neocities@1.1.1", "_inBundle": false, - "_integrity": "sha512-5TVaKLYKnaHoSiluCP0i78e0CBZbeqBL6bgK8/QsM8YOAtrhd5JO9c5DEaW4SFpmf4wV0qcamQXh0A3C7CANLw==", + "_integrity": "sha512-iYSEfp6567pmuMXcHkm09zG/+jNBH1Bg0ThbvrJDX/PhxbttK5S66VX4M5BJbiEWEBDjrjcP+Km1eBM5DSeURQ==", "_location": "/async-neocities", "_phantomChildren": {}, "_requested": { "type": "version", "registry": true, - "raw": "async-neocities@1.0.1", + "raw": "async-neocities@1.1.1", "name": "async-neocities", "escapedName": "async-neocities", - "rawSpec": "1.0.1", + "rawSpec": "1.1.1", "saveSpec": null, - "fetchSpec": "1.0.1" + "fetchSpec": "1.1.1" }, "_requiredBy": [ "/" ], - "_resolved": "https://registry.npmjs.org/async-neocities/-/async-neocities-1.0.1.tgz", - "_shasum": "3428ae48f48104b205a3537212090b00d9bbce45", - "_spec": "async-neocities@1.0.1", + "_resolved": "https://registry.npmjs.org/async-neocities/-/async-neocities-1.1.1.tgz", + "_shasum": "cca697ff75ff70225fa7e10bd1b3bada555ef8bf", + "_spec": "async-neocities@1.1.1", "_where": "/Users/bret/repos/deploy-to-neocities", "author": { "name": "Bret Comnes", @@ -37,6 +37,7 @@ "form-data": "^3.0.0", "nanoassert": "^2.0.0", "node-fetch": "^2.6.0", + "pretty-bytes": "^5.3.0", "pump": "^3.0.0", "pumpify": "^2.0.1", "qs": "^6.9.1", @@ -79,5 +80,5 @@ "dist" ] }, - "version": "1.0.1" + "version": "1.1.1" } diff --git a/node_modules/async-neocities/test.js b/node_modules/async-neocities/test.js index 0dd6e80..a2d2d1e 100644 --- a/node_modules/async-neocities/test.js +++ b/node_modules/async-neocities/test.js @@ -3,6 +3,7 @@ const tap = require('tap') const { readFileSync } = require('fs') const { resolve } = require('path') const NeocitiesAPIClient = require('.') +const statsHanlder = require('./lib/stats-handler') let token = process.env.NEOCITIES_API_TOKEN let fakeToken = false @@ -87,18 +88,10 @@ if (!fakeToken) { tap.test('can deploy folders', async t => { const client = new NeocitiesAPIClient(token) - const statsCb = (stats) => { - let logLine = `${stats.stage} ${stats.status} ${stats.timer.elapsed}` - Object.entries(stats.tasks).forEach(([key, val]) => { - logLine += ` ${key}: ${JSON.stringify(val)}` - }) - console.log(logLine) - } - const deployStats = await client.deploy( resolve(__dirname, 'fixtures'), { - statsCb, + statsCb: statsHanlder(), cleanup: false } ) @@ -110,7 +103,7 @@ if (!fakeToken) { const redeployStats = await client.deploy( resolve(__dirname, 'fixtures'), { - statsCb, + statsCb: statsHanlder(), cleanup: false } ) @@ -122,7 +115,7 @@ if (!fakeToken) { const cleanupStats = await client.deploy( resolve(__dirname, 'fixtures/empty'), { - statsCb, + statsCb: statsHanlder(), cleanup: true } ) diff --git a/node_modules/pretty-bytes/package.json b/node_modules/pretty-bytes/package.json index 90867cf..a3b6237 100644 --- a/node_modules/pretty-bytes/package.json +++ b/node_modules/pretty-bytes/package.json @@ -16,12 +16,12 @@ "fetchSpec": "^5.3.0" }, "_requiredBy": [ - "/" + "/async-neocities" ], "_resolved": "https://registry.npmjs.org/pretty-bytes/-/pretty-bytes-5.3.0.tgz", "_shasum": "f2849e27db79fb4d6cfe24764fc4134f165989f2", "_spec": "pretty-bytes@^5.3.0", - "_where": "/Users/bret/repos/deploy-to-neocities", + "_where": "/Users/bret/repos/deploy-to-neocities/node_modules/async-neocities", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", diff --git a/node_modules/pretty-time/package.json b/node_modules/pretty-time/package.json index 137776e..6716256 100644 --- a/node_modules/pretty-time/package.json +++ b/node_modules/pretty-time/package.json @@ -1,26 +1,26 @@ { - "_from": "pretty-time@^1.1.0", + "_from": "pretty-time@1.1.0", "_id": "pretty-time@1.1.0", "_inBundle": false, "_integrity": "sha512-28iF6xPQrP8Oa6uxE6a1biz+lWeTOAPKggvjB8HAs6nVMKZwf5bG++632Dx614hIWgUPkgivRfG+a8uAXGTIbA==", "_location": "/pretty-time", "_phantomChildren": {}, "_requested": { - "type": "range", + "type": "version", "registry": true, - "raw": "pretty-time@^1.1.0", + "raw": "pretty-time@1.1.0", "name": "pretty-time", "escapedName": "pretty-time", - "rawSpec": "^1.1.0", + "rawSpec": "1.1.0", "saveSpec": null, - "fetchSpec": "^1.1.0" + "fetchSpec": "1.1.0" }, "_requiredBy": [ "/" ], "_resolved": "https://registry.npmjs.org/pretty-time/-/pretty-time-1.1.0.tgz", "_shasum": "ffb7429afabb8535c346a34e41873adf3d74dd0e", - "_spec": "pretty-time@^1.1.0", + "_spec": "pretty-time@1.1.0", "_where": "/Users/bret/repos/deploy-to-neocities", "author": { "name": "Jon Schlinkert", diff --git a/package.json b/package.json index 89d1cf8..7b4f081 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "deploy-to-neocities", - "version": "0.0.4", + "version": "0.0.5", "description": "Github Action to deplpoy a folder to Neocities.org", "main": "index.js", "private": true,