Skip to content

Commit

Permalink
Merge pull request #17 from flotiq/feature/24775-remove-bash-scripts
Browse files Browse the repository at this point in the history
#24775 changed bash scripts to js scripts
  • Loading branch information
MaciejLabedzkiCodewave authored Jun 4, 2024
2 parents 0603c7a + af09dc9 commit 356ca38
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 63 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
flotiqApi
flotiqApiBuildJs
14 changes: 14 additions & 0 deletions build_to_js.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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'});

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

fce.removeSync('flotiqApi');
fce.moveSync("flotiqApiBuildJs", outputPath, {overwrite: true});
}

module.exports = buildToJs;
10 changes: 0 additions & 10 deletions build_to_js.sh

This file was deleted.

21 changes: 21 additions & 0 deletions clean_duplicate_import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const fs = require('fs');
const path = require('path');
const glob = require('glob');

const filePatterns = ['src/models/*WithoutInternal.ts', 'src/models/*WithoutRequired.ts'];
const deleteLine = "import { DataSource, DataSourceFromJSON } from './DataSource';";

const cleanDuplicateImport = (cwd) => {
filePatterns.forEach((pattern) => {
const files = glob.sync(pattern, {cwd: cwd});
files.forEach(file => {
const filePath = path.join(cwd, file);
const data = fs.readFileSync(filePath, 'utf8');
if (data.includes(deleteLine)) {
const newData = data.replace(deleteLine, '');
fs.writeFileSync(filePath, newData, 'utf8');
}
});
})
}
module.exports = cleanDuplicateImport;
13 changes: 0 additions & 13 deletions clean_duplicate_import.sh

This file was deleted.

60 changes: 21 additions & 39 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
#!/usr/bin/env node
const inquirer = require('inquirer');
const { execSync } = require('child_process');
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 buildToJs = require("./build_to_js");
const cleanDuplicateImport = require("./clean_duplicate_import");

const compileToJsFlag = "compiled-js";

async function lambdaInvoke(url) {

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

Expand Down Expand Up @@ -44,7 +47,6 @@ const argv = yargs(process.argv)
}).help().alias("help", "h").argv;



async function confirm(msg) {
const response = await inquirer.prompt([
{
Expand All @@ -56,46 +58,29 @@ async function confirm(msg) {
return response.confirmation;
}


const getMoveCommand = (outputPath, buildToJs = false) => {
const path = buildToJs ? 'flotiqApiBuildJs' : 'flotiqApi';
const clearDestination = `rm -fr ${outputPath}`;
const command = `mv ${__dirname}/${path} ${outputPath}`;

execSync(clearDestination, {stdio: 'ignore', cwd: __dirname});
execSync(command, {stdio: 'ignore', cwd: __dirname});
}

const getCleanUpCommand = (outputPath = null) => {
const cleanCommand = `${__dirname}/clean_duplicate_import.sh`;

execSync(cleanCommand, {stdio: 'ignore', cwd: !outputPath ? path.join(__dirname, 'flotiqApi') : outputPath});
}


async function main() {

const envfiles = ['.env', 'env.local', 'env.development'];
const envName = "FLOTIQ_API_KEY";
let apiKey = ''
for (const file of envfiles) {
const filepath = path.join(process.cwd(), file)
const filepath = path.join(process.cwd(), file)

if (fs.existsSync(filepath)) {
dotenv.config({ path: filepath})
if (fs.existsSync(filepath)) {
dotenv.config({path: filepath})

if (process.env[envName]) {
if (process.env[envName]) {

query = await confirm(`${envName} found in env file. \n Do you want to use API key from ${file}?`)
if (query) {
//using API key from file
apiKey = process.env[envName];
}
query = await confirm(`${envName} found in env file. \n Do you want to use API key from ${file}?`)
if (query) {
//using API key from file
apiKey = process.env[envName];
}
}
}
}

if (! apiKey) {
if (!apiKey) {
const answers = await inquirer.prompt([
{
type: 'input',
Expand All @@ -121,23 +106,19 @@ async function main() {
const outputPath = path.join(process.cwd(), 'flotiqApi');
console.log('Generating client from schema...');

if(!compileToJs){
if (!compileToJs) {
fce.removeSync('flotiqApi');
zip.extractAllTo(outputPath);
getCleanUpCommand(outputPath);
cleanDuplicateImport(outputPath);
console.log('Client generated successfully!');
return;
}

zip.extractAllTo(localPath)

// compile api to js command
const buildJsCommand = `sh build_to_js.sh`;

console.log('Compiling to javascript...');
getCleanUpCommand();

execSync(buildJsCommand, {stdio: 'ignore', cwd: __dirname});
getMoveCommand(outputPath, true);
cleanDuplicateImport(localPath);
buildToJs(localPath);

console.log('Client generated successfully!');

Expand All @@ -146,4 +127,5 @@ async function main() {
process.exit(1);
}
}

main();
2 changes: 1 addition & 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.0",
"version": "1.1.1",
"description": "CLI tool to generate API clients using Flotiq API and OpenAPI Generator",
"main": "index.js",
"bin": {
Expand Down

0 comments on commit 356ca38

Please sign in to comment.