Switch back over to CJS. sa la vie

This commit is contained in:
Bret Comnes 2019-12-26 16:50:42 -07:00
parent 364b8fc2ed
commit abc9656e71
No known key found for this signature in database
GPG Key ID: 3705F4634DC3A1AC
11 changed files with 183 additions and 1841 deletions

17
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,17 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/test.js"
}
]
}

7
README.md Normal file
View File

@ -0,0 +1,7 @@
# deploy-to-neocities
## See also
- [Neocities API Docs](https://neocities.org/api)
- [neocities/neocities-node](https://github.com/neocities/neocities-node): Node api

1678
dist/bundle.cjs.js vendored

File diff suppressed because it is too large Load Diff

BIN
fixtures/toot.gif Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 461 B

BIN
fixtures/tootzzz.png Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

View File

@ -1,4 +1,4 @@
// Set options as a parameter, environment variable, or rc file.
// eslint-disable-next-line no-global-assign
require = require('esm')(module/* , options */);
module.exports = require('./main.js');
const { NeocitiesAPIClient } = require('./lib/client.js')
module.exports = { NeocitiesAPIClient }

View File

@ -1,134 +1,124 @@
import assert from 'nanoassert';
import fetch from 'node-fetch';
import { URL } from 'url';
import qs from 'qs';
import pkg from '../package.json';
import os from 'os';
const assert = require('nanoassert')
const fetch = require('node-fetch')
const { URL } = require('url')
const qs = require('qs')
const os = require('os')
const { createReadStream } = require('fs')
const FormData = require('form-data')
const { handleResponse } = require('fetch-errors')
const pkg = require('../package.json')
const defaultURL = 'https://neocities.org/api';
const defaultURL = 'https://neocities.org'
export class NeocitiesAPIClient {
class NeocitiesAPIClient {
static getKey (sitename, password, opts) {
assert(sitename, 'must pass sitename as first arg');
assert(typeof sitename === 'string', 'user arg must be a string');
assert(password, 'must pass a password as the second arg');
assert(typeof password, 'password arg must be a string');
assert(sitename, 'must pass sitename as first arg')
assert(typeof sitename === 'string', 'user arg must be a string')
assert(password, 'must pass a password as the second arg')
assert(typeof password, 'password arg must be a string')
opts = Object.assign({
url: defaultURL
}, opts);
}, opts)
return fetch();
return fetch()
}
constructor (apiKey, opts) {
assert(apiKey, 'must pass apiKey as first argument');
assert(typeof apiKey === 'string', 'apiKey must be a string');
assert(apiKey, 'must pass apiKey as first argument')
assert(typeof apiKey === 'string', 'apiKey must be a string')
opts = Object.assign({
url: defaultURL
});
})
this.opts = opts;
this.url = opts.url;
this.apiKey = apiKey;
this.opts = opts
this.url = opts.url
this.apiKey = apiKey
}
request (endpoint, args, opts) {
assert(endpoint, 'must pass endpoint as first argument');
opts = Object.assign({}, opts);
opts.headers = Object.assign({
get defaultHeaders () {
return {
Authorization: `Bearer ${this.apiKey}`,
Accept: 'application/json',
'User-Agent': `deploy-to-neocities/${pkg.version} (${os.type()})`
}, opts.headers);
let path = `/api/${endpoint}`;
if (args) path += `?${qs.stringify(args)}`;
const url = new URL(path, this.url);
return fetch(url, opts);
}
}
get (endpoint, args, opts) {
get (endpoint, quieries, opts) {
assert(endpoint, 'must pass endpoint as first argument')
opts = Object.assign({
method: 'GET'
}, opts);
return this.request(endpoint, args, opts);
}, opts)
opts.headers = Object.assign({}, this.defaultHeaders, opts.headers)
let path = `/api/${endpoint}`
if (quieries) path += `?${qs.stringify(quieries)}`
const url = new URL(path, this.url)
return fetch(url, opts)
}
post (endpoint, args, opts) {
post (endpoint, formEntries, opts) {
assert(endpoint, 'must pass endpoint as first argument')
const form = new FormData()
opts = Object.assign({
method: 'POST'
}, opts);
return this.request(endpoint, args, opts);
method: 'POST',
body: form
}, opts)
for (const { name, value } of formEntries) {
form.append(name, value)
}
opts.headers = Object.assign(
{},
this.defaultHeaders,
form.getHeaders(),
opts.headers)
const url = new URL(`/api/${endpoint}`, this.url)
return fetch(url, opts)
}
upload (files) {
throw new Error('NOT IMPLEMENTED');
const formEntries = files.map(({ name, path }) => {
return {
name,
value: createReadStream(path)
}
})
deploy (folder) {
throw new Error('NOT IMPLEMENTED');
return this.post('upload', formEntries).then(handleResponse)
}
delete (filenames) {
const args = {
filenames
};
assert(filenames, 'filenames is a required first argument')
assert(Array.isArray(filenames), 'filenames argument must be an array of file paths in your website')
return this.post('/delete', args);
const formEntries = filenames.map(file => ({
name: 'filenames[]',
value: file
}))
return this.post('delete', formEntries).then(handleResponse)
}
list (args) {
list (queries) {
// args.path: Path to list
return this.get('/list', args).then(handleResponse);
return this.get('list', queries).then(handleResponse)
}
/**
* @param {Object} args Querystring arguments to include
* @return {Promise} Fetch request promise
*/
info (args) {
info (queries) {
// args.sitename: sitename to get info on
return this.get('/info', args).then(handleResponse);
}
return this.get('info', queries).then(handleResponse)
}
export async function handleResponse (response) {
const contentType = response.headers.get('Content-Type');
const isJSON = contentType && contentType.match(/json/);
const data = isJSON ? await response.json() : await response.text();
if (response.ok) return data;
else {
return isJSON
? Promise.reject(new JSONHTTPError(response, data))
: Promise.reject(new TextHTTPError(response, data));
}
}
export class HTTPError extends Error {
constructor (response) {
super(response.statusText);
this.name = this.constructor.name;
if (typeof Error.captureStackTrace === 'function') {
Error.captureStackTrace(this, this.constructor);
} else {
this.stack = new Error(response.statusText).stack;
}
this.status = response.status;
}
}
export class TextHTTPError extends HTTPError {
constructor (response, data) {
super(response);
this.data = data;
}
}
export class JSONHTTPError extends HTTPError {
constructor (response, json) {
super(response);
this.json = json;
deploy (folder) {
throw new Error('NOT IMPLEMENTED')
}
}
module.exports = { NeocitiesAPIClient }

View File

@ -1,4 +0,0 @@
// ESM syntax is supported.
import { NeocitiesAPIClient } from './lib/client.js';
export { NeocitiesAPIClient };

View File

@ -3,17 +3,13 @@
"version": "0.0.0",
"description": "Github Action to deplpoy a folder to Neocities.org",
"main": "index.js",
"module": "main.js",
"private": true,
"scripts": {
"test": "run-s test:*",
"test-skip:deps": "dependency-check . --no-dev --no-peer",
"test:standard": "semistandard",
"test:tape": "tape -r esm test.js",
"prepare": "run-s build",
"clean": "rimraf dist && mkdirp dist",
"build": "run-s clean build:*",
"build:browserify": "rollup --config"
"test:standard": "standard",
"test:tape": "tap",
"clean": "rimraf dist && mkdirp dist"
},
"repository": {
"type": "git",
@ -29,24 +25,21 @@
"devDependencies": {
"builtin-modules": "^3.1.0",
"dependency-check": "^4.1.0",
"esm": "^3.2.25",
"mkdirp": "^0.5.1",
"npm-run-all": "^4.1.5",
"rimraf": "^3.0.0",
"rollup": "^1.26.5",
"rollup-plugin-commonjs": "^10.1.0",
"rollup-plugin-node-resolve": "^5.2.0",
"semistandard": "^14.2.0",
"tape": "^4.11.0",
"tape-promise": "^4.0.0"
"standard": "^14.3.1",
"tap": "^14.10.5"
},
"dependencies": {
"@actions/core": "^1.2.0",
"@actions/github": "^1.1.0",
"fetch-errors": "^1.0.1",
"form-data": "^3.0.0",
"nanoassert": "^2.0.0",
"qs": "^6.9.1"
},
"semistandard": {
"standard": {
"ignore": [
"dist"
]

View File

@ -1,21 +0,0 @@
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import builtins from 'builtin-modules';
export default [
{
input: 'main.js',
output: [
{ file: 'dist/bundle.cjs.js', format: 'cjs' }
],
plugins: [
resolve({
browser: false,
preferBuiltins: true
}),
commonjs()
],
external: builtins
}
];

100
test.js
View File

@ -1,44 +1,82 @@
import tape from 'tape';
import ptape from 'tape-promise';
import { readFileSync } from 'fs';
import { resolve } from 'path';
import { NeocitiesAPIClient } from './lib/client.js';
const test = ptape(tape);
const tap = require('tap')
let token = process.env.NEOCITIES_API_TOKEN;
const { readFileSync } = require('fs')
const { resolve } = require('path')
const { NeocitiesAPIClient } = require('./lib/client')
let token = process.env.NEOCITIES_API_TOKEN
if (!token) {
try {
const config = JSON.parse(readFileSync(resolve(__dirname, 'config.json')));
token = config.token;
const config = JSON.parse(readFileSync(resolve(__dirname, 'config.json')))
token = config.token
} catch (e) {
console.warn('error loading config.json');
console.warn(e);
console.warn('error loading config.json')
console.warn(e)
}
}
if (token) {
test('token loaded', async t => {
t.ok(token);
});
tap.test('token loaded', async t => {
t.ok(token)
})
test('basic client api', async t => {
const client = new NeocitiesAPIClient(token);
tap.test('basic client api', async t => {
const client = new NeocitiesAPIClient(token)
t.ok(client.info, 'info method available');
t.ok(client.list, 'list method available');
t.ok(client.get, 'get method available');
t.ok(client.post, 'post method available');
});
t.ok(client.info, 'info method available')
t.ok(client.list, 'list method available')
t.ok(client.get, 'get method available')
t.ok(client.post, 'post method available')
})
test('can get info about site', async t => {
const client = new NeocitiesAPIClient(token);
tap.test('can get info about site', async t => {
const client = new NeocitiesAPIClient(token)
const info = await client.info();
console.log(info);
const list = await client.list();
console.log(list);
});
} else {
console.warn('No token set, live tests disabled');
const info = await client.info()
console.log(info)
t.equal(info.result, 'success', 'info requesst successfull')
const list = await client.list()
console.log(list)
t.equal(list.result, 'success', 'list result successfull')
})
// test('form data works the way I think', t => {
// const form = new FormData();
// const p = resolve(__dirname, 'package.json');
// form.append('package.json', next => next(createReadStream(p)));
//
// const concatStream = concat((data) => {
// console.log(data);
// t.end();
// });
//
// form.on('error', (err) => {
// t.error(err);
// });
// form.pipe(concatStream);
// });
tap.test('can upload and delete files', async t => {
const client = new NeocitiesAPIClient(token)
const uploadResults = await client.upload([
{
name: 'toot.gif',
path: resolve(__dirname, 'fixtures/toot.gif')
},
{
name: 'img/tootzzz.png',
path: resolve(__dirname, 'fixtures/tootzzz.png')
}
])
console.log(uploadResults)
t.equal(uploadResults.result, 'success', 'list result successfull')
const deleteResults = await client.delete([
'toot.gif',
'img/tootzzz.png'
])
console.log(deleteResults)
t.equal(deleteResults.result, 'success', 'list result successfull')
})