-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
678 additions
and
500 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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", | ||
|
@@ -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", | ||
|
@@ -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" | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.