Skip to content

Commit

Permalink
Update build process to release as an ES2015 library with type declar…
Browse files Browse the repository at this point in the history
…ations (#6)

* Update build process to release as an ES5 library with type declarations.

* Add lib directory with output
  • Loading branch information
junglebarry authored Jan 26, 2018
1 parent d0650bc commit a05e3a6
Show file tree
Hide file tree
Showing 42 changed files with 970 additions and 41 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
dist/
.tmp/
node_modules/
49 changes: 29 additions & 20 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,45 @@ const gulp = require('gulp');
const jasmine = require('gulp-jasmine');
const ts = require('gulp-typescript');

const tsProject = ts.createProject('tsconfig.json');

const tsSpec = {
sourceMap: true,
declaration: true,
module: 'commonjs',
moduleResolution: 'node',
emitDecoratorMetadata: true,
experimentalDecorators: true,
lib: [
'es2016',
],
target: 'es5',
};

gulp.task('build_tests', () => {
return gulp.src('./spec/**/*.ts')
.pipe(ts({
emitDecoratorMetadata: true,
experimentalDecorators: true,
lib: ['es2016'],
moduleResolution: 'node',
target: 'es5',
}))
.pipe(gulp.dest('./dist/out-tsc/spec'));
.pipe(ts(tsSpec))
.pipe(gulp.dest('./.tmp/spec'));
});

gulp.task('test', ['build', 'build_tests'], () => {
return gulp.src(['./dist/out-tsc/**/*.spec.js'])
gulp.task('test', ['build_src', 'build_tests'], () => {
return gulp.src(['./.tmp/**/*.spec.js'])
.pipe(jasmine({
verbose: false,
includeStackTrace: true,
}));
});

gulp.task('build', () => {
gulp.task('build_src', () => {
return gulp.src('./src/**/*.ts')
.pipe(tsProject())
.pipe(gulp.dest('./.tmp/src'));
});

gulp.task('build_release', () => {
return gulp.src('./src/**/*.ts')
.pipe(ts({
emitDecoratorMetadata: true,
experimentalDecorators: true,
lib: ['es2016'],
moduleResolution: 'node',
target: 'es5',
}))
.pipe(gulp.dest('./dist/out-tsc/src'));
.pipe(tsProject())
.pipe(gulp.dest('./lib'));
});

gulp.task('test:watch', [], () => {
Expand All @@ -41,5 +50,5 @@ gulp.task('test:watch', [], () => {
});

gulp.task('clean', function () {
return del(['dist/**/*']);
return del(['.tmp/**/*', 'lib/**/*']);
});
1 change: 0 additions & 1 deletion index.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion index.ts

This file was deleted.

8 changes: 8 additions & 0 deletions lib/decorators/attribute.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export interface AttributeOptions {
name?: string;
}
export declare function attribute(options?: AttributeOptions): PropertyDecorator;
export declare type AttributeMetadata = {
[name: string]: AttributeOptions;
};
export declare function getAttributeMetadata(target: any): AttributeMetadata;
17 changes: 17 additions & 0 deletions lib/decorators/attribute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var metadata_map_1 = require("./metadata-map");
var ATTRIBUTES_MAP = new metadata_map_1.MetadataMap();
function attribute(options) {
var opts = options || {};
return function (target, key) {
ATTRIBUTES_MAP.setMetadataByType(target.constructor.name, key, Object.assign({
name: key,
}, opts));
};
}
exports.attribute = attribute;
function getAttributeMetadata(target) {
return ATTRIBUTES_MAP.getMetadataByType(target.name);
}
exports.getAttributeMetadata = getAttributeMetadata;
24 changes: 24 additions & 0 deletions lib/decorators/entity.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { ResourceIdentifier } from '../jsonapi';
export interface ResourceIdentifierConstructor {
new (): ResourceIdentifier;
}
export declare class TypeMap {
private constructorsByJsonapiType;
get(typeName: string): ResourceIdentifierConstructor;
set(typeName: string, constructorType: ResourceIdentifierConstructor): void;
}
export declare const ENTITIES_MAP: TypeMap;
export declare function getClassForJsonapiType(type: string): ResourceIdentifierConstructor;
export declare function getConstructorForJsonapiType(type: string): Function;
export interface EntityOptions {
type: string;
}
/**
* Annotates a class to indicate that it is a JSON:API entity definition.
*
* Any class annotated with `@entity` should be considered serialisable to JSON:API,
* and should have `@attribute` and `@relationship` decorators to indicate properties
* to be serialisable to and deserialisable from appropriate JSON:API data.
*
*/
export declare function entity(options: EntityOptions): (constructor: ResourceIdentifierConstructor) => any;
65 changes: 65 additions & 0 deletions lib/decorators/entity.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var TypeMap = /** @class */ (function () {
function TypeMap() {
this.constructorsByJsonapiType = {};
}
TypeMap.prototype.get = function (typeName) {
return this.constructorsByJsonapiType[typeName];
};
TypeMap.prototype.set = function (typeName, constructorType) {
this.constructorsByJsonapiType[typeName] = constructorType;
};
return TypeMap;
}());
exports.TypeMap = TypeMap;
exports.ENTITIES_MAP = new TypeMap();
function getClassForJsonapiType(type) {
return exports.ENTITIES_MAP.get(type);
}
exports.getClassForJsonapiType = getClassForJsonapiType;
function getConstructorForJsonapiType(type) {
var clazz = getClassForJsonapiType(type);
return clazz && clazz.prototype && clazz.prototype.constructor;
}
exports.getConstructorForJsonapiType = getConstructorForJsonapiType;
/**
* Annotates a class to indicate that it is a JSON:API entity definition.
*
* Any class annotated with `@entity` should be considered serialisable to JSON:API,
* and should have `@attribute` and `@relationship` decorators to indicate properties
* to be serialisable to and deserialisable from appropriate JSON:API data.
*
*/
function entity(options) {
var type = options.type;
return function (constructor) {
var original = constructor;
// a utility function to generate instances of a class
var construct = function (constructorFunc, args) {
var constructorClosure = function () {
return constructorFunc.apply(this, args);
};
constructorClosure.prototype = constructorFunc.prototype;
// construct an instance and bind "type" correctly
var instance = new constructorClosure();
instance.type = type;
return instance;
};
// the new constructor behaviour
var wrappedConstructor = function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
return construct(original, args);
};
// copy prototype so intanceof operator still works
wrappedConstructor.prototype = original.prototype;
// add the type to the reverse lookup for deserialisation
exports.ENTITIES_MAP.set(type, wrappedConstructor);
// return new constructor (will override original)
return wrappedConstructor;
};
}
exports.entity = entity;
5 changes: 5 additions & 0 deletions lib/decorators/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from './attribute';
export * from './entity';
export * from './link';
export * from './meta';
export * from './relationship';
10 changes: 10 additions & 0 deletions lib/decorators/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"use strict";
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
Object.defineProperty(exports, "__esModule", { value: true });
__export(require("./attribute"));
__export(require("./entity"));
__export(require("./link"));
__export(require("./meta"));
__export(require("./relationship"));
8 changes: 8 additions & 0 deletions lib/decorators/link.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export interface LinkOptions {
name?: string;
}
export declare function link(options?: LinkOptions): PropertyDecorator;
export declare type LinkMetadata = {
[name: string]: LinkOptions;
};
export declare function getLinkMetadata(target: any): LinkMetadata;
17 changes: 17 additions & 0 deletions lib/decorators/link.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var metadata_map_1 = require("./metadata-map");
var LINKS_MAP = new metadata_map_1.MetadataMap();
function link(options) {
var opts = options || {};
return function (target, key) {
LINKS_MAP.setMetadataByType(target.constructor.name, key, Object.assign({
name: key,
}, opts));
};
}
exports.link = link;
function getLinkMetadata(target) {
return LINKS_MAP.getMetadataByType(target.name);
}
exports.getLinkMetadata = getLinkMetadata;
8 changes: 8 additions & 0 deletions lib/decorators/meta.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export interface MetaOptions {
name?: string;
}
export declare function meta(options?: MetaOptions): PropertyDecorator;
export declare type MetaMetadata = {
[name: string]: MetaOptions;
};
export declare function getMetaMetadata(target: any): MetaMetadata;
17 changes: 17 additions & 0 deletions lib/decorators/meta.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var metadata_map_1 = require("./metadata-map");
var META_PROPERTIES_MAP = new metadata_map_1.MetadataMap();
function meta(options) {
var opts = options || {};
return function (target, key) {
META_PROPERTIES_MAP.setMetadataByType(target.constructor.name, key, Object.assign({
name: key,
}, opts));
};
}
exports.meta = meta;
function getMetaMetadata(target) {
return META_PROPERTIES_MAP.getMetadataByType(target.name);
}
exports.getMetaMetadata = getMetaMetadata;
11 changes: 11 additions & 0 deletions lib/decorators/metadata-map.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export declare type MetadataByPropertyName<T> = {
[attributeName: string]: T;
};
export declare type PropertyTypesForType<T> = {
[typeName: string]: MetadataByPropertyName<T>;
};
export declare class MetadataMap<T> {
private metadataByType;
getMetadataByType(typeName: string): MetadataByPropertyName<T>;
setMetadataByType(typeName: string, keyName: string, metadata: T): void;
}
18 changes: 18 additions & 0 deletions lib/decorators/metadata-map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var MetadataMap = /** @class */ (function () {
function MetadataMap() {
this.metadataByType = {};
}
MetadataMap.prototype.getMetadataByType = function (typeName) {
return this.metadataByType[typeName] || {};
};
MetadataMap.prototype.setMetadataByType = function (typeName, keyName, metadata) {
this.metadataByType[typeName] = Object.assign({}, this.getMetadataByType(typeName), (_a = {},
_a[keyName] = metadata,
_a));
var _a;
};
return MetadataMap;
}());
exports.MetadataMap = MetadataMap;
9 changes: 9 additions & 0 deletions lib/decorators/relationship.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export interface RelationshipOptions {
allowUnresolvedIdentifiers?: boolean;
name?: string;
}
export declare function relationship(options?: RelationshipOptions): PropertyDecorator;
export declare type RelationshipMetadata = {
[name: string]: RelationshipOptions;
};
export declare function getRelationshipMetadata(target: any): RelationshipMetadata;
20 changes: 20 additions & 0 deletions lib/decorators/relationship.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var metadata_map_1 = require("./metadata-map");
var RELATIONSHIPS_MAP = new metadata_map_1.MetadataMap();
var DefaultRelationshipOptions = {
allowUnresolvedIdentifiers: false,
};
function relationship(options) {
var opts = Object.assign({}, DefaultRelationshipOptions, options || {});
return function (target, key) {
RELATIONSHIPS_MAP.setMetadataByType(target.constructor.name, key, Object.assign({
name: key,
}, opts));
};
}
exports.relationship = relationship;
function getRelationshipMetadata(target) {
return RELATIONSHIPS_MAP.getMetadataByType(target.name);
}
exports.getRelationshipMetadata = getRelationshipMetadata;
3 changes: 3 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './decorators';
export * from './jsonapi';
export * from './serialisation';
8 changes: 8 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"use strict";
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
Object.defineProperty(exports, "__esModule", { value: true });
__export(require("./decorators"));
__export(require("./jsonapi"));
__export(require("./serialisation"));
2 changes: 2 additions & 0 deletions lib/jsonapi/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './types';
export * from './unresolved-identifiers';
7 changes: 7 additions & 0 deletions lib/jsonapi/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"use strict";
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
Object.defineProperty(exports, "__esModule", { value: true });
__export(require("./types"));
__export(require("./unresolved-identifiers"));
Loading

0 comments on commit a05e3a6

Please sign in to comment.