Skip to content

Commit

Permalink
Export logging logic
Browse files Browse the repository at this point in the history
  • Loading branch information
bcomnes committed Nov 20, 2024
1 parent 9b502fa commit e01da2d
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 48 deletions.
66 changes: 23 additions & 43 deletions bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* @import {ArgscloptsParseArgsOptionsConfig} from 'argsclopts'
*/

import { format } from '@lukeed/ms'
import createApplicationConfig from 'application-config'
import { printHelpText } from 'argsclopts'
import { minimatch } from 'minimatch'
Expand All @@ -19,6 +18,9 @@ import { pkg } from './pkg.cjs'

// @ts-ignore
import passwordPrompt from 'password-prompt'
import { printDeployText, printPreviewText, printResultsErrorDump } from './lib/output-strings.js'

export { printDeployText, printPreviewText, printResultsErrorDump }

/** @type {ArgscloptsParseArgsOptionsConfig} */
const options = {
Expand Down Expand Up @@ -266,7 +268,7 @@ async function main () {
const stat = await fs.stat(distDir)
assert(stat.isDirectory(), 'dist_dir must be a directory that exists')

const deployTimer = new SimpleTimer()
const timer = new SimpleTimer()

const client = new NeocitiesAPIClient(apiKey)

Expand All @@ -276,19 +278,14 @@ async function main () {
protectedFileFilter: minimatch.filter(protectedFiles),
includeUnsupportedFiles
})
deployTimer.stop()
console.log(`Preview deploy to Neocities in ${format(deployTimer.elapsed)}:`)
console.log(` Upload ${diff.filesToUpload.length} files`)
console.log(` ${cleanup ? 'Delete' : 'Orphan'} ${diff.filesToDelete.length} files`)
console.log(` Skip ${diff.filesSkipped.length} files`)
console.log(` ${includeUnsupportedFiles ? 'Include' : 'Ignore'} ${diff.unsupportedFiles.length} unsupported files:`)
if (diff.unsupportedFiles.length) {
console.log(diff.unsupportedFiles)
}
console.log(` Found ${diff.protectedFiles.length} protected files:`)
if (diff.protectedFiles.length) {
console.log(diff.protectedFiles)
}
timer.stop()

printPreviewText({
diff,
timer,
cleanup,
includeUnsupportedFiles
})

process.exit(0)
} else {
Expand All @@ -299,38 +296,21 @@ async function main () {
includeUnsupportedFiles
})

deployTimer.stop()
timer.stop()

if (results.errors.length > 0) {
console.log('The Deploy finished with Errors! Dumping the results:\n\n')
console.log('Successful results:')
console.dir({ results: results.results }, { depth: null })
console.log('\n\n')

console.log('Deploy Diff:')
console.dir({ diff: results.diff }, { depth: null })
console.log('\n\n')

console.log('Deploy Errors:')
console.dir({ errors: results.errors }, { depth: null })
console.log('\n\n')

console.log('Please inspect the errors and debug output to look for hints as to why this might have failed.')
console.log('Your website may have ')
printResultsErrorDump({
results,
timer
})
process.exit(1)
} else {
console.log(`Deployed to Neocities in ${format(deployTimer.elapsed)}:`)
console.log(` Uploaded ${results.diff.filesToUpload.length} files`)
console.log(` ${cleanup ? 'Deleted' : 'Orphaned'} ${results.diff.filesToDelete.length} files`)
console.log(` Skipped ${results.diff.filesSkipped.length} files`)
console.log(` ${includeUnsupportedFiles ? 'Included' : 'Ignored'} ${results.diff.unsupportedFiles.length} unsupported files:`)
if (results.diff.unsupportedFiles.length) {
console.log(results.diff.unsupportedFiles)
}
console.log(` Found ${results.diff.protectedFiles.length} protected files:`)
if (results.diff.protectedFiles.length) {
console.log(results.diff.protectedFiles)
}
printDeployText({
results,
timer,
cleanup,
includeUnsupportedFiles
})
process.exit(0)
}
}
Expand Down
87 changes: 87 additions & 0 deletions lib/output-strings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/**
* @import { AsyncNeocitiesDiff } from './folder-diff.js'
* @import { SimpleTimer } from './timer.js'
* @import { DeployResults } from './api-endpoints.js'
*/

import { format } from '@lukeed/ms'

/**
* @param {object} params
* @param {AsyncNeocitiesDiff} params.diff
* @param {SimpleTimer} params.timer
* @param {boolean} params.cleanup
* @param {boolean} params.includeUnsupportedFiles
*/
export function printPreviewText ({
diff,
timer,
cleanup,
includeUnsupportedFiles
}) {
console.log(`Preview deploy to Neocities in ${format(timer.elapsed)}:`)
console.log(` Upload ${diff.filesToUpload.length} files`)
console.log(` ${cleanup ? 'Delete' : 'Orphan'} ${diff.filesToDelete.length} files`)
console.log(` Skip ${diff.filesSkipped.length} files`)
console.log(` ${includeUnsupportedFiles ? 'Include' : 'Ignore'} ${diff.unsupportedFiles.length} unsupported files:`)
if (diff.unsupportedFiles.length) {
console.log(diff.unsupportedFiles)
}
console.log(` Found ${diff.protectedFiles.length} protected files:`)
if (diff.protectedFiles.length) {
console.log(diff.protectedFiles)
}
}

/**
* @param {object} params
* @param {DeployResults} params.results
* @param {SimpleTimer} params.timer
* @param {boolean} params.cleanup
* @param {boolean} params.includeUnsupportedFiles
*/
export function printDeployText ({
results,
timer,
cleanup,
includeUnsupportedFiles
}) {
console.log(`Deployed to Neocities in ${format(timer.elapsed)}:`)
console.log(` Uploaded ${results.diff.filesToUpload.length} files`)
console.log(` ${cleanup ? 'Deleted' : 'Orphaned'} ${results.diff.filesToDelete.length} files`)
console.log(` Skipped ${results.diff.filesSkipped.length} files`)
console.log(` ${includeUnsupportedFiles ? 'Included' : 'Ignored'} ${results.diff.unsupportedFiles.length} unsupported files:`)
if (results.diff.unsupportedFiles.length) {
console.log(results.diff.unsupportedFiles)
}
console.log(` Found ${results.diff.protectedFiles.length} protected files:`)
if (results.diff.protectedFiles.length) {
console.log(results.diff.protectedFiles)
}
}

/**
* @param {object} params
* @param {DeployResults} params.results
* @param {SimpleTimer} params.timer
*/
export function printResultsErrorDump ({
results,
timer
}) {
console.log(`The Deploy finished in ${format(timer.elapsed)} with Errors! Dumping the results:\n\n`)
console.log('Successful results:')
console.dir({ results: results.results }, { depth: null })
console.log('\n\n')

console.log('Deploy Diff:')
console.dir({ diff: results.diff }, { depth: null })
console.log('\n\n')

console.log('Deploy Errors:')
console.dir({ errors: results.errors }, { depth: null })
console.log('\n\n')

console.log('Please inspect the errors and debug output to look for hints as to why this might have failed.')
console.log('Your website may have ')
}
5 changes: 0 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,5 @@
"clean:declarations-lib": "rm -rf $(find lib -type f -name '*.d.ts*' ! -name '*-types.d.ts')",
"build": "npm run clean && run-p build:*",
"build:declaration": "tsc -p declaration.tsconfig.json"
},
"standard": {
"ignore": [
"dist"
]
}
}

0 comments on commit e01da2d

Please sign in to comment.