changed chalk, fixed mongodb import, changed browser package structure

This commit is contained in:
Daniel Lang 2017-09-07 12:47:50 +02:00
parent e23d8c456a
commit b711c71358
5 changed files with 97 additions and 66 deletions

View File

@ -62,7 +62,7 @@ export class Gulpfile {
"!./src/typeorm-model-shim.ts",
"!./src/platform/PlatformTools.ts"
])
.pipe(gulp.dest("./build/browser/typeorm"));
.pipe(gulp.dest("./build/browser"));
}
/**
@ -82,14 +82,14 @@ export class Gulpfile {
browserCopyPlatformTools() {
return gulp.src("./src/platform/BrowserPlatformTools.template")
.pipe(rename("PlatformTools.ts"))
.pipe(gulp.dest("./build/browser/typeorm/platform"));
.pipe(gulp.dest("./build/browser/platform"));
}
/**
* Runs files compilation of browser-specific source code.
*/
@MergedTask()
browserCompile() {
browserCompileSystemJS() {
const tsProject = ts.createProject("tsconfig.json", {
outFile: "typeorm-browser.js",
module: "system",
@ -104,7 +104,26 @@ export class Gulpfile {
// tsResult.dts.pipe(gulp.dest("./build/package")),
tsResult.js
.pipe(sourcemaps.write(".", { sourceRoot: "", includeContent: true }))
.pipe(gulp.dest("./build/package"))
.pipe(gulp.dest("./build/browser"))
];
}
@MergedTask()
browserCompile() {
const tsProject = ts.createProject("tsconfig.json", {
module: "es2015",
"lib": ["es5", "es6", "dom"],
typescript: require("typescript")
});
const tsResult = gulp.src(["./build/browser/**/*.ts", "./node_modules/reflect-metadata/**/*.d.ts", "./node_modules/@types/**/*.ts"])
.pipe(sourcemaps.init())
.pipe(tsProject());
return [
// tsResult.dts.pipe(gulp.dest("./build/package")),
tsResult.js
.pipe(sourcemaps.write(".", { sourceRoot: "", includeContent: true }))
.pipe(gulp.dest("./build/browser"))
];
}
@ -113,10 +132,10 @@ export class Gulpfile {
*/
@Task()
browserUglify() {
return gulp.src("./build/package/typeorm-browser.js")
return gulp.src("./build/browser/typeorm-browser.js")
.pipe(uglify())
.pipe(rename("typeorm-browser.min.js"))
.pipe(gulp.dest("./build/package"));
.pipe(gulp.dest("./build/browser"));
}
// -------------------------------------------------------------------------
@ -200,7 +219,8 @@ export class Gulpfile {
packagePreparePackageFile() {
return gulp.src("./package.json")
.pipe(replace("\"private\": true,", "\"private\": false,"))
.pipe(gulp.dest("./build/package"));
.pipe(gulp.dest("./build/package"))
.pipe(gulp.dest("./build/browser"));
}
/**
@ -210,7 +230,8 @@ export class Gulpfile {
packageCopyReadme() {
return gulp.src("./README.md")
.pipe(replace(/```typescript([\s\S]*?)```/g, "```javascript$1```"))
.pipe(gulp.dest("./build/package"));
.pipe(gulp.dest("./build/package"))
.pipe(gulp.dest("./build/browser"));
}
/**
@ -219,7 +240,7 @@ export class Gulpfile {
@Task()
packageCopyShims() {
return gulp.src(["./extra/typeorm-model-shim.js", "./extra/typeorm-class-transformer-shim.js"])
.pipe(gulp.dest("./build/package"));
.pipe(gulp.dest("./build/browser"));
}
/**
@ -229,8 +250,8 @@ export class Gulpfile {
package() {
return [
"clean",
["browserCopySources", "browserCopyMainBrowserFile", "browserCopyPlatformTools"],
["packageCompile", "browserCompile"],
["browserCopySources", "browserCopyPlatformTools"],
["packageCompile", "browserCompile", "browserCompileSystemJS"],
["packageMoveCompiledFiles", "browserUglify"],
[
"packageClearPackageDirectory",

View File

@ -41,6 +41,7 @@ import {DocumentToEntityTransformer} from "../query-builder/transformer/Document
import {FindManyOptions} from "../find-options/FindManyOptions";
import {FindOptionsUtils} from "../find-options/FindOptionsUtils";
import {FindOneOptions} from "../find-options/FindOneOptions";
import {PlatformTools} from "../platform/PlatformTools";
/**
* Entity manager supposed to work with any entity, automatically find its repository and call its methods,
@ -120,7 +121,7 @@ export class MongoEntityManager extends EntityManager {
async findByIds<Entity>(entityClassOrName: ObjectType<Entity>|string, ids: any[], optionsOrConditions?: FindManyOptions<Entity>|Partial<Entity>): Promise<Entity[]> {
const metadata = this.connection.getMetadata(entityClassOrName);
const query = this.convertFindManyOptionsOrConditionsToMongodbQuery(optionsOrConditions) || {};
const objectIdInstance = require("mongodb").ObjectID;
const objectIdInstance = PlatformTools.load("mongodb").ObjectID;
query["_id"] = { $in: ids.map(id => {
if (id instanceof objectIdInstance)
return id;
@ -194,7 +195,7 @@ export class MongoEntityManager extends EntityManager {
const metadata = this.connection.getMetadata(entityClassOrName);
const cursor = this.createCursor(entityClassOrName, query);
const ParentCursor = require("mongodb").Cursor;
const ParentCursor = PlatformTools.load("mongodb").Cursor;
cursor.toArray = function (callback?: MongoCallback<Entity[]>) {
if (callback) {
ParentCursor.prototype.toArray.call(this, (error: MongoError, results: Entity[]): void => {
@ -535,4 +536,4 @@ export class MongoEntityManager extends EntityManager {
return orderCriteria;
}
}
}

View File

@ -1,5 +1,5 @@
import {LoggerOptions} from "./LoggerOptions";
import {PlatformTools, chalk} from "../platform/PlatformTools";
import {PlatformTools} from "../platform/PlatformTools";
import {QueryRunner} from "../query-runner/QueryRunner";
import {Logger} from "./Logger";
@ -26,7 +26,7 @@ export class AdvancedConsoleLogger implements Logger {
logQuery(query: string, parameters?: any[], queryRunner?: QueryRunner) {
if (this.options === "all" || this.options === true || (this.options instanceof Array && this.options.indexOf("query") !== -1)) {
const sql = query + (parameters && parameters.length ? " -- PARAMETERS: " + this.stringifyParams(parameters) : "");
console.log(chalk.gray.underline("executing query") + ": " + PlatformTools.highlightSql(sql));
PlatformTools.logInfo("executing query:", PlatformTools.highlightSql(sql));
}
}
@ -36,8 +36,8 @@ export class AdvancedConsoleLogger implements Logger {
logQueryError(error: string, query: string, parameters?: any[], queryRunner?: QueryRunner) {
if (this.options === "all" || this.options === true || (this.options instanceof Array && this.options.indexOf("error") !== -1)) {
const sql = query + (parameters && parameters.length ? " -- PARAMETERS: " + this.stringifyParams(parameters) : "");
console.log(chalk.underline.red(`query failed:`) + " " + chalk.bold(PlatformTools.highlightSql(sql)));
console.log(chalk.underline.red(`error:`), error);
PlatformTools.logError(`query failed:`, PlatformTools.highlightSql(sql, true));
PlatformTools.logError(`error:`, error);
}
}
@ -46,8 +46,8 @@ export class AdvancedConsoleLogger implements Logger {
*/
logQuerySlow(time: number, query: string, parameters?: any[], queryRunner?: QueryRunner) {
const sql = query + (parameters && parameters.length ? " -- PARAMETERS: " + this.stringifyParams(parameters) : "");
console.log(chalk.underline.yellow(`query is slow:`) + " " + chalk.bold(PlatformTools.highlightSql(sql)));
console.log(chalk.underline.yellow(`execution time:`), time);
PlatformTools.logWarn(`query is slow:`, PlatformTools.highlightSql(sql, true));
PlatformTools.logWarn(`execution time:`, time);
}
/**
@ -55,7 +55,7 @@ export class AdvancedConsoleLogger implements Logger {
*/
logSchemaBuild(message: string, queryRunner?: QueryRunner) {
if (this.options === "all" || (this.options instanceof Array && this.options.indexOf("schema") !== -1)) {
console.log(chalk.underline(message));
PlatformTools.log(message);
}
}
@ -75,7 +75,7 @@ export class AdvancedConsoleLogger implements Logger {
break;
case "warn":
if (this.options === "all" || (this.options instanceof Array && this.options.indexOf("warn") !== -1))
console.warn(chalk.yellow(message));
console.warn(PlatformTools.warn(message));
break;
}
}

View File

@ -93,7 +93,7 @@ export class PlatformTools {
/**
* Highlights sql string to be print in the console.
*/
static highlightSql(sql: string) {
static highlightSql(sql: string, bold: boolean = false) {
return sql;
}
@ -104,49 +104,29 @@ export class PlatformTools {
return json;
}
}
/**
* Logging functions needed by AdvancedConsoleLogger (but here without chalk)
*/
static logInfo(prefix: string, info: any) {
console.info(prefix + " ", info);
}
/**
* Simple version of the chalk library without color and format
* TODO: make it more dynamic or maybe replace it with an actual library that
* works in the browser
*/
function Chalk() {}
let colors = ['underline', 'bold', 'yellow', 'gray', 'red'];
let styles: any = {};
for(let i = 0; i < colors.length; i++) {
styles[colors[i]] = {
get() {
return build.call(this, colors[i]);
}
};
}
const proto = Object.defineProperties(() => {}, styles);
function build() {
const builder = function () {
const args = arguments;
let str = String(args[0]);
if(args.length === 0) {
return '';
}
if (args.length > 1) {
for (let a = 1; a < args.length; a++) {
str += ' ' + args[a];
}
}
return str;
};
static logError(prefix: string, error: any) {
console.error(prefix + " ", error);
}
(builder as any).__proto__ = proto;
return builder;
}
static logWarn(prefix: string, warning: any) {
console.warn(prefix + " ", warning);
}
static log(message: string) {
console.log(message);
}
Object.defineProperties(Chalk.prototype, styles);
export const chalk = new (Chalk as any)();
static warn(message: string) {
return message;
}
}
/**
* These classes are needed for stream operations or

View File

@ -5,7 +5,7 @@ export {ReadStream} from "fs";
export {EventEmitter} from "events";
export {Readable, Writable} from "stream";
export const chalk = require("chalk");
const chalk = require("chalk");
/**
* Platform-specific tools.
@ -92,7 +92,7 @@ export class PlatformTools {
/**
* Highlights sql string to be print in the console.
*/
static highlightSql(sql: string) {
static highlightSql(sql: string, bold: boolean = false) {
const theme: Theme = {
"keyword": chalk.blueBright,
"literal": chalk.blueBright,
@ -101,7 +101,13 @@ export class PlatformTools {
"built_in": chalk.magentaBright,
"comment": chalk.gray,
};
return highlight(sql, { theme: theme, language: "sql" });
let highlighted = highlight(sql, { theme: theme, language: "sql" });
if (bold) {
return chalk.bold(highlighted);
}
return highlighted;
}
/**
@ -110,4 +116,27 @@ export class PlatformTools {
static highlightJson(json: string) {
return highlight(json, { language: "json" });
}
/**
* Logging functions needed by AdvancedConsoleLogger
*/
static logInfo(prefix: string, info: any) {
console.log(chalk.gray.underline(prefix) + " ", info);
}
static logError(prefix: string, error: any) {
console.log(chalk.underline.red(prefix) + " ", error);
}
static logWarn(prefix: string, warning: any) {
console.log(chalk.underline.yellow(prefix) + " ", warning);
}
static log(message: string) {
console.log(chalk.underline(message));
}
static warn(message: string) {
return chalk.yellow(message);
}
}