Skip to content

Commit

Permalink
Add proper command line parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
jjrv committed Feb 6, 2016
1 parent 7daa27e commit fdb58af
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 51 deletions.
3 changes: 3 additions & 0 deletions cxsd-cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env node

require('./dist/cli.js');
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
89 changes: 60 additions & 29 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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('<url>')
.description('XSD download and conversion tool')
.option('-H, --force-host <host>', 'Fetch all xsd files from <host>\n (original host is passed in GET parameter "host")')
.option('-P, --force-port <port>', 'Connect to <port> when using --force-host')
.option('-c, --cache-xsd <path>', 'Cache downloaded XSD filed under <path>')
.option('-t, --out-ts <path>', 'Output TypeScript definitions under <path>')
.option('-j, --out-js <path>', 'Output JavaScript modules under <path>')
.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);
}
});
}
27 changes: 19 additions & 8 deletions src/schema/exporter/Exporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Exporter, string, void> {
import {Transform} from '../transform/Transform';
import {Type} from '../Type';

export interface State {
cache: Cache;
}

export abstract class Exporter extends Transform<Exporter, string, State> {
constructor(doc: Type, cache: Cache) {
super(doc);
this.state = { cache: cache };
}

writeHeader() {
var output: string[] = [];
var importTbl = this.namespace.getUsedImportTbl();
Expand All @@ -30,15 +41,15 @@ export abstract class Exporter extends Transform<Exporter, string, void> {
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);
Expand All @@ -53,7 +64,7 @@ export abstract class Exporter extends Transform<Exporter, string, void> {
// 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,
Expand All @@ -65,10 +76,10 @@ export abstract class Exporter extends Transform<Exporter, string, void> {
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;

Expand Down
7 changes: 0 additions & 7 deletions src/schema/exporter/JS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
7 changes: 0 additions & 7 deletions src/schema/exporter/TS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
1 change: 1 addition & 0 deletions typings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit fdb58af

Please sign in to comment.