From fdb58af988ea7dbed3c40d655c4729f55269ee81 Mon Sep 17 00:00:00 2001 From: jjrv Date: Sat, 6 Feb 2016 08:10:09 +0200 Subject: [PATCH] Add proper command line parsing. --- cxsd-cli.js | 3 ++ package.json | 1 + src/cli.ts | 89 ++++++++++++++++++++++----------- src/schema/exporter/Exporter.ts | 27 +++++++--- src/schema/exporter/JS.ts | 7 --- src/schema/exporter/TS.ts | 7 --- typings.json | 1 + 7 files changed, 84 insertions(+), 51 deletions(-) create mode 100755 cxsd-cli.js diff --git a/cxsd-cli.js b/cxsd-cli.js new file mode 100755 index 0000000..d9da764 --- /dev/null +++ b/cxsd-cli.js @@ -0,0 +1,3 @@ +#!/usr/bin/env node + +require('./dist/cli.js'); diff --git a/package.json b/package.json index d8ec6c2..7cdce53 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ "dependencies": { "bluebird": "~3.2.2", "cget": "~0.0.3", + "commander": "~2.9.0", "cxml": "~0.0.4", "node-expat": "~2.3.13" }, diff --git a/src/cli.ts b/src/cli.ts index b80d826..bb910a9 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -1,7 +1,9 @@ // This file is part of cxsd, copyright (c) 2015-2016 BusFaster Ltd. // Released under the MIT license, see LICENSE. -import {Cache} from 'cget'; +import * as cmd from 'commander'; + +import {Cache, FetchOptions} from 'cget'; import {Namespace, PrimitiveSpace} from './xsd/Namespace'; import {Loader} from './xsd/Loader'; import {exportNamespace} from './xsd/Exporter'; @@ -10,32 +12,61 @@ import {AddImports} from './schema/transform/AddImports'; import {Sanitize} from './schema/transform/Sanitize'; import {ListImports} from './schema/transform/ListImports'; -Cache.patchRequest(); - -var xmlSpace = Namespace.register('http://www.w3.org/XML/1998/namespace', 'http://www.w3.org/2001/xml.xsd', 'xml'); - -var loader = new Loader({}); - -loader.import(process.argv[2]).then((namespace: Namespace) => { - try { - exportNamespace(PrimitiveSpace.get()); - exportNamespace(xmlSpace); - - var spec = exportNamespace(namespace); - - new AddImports(spec).exec().then(() => - new Sanitize(spec).exec() - ).then((sanitize: Sanitize) => - sanitize.finish() - ).then(() => - new ListImports(spec).exec() - ).then(() => - new schema.exporter.JS(spec).exec() - ).then(() => - new schema.exporter.TS(spec).exec() - ); - } catch(err) { - console.log(err); - console.log(err.stack); +type _ICommand = typeof cmd; +interface ICommand extends _ICommand { + arguments(spec: string): ICommand; +} + +((cmd.version(require('../package.json').version) as ICommand) + .arguments('') + .description('XSD download and conversion tool') + .option('-H, --force-host ', 'Fetch all xsd files from \n (original host is passed in GET parameter "host")') + .option('-P, --force-port ', 'Connect to when using --force-host') + .option('-c, --cache-xsd ', 'Cache downloaded XSD filed under ') + .option('-t, --out-ts ', 'Output TypeScript definitions under ') + .option('-j, --out-js ', 'Output JavaScript modules under ') + .action(handleConvert) + .parse(process.argv) +); + +function handleConvert(urlRemote: string, opts: { [key: string]: any }) { + var xmlSpace = Namespace.register('http://www.w3.org/XML/1998/namespace', 'http://www.w3.org/2001/xml.xsd', 'xml'); + + var fetchOptions: FetchOptions = {}; + + if(opts['forceHost']) { + fetchOptions.forceHost = opts['forceHost']; + if(opts['forcePort']) fetchOptions.forcePort = opts['forcePort']; + + Cache.patchRequest(); } -}); + + var jsCache = new Cache(opts['outJs'] || 'cache/js', '_index.js'); + var tsCache = new Cache(opts['outTs'] || 'cache/js', '_index.d.ts'); + + var loader = new Loader(fetchOptions); + + loader.import(urlRemote).then((namespace: Namespace) => { + try { + exportNamespace(PrimitiveSpace.get()); + exportNamespace(xmlSpace); + + var spec = exportNamespace(namespace); + + new AddImports(spec).exec().then(() => + new Sanitize(spec).exec() + ).then((sanitize: Sanitize) => + sanitize.finish() + ).then(() => + new ListImports(spec).exec() + ).then(() => + new schema.exporter.JS(spec, jsCache).exec() + ).then(() => + new schema.exporter.TS(spec, tsCache).exec() + ); + } catch(err) { + console.log(err); + console.log(err.stack); + } + }); +} diff --git a/src/schema/exporter/Exporter.ts b/src/schema/exporter/Exporter.ts index 14c4d2c..c932473 100644 --- a/src/schema/exporter/Exporter.ts +++ b/src/schema/exporter/Exporter.ts @@ -4,10 +4,21 @@ import * as Promise from 'bluebird'; import * as path from 'path'; -import {Transform} from '../transform/Transform'; import {Address, Cache} from 'cget' -export abstract class Exporter extends Transform { +import {Transform} from '../transform/Transform'; +import {Type} from '../Type'; + +export interface State { + cache: Cache; +} + +export abstract class Exporter extends Transform { + constructor(doc: Type, cache: Cache) { + super(doc); + this.state = { cache: cache }; + } + writeHeader() { var output: string[] = []; var importTbl = this.namespace.getUsedImportTbl(); @@ -30,15 +41,15 @@ export abstract class Exporter extends Transform { if(!doc) return(null); this.cacheDir = path.dirname( - this.getCache().getCachePathSync(new Address(doc.namespace.name)) + this.state.cache.getCachePathSync(new Address(doc.namespace.name)) ); var outName = this.getOutName(doc.namespace.name); - return(this.getCache().ifCached(outName).then((isCached: boolean) => { + return(this.state.cache.ifCached(outName).then((isCached: boolean) => { if(isCached) return(null) - return(this.getCache().store( + return(this.state.cache.store( outName, this.writeContents() )).then(() => false); @@ -53,7 +64,7 @@ export abstract class Exporter extends Transform { // Append and then strip a file extension so references to a parent // directory will target the directory by name instead of .. or similar. - var targetPath = this.getCache().getCachePathSync(new Address(name)) + '.js'; + var targetPath = this.state.cache.getCachePathSync(new Address(name)) + '.js'; var relPath = path.relative( this.cacheDir, @@ -65,10 +76,10 @@ export abstract class Exporter extends Transform { return(relPath); } - abstract getCache(): Cache; - protected abstract getOutName(name: string): string; + protected state: State; + /** Full path of directory containing exported output for the current namespace. */ protected cacheDir: string; diff --git a/src/schema/exporter/JS.ts b/src/schema/exporter/JS.ts index 146bdff..d478dfa 100644 --- a/src/schema/exporter/JS.ts +++ b/src/schema/exporter/JS.ts @@ -156,16 +156,9 @@ export class JS extends Exporter { ).join('\n')); } - getCache() { - return(JS.cache); - } - getOutName(name: string) { return(name + '.js'); } construct = JS; - - /** Cache where all output is written. */ - private static cache = new Cache('cache/js', '_index.js'); } diff --git a/src/schema/exporter/TS.ts b/src/schema/exporter/TS.ts index 5654fd9..e65094f 100644 --- a/src/schema/exporter/TS.ts +++ b/src/schema/exporter/TS.ts @@ -247,16 +247,9 @@ export class TS extends Exporter { return(output.join('\n')); } - getCache() { - return(TS.cache); - } - getOutName(name: string) { return(name + '.d.ts'); } construct = TS; - - /** Cache where all output is written. */ - private static cache = new Cache('cache/js', '_index.js'); } diff --git a/typings.json b/typings.json index 3fcd94e..2c36117 100644 --- a/typings.json +++ b/typings.json @@ -4,6 +4,7 @@ }, "devDependencies": {}, "ambientDependencies": { + "commander": "github:DefinitelyTyped/DefinitelyTyped/commander/commander.d.ts#6d09e1a5aea3240e7290be602ed03c88e38cd07d", "form-data": "github:DefinitelyTyped/DefinitelyTyped/form-data/form-data.d.ts#bb1b99052bf1d697f0368fbff56898bc1f5a525c", "node": "github:DefinitelyTyped/DefinitelyTyped/node/node.d.ts#1c56e368e17bb28ca57577250624ca5bd561aa81", "request": "github:DefinitelyTyped/DefinitelyTyped/request/request.d.ts#e252a088123eb595d9d772eb373f41c084360278"