kumuniter/gentleman.js

108 lines
3.2 KiB
JavaScript

import http from 'https'
import { readFile, writeFile, mkdir, stat } from 'fs'
import { resolve } from 'path'
const params = process.argv.slice(2)
export const args = params.map( p=> p.split('=') ).reduce( (o,a)=> (o[a[0]]=a[1],o), {} )
// shorcuts
export const exists = path=> new Promise((ok,ko)=> stat( path, (err,stat)=> stat && !err ? ok(true) : ok(false) ))
// loading
let cacheDir
const _cachePath = url=> cacheDir + '/' + url.replace(
/^https?:\/\/(.*?)\/(.*?)(\?(.*?))?$/
, (s, domain, path,q, query)=>
`${domain}/${path}${q ? `/${query.replace(/[=&\\\/%]/g,'-')}` : ``}`
)
export const cache = folder=> exists( cacheDir = folder ).then( being=> !being && mkdir(cacheDir,o=>o) )
export const get = (url,enc='utf8')=> new Promise( async(ok,ko)=>
log('GET ')( url )
&&
cacheDir && await exists( _cachePath(url) ) ?
readFile( _cachePath(url), 'utf-8', (err,data)=> ok(data) )
:
http.get( url, res=> {
if (res.statusCode !== 200) {
ko( new Error('Request Failed.\n' +
`Status Code: ${res.statusCode}`) )
res.resume()
return
}
res.setEncoding( enc )
let rawData = ''
res.on('data', chunk=> rawData += chunk )
res.on('end', async()=> {
ok(rawData)
// console.log(cacheDir, _cachePath(url))
cacheDir
&& await ensure( _cachePath(url) )
&& toFile( _cachePath(url) )( rawData )
})
})
.on('error', e=> ko(`Got error: ${e.message}`) )
)
export const delayR = (action,...args)=> new Promise( ok=> setTimeout(
()=> ok( action(...args) )
, Math.random() * delayR.amount
))
delayR.amount = 15000
export const load = person=> fetch(!person ? `/wot/members` : `/wot/certified-by/${person.pubkey}`).then( res=> res.json() )
export const gentlyLoad = person=> delayR( load, person )
// misc promise
export const log = str=> o=> console.log(str, o) || o
// Array
export const toCSV = arr=> `${Object.keys(arr[0]).map(quoted)}
${arr.map( item=> Object.keys(item).map(key=> item[key]).map(quoted) ).join('\n')}`
export const concat = (a,o)=> a.concat ( o )
export const only = n=> (o,i)=> n ? i < n : 1
// String
export const stick = (ss,...pp)=> ss.map((s,i)=>s+(pp[i]||'')).join('')
export const quoted = str=> `"${str}"`
export const firstCase = str=> str.split(/\s/).map( s=> s[0].toUpperCase() + s.slice(1).toLowerCase() ).join(' ')
export const noAccent = str=> str.normalize("NFKD").replace( /[\u0300-\u036F]/g, "" )
// Object
export const toJson = data=> JSON.parse(data)
// export const toCSV = data=> `${Object.keys(data[0]).map(JSON.stringify).join(',')}
// ${data.map(o=> Object.keys(o).map( k=> JSON.stringify(o[k]) ).join(',')).join('\n')}`
// files
export const toFile = (path,enc='utf8')=> data=> new Promise( (ok,ko)=>
log('File write:')( path )
&&
writeFile(path, data, 'utf8', err=> err && ko(err) || ok(data) )
)
export const ensure = path=> Promise.all(
// log( 'ici') ( resolve( __dirname, path ).split(/[\\/]/).slice( 0, -1 )) &&
resolve( __dirname, path ).split(/[\\/]/).slice( 0, -1 )
.map( (folder,i,path)=> path.filter(only(i+1)).join('/') )
// .map( log('Folder: ') )
// .map( async folder=> await exists(folder) )
// .map( async folder=> !(await exists(folder)) ? 'no: create' : folder )
.map( async folder=> !(await exists(folder)) ? mkdir(folder,o=>o) : folder )
)