Update logging

This commit is contained in:
Bret Comnes
2020-02-12 16:52:32 -07:00
parent e49d0b8d3a
commit 383cb4ca53
36 changed files with 824 additions and 597 deletions

View File

@@ -1,16 +1,16 @@
const crypto = require('crypto')
const util = require('util')
const fs = require('fs')
const ppump = util.promisify(require('pump'))
/**
* neocitiesLocalDiff returns an array of files to delete and update and some useful stats.
* @param {Array} neocitiesFiles Array of files returned from the neocities list api.
* @param {Array} localListing Array of files returned by a full data async-folder-walker run.
* @return {Promise<Object>} Object of filesToUpload, filesToDelete and filesSkipped.
*/
async function neocitiesLocalDiff (neocitiesFiles, localListing, opts = {}) {
opts = {
...opts
}
async function neocitiesLocalDiff (neocitiesFiles, localListing) {
const localIndex = {}
const ncIndex = {}
@@ -19,9 +19,8 @@ async function neocitiesLocalDiff (neocitiesFiles, localListing, opts = {}) {
const ncFiles = new Set(neoCitiesFiltered.map(f => f.path)) // shape
const localListingFiltered = localListing.filter(f => !f.stat.isDirectory()) // files only
localListingFiltered.forEach(f => { localIndex[f.relname] = f }) // index
// TODO: convert windows to unix paths
const localFiles = new Set(localListingFiltered.map(f => f.relname)) // shape
localListingFiltered.forEach(f => { localIndex[forceUnixRelname(f.relname)] = f }) // index
const localFiles = new Set(localListingFiltered.map(f => forceUnixRelname(f.relname))) // shape
const filesToAdd = difference(localFiles, ncFiles)
const filesToDelete = difference(ncFiles, localFiles)
@@ -43,7 +42,7 @@ async function neocitiesLocalDiff (neocitiesFiles, localListing, opts = {}) {
return {
filesToUpload: Array.from(filesToAdd).map(p => ({
name: localIndex[p].relname,
name: forceUnixRelname(localIndex[p].relname),
path: localIndex[p].filepath
})),
filesToDelete: Array.from(filesToDelete).map(p => ncIndex[p].path),
@@ -72,7 +71,7 @@ async function sha1FromPath (p) {
// From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set#Implementing_basic_set_operations
/**
* difference betwen setA and setB
* difference returnss the difference betwen setA and setB.
* @param {Set} setA LHS set
* @param {Set} setB RHS set
* @return {Set} The difference Set
@@ -85,6 +84,12 @@ function difference (setA, setB) {
return _difference
}
/**
* intersection returns the interesction between setA and setB.
* @param {Set} setA setA LHS set
* @param {Set} setB setB RHS set
* @return {Set} The intersection set between setA and setB.
*/
function intersection (setA, setB) {
const _intersection = new Set()
for (const elem of setB) {
@@ -95,6 +100,18 @@ function intersection (setA, setB) {
return _intersection
}
/**
* forceUnixRelname forces a OS dependent path to a unix style path.
* @param {String} relname String path to convert to unix style.
* @return {String} The unix variant of the path
*/
function forceUnixRelname (relname) {
return relname.split(relname.sep).join('/')
}
/**
* Example of neocitiesFiles
*/
// [
// {
// path: 'img',
@@ -131,6 +148,9 @@ function intersection (setA, setB) {
// }
// ]
/**
* Example of localListing
*/
// [{
// root: '/Users/bret/repos/async-folder-walker/fixtures',
// filepath: '/Users/bret/repos/async-folder-walker/fixtures/sub-folder/sub-sub-folder',

View File

@@ -1,6 +1,7 @@
const tap = require('tap')
const afw = require('async-folder-walker')
const path = require('path')
const tap = require('tap')
const { neocitiesLocalDiff } = require('./folder-diff')
const remoteFiles = [
@@ -53,7 +54,6 @@ tap.test('test differ', async t => {
}), 'every file to upload is included')
t.deepEqual(filesToDelete, [
'index.html',
'not_found.html',
'style.css'
], 'filesToDelete returned correctly')