diff --git a/.gitignore b/.gitignore index 12d09b83..e02f9cdb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,6 @@ node_modules/ -typings/ cache/ -*.js -*.d.ts +dist/ *.log.* *.log *.tgz diff --git a/.npmignore b/.npmignore index 3aed0526..89801822 100644 --- a/.npmignore +++ b/.npmignore @@ -1,5 +1,4 @@ node_modules/ -typings/ cache/ src/ .travis.yml diff --git a/package.json b/package.json index bddd555e..aa5f5e27 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "scripts": { "tsc": "tsc", "typings": "typings", - "prepublish": "typings install && tsc", + "prepublish": "tsc -p src", "test": "node cxsd-cli.js --help" }, "author": "Juha Järvi", @@ -30,14 +30,17 @@ "typescript" ], "dependencies": { - "bluebird": "^3.4.1", - "cget": "~0.0.5", - "commander": "~2.9.0", - "cxml": "~0.1.0", - "node-expat": "~2.3.15" + "@types/bluebird": "^3.5.18", + "@types/commander": "^2.11.0", + "@types/node": "^8.0.47", + "bluebird": "^3.4.7", + "cget": "^0.2.1", + "commander": "^2.11.0", + "cxml": "~0.1.1", + "node-expat": "^2.3.16" }, "devDependencies": { - "typescript": "^1.8.10", - "typings": "^1.3.2" + "tslint": "^5.8.0", + "typescript": "^2.5.3" } } diff --git a/src/cli.ts b/src/cli.ts index 6b89b985..f29ddb4e 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -13,6 +13,7 @@ import {exportNamespace} from './xsd/Exporter'; import * as schema from './schema'; import {AddImports} from './schema/transform/AddImports'; import {Sanitize} from './schema/transform/Sanitize'; +import * as URL from 'url'; type _ICommand = typeof cmd; interface ICommand extends _ICommand { @@ -40,14 +41,16 @@ function handleConvert(urlRemote: string, opts: { [key: string]: any }) { var fetchOptions: FetchOptions = {}; if(opts['forceHost']) { - fetchOptions.forceHost = opts['forceHost']; - if(opts['forcePort']) fetchOptions.forcePort = opts['forcePort']; - - Cache.patchRequest(); + fetchOptions.rewrite = (url: string) => { + const currentURL = URL.parse(url); + currentURL.hostname = opts.forceHost; + if(opts['forcePort']) currentURL.port = opts['forcePort']; + return currentURL.toString(); + } } - var jsCache = new Cache(opts['outJs'] || 'xmlns', '_index.js'); - var tsCache = new Cache(opts['outTs'] || 'xmlns', '_index.d.ts'); + var jsCache = new Cache(opts['outJs'] || 'xmlns', { indexName: '_index.js' }); + var tsCache = new Cache(opts['outTs'] || 'xmlns', { indexName: '_index.d.ts' }); var loader = new Loader(xsdContext, fetchOptions); diff --git a/typings-custom/node-expat.d.ts b/src/node-expat.d.ts similarity index 100% rename from typings-custom/node-expat.d.ts rename to src/node-expat.d.ts diff --git a/src/schema/exporter/Exporter.ts b/src/schema/exporter/Exporter.ts index 7ca98f41..76ca5c51 100644 --- a/src/schema/exporter/Exporter.ts +++ b/src/schema/exporter/Exporter.ts @@ -1,22 +1,28 @@ // This file is part of cxsd, copyright (c) 2015-2016 BusFaster Ltd. // Released under the MIT license, see LICENSE. +import { Packet } from '_debugger'; import * as Promise from 'bluebird'; import * as path from 'path'; import {Address, Cache} from 'cget' +import { FileSystemCache } from 'cget/dist/strategy'; import {Transform} from '../transform/Transform'; import {Type} from '../Type'; export interface State { cache: Cache; + fsStrategy: FileSystemCache; } -export abstract class Exporter extends Transform { +export abstract class Exporter extends Transform { constructor(doc: Type, cache: Cache) { super(doc); - this.state = { cache: cache }; + this.state = { + cache, + fsStrategy: cache && cache.storePipeline[0] as FileSystemCache + } } writeHeader() { @@ -41,12 +47,15 @@ export abstract class Exporter extends Transform { if(!doc) return(null); this.cacheDir = path.dirname( - this.state.cache.getCachePathSync(new Address(doc.namespace.name)) + this.state.fsStrategy.getCachePathSync(doc.namespace.name) ); + + let outName = this.getOutName(doc.namespace.name); + if (doc.namespace.isPrimitiveSpace || new Address(outName).isLocal) { + outName = `urn:cxsd:${outName}` // FIXME: Hacl to generate "remote" UR{I,L} + } - var outName = this.getOutName(doc.namespace.name); - - return(this.state.cache.ifCached(outName).then((isCached: boolean) => { + return((this.state.fsStrategy.isCached(outName) as Promise).then((isCached: boolean) => { if(isCached) return(null) return(this.state.cache.store( @@ -64,7 +73,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.state.cache.getCachePathSync(new Address(name)) + '.js'; + const targetPath = this.state.fsStrategy.getCachePathSync(name) + '.js'; // Always output forward slashes. // If path.sep is a backslash as on Windows, we need to escape it (as a double-backslash) for it to be a valid Regex. diff --git a/tsconfig.json b/src/tsconfig.json similarity index 55% rename from tsconfig.json rename to src/tsconfig.json index d1b9aef5..d87da4c4 100644 --- a/tsconfig.json +++ b/src/tsconfig.json @@ -5,13 +5,15 @@ "module": "commonjs", "moduleResolution": "node", "noImplicitAny": true, - "outDir": "dist", - "target": "es5" + "noImplicitThis": true, + "outDir": "../dist", + "removeComments": false, + "target": "es5", + "lib": ["es6"] }, "files": [ - "typings/index.d.ts", - "typings-custom/node-expat.d.ts", + "node-expat.d.ts", - "src/cli.ts" + "cli.ts" ] } diff --git a/src/xsd/Loader.ts b/src/xsd/Loader.ts index 8bb8246c..084ba717 100644 --- a/src/xsd/Loader.ts +++ b/src/xsd/Loader.ts @@ -3,19 +3,35 @@ import * as Promise from 'bluebird'; -import {Address, FetchOptions, Cache, CacheResult, util} from 'cget'; +import {FetchOptions, Cache, CacheResult} from 'cget'; import {Context} from './Context'; import {Namespace} from './Namespace'; import {Source} from './Source'; import {Parser} from './Parser'; +/** Copy all members of src object to dst object. */ + +export function extend(dst: {[key: string]: any}, src: {[key: string]: any}) { + for(var key of Object.keys(src)) { + dst[key] = src[key]; + } + return(dst); +} + +/** Make shallow clone of object. */ + +export function clone(src: Object) { + return(extend({}, src)); +} + + /** Loader handles caching schema definitions and calling parser stages. */ export class Loader { constructor(context: Context, options?: FetchOptions) { this.context = context; - this.options = util.clone(options); + this.options = clone(options || {}); this.parser = new Parser(context); } @@ -31,15 +47,14 @@ export class Loader { } importFile(urlRemote: string, namespace?: Namespace) { - var options = this.options; - options.address = new Address(urlRemote); + const options = this.options; var source = Loader.sourceTbl[urlRemote]; if(!source) { source = new Source(urlRemote, this.context, namespace); - Loader.cache.fetch(options).then((cached: CacheResult) => { + Loader.cache.fetch(urlRemote, options).then((cached: CacheResult) => { source.updateUrl(urlRemote, cached.address.url); return(this.parser.init(cached, source, this)); @@ -64,7 +79,7 @@ export class Loader { getOptions() { return(this.options); } - private static cache = new Cache('cache/xsd', '_index.xsd'); + private static cache = new Cache('cache/xsd', { indexName: '_index.xsd'}); private static sourceTbl: {[url: string]: Source} = {}; private context: Context; diff --git a/src/xsd/types/Attribute.ts b/src/xsd/types/Attribute.ts index 71c125d7..c708eab3 100644 --- a/src/xsd/types/Attribute.ts +++ b/src/xsd/types/Attribute.ts @@ -28,6 +28,6 @@ export class Attribute extends MemberBase { var attribute = this.resolveMember(state, 'attribute') as Attribute; } - use: string = null; - default: XmlAttribute = null; + use: string | null = null; + default: XmlAttribute | null = null; } diff --git a/src/xsd/types/AttributeGroup.ts b/src/xsd/types/AttributeGroup.ts index daaddcaf..ad8e9d18 100644 --- a/src/xsd/types/AttributeGroup.ts +++ b/src/xsd/types/AttributeGroup.ts @@ -20,10 +20,11 @@ export class AttributeGroup extends types.Base { } resolve(state: State) { - var attributeGroup: AttributeGroup = this; + let attributeGroup: AttributeGroup = this; + let ref: QName = null; if(this.ref) { - var ref = new QName(this.ref, state.source); + ref = new QName(this.ref, state.source); attributeGroup = this.scope.lookup(ref, 'attributeGroup') as AttributeGroup; } @@ -38,7 +39,7 @@ export class AttributeGroup extends types.Base { } } - id: string = null; - name: string = null; - ref: string = null; + id: string | null = null; + name: string | null = null; + ref: string | null = null; } diff --git a/src/xsd/types/Base.ts b/src/xsd/types/Base.ts index 37036845..75c4d3e0 100644 --- a/src/xsd/types/Base.ts +++ b/src/xsd/types/Base.ts @@ -67,6 +67,5 @@ export class Base { bytePos: number; name: string; - static name: string; static rule: Rule; } diff --git a/src/xsd/types/DerivationBase.ts b/src/xsd/types/DerivationBase.ts index 13f09872..099683d9 100644 --- a/src/xsd/types/DerivationBase.ts +++ b/src/xsd/types/DerivationBase.ts @@ -29,6 +29,6 @@ export class DerivationBase extends types.Base { this.scope.addAllToParent('attributeGroup'); } - id: string = null; - base: string = null; + id: string | null = null; + base: string | null = null; } diff --git a/src/xsd/types/Element.ts b/src/xsd/types/Element.ts index e758cdbd..f1f8cc15 100644 --- a/src/xsd/types/Element.ts +++ b/src/xsd/types/Element.ts @@ -55,12 +55,12 @@ export class Element extends MemberBase implements ElementLike { minOccurs: string = "1"; maxOccurs: string = "1"; - default: string = null; + default: string | null = null; /** Abstract elements are disallowed in the XML document, * and another member of the same substitution group should be used. */ - abstract: string = null; // boolean - substitutionGroup: string = null; + abstract: string | null = null; // boolean + substitutionGroup: string | null = null; substitutes: Element; isSubstituted: boolean; diff --git a/src/xsd/types/MemberBase.ts b/src/xsd/types/MemberBase.ts index 82652580..df701ec5 100644 --- a/src/xsd/types/MemberBase.ts +++ b/src/xsd/types/MemberBase.ts @@ -60,10 +60,10 @@ export class MemberBase extends TypedBase { return(false); } - id: string = null; - name: string = null; - ref: string = null; - type: string = null; + id: string | null = null + name: string | null = null + ref: string | null = null + type: string | null = null min: number; max: number; diff --git a/src/xsd/types/TypeBase.ts b/src/xsd/types/TypeBase.ts index ee639d4d..5994fb73 100644 --- a/src/xsd/types/TypeBase.ts +++ b/src/xsd/types/TypeBase.ts @@ -7,6 +7,8 @@ import {QName} from '../QName'; import {NamedTypeMember} from '../Scope'; import * as schema from '../../schema'; +export interface TypeBaseChild extends TypeBase {} + export class TypeBase extends Base { init(state: State) { if(!this.scope) this.scope = state.getScope(); @@ -37,9 +39,9 @@ export class TypeBase extends Base { /** Find parent type inheriting from a base type. */ - getParent(base: BaseClass, breakAtContent: boolean) { - var next: TypeBase | QName = this; - var type: TypeBase | QName; + getParent(base: BaseClass, breakAtContent: boolean): TypeBase { + var next: TypeBaseChild | QName = this; + var type: TypeBaseChild | QName; /** Maximum iterations in case type inheritance forms a loop. */ var iter = 100; @@ -48,11 +50,11 @@ export class TypeBase extends Base { if(!(type instanceof TypeBase)) break; else if(type instanceof base) return(type); - else if(breakAtContent && type.scope && ( - type.scope.dumpTypes('attribute') || - type.scope.dumpTypes('attributeGroup') + else if(breakAtContent && (type as TypeBase).scope && ( + (type as TypeBase).scope.dumpTypes('attribute') || + (type as TypeBase).scope.dumpTypes('attributeGroup') )) break; - else next = type.parent; + else next = (type as TypeBase).parent; } return(null); diff --git a/typings.json b/typings.json deleted file mode 100644 index 7526f9ec..00000000 --- a/typings.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "name": "cxsd", - "dependencies": { - "bluebird": "registry:npm/bluebird#3.4.1+20160811213708", - "request": "registry:npm/request#2.69.0+20160428223725" - }, - "globalDependencies": { - "commander": "registry:dt/commander#2.3.0+20160317120654", - "node": "registry:dt/node#6.0.0+20160807145350" - } -}