Commit ff1539b0 authored by Ilan Arad's avatar Ilan Arad

Filter content before uploading;

painted some console logs;
meta: compact links.
parent 0b3f2948
......@@ -7,15 +7,12 @@
"wordpress",
"rest"
],
"homepage": "https://decodecode.net/elitist/2019/07/wpsuck/",
"private": true,
"homepage": "https://decodecode.net/elitist/2019/07/wpsuck/",
"repository": "https://gitlab.com/decodecode/wpsuck.git",
"bugs": "https://gitlab.com/decodecode/wpsuck/issues",
"author": "Ilan <ilan@decodecode.net>",
"license": "WTFPL",
"repository": {
"type": "git",
"url": "https://gitlab.com/decodecode/wpsuck.git"
},
"bugs": "https://gitlab.com/decodecode/wpsuck/issues",
"engines": {},
"main": "wpsuck.coffee",
"scripts": {},
......
......@@ -16,7 +16,7 @@ For more about this hack see my devlog: https://decodecode.net/elitist/2019/07/w
## Dependencies
assert=require 'assert'
fse=require 'fs-extra' # Promisified fs.
#? require colors?
require 'colors'
## Parse CLI
argv=(require 'minimist') process.argv.slice 2
......@@ -40,7 +40,7 @@ wpsuck_api=
#? Should return entire post's JSON, so API users can work with, eg, title, not just content!
down:(website,auth,post_id)->
return new Promise (resolve,reject)->
assert website and auth and post_id,"ERROR: invalid options: #{JSON.stringify argv}."
assert website and auth and post_id,"ERROR: invalid options: #{JSON.stringify argv}.".red
url=(strip_trailing '/',website)+'/wp-json/wp/v2/posts/'+post_id
options={url,auth}
unless argv.quiet then console.error "request.GET #{url} …"
......@@ -55,7 +55,7 @@ wpsuck_api=
## `up`
up:(website,auth,post_id,data)->
return new Promise (resolve,reject)->
assert website and auth and post_id and data,"ERROR: invalid parameters: #{JSON.stringify {website,auth,post_id,data}}."
assert website and auth and post_id and data,"ERROR: invalid parameters: #{JSON.stringify {website,auth,post_id,data}}.".red
url=(strip_trailing '/',website)+'/wp-json/wp/v2/posts/'+post_id
options={url,auth,body:data,json:yes}
unless argv.quiet then console.error "request.POST #{url} …"
......@@ -69,7 +69,7 @@ wpsuck_api=
else resolve body
## Action!
if argv.website and not argv['force-unsafe'] then assert argv.website.startsWith 'https://',"ERROR: must use HTTPS! Because credentials passed in clear text. \"#{website}\". (Workaround with --force-unsafe.)" #? .red?
if argv.website and not argv['force-unsafe'] then assert argv.website.startsWith 'https://',"ERROR: must use HTTPS! Because credentials passed in clear text. \"#{website}\". (Workaround with --force-unsafe.)".red
switch
when argv.help
......@@ -86,7 +86,7 @@ switch
when argv.down
{website,user,password,post_id}=argv
assert website and user and password and post_id,"ERROR: invalid options: #{JSON.stringify argv}."
assert website and user and password and post_id,"ERROR: invalid options: #{JSON.stringify argv}.".red
unless argv.quiet then console.error 'Downloading…'
auth={user,password} # HTTP Basic.
wpsuck_api
......@@ -96,9 +96,9 @@ switch
# Save configuration to output: prepend to content as HTML comment.
config="<!--#{JSON.stringify {website,user,password,post_id}}-->\n"
fse.writeFile argv.down,config+content
.then (->unless argv.quiet then console.error ';o)' #? .green?
),(reason)->console.error 'ERROR: saving to file failed!\n',reason
),(reason)->console.error 'ERROR: downloading failed!\n',reason
.then (->unless argv.quiet then console.error ';o)'.green.inverse
),(reason)->console.error 'ERROR: saving to file failed!\n'.red,reason
),(reason)->console.error 'ERROR: downloading failed!\n'.red,reason
when argv.up
unless argv.quiet then console.log 'Uploading…'
......@@ -108,18 +108,28 @@ switch
unless argv.quiet then console.error f.length,'bytes read.'
m=/<!--(.+)-->\n([^]+)/.exec f
{website,user,password,post_id}=JSON.parse m[1]
assert website and user and password and post_id,"ERROR: invalid configuration: \"#{m[1]}\"."
assert website and user and password and post_id,"ERROR: invalid configuration: \"#{m[1]}\".".red
#? … Override from CLI, or environment?
#?{user,password}=argv
auth={user,password} # HTTP Basic.
# Filter content.
count=0 #? Ugly?
content=content
.split '\n'
.filter (s)->
if s is '' then return false # Remove empty lines.
if /^<(meta|script|link)[^>]*>(.*<\/script>)?$/i.test s then return false # Remove hacks for local dev.
++count
.join '\n'
if count then console.error "#{count} lines filtered.".red
# Payload must follow schema spec…
payload=content:m[2] #?payload=JSON.stringify content:m[2]
payload={content} #?payload=JSON.stringify content:m[2]
wpsuck_api
.up website,auth,post_id,payload
.then ((body)->unless argv.quiet then console.error ';o)' #? .green? #? console.log body
),(reason)->console.error 'ERROR: uploading failed!\n',reason
),(reason)->console.error 'ERROR: can\'t read file!\n',reason
.then ((body)->unless argv.quiet then console.error ';o)'.green.inverse #? console.log body
),(reason)->console.error 'ERROR: uploading failed!\n'.red,reason
),(reason)->console.error 'ERROR: can\'t read file!\n'.red,reason
else
console.error 'ERROR: missing command! Must specify either --up or --down. See --help.'
console.error 'ERROR: missing command! Must specify either --up or --down. See --help.'.red
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment