Skip to content

Commit

Permalink
feat: fetch latest spec, add cli
Browse files Browse the repository at this point in the history
  • Loading branch information
jrea committed Apr 26, 2022
1 parent 2ac6027 commit 8b03527
Show file tree
Hide file tree
Showing 14 changed files with 678 additions and 500 deletions.
18 changes: 16 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
{
"parser": "@typescript-eslint/parser",
"plugins": ["react-hooks", "@typescript-eslint", "prettier", "import"],
"plugins": [
"react-hooks",
"@typescript-eslint",
"prettier",
"import",
"react"
],
"settings": {
"react": {
"version": "detect"
}
},
"extends": [
"plugin:react/recommended",
"eslint:recommended",
"prettier",
"plugin:@typescript-eslint/recommended",
"plugin:react-hooks/recommended"
],
"parserOptions": {
"extraFileExtensions": [".mjs"]
},
"env": {
"browser": true,
"node": true
Expand All @@ -25,7 +39,7 @@
"trailingComma": "es5"
}
],
"indent": ["error", 2],
"indent": ["error", 2, { "SwitchCase": 1 }],
"semi": "error",
"import/namespace": 2,
"quotes": [
Expand Down
163 changes: 163 additions & 0 deletions lib/nile/bin/nile.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
#!/usr/bin/env node
/* eslint-disable no-console */

import fs from 'fs';
import { exec } from 'child_process';
import path from 'path';
import readline from 'readline';

import fetch from 'node-fetch';
import sade from 'sade';

const prog = sade('nile');

const stdin = process.stdin;
stdin.setEncoding('utf-8');

let rootDir = process.cwd();
if (!rootDir.endsWith('/lib/nile')) {
rootDir = path.join(process.cwd(), 'node_modules/@theniledev/js');
}
const specDir = path.join(rootDir, 'spec');
const apiFile = path.join(specDir, 'api.yaml');

const handlePassword = (query) =>
new Promise((resolve) => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const stdin = process.openStdin();
process.stdin.on('data', (char) => {
char = char + '';
switch (char) {
case '\n':
case '\r':
case '\u0004':
stdin.pause();
break;
default:
process.stdout.clearLine();
readline.cursorTo(process.stdout, 0);
process.stdout.write(query);
break;
}
});
rl.question(query, (value) => {
rl.history = rl.history.slice(1);
rl.close();
resolve(value);
});
});

prog
.command('rebuild <url>')
.option('-v, --verbose', 'Enable verbose output')
.option('-u, --user', 'The email address to use for authentication.')
.describe('Rebuilds the the SDK after entities have been updated.')
.example('rebuild http://localhost:8080 -u [email protected]')
.action((url, opts) => {
const user = opts.user || opts.u;
if (!user) {
console.log('The user param is required.');
return;
}
const run = async () => {
const password = await handlePassword('password:');
await rebuild(url, opts, password);
};

run();
});

async function rebuild(url, opts, password) {
const verbose = opts.verbose || opts.v;
const user = opts.user || opts.u;

if (verbose) {
console.log('logging in to', url);
}

const tokenRes = await fetch(`${url}/auth/login`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ email: String(user), password: String(password) }),
}).then((res) => res.json());

if (!tokenRes.token) {
if (verbose) {
console.log(tokenRes);
}

console.log('Invalid credentials');
return;
}

if (verbose) {
console.log('token retrieved', String(tokenRes.token));
console.log('fetching from ', url);
}

console.log('fetching latest spec...');
const response = await fetch(url, {
headers: {
Authorization: `Bearer ${tokenRes.token}`,
},
});

const text = await response.text();

if (verbose) {
console.log('spec to be loaded:', text);
}

if (!fs.existsSync(specDir)) {
if (verbose) {
console.log('creating spec directory', specDir);
}
fs.mkdirSync(specDir);
}

if (verbose) {
console.log('writing api.yaml');
}

fs.writeFileSync(apiFile, text);

console.log('building SDK...');

if (verbose) {
console.log('running npm install in', rootDir);
}

exec('npm install', { cwd: rootDir }, async (err, stdout) => {
if (verbose) {
console.log(stdout);
}

if (err) {
console.error(err);
console.log('for additional information, add --verbose to the command.');
return;
}

console.log('generating updated javascript SDK...');

exec('npm run build', { cwd: rootDir }, (err, stdout) => {
if (verbose) {
console.log(stdout);
}
if (err) {
console.error(err);
console.log(
'for additional information, add --verbose to the command.'
);
return;
}
console.log('done!');
});
});
}
prog.parse(process.argv);
7 changes: 0 additions & 7 deletions lib/nile/openapi-merge.json

This file was deleted.

16 changes: 11 additions & 5 deletions lib/nile/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,29 @@
"packageManager": "[email protected]",
"files": [
"dist",
"src"
"src",
"spec",
"bin/*.mjs"
],
"engines": {
"node": ">=10"
},
"scripts": {
"start": "tsdx watch",
"build": "tsdx build && yarn build:types",
"prebuild": "yarn build:api:merge && yarn build:api:gen",
"prebuild": "yarn build:api:gen && ./scripts/api-cleaner.sh",
"build:readme": "./scripts/lib-readme-shim.sh",
"build:types": "tsc -d --declarationDir dist --emitDeclarationOnly",
"build:api:merge": "yarn openapi-merge-cli",
"build:api:gen": "yarn openapi-generator-cli generate -i spec/output.swagger.json -g typescript --package-name nile -o src/generated/openapi --additional-properties=ngVersion=6.1.7,npmName=theniledev,supportsES6=true,npmVersion=6.9.0,withInterfaces=true,withSeparateModelsAndApi=true,moduleName=Nile,projectName=@theniledev/js",
"build:api:gen": "yarn openapi-generator-cli generate -t templates -i spec/api.yaml -g typescript --package-name nile -o src/generated/openapi --additional-properties=ngVersion=6.1.7,npmName=theniledev,supportsES6=true,npmVersion=6.9.0,withInterfaces=true,withSeparateModelsAndApi=true,moduleName=Nile,projectName=@theniledev/js",
"test": "tsdx test",
"prepare": "yarn prebuild && tsdx build && yarn build:types",
"size": "size-limit",
"analyze": "size-limit --why"
},
"bin": {
"nile": "./bin/nile.mjs"
},
"name": "@theniledev/js",
"author": "jrea",
"module": "dist/js.esm.js",
Expand All @@ -38,11 +43,9 @@
}
],
"devDependencies": {
"@openapitools/openapi-generator-cli": "^2.4.26",
"@size-limit/preset-small-lib": "^7.0.8",
"@types/jest": "^27.4.1",
"@types/node": "^17.0.23",
"openapi-merge-cli": "^1.3.1",
"size-limit": "^7.0.8",
"tsdx": "^0.14.1",
"tslib": "^2.3.1",
Expand All @@ -60,6 +63,9 @@
},
"dependencies": {
"es6-promise": "^4.2.8",
"@openapitools/openapi-generator-cli": "^2.4.26",
"node-fetch": "^3.2.3",
"sade": "^1.8.1",
"url-parse": "^1.5.10",
"whatwg-fetch": "^3.6.2"
}
Expand Down
4 changes: 4 additions & 0 deletions lib/nile/scripts/api-cleaner.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

# remove login override
sed -i -e '/public login/,/}/d' ./src/generated/openapi/types/PromiseAPI.ts
Loading

0 comments on commit 8b03527

Please sign in to comment.