Skip to content

Commit

Permalink
Merge pull request #20 from flotiq/hotfix/tsc-error
Browse files Browse the repository at this point in the history
Missing files after command run
  • Loading branch information
KrzysztofBrylski authored Jun 7, 2024
2 parents 1fd327f + 0720179 commit 729eb86
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 31 deletions.
18 changes: 11 additions & 7 deletions build_to_js.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
const fce = require('fs-extra');
const {execSync} = require('child_process');
const buildToJs = (outputPath) => {

// fce.mkdirp('flotiqApiBuildJs').then().catch(err => err && console.error(err));
execSync('tsc -p ./flotiqApi', {stdio: 'ignore'});
/**
* Move raw TypeScript code to tmp _copy dir
* Run compilation
* Move back compiled files to standard flotiqApi dir
* @param tmpSDKPath
*/
const buildToJs = (tmpSDKPath) => {
const sdkCopyPath = tmpSDKPath.replace('flotiqApi', 'flotiqApi_copy');
fce.moveSync(tmpSDKPath, sdkCopyPath);

fce.moveSync('flotiqApi/dist/', 'flotiqApiBuildJs', {overwrite: true});

fce.removeSync('flotiqApi');
fce.moveSync("flotiqApiBuildJs", outputPath, {overwrite: true});
execSync(`tsc -p ${sdkCopyPath}`, {stdio: 'inherit'});
fce.moveSync(`${sdkCopyPath}/dist/`, `${tmpSDKPath}`, {overwrite: true});
}

module.exports = buildToJs;
62 changes: 39 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
#!/usr/bin/env node
const inquirer = require('inquirer');
const {execSync} = require('child_process');
const axios = require('axios');
const fs = require('fs');
const fce = require('fs-extra');
const path = require('path');
const dotenv = require('dotenv');
const yargs = require('yargs');
const admZip = require('adm-zip')
const admZip = require('adm-zip');
const os = require('os');
const buildToJs = require("./build_to_js");
const cleanDuplicateImport = require("./clean_duplicate_import");

const compileToJsFlag = "compiled-js";

const CLI_GREEN = "\x1b[32m%s\x1b[0m";
const CLI_BLUE = "\x1b[36m%s\x1b[0m";

const getWorkingPath = () => fs.mkdtempSync(`${os.tmpdir()}${path.sep}`);

async function lambdaInvoke(url) {

try {
const response = await axios.get(url, {responseType: 'arraybuffer'})
const decoded = Buffer.from(response.data, 'base64')
return decoded;

return Buffer.from(response.data, 'base64');
} catch (error) {
if (error.response) {
const decoder = new TextDecoder('utf-8')
Expand All @@ -39,7 +42,7 @@ const argv = yargs(process.argv)
.command("flotiq-codegen-ts generate [options]", "Generate api integration for your Flotiq project", {})
.usage("Use flotiq-codegen-ts generates typescript Fetch API integration for your Flotiq project.")
.option(compileToJsFlag, {
description: "generates Fetch API integration compiled to JS",
description: "Generates Fetch API integration compiled to JavaScript",
alias: "",
type: "boolean",
default: false,
Expand All @@ -60,7 +63,13 @@ async function confirm(msg) {

async function main() {

const envfiles = ['.env', 'env.local', 'env.development'];
const envfiles = [
'.env',
'.env.local',
'.env.development',
'env.local',
'env.development'
];
const envName = "FLOTIQ_API_KEY";
let apiKey = ''
for (const file of envfiles) {
Expand All @@ -71,10 +80,11 @@ async function main() {

if (process.env[envName]) {

query = await confirm(`${envName} found in env file. \n Do you want to use API key from ${file}?`)
query = await confirm(`${envName} found in '${file}' file. \n Do you want to use API key from ${file}?`)
if (query) {
//using API key from file
apiKey = process.env[envName];
break;
}
}
}
Expand All @@ -97,30 +107,36 @@ async function main() {
const compileToJs = argv[compileToJsFlag];

try {
console.log('Downloading OpenAPI schema...');
console.log('Generating client from schema...');

// Generate command
const lambdaUrl = `https://0c8judkapg.execute-api.us-east-1.amazonaws.com/default/codegen-ts?token=${apiKey}`
var zip = new admZip(await lambdaInvoke(lambdaUrl))
const localPath = path.join(__dirname, 'flotiqApi');
const zip = new admZip(await lambdaInvoke(lambdaUrl));
const tmpPath = getWorkingPath();
const tmpSDKPath = `${tmpPath}/flotiqApi`;
const outputPath = path.join(process.cwd(), 'flotiqApi');
console.log('Generating client from schema...');

if (!compileToJs) {
fce.removeSync('flotiqApi');
zip.extractAllTo(outputPath);
cleanDuplicateImport(outputPath);
console.log('Client generated successfully!');
return;
console.log(`Extracting SDK client to tmp dir '${tmpPath}'...`);
zip.extractAllTo(tmpSDKPath);
cleanDuplicateImport(tmpSDKPath);

if (compileToJs) {
console.log('Compiling to JavaScript...');
buildToJs(tmpSDKPath);
}

zip.extractAllTo(localPath)
if (fs.existsSync(outputPath)) {
console.log(`Found existing SDK in '${outputPath}' - cleaning up...`);
fce.removeSync(outputPath);
}

console.log('Compiling to javascript...');
cleanDuplicateImport(localPath);
buildToJs(localPath);
console.log(`Moving SDK to '${outputPath}'...`);
fce.moveSync(tmpSDKPath, outputPath);
fce.removeSync(tmpPath);

console.log('Client generated successfully!');
console.log(CLI_GREEN, 'Client generated successfully!');
console.log(CLI_GREEN, 'You can start using your Flotiq SDK');
console.log(CLI_BLUE, 'Read more: https://github.com/flotiq/flotiq-codegen-ts');

} catch (error) {
console.error('An error occurred:', error);
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "flotiq-codegen-ts",
"version": "1.1.2",
"version": "1.2.0",
"description": "CLI tool to generate API clients using Flotiq API and OpenAPI Generator",
"main": "index.js",
"bin": {
Expand All @@ -21,6 +21,7 @@
"adm-zip": "^0.5.12",
"axios": "^1.6.8",
"dotenv": "^16.4.5",
"fs-extra": "^11.2.0",
"inquirer": "^8.2.0",
"typescript": "^4.0",
"yargs": "^15.3.1"
Expand Down
9 changes: 9 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,15 @@ [email protected]:
jsonfile "^6.0.1"
universalify "^2.0.0"

fs-extra@^11.2.0:
version "11.2.0"
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-11.2.0.tgz#e70e17dfad64232287d01929399e0ea7c86b0e5b"
integrity sha512-PmDi3uwK5nFuXh7XDTlVnS17xJS7vW36is2+w3xcv8SVxiB4NyATf4ctkVY5bkSjX0Y4nbvZCq1/EjtEyr9ktw==
dependencies:
graceful-fs "^4.2.0"
jsonfile "^6.0.1"
universalify "^2.0.0"

fs.realpath@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f"
Expand Down

0 comments on commit 729eb86

Please sign in to comment.