Skip to content

Commit

Permalink
Fix dir args handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
scharris committed May 19, 2021
1 parent ed6fabf commit 5317595
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 27 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sqljson-query",
"version": "1.0.9",
"version": "1.0.10",
"description": "Command line tool to generate SQL/JSON queries and Typescript types for query results.",
"keywords": [
"SQL",
Expand Down
74 changes: 48 additions & 26 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env node
import {parseAppArgs} from './util';
import {generateQueries, SourceGenerationOptions} from './index';
import {generateQueries, generateRelationsMetadataSource, SourceGenerationOptions, SourceLanguage} from './index';

function printUsage(to: 'stderr' | 'stdout')
function printUsage(to: 'stderr' | 'stdout', reqdNamedParams: string[])
{
const out = to === 'stderr' ? console.error : console.log;
out(`Expected arguments: [options] ${reqdNamedParams.map(p => "--" + p).join(" ")}`);
Expand All @@ -13,37 +13,59 @@ function printUsage(to: 'stderr' | 'stdout')
out(` --help - Show this message.`);
}

const reqdNamedParams = ['dbmd', 'query-specs', 'src-output-dir', 'sql-output-dir'];
const optlNamedParams = ['src-lang', 'sql-resource-path-prefix', 'types-header'];

const parsedArgs = parseAppArgs(process.argv.slice(2), reqdNamedParams, optlNamedParams, 0);
function getSourceLanguage(srcLang: string | undefined): SourceLanguage
{
switch (srcLang)
{
case 'TS':
case undefined: return 'TS';
case 'Java': return 'Java';
default: throw new Error(`Missed language case: ${srcLang}`);
}
}

if ( typeof parsedArgs === 'string' )
async function main(): Promise<void>
{
if ( parsedArgs === 'help' )
const reqdNamedParams = ['dbmd', 'query-specs', 'src-output-dir', 'sql-output-dir'];
const optlNamedParams = ['src-lang', 'sql-resource-path-prefix', 'types-header'];

const parsedArgs = parseAppArgs(process.argv.slice(2), reqdNamedParams, optlNamedParams, 0);

if ( typeof parsedArgs === 'string' )
{
console.log('Help requested:');
printUsage('stdout');
process.exit(0);
if ( parsedArgs === 'help' )
{
console.log('Help requested:');
printUsage('stdout', reqdNamedParams);
process.exit(0);
}
else // error
{
console.error(`Error: ${parsedArgs}`);
process.exit(1);
}
}
else // error

const {querySpecs, dbmd, srcOutputDir, sqlOutputDir} = parsedArgs;

try
{
const srcGenOpts: SourceGenerationOptions & { sourceLanguage: SourceLanguage } = {
sourceLanguage: getSourceLanguage(parsedArgs['src-lang']),
sqlResourcePathPrefix: parsedArgs['sql-resource-path-prefix'] || '',
typesHeaderFile: parsedArgs['types-header']
};

await generateQueries(querySpecs, dbmd, srcOutputDir, sqlOutputDir, srcGenOpts);
await generateRelationsMetadataSource(dbmd, srcOutputDir, srcGenOpts.sourceLanguage);
}
catch(e: any)
{
console.error(`Error: ${parsedArgs}`);
console.error('Query generation failed.');
console.error(e);
process.exit(1);
}
}

const srcGenOpts: SourceGenerationOptions = {
sourceLanguage: parsedArgs['src-lang'] || 'TypeScript',
sqlResourcePathPrefix: parsedArgs['sql-resource-path-prefix'] || '',
typesHeaderFile: parsedArgs['types-header']
};

const {querySpecs, dbmd, srcOutDir, sqlOutDir} = parsedArgs;

generateQueries(querySpecs, dbmd, srcOutDir, sqlOutDir, srcGenOpts)
.catch((err: any) => {
console.error('Query generation failed.');
console.error(err);
process.exit(1);
});
main().then(() => console.log("Source generation completed."));
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {ResultTypesSourceGenerator} from './result-types-source-generator';
export * from './source-generation-options';
export * from './query-specs';
export * from './result-types';
export * from './relations-md-generator';

export async function generateQueries
(
Expand Down

0 comments on commit 5317595

Please sign in to comment.