From f161514617d7432a2aeee085ef7c65afcc705ef0 Mon Sep 17 00:00:00 2001 From: thatcomputerguy Date: Sun, 24 Feb 2019 09:40:07 -0500 Subject: [PATCH 01/15] All the changes before I started using GitHub This is a large commit since I started working on this project before I started using GitHub, and I didn't want to leave the README.md file behind. --- .gitignore | 3 + README.md | 65 +++++++++++-- index.js | 281 ++++++++++++++++++++++++++++++++++++----------------- 3 files changed, 256 insertions(+), 93 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ca75e6e --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ + +.eslintrc.json +.DS_Store diff --git a/README.md b/README.md index f8960cd..8e3c1f3 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,10 @@ -# NeoCities Node.js Client Library -A node.js library for interacting with the [NeoCities](https://neocities.org/api) api. +# Neocities-Extended Node.js Client Library +A node.js library for interacting with the [Neocities](https://neocities.org/api) api that actually has more capabilities than the original api itself. ## Installation ``` -$ npm install neocities +$ npm install neocities-extended ``` ## Usage @@ -12,8 +12,8 @@ $ npm install neocities First, require the library and initialize: ``` javascript -var NeoCities = require('neocities') -var api = new NeoCities('YOURUSERNAME', 'YOURPASSWORD') +var Neocities = require('neocities-extended') +var api = new Neocities('YOURUSERNAME', 'YOURPASSWORD') ``` ### Uploading files to your site @@ -23,11 +23,23 @@ var api = new NeoCities('YOURUSERNAME', 'YOURPASSWORD') api.upload([ {name: 'derp.js', path: './index.js'} -], function(resp) { + ], function(resp) { console.log(resp) }) ``` +### Downloading files from your site + +``` javascript +// site path is derp.js, saved on client as index.js + +api.download([ + {name: 'derp.js', path: 'index.js'} + ], function(resp) { + console.log(resp) +}) +``` + ### Deleting files from your site ``` javascript @@ -36,6 +48,14 @@ api.delete(['derp.js'], function(resp) { }) ``` +### List the files on your site + +``` javascript +api.list('dirname', function(resp) { + console.log(resp) +}) +``` + ### Get site info (hits, et cetera) ``` javascript @@ -49,3 +69,36 @@ api.info('youpi', function(resp) { console.log(resp) }) ``` + +### Use an API Key + +The API key is a more secure way to upload data, since it doesn't store or send your username or password. First, Log in normally with a callback for the key option. (This then uses the key once it is aquired instead of your username and password.) + +``` javascript +var api = new Neocities('YOURUSERNAME', 'YOURPASSWORD', {key: function(key) {/* store your key here */}}) +``` + +Then, use the key instead of the username or password the next time you log in. + +``` javascript +var api = new Neocities('YOURAPIKEY') +``` + +### Pushing a folder + +``` javascript +// foo is the local folder, images is what it will be named on your site +// hidden.json is not uploaded, nor any file ending with .conf.json + +api.push('foo/', 'images/', ['hidden.json', /.conf.json$/]) +``` + +### Pulling a folder + +Similar to download, but for folders + +``` javascript +// same argument syntax as push + +api.pull('foo/', 'images', ['site.png']) +``` diff --git a/index.js b/index.js index 3352f83..2eba9cd 100644 --- a/index.js +++ b/index.js @@ -1,105 +1,212 @@ -var fs = require('fs') -var http = require('http') -var https = require('https') -var path = require('path') -var url = require('url') -var qs = require('querystring') -var FormData = require('form-data') - -function NeoCities(user, pass, opts) { - this.user = user - this.pass = pass - this.opts = opts || {} - this.url = url.parse(this.opts.url || 'https://neocities.org') - this.client = this.url.protocol == 'https:' ? https : http +var fs = require('fs'), + http = require('http'), + https = require('https'), + path = require('path'), + url = require('url'), + qs = require('querystring'), + formData = require('form-data'); + +function Neocities(user, pass, opts) { + if (typeof pass == "object" || pass == undefined) { + this.key = user; + } + this.opts = opts || (typeof pass == "object" ? pass : {}) + if (!this.opts.key && !this.key) { + this.user = user; + this.pass = pass; + } + else { + this.opts.key = this.opts.key || true; + } + this.url = url.parse(this.opts.url || 'https://Neocities.org'); + this.siteurl = this.opts.siteurl ? url.parse(this.ops.siteurl) : null; + this.client = this.url.protocol == 'https:' ? https : http + if (this.opts.key && !this.key) { + this.get("key", null, res => {this.key = res.api_key; if (typeof this.opts.key == "function") this.opts.key(this)}, user, pass); + } } -NeoCities.prototype.get = function(method, args, callback) { - var path = '/api/'+method - - if(args) - path += '?'+qs.stringify(args) - - var request = this.client.request({ - method: 'get', - host: this.url.hostname, - port: this.url.port, - path: path, - auth: this.user+':'+this.pass - }, function(res) { - var body = '' - - res.on('data', function (chunk) { - body += chunk - }) +Neocities.prototype.download = function(files, callback) { + var i = 0; + var that = this; + var returnData = {}, returning = false; + + if (!this.siteurl) + this.info((resp) => { + that.siteurl = url.parse(that.url.protocol + "//" + (resp.info.domain || resp.info.sitename + "." + that.url.hostname)); + dwnldFile(); + }); + else dwnldFile(); + + function dwnldFile() { + if (i < files.length) { + if (files[i].path) { + var file = fs.createWriteStream(files[i].path); + file.on("finish", () => file.close((dwnldFile))); + that.client.get(that.siteurl.href + files[i].name.replace(/^\/?/, "/"), data => data.pipe(file)); + } +// else { +// returning = true; +// that.client.request(that.siteurl.href + files[i].name.replace(/^\/?/, "/"), data => returnData[files[i].name] = data); +// } + i ++; + } + else if (typeof callback == "function" && !returning) + callback({status:"success"}); + } +} - res.on('end', function() { - var resObj = JSON.parse(body) - callback(resObj) +Neocities.prototype.get = function(method, args, callback, user, pass) { + var opts = { + method: "get", + host: this.url.hostname, + port: this.url.port, + path: "/api/" + method + (args ? "?" + qs.stringify(args) : "") + }; + + if (!this.key) { + opts.auth = (user || this.user) + ":" + (pass || this.pass); + } + else { + opts.headers = {Authorization: "Bearer " + this.key}; + } + + var request = this.client.request(opts, function(res) { + var body = ""; + + res.on("data", function (chunk) { + body += chunk; + }) + + res.on("end", function() { + var resObj = JSON.parse(body); + callback(resObj); + }) }) - }) - request.end() + request.end(); } -NeoCities.prototype.info = function(sitename, callback) { - var args = null - - if(typeof sitename == 'function') - callback = sitename - else if(typeof sitename == 'string') - args = {sitename: sitename} - - this.get('info', args, callback) +Neocities.prototype.info = function(sitename, callback) { + var args = null; + + if(typeof sitename == "function") + callback = sitename; + else if(typeof sitename == "string") + args = {sitename: sitename}; + + this.get("info", args, callback); } -NeoCities.prototype.post = function(method, args, callback) { - var form = new FormData() - var i - - for(i=0;i path.resolve(localPath, dir)); + list(localPath); + + function list(dir) { + activePaths.push(dir); + fs.readdir(dir, parseFiles); + function parseFiles(err, dirContents) { + dirContents = dirContents.map(file => path.resolve(dir, file)); + var uploadArgs = dirContents.filter(file => { + if (excludes.includes(path.relative(localPath, file)) || excludes.some(exclude => (exclude.constructor == RegExp && file.match(exclude)) || (typeof exclude == "function" && exclude(file)))) { + return false; + } + var fileData = fs.statSync(file); + if (fileData.isDirectory()) + list(file); + else + return true; + }).map(file => ({path: file, name: webPath.replace(/[/\\]?$/, "/") + path.relative(path.resolve(localPath), file)})) + activePaths.splice(activePaths.indexOf(dir), 1) + that.upload(uploadArgs, activePaths.length ? () => {} : callback); + } + } +} - this.post('upload', args, callback) +Neocities.prototype.pull = function(localPath, webPath, excludes, callback) { + if (typeof excludes == "function") { + callback = excludes; + excludes = []; + } + var that = this; + this.list(filterDirs); + + function filterDirs(files) { + if (files.result == "success") { + that.download(Array.prototype.filter.apply(files, [file => !file.is_directory && (!path.relative(webPath, file.path).match(/^\.\.[/\\]/) || webPath == "/") && !excludes.includes(path.relative(webPath, file.path)) && !excludes.any(exclude => (exclude.prototype == RegExp && file.path.match(exclude)) || (typeof exclude == "function" && exclude(file.path)) || (typeof exclude == "string" && path.relative(exclude, path.relative(webPath, file.path)).match(/^\.\.[/\\]/)))]).map(file => ({path: localPath.replace(/[/\\]?$/, "/") + path.relative(webPath, file.path), name: file})), callback); + } + } } -module.exports = NeoCities +module.exports = Neocities; \ No newline at end of file From b41c3ff78760b04a2c63ac223ea8462937b75362 Mon Sep 17 00:00:00 2001 From: thatcomputerguy Date: Mon, 25 Feb 2019 07:36:36 -0500 Subject: [PATCH 02/15] Finishing up the NPM requirements --- .gitignore | 2 ++ package.json | 53 ++++++++++++++++++++++++++-------------------------- 2 files changed, 29 insertions(+), 26 deletions(-) diff --git a/.gitignore b/.gitignore index ca75e6e..1e193a1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ .eslintrc.json .DS_Store +node_modules +package-lock.json diff --git a/package.json b/package.json index df1739e..b6b1674 100644 --- a/package.json +++ b/package.json @@ -1,28 +1,29 @@ { - "name": "neocities", - "version": "0.0.3", - "description": "library for the NeoCities.org API", - "main": "index.js", - "repository": { - "type": "git", - "url": "git@github.com:neocities/neocities-node.git" - }, - "keywords": [ - "neocities" - ], - "author": "Kyle Drake (http://kyledrake.net/)", - "license": "BSD-2-Clause", - "bugs": { - "url": "https://github.com/neocities/neocities-node/issues" - }, - "dependencies": { - "form-data": "^2.1.4" - }, - "devDependencies": { - "browserify": "^14.3.0", - "uglify-js": "^3.0.4" - }, - "scripts": { - "browserify": "./node_modules/.bin/browserify ./index.js | ./node_modules/.bin/uglifyjs > neocities.min.js" - } + "name": "neocities-extended", + "version": "0.0.4", + "description": "Extended library for the Neocities.org API", + "main": "index.js", + "repository": { + "type": "git", + "url": "git@github.com:thatcomputerguy0101/neocities-node-extended.git" + }, + "keywords": [ + "neocities", + "extended" + ], + "author": "ThatComputerGuy (http://thatcomputerguy.neocities.org/about)", + "license": "BSD-2-Clause", + "bugs": { + "url": "https://github.com/thatcomputerguy0101/neocities-node-extended/issues" + }, + "dependencies": { + "form-data": "^2.3.3" + }, + "devDependencies": { + "browserify": "^14.3.0", + "uglify-js": "^3.0.4" + }, + "scripts": { + "browserify": "./node_modules/.bin/browserify ./index.js | ./node_modules/.bin/uglifyjs > neocities.min.js" + } } From 4ab3d6f5d03aa3806735bdecba5dff411b755351 Mon Sep 17 00:00:00 2001 From: thatcomputerguy Date: Mon, 25 Feb 2019 11:34:34 -0500 Subject: [PATCH 03/15] Revert "Finishing up the NPM requirements" This reverts commit b41c3ff78760b04a2c63ac223ea8462937b75362. --- .gitignore | 2 -- package.json | 53 ++++++++++++++++++++++++++-------------------------- 2 files changed, 26 insertions(+), 29 deletions(-) diff --git a/.gitignore b/.gitignore index 1e193a1..ca75e6e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,3 @@ .eslintrc.json .DS_Store -node_modules -package-lock.json diff --git a/package.json b/package.json index b6b1674..df1739e 100644 --- a/package.json +++ b/package.json @@ -1,29 +1,28 @@ { - "name": "neocities-extended", - "version": "0.0.4", - "description": "Extended library for the Neocities.org API", - "main": "index.js", - "repository": { - "type": "git", - "url": "git@github.com:thatcomputerguy0101/neocities-node-extended.git" - }, - "keywords": [ - "neocities", - "extended" - ], - "author": "ThatComputerGuy (http://thatcomputerguy.neocities.org/about)", - "license": "BSD-2-Clause", - "bugs": { - "url": "https://github.com/thatcomputerguy0101/neocities-node-extended/issues" - }, - "dependencies": { - "form-data": "^2.3.3" - }, - "devDependencies": { - "browserify": "^14.3.0", - "uglify-js": "^3.0.4" - }, - "scripts": { - "browserify": "./node_modules/.bin/browserify ./index.js | ./node_modules/.bin/uglifyjs > neocities.min.js" - } + "name": "neocities", + "version": "0.0.3", + "description": "library for the NeoCities.org API", + "main": "index.js", + "repository": { + "type": "git", + "url": "git@github.com:neocities/neocities-node.git" + }, + "keywords": [ + "neocities" + ], + "author": "Kyle Drake (http://kyledrake.net/)", + "license": "BSD-2-Clause", + "bugs": { + "url": "https://github.com/neocities/neocities-node/issues" + }, + "dependencies": { + "form-data": "^2.1.4" + }, + "devDependencies": { + "browserify": "^14.3.0", + "uglify-js": "^3.0.4" + }, + "scripts": { + "browserify": "./node_modules/.bin/browserify ./index.js | ./node_modules/.bin/uglifyjs > neocities.min.js" + } } From bfc0849c8c031dee00bca78dd6d44db750051280 Mon Sep 17 00:00:00 2001 From: thatcomputerguy Date: Mon, 25 Feb 2019 11:41:53 -0500 Subject: [PATCH 04/15] More "Reverts" to match upstream/master --- .DS_Store | Bin 0 -> 6148 bytes .gitignore | 3 --- README.md | 10 +++++----- index.js | 24 ++++++++++++------------ 4 files changed, 17 insertions(+), 20 deletions(-) create mode 100644 .DS_Store delete mode 100644 .gitignore diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..f0294a3eab57ee2fddb408d093a5d368e5fa545a GIT binary patch literal 6148 zcmeHK%}N6?5T3CG7d%)Gg2%i<-yoLt=&3Itb^U|grbv71ArI!oH}jj!s9koU;6X%Y zVDe3pnPm2ZO)^Bpi(NA(nh{Y26=X3+M21JFPRw}<G|YHptH^i>2l_*x5aNw9@Cyul01B-(h5!Hn literal 0 HcmV?d00001 diff --git a/.gitignore b/.gitignore deleted file mode 100644 index ca75e6e..0000000 --- a/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ - -.eslintrc.json -.DS_Store diff --git a/README.md b/README.md index 8e3c1f3..ef9c8f1 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,10 @@ -# Neocities-Extended Node.js Client Library -A node.js library for interacting with the [Neocities](https://neocities.org/api) api that actually has more capabilities than the original api itself. +# NeoCities Node.js Client Library +A node.js library for interacting with the [NeoCities](https://neocities.org/api) api. ## Installation ``` -$ npm install neocities-extended +$ npm install neocities ``` ## Usage @@ -12,8 +12,8 @@ $ npm install neocities-extended First, require the library and initialize: ``` javascript -var Neocities = require('neocities-extended') -var api = new Neocities('YOURUSERNAME', 'YOURPASSWORD') +var NeoCities = require('neocities') +var api = new NeoCities('YOURUSERNAME', 'YOURPASSWORD') ``` ### Uploading files to your site diff --git a/index.js b/index.js index 2eba9cd..21745bb 100644 --- a/index.js +++ b/index.js @@ -6,7 +6,7 @@ var fs = require('fs'), qs = require('querystring'), formData = require('form-data'); -function Neocities(user, pass, opts) { +function NeoCities(user, pass, opts) { if (typeof pass == "object" || pass == undefined) { this.key = user; } @@ -18,7 +18,7 @@ function Neocities(user, pass, opts) { else { this.opts.key = this.opts.key || true; } - this.url = url.parse(this.opts.url || 'https://Neocities.org'); + this.url = url.parse(this.opts.url || 'https://neocities.org'); this.siteurl = this.opts.siteurl ? url.parse(this.ops.siteurl) : null; this.client = this.url.protocol == 'https:' ? https : http if (this.opts.key && !this.key) { @@ -26,7 +26,7 @@ function Neocities(user, pass, opts) { } } -Neocities.prototype.download = function(files, callback) { +NeoCities.prototype.download = function(files, callback) { var i = 0; var that = this; var returnData = {}, returning = false; @@ -56,7 +56,7 @@ Neocities.prototype.download = function(files, callback) { } } -Neocities.prototype.get = function(method, args, callback, user, pass) { +NeoCities.prototype.get = function(method, args, callback, user, pass) { var opts = { method: "get", host: this.url.hostname, @@ -86,7 +86,7 @@ Neocities.prototype.get = function(method, args, callback, user, pass) { request.end(); } -Neocities.prototype.info = function(sitename, callback) { +NeoCities.prototype.info = function(sitename, callback) { var args = null; if(typeof sitename == "function") @@ -97,7 +97,7 @@ Neocities.prototype.info = function(sitename, callback) { this.get("info", args, callback); } -Neocities.prototype.list = function(path, callback) { +NeoCities.prototype.list = function(path, callback) { var args = null; if (typeof path == "function") @@ -108,7 +108,7 @@ Neocities.prototype.list = function(path, callback) { this.get("list", args, callback); } -Neocities.prototype.post = function(method, args, callback) { +NeoCities.prototype.post = function(method, args, callback) { var form = new formData(); for(var i = 0; i < args.length; i ++) @@ -145,7 +145,7 @@ Neocities.prototype.post = function(method, args, callback) { form.pipe(request); } -Neocities.prototype.delete = function(filenames, callback) { +NeoCities.prototype.delete = function(filenames, callback) { var args = []; for(var i = 0; i < filenames.length; i ++) @@ -154,7 +154,7 @@ Neocities.prototype.delete = function(filenames, callback) { this.post("delete", args, callback); } -Neocities.prototype.upload = function(files, callback) { +NeoCities.prototype.upload = function(files, callback) { var args = []; for(var i = 0; i < files.length; i ++) @@ -163,7 +163,7 @@ Neocities.prototype.upload = function(files, callback) { this.post("upload", args, callback); } -Neocities.prototype.push = function(localPath, webPath, excludes, callback) { +NeoCities.prototype.push = function(localPath, webPath, excludes, callback) { if (typeof excludes == "function") { callback = excludes; excludes = []; @@ -194,7 +194,7 @@ Neocities.prototype.push = function(localPath, webPath, excludes, callback) { } } -Neocities.prototype.pull = function(localPath, webPath, excludes, callback) { +NeoCities.prototype.pull = function(localPath, webPath, excludes, callback) { if (typeof excludes == "function") { callback = excludes; excludes = []; @@ -209,4 +209,4 @@ Neocities.prototype.pull = function(localPath, webPath, excludes, callback) { } } -module.exports = Neocities; \ No newline at end of file +module.exports = NeoCities; \ No newline at end of file From a46016a85e2303c356faef0213a89cdc0cf4c68b Mon Sep 17 00:00:00 2001 From: thatcomputerguy Date: Mon, 25 Feb 2019 13:28:36 -0500 Subject: [PATCH 05/15] Reverted formatting changes --- index.js | 336 +++++++++++++++++++++++++++---------------------------- 1 file changed, 168 insertions(+), 168 deletions(-) diff --git a/index.js b/index.js index 21745bb..5178220 100644 --- a/index.js +++ b/index.js @@ -1,212 +1,212 @@ -var fs = require('fs'), - http = require('http'), - https = require('https'), - path = require('path'), - url = require('url'), - qs = require('querystring'), - formData = require('form-data'); +var fs = require('fs'); +var http = require('http'); +var https = require('https'); +var path = require('path'); +var url = require('url'); +var qs = require('querystring'); +var formData = require('form-data'); function NeoCities(user, pass, opts) { - if (typeof pass == "object" || pass == undefined) { - this.key = user; - } - this.opts = opts || (typeof pass == "object" ? pass : {}) - if (!this.opts.key && !this.key) { - this.user = user; - this.pass = pass; - } - else { - this.opts.key = this.opts.key || true; - } - this.url = url.parse(this.opts.url || 'https://neocities.org'); - this.siteurl = this.opts.siteurl ? url.parse(this.ops.siteurl) : null; - this.client = this.url.protocol == 'https:' ? https : http - if (this.opts.key && !this.key) { - this.get("key", null, res => {this.key = res.api_key; if (typeof this.opts.key == "function") this.opts.key(this)}, user, pass); - } + if (typeof pass == "object" || pass == undefined) { + this.key = user; + } + this.opts = opts || (typeof pass == "object" ? pass : {}) + if (!this.opts.key && !this.key) { + this.user = user; + this.pass = pass; + } + else { + this.opts.key = this.opts.key || true; + } + this.url = url.parse(this.opts.url || 'https://neocities.org'); + this.siteurl = this.opts.siteurl ? url.parse(this.ops.siteurl) : null; + this.client = this.url.protocol == 'https:' ? https : http + if (this.opts.key && !this.key) { + this.get("key", null, res => {this.key = res.api_key; if (typeof this.opts.key == "function") this.opts.key(this)}, user, pass); + } } NeoCities.prototype.download = function(files, callback) { - var i = 0; - var that = this; - var returnData = {}, returning = false; - - if (!this.siteurl) - this.info((resp) => { - that.siteurl = url.parse(that.url.protocol + "//" + (resp.info.domain || resp.info.sitename + "." + that.url.hostname)); - dwnldFile(); - }); - else dwnldFile(); - - function dwnldFile() { - if (i < files.length) { - if (files[i].path) { - var file = fs.createWriteStream(files[i].path); - file.on("finish", () => file.close((dwnldFile))); - that.client.get(that.siteurl.href + files[i].name.replace(/^\/?/, "/"), data => data.pipe(file)); - } + var i = 0; + var that = this; + var returnData = {}, returning = false; + + if (!this.siteurl) + this.info((resp) => { + that.siteurl = url.parse(that.url.protocol + "//" + (resp.info.domain || resp.info.sitename + "." + that.url.hostname)); + dwnldFile(); + }); + else dwnldFile(); + + function dwnldFile() { + if (i < files.length) { + if (files[i].path) { + var file = fs.createWriteStream(files[i].path); + file.on("finish", () => file.close((dwnldFile))); + that.client.get(that.siteurl.href + files[i].name.replace(/^\/?/, "/"), data => data.pipe(file)); + } // else { // returning = true; // that.client.request(that.siteurl.href + files[i].name.replace(/^\/?/, "/"), data => returnData[files[i].name] = data); // } - i ++; - } - else if (typeof callback == "function" && !returning) - callback({status:"success"}); + i ++; } + else if (typeof callback == "function" && !returning) + callback({status:"success"}); + } } -NeoCities.prototype.get = function(method, args, callback, user, pass) { - var opts = { - method: "get", - host: this.url.hostname, - port: this.url.port, - path: "/api/" + method + (args ? "?" + qs.stringify(args) : "") - }; - - if (!this.key) { - opts.auth = (user || this.user) + ":" + (pass || this.pass); - } - else { - opts.headers = {Authorization: "Bearer " + this.key}; - } +NeoCities.prototype.get = function(method, args, callback) { + var opts = { + method: "get", + host: this.url.hostname, + port: this.url.port, + path: "/api/" + method + (args ? "?" + qs.stringify(args) : "") + }; + + if (!this.key) { + opts.auth = this.user + ":" + this.pass; + } + else { + opts.headers = {Authorization: "Bearer " + this.key}; + } - var request = this.client.request(opts, function(res) { - var body = ""; + var request = this.client.request(opts, function(res) { + var body = ""; - res.on("data", function (chunk) { - body += chunk; - }) + res.on("data", function (chunk) { + body += chunk; + }) - res.on("end", function() { - var resObj = JSON.parse(body); - callback(resObj); - }) + res.on("end", function() { + var resObj = JSON.parse(body); + callback(resObj); }) - request.end(); + }) + request.end(); } NeoCities.prototype.info = function(sitename, callback) { - var args = null; - - if(typeof sitename == "function") - callback = sitename; - else if(typeof sitename == "string") - args = {sitename: sitename}; - - this.get("info", args, callback); + var args = null; + + if(typeof sitename == "function") + callback = sitename; + else if(typeof sitename == "string") + args = {sitename: sitename}; + + this.get("info", args, callback); } NeoCities.prototype.list = function(path, callback) { - var args = null; - - if (typeof path == "function") - callback = path; - else if (typeof path == "string") - args = {path: path}; - - this.get("list", args, callback); + var args = null; + + if (typeof path == "function") + callback = path; + else if (typeof path == "string") + args = {path: path}; + + this.get("list", args, callback); } NeoCities.prototype.post = function(method, args, callback) { - var form = new formData(); - - for(var i = 0; i < args.length; i ++) - form.append(args[i].name, args[i].value); - - var opts = { - method: "post", - host: this.url.hostname, - port: this.url.port, - path: "/api/" + method, - headers: form.getHeaders() - } - - if (!this.key) { - opts.auth = this.user + ":" + this.pass; - } - else { - opts.headers.Authorization = "Bearer " + this.key; - } - - var request = this.client.request(opts, function(res) { - var body = ""; - - res.on("data", function (chunk) { - body += chunk; - }) - - res.on("end", function() { - var resObj = JSON.parse(body); - callback(resObj); - }) + var form = new formData(); + + for(var i = 0; i < args.length; i ++) + form.append(args[i].name, args[i].value); + + var opts = { + method: "post", + host: this.url.hostname, + port: this.url.port, + path: "/api/" + method, + headers: form.getHeaders() + } + + if (!this.key) { + opts.auth = this.user + ":" + this.pass; + } + else { + opts.headers.Authorization = "Bearer " + this.key; + } + + var request = this.client.request(opts, function(res) { + var body = ""; + + res.on("data", function (chunk) { + body += chunk; }) - form.pipe(request); + res.on("end", function() { + var resObj = JSON.parse(body); + callback(resObj); + }) + }) + + form.pipe(request); } NeoCities.prototype.delete = function(filenames, callback) { - var args = []; - - for(var i = 0; i < filenames.length; i ++) - args.push({name: "filenames[]", value: filenames[i]}); - - this.post("delete", args, callback); + var args = []; + + for(var i = 0; i < filenames.length; i ++) + args.push({name: "filenames[]", value: filenames[i]}); + + this.post("delete", args, callback); } NeoCities.prototype.upload = function(files, callback) { - var args = []; - - for(var i = 0; i < files.length; i ++) - args.push({name: files[i].name, value: fs.createReadStream(files[i].path)}); - - this.post("upload", args, callback); + var args = []; + + for(var i = 0; i < files.length; i ++) + args.push({name: files[i].name, value: fs.createReadStream(files[i].path)}); + + this.post("upload", args, callback); } NeoCities.prototype.push = function(localPath, webPath, excludes, callback) { - if (typeof excludes == "function") { - callback = excludes; - excludes = []; - } - var activePaths = []; - var that = this; - excludes = excludes.map(dir => path.resolve(localPath, dir)); - list(localPath); - - function list(dir) { - activePaths.push(dir); - fs.readdir(dir, parseFiles); - function parseFiles(err, dirContents) { - dirContents = dirContents.map(file => path.resolve(dir, file)); - var uploadArgs = dirContents.filter(file => { - if (excludes.includes(path.relative(localPath, file)) || excludes.some(exclude => (exclude.constructor == RegExp && file.match(exclude)) || (typeof exclude == "function" && exclude(file)))) { - return false; - } - var fileData = fs.statSync(file); - if (fileData.isDirectory()) - list(file); - else - return true; - }).map(file => ({path: file, name: webPath.replace(/[/\\]?$/, "/") + path.relative(path.resolve(localPath), file)})) - activePaths.splice(activePaths.indexOf(dir), 1) - that.upload(uploadArgs, activePaths.length ? () => {} : callback); + if (typeof excludes == "function") { + callback = excludes; + excludes = []; + } + var activePaths = []; + var that = this; + excludes = excludes.map(dir => path.resolve(localPath, dir)); + list(localPath); + + function list(dir) { + activePaths.push(dir); + fs.readdir(dir, parseFiles); + function parseFiles(err, dirContents) { + dirContents = dirContents.map(file => path.resolve(dir, file)); + var uploadArgs = dirContents.filter(file => { + if (excludes.includes(path.relative(localPath, file)) || excludes.some(exclude => (exclude.constructor == RegExp && file.match(exclude)) || (typeof exclude == "function" && exclude(file)))) { + return false; } + var fileData = fs.statSync(file); + if (fileData.isDirectory()) + list(file); + else + return true; + }).map(file => ({path: file, name: webPath.replace(/[/\\]?$/, "/") + path.relative(path.resolve(localPath), file)})) + activePaths.splice(activePaths.indexOf(dir), 1) + that.upload(uploadArgs, activePaths.length ? () => {} : callback); } + } } NeoCities.prototype.pull = function(localPath, webPath, excludes, callback) { - if (typeof excludes == "function") { - callback = excludes; - excludes = []; - } - var that = this; - this.list(filterDirs); - - function filterDirs(files) { - if (files.result == "success") { - that.download(Array.prototype.filter.apply(files, [file => !file.is_directory && (!path.relative(webPath, file.path).match(/^\.\.[/\\]/) || webPath == "/") && !excludes.includes(path.relative(webPath, file.path)) && !excludes.any(exclude => (exclude.prototype == RegExp && file.path.match(exclude)) || (typeof exclude == "function" && exclude(file.path)) || (typeof exclude == "string" && path.relative(exclude, path.relative(webPath, file.path)).match(/^\.\.[/\\]/)))]).map(file => ({path: localPath.replace(/[/\\]?$/, "/") + path.relative(webPath, file.path), name: file})), callback); - } + if (typeof excludes == "function") { + callback = excludes; + excludes = []; + } + var that = this; + this.list(filterDirs); + + function filterDirs(files) { + if (files.result == "success") { + that.download(Array.prototype.filter.apply(files, [file => !file.is_directory && (!path.relative(webPath, file.path).match(/^\.\.[/\\]/) || webPath == "/") && !excludes.includes(path.relative(webPath, file.path)) && !excludes.any(exclude => (exclude.prototype == RegExp && file.path.match(exclude)) || (typeof exclude == "function" && exclude(file.path)) || (typeof exclude == "string" && path.relative(exclude, path.relative(webPath, file.path)).match(/^\.\.[/\\]/)))]).map(file => ({path: localPath.replace(/[/\\]?$/, "/") + path.relative(webPath, file.path), name: file})), callback); } + } } -module.exports = NeoCities; \ No newline at end of file +module.exports = NeoCities; From ce077463f0996dff43f55f6fd26e1d7c53ea0ebd Mon Sep 17 00:00:00 2001 From: thatcomputerguy Date: Mon, 25 Feb 2019 13:34:07 -0500 Subject: [PATCH 06/15] Additional Formatting Changes --- index.js | 76 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/index.js b/index.js index 5178220..3a5e8f5 100644 --- a/index.js +++ b/index.js @@ -7,10 +7,10 @@ var qs = require('querystring'); var formData = require('form-data'); function NeoCities(user, pass, opts) { - if (typeof pass == "object" || pass == undefined) { + if (typeof pass == 'object' || pass == undefined) { this.key = user; } - this.opts = opts || (typeof pass == "object" ? pass : {}) + this.opts = opts || (typeof pass == 'object' ? pass : {}) if (!this.opts.key && !this.key) { this.user = user; this.pass = pass; @@ -22,7 +22,7 @@ function NeoCities(user, pass, opts) { this.siteurl = this.opts.siteurl ? url.parse(this.ops.siteurl) : null; this.client = this.url.protocol == 'https:' ? https : http if (this.opts.key && !this.key) { - this.get("key", null, res => {this.key = res.api_key; if (typeof this.opts.key == "function") this.opts.key(this)}, user, pass); + this.get('key', null, res => {this.key = res.api_key; if (typeof this.opts.key == 'function') this.opts.key(this)}, user, pass); } } @@ -33,7 +33,7 @@ NeoCities.prototype.download = function(files, callback) { if (!this.siteurl) this.info((resp) => { - that.siteurl = url.parse(that.url.protocol + "//" + (resp.info.domain || resp.info.sitename + "." + that.url.hostname)); + that.siteurl = url.parse(that.url.protocol + '//' + (resp.info.domain || resp.info.sitename + '.' + that.url.hostname)); dwnldFile(); }); else dwnldFile(); @@ -42,43 +42,43 @@ NeoCities.prototype.download = function(files, callback) { if (i < files.length) { if (files[i].path) { var file = fs.createWriteStream(files[i].path); - file.on("finish", () => file.close((dwnldFile))); - that.client.get(that.siteurl.href + files[i].name.replace(/^\/?/, "/"), data => data.pipe(file)); + file.on('finish', () => file.close((dwnldFile))); + that.client.get(that.siteurl.href + files[i].name.replace(/^\/?/, '/'), data => data.pipe(file)); } // else { // returning = true; -// that.client.request(that.siteurl.href + files[i].name.replace(/^\/?/, "/"), data => returnData[files[i].name] = data); +// that.client.request(that.siteurl.href + files[i].name.replace(/^\/?/, '/'), data => returnData[files[i].name] = data); // } i ++; } - else if (typeof callback == "function" && !returning) - callback({status:"success"}); + else if (typeof callback == 'function' && !returning) + callback({status:'success'}); } } NeoCities.prototype.get = function(method, args, callback) { var opts = { - method: "get", + method: 'get', host: this.url.hostname, port: this.url.port, - path: "/api/" + method + (args ? "?" + qs.stringify(args) : "") + path: '/api/' + method + (args ? '?' + qs.stringify(args) : '') }; if (!this.key) { - opts.auth = this.user + ":" + this.pass; + opts.auth = this.user + ':' + this.pass; } else { - opts.headers = {Authorization: "Bearer " + this.key}; + opts.headers = {Authorization: 'Bearer ' + this.key}; } var request = this.client.request(opts, function(res) { - var body = ""; + var body = ''; - res.on("data", function (chunk) { + res.on('data', function (chunk) { body += chunk; }) - res.on("end", function() { + res.on('end', function() { var resObj = JSON.parse(body); callback(resObj); }) @@ -89,23 +89,23 @@ NeoCities.prototype.get = function(method, args, callback) { NeoCities.prototype.info = function(sitename, callback) { var args = null; - if(typeof sitename == "function") + if(typeof sitename == 'function') callback = sitename; - else if(typeof sitename == "string") + else if(typeof sitename == 'string') args = {sitename: sitename}; - this.get("info", args, callback); + this.get('info', args, callback); } NeoCities.prototype.list = function(path, callback) { var args = null; - if (typeof path == "function") + if (typeof path == 'function') callback = path; - else if (typeof path == "string") + else if (typeof path == 'string') args = {path: path}; - this.get("list", args, callback); + this.get('list', args, callback); } NeoCities.prototype.post = function(method, args, callback) { @@ -115,28 +115,28 @@ NeoCities.prototype.post = function(method, args, callback) { form.append(args[i].name, args[i].value); var opts = { - method: "post", + method: 'post', host: this.url.hostname, port: this.url.port, - path: "/api/" + method, + path: '/api/' + method, headers: form.getHeaders() } if (!this.key) { - opts.auth = this.user + ":" + this.pass; + opts.auth = this.user + ':' + this.pass; } else { - opts.headers.Authorization = "Bearer " + this.key; + opts.headers.Authorization = 'Bearer ' + this.key; } var request = this.client.request(opts, function(res) { - var body = ""; + var body = ''; - res.on("data", function (chunk) { + res.on('data', function (chunk) { body += chunk; }) - res.on("end", function() { + res.on('end', function() { var resObj = JSON.parse(body); callback(resObj); }) @@ -149,9 +149,9 @@ NeoCities.prototype.delete = function(filenames, callback) { var args = []; for(var i = 0; i < filenames.length; i ++) - args.push({name: "filenames[]", value: filenames[i]}); + args.push({name: 'filenames[]', value: filenames[i]}); - this.post("delete", args, callback); + this.post('delete', args, callback); } NeoCities.prototype.upload = function(files, callback) { @@ -160,11 +160,11 @@ NeoCities.prototype.upload = function(files, callback) { for(var i = 0; i < files.length; i ++) args.push({name: files[i].name, value: fs.createReadStream(files[i].path)}); - this.post("upload", args, callback); + this.post('upload', args, callback); } NeoCities.prototype.push = function(localPath, webPath, excludes, callback) { - if (typeof excludes == "function") { + if (typeof excludes == 'function') { callback = excludes; excludes = []; } @@ -179,7 +179,7 @@ NeoCities.prototype.push = function(localPath, webPath, excludes, callback) { function parseFiles(err, dirContents) { dirContents = dirContents.map(file => path.resolve(dir, file)); var uploadArgs = dirContents.filter(file => { - if (excludes.includes(path.relative(localPath, file)) || excludes.some(exclude => (exclude.constructor == RegExp && file.match(exclude)) || (typeof exclude == "function" && exclude(file)))) { + if (excludes.includes(path.relative(localPath, file)) || excludes.some(exclude => (exclude.constructor == RegExp && file.match(exclude)) || (typeof exclude == 'function' && exclude(file)))) { return false; } var fileData = fs.statSync(file); @@ -187,7 +187,7 @@ NeoCities.prototype.push = function(localPath, webPath, excludes, callback) { list(file); else return true; - }).map(file => ({path: file, name: webPath.replace(/[/\\]?$/, "/") + path.relative(path.resolve(localPath), file)})) + }).map(file => ({path: file, name: webPath.replace(/[/\\]?$/, '/') + path.relative(path.resolve(localPath), file)})) activePaths.splice(activePaths.indexOf(dir), 1) that.upload(uploadArgs, activePaths.length ? () => {} : callback); } @@ -195,7 +195,7 @@ NeoCities.prototype.push = function(localPath, webPath, excludes, callback) { } NeoCities.prototype.pull = function(localPath, webPath, excludes, callback) { - if (typeof excludes == "function") { + if (typeof excludes == 'function') { callback = excludes; excludes = []; } @@ -203,8 +203,8 @@ NeoCities.prototype.pull = function(localPath, webPath, excludes, callback) { this.list(filterDirs); function filterDirs(files) { - if (files.result == "success") { - that.download(Array.prototype.filter.apply(files, [file => !file.is_directory && (!path.relative(webPath, file.path).match(/^\.\.[/\\]/) || webPath == "/") && !excludes.includes(path.relative(webPath, file.path)) && !excludes.any(exclude => (exclude.prototype == RegExp && file.path.match(exclude)) || (typeof exclude == "function" && exclude(file.path)) || (typeof exclude == "string" && path.relative(exclude, path.relative(webPath, file.path)).match(/^\.\.[/\\]/)))]).map(file => ({path: localPath.replace(/[/\\]?$/, "/") + path.relative(webPath, file.path), name: file})), callback); + if (files.result == 'success') { + that.download(Array.prototype.filter.apply(files, [file => !file.is_directory && (!path.relative(webPath, file.path).match(/^\.\.[/\\]/) || webPath == '/') && !excludes.includes(path.relative(webPath, file.path)) && !excludes.any(exclude => (exclude.prototype == RegExp && file.path.match(exclude)) || (typeof exclude == 'function' && exclude(file.path)) || (typeof exclude == 'string' && path.relative(exclude, path.relative(webPath, file.path)).match(/^\.\.[/\\]/)))]).map(file => ({path: localPath.replace(/[/\\]?$/, '/') + path.relative(webPath, file.path), name: file})), callback); } } } From 1c0adce94dc5079670ad91e32bab33b9ae9880b5 Mon Sep 17 00:00:00 2001 From: thatcomputerguy Date: Mon, 25 Feb 2019 15:27:03 -0500 Subject: [PATCH 07/15] Still more formatting corrections --- index.js | 185 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 101 insertions(+), 84 deletions(-) diff --git a/index.js b/index.js index 3a5e8f5..692d01e 100644 --- a/index.js +++ b/index.js @@ -1,118 +1,121 @@ -var fs = require('fs'); -var http = require('http'); -var https = require('https'); -var path = require('path'); -var url = require('url'); -var qs = require('querystring'); -var formData = require('form-data'); +var fs = require('fs') +var http = require('http') +var https = require('https') +var path = require('path') +var url = require('url') +var qs = require('querystring') +var formData = require('form-data') function NeoCities(user, pass, opts) { if (typeof pass == 'object' || pass == undefined) { - this.key = user; + this.key = user } this.opts = opts || (typeof pass == 'object' ? pass : {}) if (!this.opts.key && !this.key) { - this.user = user; - this.pass = pass; + this.user = user + this.pass = pass } else { - this.opts.key = this.opts.key || true; + this.opts.key = this.opts.key || true } - this.url = url.parse(this.opts.url || 'https://neocities.org'); - this.siteurl = this.opts.siteurl ? url.parse(this.ops.siteurl) : null; + this.url = url.parse(this.opts.url || 'https://neocities.org') + this.siteurl = this.opts.siteurl ? url.parse(this.ops.siteurl) : null this.client = this.url.protocol == 'https:' ? https : http if (this.opts.key && !this.key) { - this.get('key', null, res => {this.key = res.api_key; if (typeof this.opts.key == 'function') this.opts.key(this)}, user, pass); + this.get('key', null, function (res) { + this.key = res.api_key + if (typeof this.opts.key == 'function') this.opts.key(this) + }, user, pass) } } NeoCities.prototype.download = function(files, callback) { - var i = 0; - var that = this; - var returnData = {}, returning = false; + var i = 0 + var that = this + var returning = false if (!this.siteurl) - this.info((resp) => { - that.siteurl = url.parse(that.url.protocol + '//' + (resp.info.domain || resp.info.sitename + '.' + that.url.hostname)); - dwnldFile(); - }); - else dwnldFile(); + this.info(function (resp) { + that.siteurl = url.parse(that.url.protocol + '//' + (resp.info.domain || resp.info.sitename + '.' + that.url.hostname)) + dwnldFile() + }) + else dwnldFile() function dwnldFile() { if (i < files.length) { if (files[i].path) { - var file = fs.createWriteStream(files[i].path); - file.on('finish', () => file.close((dwnldFile))); - that.client.get(that.siteurl.href + files[i].name.replace(/^\/?/, '/'), data => data.pipe(file)); + var file = fs.createWriteStream(files[i].path) + file.on('finish', function() {file.close((dwnldFile))}) + that.client.get(that.siteurl.href + files[i].name.replace(/^\/?/, '/'), function(data) {return data.pipe(file)}) } // else { -// returning = true; -// that.client.request(that.siteurl.href + files[i].name.replace(/^\/?/, '/'), data => returnData[files[i].name] = data); +// returning = true +// that.client.request(that.siteurl.href + files[i].name.replace(/^\/?/, '/'), function (data) {returnData[files[i].name] = data}) // } - i ++; + i ++ } else if (typeof callback == 'function' && !returning) - callback({status:'success'}); + callback({status:'success'}) } } -NeoCities.prototype.get = function(method, args, callback) { +NeoCities.prototype.get = function(method, args, callback, user, pass) { var opts = { method: 'get', host: this.url.hostname, port: this.url.port, path: '/api/' + method + (args ? '?' + qs.stringify(args) : '') - }; + } if (!this.key) { - opts.auth = this.user + ':' + this.pass; + opts.auth = (user || this.user) + ':' + (pass || this.pass) } else { - opts.headers = {Authorization: 'Bearer ' + this.key}; + opts.headers = {Authorization: 'Bearer ' + this.key} } var request = this.client.request(opts, function(res) { - var body = ''; + var body = '' res.on('data', function (chunk) { - body += chunk; + body += chunk }) res.on('end', function() { - var resObj = JSON.parse(body); - callback(resObj); + var resObj = JSON.parse(body) + callback(resObj) }) }) - request.end(); + request.end() } NeoCities.prototype.info = function(sitename, callback) { - var args = null; + var args = null if(typeof sitename == 'function') - callback = sitename; + callback = sitename else if(typeof sitename == 'string') - args = {sitename: sitename}; + args = {sitename: sitename} - this.get('info', args, callback); + this.get('info', args, callback) } NeoCities.prototype.list = function(path, callback) { - var args = null; + var args = null if (typeof path == 'function') - callback = path; + callback = path else if (typeof path == 'string') - args = {path: path}; + args = {path: path} - this.get('list', args, callback); + this.get('list', args, callback) } NeoCities.prototype.post = function(method, args, callback) { - var form = new formData(); + var form = new formData() for(var i = 0; i < args.length; i ++) - form.append(args[i].name, args[i].value); + form.append(args[i].name, args[i].value) var opts = { method: 'post', @@ -123,90 +126,104 @@ NeoCities.prototype.post = function(method, args, callback) { } if (!this.key) { - opts.auth = this.user + ':' + this.pass; + opts.auth = this.user + ':' + this.pass } else { - opts.headers.Authorization = 'Bearer ' + this.key; + opts.headers.Authorization = 'Bearer ' + this.key } var request = this.client.request(opts, function(res) { - var body = ''; + var body = '' res.on('data', function (chunk) { - body += chunk; + body += chunk }) res.on('end', function() { - var resObj = JSON.parse(body); - callback(resObj); + var resObj = JSON.parse(body) + callback(resObj) }) }) - form.pipe(request); + form.pipe(request) } NeoCities.prototype.delete = function(filenames, callback) { - var args = []; + var args = [] for(var i = 0; i < filenames.length; i ++) - args.push({name: 'filenames[]', value: filenames[i]}); + args.push({name: 'filenames[]', value: filenames[i]}) - this.post('delete', args, callback); + this.post('delete', args, callback) } NeoCities.prototype.upload = function(files, callback) { - var args = []; + var args = [] for(var i = 0; i < files.length; i ++) - args.push({name: files[i].name, value: fs.createReadStream(files[i].path)}); + args.push({name: files[i].name, value: fs.createReadStream(files[i].path)}) - this.post('upload', args, callback); + this.post('upload', args, callback) } NeoCities.prototype.push = function(localPath, webPath, excludes, callback) { + if (typeof webPath == 'function' || typeof webPath == 'object') { + callback = excludes + excludes = webPath + webPath = '/' + } if (typeof excludes == 'function') { - callback = excludes; - excludes = []; + callback = excludes + excludes = [] } - var activePaths = []; - var that = this; - excludes = excludes.map(dir => path.resolve(localPath, dir)); - list(localPath); + var activePaths = [] + var that = this + excludes = excludes.map(function(dir) {return path.resolve(localPath, dir)}) + list(localPath) function list(dir) { - activePaths.push(dir); - fs.readdir(dir, parseFiles); + activePaths.push(dir) + fs.readdir(dir, parseFiles) function parseFiles(err, dirContents) { - dirContents = dirContents.map(file => path.resolve(dir, file)); - var uploadArgs = dirContents.filter(file => { - if (excludes.includes(path.relative(localPath, file)) || excludes.some(exclude => (exclude.constructor == RegExp && file.match(exclude)) || (typeof exclude == 'function' && exclude(file)))) { - return false; + dirContents = dirContents.map(function(file) {return path.resolve(dir, file)}) + var uploadArgs = dirContents.filter(function(file) { + if (excludes.includes(path.relative(localPath, file)) || excludes.some(function(exclude) {return (exclude.constructor == RegExp && file.match(exclude)) || (typeof exclude == 'function' && exclude(file))})) { + return false } - var fileData = fs.statSync(file); + var fileData = fs.statSync(file) if (fileData.isDirectory()) - list(file); + list(file) else - return true; - }).map(file => ({path: file, name: webPath.replace(/[/\\]?$/, '/') + path.relative(path.resolve(localPath), file)})) + return true + }).map(function(file) {return {path: file, name: webPath.replace(/[/\\]?$/, '/') + path.relative(path.resolve(localPath), file)}}) activePaths.splice(activePaths.indexOf(dir), 1) - that.upload(uploadArgs, activePaths.length ? () => {} : callback); + that.upload(uploadArgs, activePaths.length ? function() {} : callback) } } } NeoCities.prototype.pull = function(localPath, webPath, excludes, callback) { + if (typeof webPath == 'function' || typeof webPath == 'object') { + callback = excludes + excludes = webPath + webPath = '/' + } if (typeof excludes == 'function') { - callback = excludes; - excludes = []; + callback = excludes + excludes = [] } - var that = this; - this.list(filterDirs); + var that = this + this.list(filterDirs) function filterDirs(files) { if (files.result == 'success') { - that.download(Array.prototype.filter.apply(files, [file => !file.is_directory && (!path.relative(webPath, file.path).match(/^\.\.[/\\]/) || webPath == '/') && !excludes.includes(path.relative(webPath, file.path)) && !excludes.any(exclude => (exclude.prototype == RegExp && file.path.match(exclude)) || (typeof exclude == 'function' && exclude(file.path)) || (typeof exclude == 'string' && path.relative(exclude, path.relative(webPath, file.path)).match(/^\.\.[/\\]/)))]).map(file => ({path: localPath.replace(/[/\\]?$/, '/') + path.relative(webPath, file.path), name: file})), callback); + that.download(Array.prototype.filter.call(files, function(file) { + return !file.is_directory && (!path.relative(webPath, file.path).match(/^\.\.[/\\]/) || webPath == '/') && !excludes.includes(path.relative(webPath, file.path)) && !excludes.any(function(exclude) { + return (exclude.prototype == RegExp && file.path.match(exclude)) || (typeof exclude == 'function' && exclude(file.path)) || (typeof exclude == 'string' && path.relative(exclude, path.relative(webPath, file.path)).match(/^\.\.[/\\]/)) + }) + }).map(function(file) {return ({path: localPath.replace(/[/\\]?$/, '/') + path.relative(webPath, file.path), name: file})}), callback) } } } -module.exports = NeoCities; +module.exports = NeoCities From 9e0d37c82e4f941fb5be22905aa507827a22682d Mon Sep 17 00:00:00 2001 From: thatcomputerguy Date: Mon, 25 Feb 2019 15:30:39 -0500 Subject: [PATCH 08/15] Unrenamed FormData from formData --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 692d01e..9c9b7a3 100644 --- a/index.js +++ b/index.js @@ -4,7 +4,7 @@ var https = require('https') var path = require('path') var url = require('url') var qs = require('querystring') -var formData = require('form-data') +var FormData = require('form-data') function NeoCities(user, pass, opts) { if (typeof pass == 'object' || pass == undefined) { @@ -112,7 +112,7 @@ NeoCities.prototype.list = function(path, callback) { } NeoCities.prototype.post = function(method, args, callback) { - var form = new formData() + var form = new FormData() for(var i = 0; i < args.length; i ++) form.append(args[i].name, args[i].value) From 3947622dab3e77c791a7f2b3106618336d25499f Mon Sep 17 00:00:00 2001 From: thatcomputerguy0101 Date: Tue, 26 Feb 2019 06:53:41 -0500 Subject: [PATCH 09/15] Remove .DS_Store (MacOS hidden file) --- .DS_Store | Bin 6148 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index f0294a3eab57ee2fddb408d093a5d368e5fa545a..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHK%}N6?5T3CG7d%)Gg2%i<-yoLt=&3Itb^U|grbv71ArI!oH}jj!s9koU;6X%Y zVDe3pnPm2ZO)^Bpi(NA(nh{Y26=X3+M21JFPRw}<G|YHptH^i>2l_*x5aNw9@Cyul01B-(h5!Hn From bab3f61d1fc7060e49214fcfae96b276c294753a Mon Sep 17 00:00:00 2001 From: thatcomputerguy Date: Tue, 26 Feb 2019 08:01:57 -0500 Subject: [PATCH 10/15] Tempoary Instalation instructions --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8e3c1f3..4f14af1 100644 --- a/README.md +++ b/README.md @@ -3,16 +3,19 @@ A node.js library for interacting with the [Neocities](https://neocities.org/api ## Installation +WARNING - This module is not yet on NPM; the following command will not work ``` $ npm install neocities-extended ``` +Instead, clone this repository where you want it to be installed, and reference the folder directly in the require statement. + ## Usage First, require the library and initialize: ``` javascript -var Neocities = require('neocities-extended') +var Neocities = require('neocities-extended') // or reference to the folder var api = new Neocities('YOURUSERNAME', 'YOURPASSWORD') ``` From fa62b4f531a57a447b3307dd85beb6f82f07f114 Mon Sep 17 00:00:00 2001 From: thatcomputerguy0101 Date: Wed, 21 Aug 2019 11:56:00 -0400 Subject: [PATCH 11/15] Update JS formatting --- index.js | 50 +++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 39 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index 9c9b7a3..3f29b64 100644 --- a/index.js +++ b/index.js @@ -34,11 +34,12 @@ NeoCities.prototype.download = function(files, callback) { var that = this var returning = false - if (!this.siteurl) + if (!this.siteurl) { this.info(function (resp) { that.siteurl = url.parse(that.url.protocol + '//' + (resp.info.domain || resp.info.sitename + '.' + that.url.hostname)) dwnldFile() }) + } else dwnldFile() function dwnldFile() { @@ -46,11 +47,13 @@ NeoCities.prototype.download = function(files, callback) { if (files[i].path) { var file = fs.createWriteStream(files[i].path) file.on('finish', function() {file.close((dwnldFile))}) - that.client.get(that.siteurl.href + files[i].name.replace(/^\/?/, '/'), function(data) {return data.pipe(file)}) + that.client.get(that.siteurl.href + files[i].name.replace(/^\/?/, '/'), + function(data) {return data.pipe(file)}) } // else { // returning = true -// that.client.request(that.siteurl.href + files[i].name.replace(/^\/?/, '/'), function (data) {returnData[files[i].name] = data}) +// that.client.request(that.siteurl.href + files[i].name.replace(/^\/?/, '/'), +// . function (data) {returnData[files[i].name] = data}) // } i ++ } @@ -179,23 +182,38 @@ NeoCities.prototype.push = function(localPath, webPath, excludes, callback) { var activePaths = [] var that = this excludes = excludes.map(function(dir) {return path.resolve(localPath, dir)}) + list(localPath) function list(dir) { activePaths.push(dir) fs.readdir(dir, parseFiles) + function parseFiles(err, dirContents) { dirContents = dirContents.map(function(file) {return path.resolve(dir, file)}) var uploadArgs = dirContents.filter(function(file) { - if (excludes.includes(path.relative(localPath, file)) || excludes.some(function(exclude) {return (exclude.constructor == RegExp && file.match(exclude)) || (typeof exclude == 'function' && exclude(file))})) { + if (excludes.includes(path.relative(localPath, file)) || + excludes.some(function(exclude) { + return (exclude.constructor == RegExp && file.match(exclude)) || + (typeof exclude == 'function' && exclude(file)) + })) { return false } + var fileData = fs.statSync(file) - if (fileData.isDirectory()) + + if (fileData.isDirectory()) { list(file) - else + } + else { return true - }).map(function(file) {return {path: file, name: webPath.replace(/[/\\]?$/, '/') + path.relative(path.resolve(localPath), file)}}) + } + }).map(function(file) { + return { + path: file, name: webPath.replace(/[/\\]?$/, '/') + + path.relative(path.resolve(localPath), file) + } + }) activePaths.splice(activePaths.indexOf(dir), 1) that.upload(uploadArgs, activePaths.length ? function() {} : callback) } @@ -218,10 +236,20 @@ NeoCities.prototype.pull = function(localPath, webPath, excludes, callback) { function filterDirs(files) { if (files.result == 'success') { that.download(Array.prototype.filter.call(files, function(file) { - return !file.is_directory && (!path.relative(webPath, file.path).match(/^\.\.[/\\]/) || webPath == '/') && !excludes.includes(path.relative(webPath, file.path)) && !excludes.any(function(exclude) { - return (exclude.prototype == RegExp && file.path.match(exclude)) || (typeof exclude == 'function' && exclude(file.path)) || (typeof exclude == 'string' && path.relative(exclude, path.relative(webPath, file.path)).match(/^\.\.[/\\]/)) - }) - }).map(function(file) {return ({path: localPath.replace(/[/\\]?$/, '/') + path.relative(webPath, file.path), name: file})}), callback) + return !file.is_directory && + (!path.relative(webPath, file.path).match(/^\.\.[/\\]/) || webPath == '/') && + !excludes.includes(path.relative(webPath, file.path)) && + !excludes.any(function(exclude) { + return (exclude.prototype == RegExp && file.path.match(exclude)) || + (typeof exclude == 'function' && exclude(file.path)) || + (typeof exclude == 'string' && path.relative(exclude, path.relative(webPath, file.path)).match(/^\.\.[/\\]/)) + }) + }).map(function(file) { + return { + path: localPath.replace(/[/\\]?$/, '/') + path.relative(webPath, file.path), + name: file + } + }), callback) } } } From f7079bc36bf27a800209307b8700686579188f3a Mon Sep 17 00:00:00 2001 From: thatcomputerguy Date: Mon, 30 Mar 2020 10:31:16 -0400 Subject: [PATCH 12/15] Correct spelling --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 816471d..b8ddbfc 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,7 @@ api.info('youpi', function(resp) { ### Use an API Key -The API key is a more secure way to upload data, since it doesn't store or send your username or password. First, Log in normally with a callback for the key option. (This then uses the key once it is aquired instead of your username and password.) +The API key is a more secure way to upload data, since it doesn't store or send your username or password. First, Log in normally with a callback for the key option. (This then uses the key once it is acquired instead of your username and password.) ``` javascript var api = new NeoCities('YOURUSERNAME', 'YOURPASSWORD', {key: function(key) {/* store your key here */}}) From 8fbe3ac039ba0e389737e983008251855b982c7e Mon Sep 17 00:00:00 2001 From: thatcomputerguy Date: Mon, 30 Mar 2020 10:32:55 -0400 Subject: [PATCH 13/15] Fix an error from improper Git usage --- README.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/README.md b/README.md index b8ddbfc..0b3c709 100644 --- a/README.md +++ b/README.md @@ -3,13 +3,10 @@ A node.js library for interacting with the [NeoCities](https://neocities.org/api ## Installation -WARNING - This module is not yet on NPM; the following command will not work ``` $ npm install neocities ``` -Instead, clone this repository where you want it to be installed, and reference the folder directly in the require statement. - ## Usage First, require the library and initialize: From ccf3c5d0f353546cc98cb8dd99a4c745a19f1709 Mon Sep 17 00:00:00 2001 From: thatcomputerguy Date: Mon, 30 Mar 2020 19:14:53 -0400 Subject: [PATCH 14/15] Fix errors from code that I didn't test --- index.js | 52 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/index.js b/index.js index 3f29b64..bf6e370 100644 --- a/index.js +++ b/index.js @@ -30,6 +30,7 @@ function NeoCities(user, pass, opts) { } NeoCities.prototype.download = function(files, callback) { + console.log(files) var i = 0 var that = this var returning = false @@ -230,26 +231,41 @@ NeoCities.prototype.pull = function(localPath, webPath, excludes, callback) { callback = excludes excludes = [] } + var that = this - this.list(filterDirs) - function filterDirs(files) { - if (files.result == 'success') { - that.download(Array.prototype.filter.call(files, function(file) { - return !file.is_directory && - (!path.relative(webPath, file.path).match(/^\.\.[/\\]/) || webPath == '/') && - !excludes.includes(path.relative(webPath, file.path)) && - !excludes.any(function(exclude) { - return (exclude.prototype == RegExp && file.path.match(exclude)) || - (typeof exclude == 'function' && exclude(file.path)) || - (typeof exclude == 'string' && path.relative(exclude, path.relative(webPath, file.path)).match(/^\.\.[/\\]/)) - }) - }).map(function(file) { - return { - path: localPath.replace(/[/\\]?$/, '/') + path.relative(webPath, file.path), - name: file - } - }), callback) + fs.exists(localPath, createIfFailed) + + function createIfFailed(pathExists) { + if (!pathExists) { + fs.mkdir(localPath, {}, startDownload) + } + else { + startDownload() + } + } + + function startDownload() { + that.list(filterDirs) + + function filterDirs(files) { + if (files.result == 'success') { + that.download(files.files.filter(function(file) { + return !file.is_directory && + (!path.relative(webPath, file.path).match(/^\.\.[/\\]/) || webPath == '/') && + !excludes.includes(path.relative(webPath, file.path)) && + !excludes.some(function(exclude) { + return (exclude.prototype == RegExp && file.path.match(exclude)) || + (typeof exclude == 'function' && exclude(file.path)) || + (typeof exclude == 'string' && path.relative(exclude, path.relative(webPath, file.path)).match(/^\.\.[/\\]/)) + }) + }).map(function(file) { + return { + path: localPath.replace(/[/\\]?$/, '/') + path.relative(webPath, file.path), + name: file.path + } + }), callback) + } } } } From c91b0c303a5daf797024753a166dfe5016bddcfc Mon Sep 17 00:00:00 2001 From: thatcomputerguy0101 Date: Fri, 6 Nov 2020 18:39:12 -0500 Subject: [PATCH 15/15] Fix regular expression period escapes --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4f14af1..52d336f 100644 --- a/README.md +++ b/README.md @@ -93,7 +93,7 @@ var api = new Neocities('YOURAPIKEY') // foo is the local folder, images is what it will be named on your site // hidden.json is not uploaded, nor any file ending with .conf.json -api.push('foo/', 'images/', ['hidden.json', /.conf.json$/]) +api.push('foo/', 'images/', ['hidden.json', /\.conf\.json$/]) ``` ### Pulling a folder