| @@ -0,0 +1,100 | |||
|
|
1 | import { TextWriterBase } from "../text/TextWriterBase"; | |
|
|
2 | import { isNull, isNullOrEmptyString, isPrimitive } from "../safe"; | |
|
|
3 | import { NullConsole } from "./NullConsole"; | |
|
|
4 | ||
|
|
5 | interface LogConsole { | |
|
|
6 | debug(...args: any[]): void; | |
|
|
7 | log(...args: any[]): void; | |
|
|
8 | warn(...args: any[]): void; | |
|
|
9 | error(...args: any[]): void; | |
|
|
10 | } | |
|
|
11 | ||
|
|
12 | function hasConsole() { | |
|
|
13 | try { | |
|
|
14 | // tslint:disable-next-line:no-console | |
|
|
15 | return (typeof console !== "undefined" && typeof console.log === "function"); | |
|
|
16 | } catch { | |
|
|
17 | return false; | |
|
|
18 | } | |
|
|
19 | } | |
|
|
20 | ||
|
|
21 | function getConsole() { | |
|
|
22 | return hasConsole() ? console : NullConsole.instance; | |
|
|
23 | } | |
|
|
24 | ||
|
|
25 | export class ConsoleWriter extends TextWriterBase { | |
|
|
26 | static readonly default = new ConsoleWriter(getConsole()); | |
|
|
27 | ||
|
|
28 | private _buffer: any[]; | |
|
|
29 | ||
|
|
30 | private _out: LogConsole; | |
|
|
31 | private _level: keyof LogConsole; | |
|
|
32 | ||
|
|
33 | constructor(out?: LogConsole) { | |
|
|
34 | super(); | |
|
|
35 | this._out = out || NullConsole.instance; | |
|
|
36 | this._buffer = []; | |
|
|
37 | this._level = "log"; | |
|
|
38 | } | |
|
|
39 | ||
|
|
40 | getLogLevel() { | |
|
|
41 | return this._level; | |
|
|
42 | } | |
|
|
43 | ||
|
|
44 | setLogLevel(level: keyof LogConsole) { | |
|
|
45 | this._level = level; | |
|
|
46 | } | |
|
|
47 | ||
|
|
48 | /** Flushes the buffer to the console | |
|
|
49 | */ | |
|
|
50 | writeNewLine() { | |
|
|
51 | // group text chunks together, and let objects as is | |
|
|
52 | // ['a', 'b', {foo: 'bar'}, 'c', 'd'] -> ['ab', {foo: 'bar'}, 'cd'] | |
|
|
53 | // this will prevent from additional spaces to occur in the console | |
|
|
54 | // ['a', 'b'] will be printed as 'a b' rather then 'ab'. | |
|
|
55 | ||
|
|
56 | // console.log("writeLine", this._buffer); | |
|
|
57 | ||
|
|
58 | let offset = 0; | |
|
|
59 | const args = []; | |
|
|
60 | this._buffer.forEach((v, i) => { | |
|
|
61 | if (!isPrimitive(v)) { | |
|
|
62 | if (offset < i) | |
|
|
63 | args.push(i - offset > 1 ? this._buffer.slice(offset, i).join("") : this._buffer[offset]); | |
|
|
64 | args.push(v); | |
|
|
65 | offset = i + 1; | |
|
|
66 | } | |
|
|
67 | }); | |
|
|
68 | if (offset < this._buffer.length) | |
|
|
69 | args.push(this._buffer.slice(offset).join("")); | |
|
|
70 | ||
|
|
71 | // console.log("WriteLine", args); | |
|
|
72 | ||
|
|
73 | this._out[this._level].apply(this._out, args); | |
|
|
74 | ||
|
|
75 | this._buffer = []; | |
|
|
76 | } | |
|
|
77 | ||
|
|
78 | /** Adds a text chunk to the buffer. Buffer contents will be flushed when | |
|
|
79 | * the end of line will be printed. | |
|
|
80 | * | |
|
|
81 | * @param text The text to be added to the buffer. | |
|
|
82 | */ | |
|
|
83 | writeText(text: string) { | |
|
|
84 | this._buffer.push(text); | |
|
|
85 | } | |
|
|
86 | ||
|
|
87 | /** Wrotes the specified value to the buffer. | |
|
|
88 | * | |
|
|
89 | * @param value The value to be added to the buffer | |
|
|
90 | * @param spec The instructions how to format the value, is this parameter | |
|
|
91 | * is ommited the raw value will be added to the buffer and | |
|
|
92 | * passed directly to the console out. | |
|
|
93 | */ | |
|
|
94 | writeValue(value: any, spec?: string) { | |
|
|
95 | if (isNullOrEmptyString(spec)) | |
|
|
96 | this._buffer.push(value); | |
|
|
97 | else | |
|
|
98 | super.writeValue(value); | |
|
|
99 | } | |
|
|
100 | } | |
| @@ -0,0 +1,52 | |||
|
|
1 | export class NullConsole { | |
|
|
2 | static readonly instance = new NullConsole(); | |
|
|
3 | ||
|
|
4 | assert(condition?: boolean, message?: string, ...data: any[]): void { | |
|
|
5 | } | |
|
|
6 | clear(): void { | |
|
|
7 | } | |
|
|
8 | count(label?: string): void { | |
|
|
9 | } | |
|
|
10 | debug(message?: any, ...optionalParams: any[]): void { | |
|
|
11 | } | |
|
|
12 | dir(value?: any, ...optionalParams: any[]): void { | |
|
|
13 | } | |
|
|
14 | dirxml(value: any): void { | |
|
|
15 | } | |
|
|
16 | error(message?: any, ...optionalParams: any[]): void { | |
|
|
17 | } | |
|
|
18 | exception(message?: string, ...optionalParams: any[]): void { | |
|
|
19 | } | |
|
|
20 | group(groupTitle?: string, ...optionalParams: any[]): void { | |
|
|
21 | } | |
|
|
22 | groupCollapsed(groupTitle?: string, ...optionalParams: any[]): void { | |
|
|
23 | } | |
|
|
24 | groupEnd(): void { | |
|
|
25 | } | |
|
|
26 | info(message?: any, ...optionalParams: any[]): void { | |
|
|
27 | } | |
|
|
28 | log(message?: any, ...optionalParams: any[]): void { | |
|
|
29 | } | |
|
|
30 | markTimeline(label?: string): void { | |
|
|
31 | } | |
|
|
32 | profile(reportName?: string): void { | |
|
|
33 | } | |
|
|
34 | profileEnd(reportName?: string): void { | |
|
|
35 | } | |
|
|
36 | table(...tabularData: any[]): void { | |
|
|
37 | } | |
|
|
38 | time(label?: string): void { | |
|
|
39 | } | |
|
|
40 | timeEnd(label?: string): void { | |
|
|
41 | } | |
|
|
42 | timeStamp(label?: string): void { | |
|
|
43 | } | |
|
|
44 | timeline(label?: string): void { | |
|
|
45 | } | |
|
|
46 | timelineEnd(label?: string): void { | |
|
|
47 | } | |
|
|
48 | trace(message?: any, ...optionalParams: any[]): void { | |
|
|
49 | } | |
|
|
50 | warn(message?: any, ...optionalParams: any[]): void { | |
|
|
51 | } | |
|
|
52 | } | |
| @@ -0,0 +1,20 | |||
|
|
1 | import { TraceEvent, TraceSource } from "./TraceSource"; | |
|
|
2 | import { format } from "../text/StringBuilder"; | |
|
|
3 | ||
|
|
4 | export class TraceEventData implements TraceEvent { | |
|
|
5 | source: TraceSource; | |
|
|
6 | level: number; | |
|
|
7 | message: any; | |
|
|
8 | args?: any[]; | |
|
|
9 | ||
|
|
10 | constructor(source: TraceSource, level: number, message: any, args: any[]) { | |
|
|
11 | this.source = source; | |
|
|
12 | this.level = level; | |
|
|
13 | this.message = message; | |
|
|
14 | this.args = args; | |
|
|
15 | } | |
|
|
16 | ||
|
|
17 | toString() { | |
|
|
18 | return format(this.message, ...this.args); | |
|
|
19 | } | |
|
|
20 | } | |
| @@ -0,0 +1,41 | |||
|
|
1 | import { IObservable, IDestroyable, ICancellation } from "../../interfaces"; | |
|
|
2 | import { TraceEvent, LogLevel, WarnLevel, DebugLevel } from "../TraceSource"; | |
|
|
3 | import { Cancellation } from "../../Cancellation"; | |
|
|
4 | import { destroy, argumentNotNull } from "../../safe"; | |
|
|
5 | import { ConsoleWriter } from "../ConsoleWriter"; | |
|
|
6 | ||
|
|
7 | export class ConsoleLogger implements IDestroyable { | |
|
|
8 | private readonly _subscriptions = new Array<IDestroyable>(); | |
|
|
9 | private readonly _writer: ConsoleWriter; | |
|
|
10 | ||
|
|
11 | constructor(writer = ConsoleWriter.default) { | |
|
|
12 | argumentNotNull(writer, "writer"); | |
|
|
13 | this._writer = writer; | |
|
|
14 | } | |
|
|
15 | ||
|
|
16 | writeEvents(source: IObservable<TraceEvent>, ct: ICancellation = Cancellation.none) { | |
|
|
17 | const subscription = source.on(this.writeEvent.bind(this)); | |
|
|
18 | if (ct.isSupported()) { | |
|
|
19 | ct.register(subscription.destroy.bind(subscription)); | |
|
|
20 | } | |
|
|
21 | this._subscriptions.push(subscription); | |
|
|
22 | } | |
|
|
23 | ||
|
|
24 | writeEvent(next: TraceEvent) { | |
|
|
25 | if (next.level >= DebugLevel) { | |
|
|
26 | this._writer.setLogLevel("debug"); | |
|
|
27 | } else if (next.level >= LogLevel) { | |
|
|
28 | this._writer.setLogLevel("log"); | |
|
|
29 | } else if (next.level >= WarnLevel) { | |
|
|
30 | this._writer.setLogLevel("warn"); | |
|
|
31 | } else { | |
|
|
32 | this._writer.setLogLevel("error"); | |
|
|
33 | } | |
|
|
34 | this._writer.write("{0}: ", next.source.id); | |
|
|
35 | this._writer.writeLine(next.message, ...next.args); | |
|
|
36 | } | |
|
|
37 | ||
|
|
38 | destroy() { | |
|
|
39 | this._subscriptions.forEach(destroy); | |
|
|
40 | } | |
|
|
41 | } | |
| @@ -0,0 +1,30 | |||
|
|
1 | import { isPrimitive, isNull } from "../safe"; | |
|
|
2 | ||
|
|
3 | export class Converter { | |
|
|
4 | static readonly default = new Converter(); | |
|
|
5 | ||
|
|
6 | convert(value: any, pattern: string) { | |
|
|
7 | if (pattern && pattern.toLocaleLowerCase() === "json") { | |
|
|
8 | const seen = []; | |
|
|
9 | return JSON.stringify(value, (k, v) => { | |
|
|
10 | if (!isPrimitive(v)) { | |
|
|
11 | const id = seen.indexOf(v); | |
|
|
12 | if (id >= 0) | |
|
|
13 | return "@ref-" + id; | |
|
|
14 | else { | |
|
|
15 | seen.push(v); | |
|
|
16 | return v; | |
|
|
17 | } | |
|
|
18 | } else { | |
|
|
19 | return v; | |
|
|
20 | } | |
|
|
21 | }, 2); | |
|
|
22 | } else if (isNull(value)) { | |
|
|
23 | return ""; | |
|
|
24 | } else if (value instanceof Date) { | |
|
|
25 | return value.toISOString(); | |
|
|
26 | } else { | |
|
|
27 | return value.toString(); | |
|
|
28 | } | |
|
|
29 | } | |
|
|
30 | } | |
| @@ -0,0 +1,19 | |||
|
|
1 | import { TextWriter } from "../interfaces"; | |
|
|
2 | ||
|
|
3 | export class NullTextWriter implements TextWriter { | |
|
|
4 | static readonly instance = new NullTextWriter(); | |
|
|
5 | ||
|
|
6 | write(obj: any): void; | |
|
|
7 | write(format: string, ...args: any[]): void; | |
|
|
8 | write() { | |
|
|
9 | } | |
|
|
10 | ||
|
|
11 | writeLine(obj: any): void; | |
|
|
12 | writeLine(format: string, ...args: any[]): void; | |
|
|
13 | writeLine() { | |
|
|
14 | } | |
|
|
15 | ||
|
|
16 | writeValue(value: any, spec?: string): void; | |
|
|
17 | writeValue() { | |
|
|
18 | } | |
|
|
19 | } | |
| @@ -0,0 +1,48 | |||
|
|
1 | import { TextWriter } from "../interfaces"; | |
|
|
2 | import { FormatCompiler } from "./FormatCompiler"; | |
|
|
3 | import { isString, argumentNotNull } from "../safe"; | |
|
|
4 | import { Converter } from "./Converter"; | |
|
|
5 | ||
|
|
6 | const compiler = new FormatCompiler(); | |
|
|
7 | ||
|
|
8 | export abstract class TextWriterBase implements TextWriter { | |
|
|
9 | private _converter: Converter; | |
|
|
10 | ||
|
|
11 | constructor(converter = Converter.default) { | |
|
|
12 | argumentNotNull(converter, "converter"); | |
|
|
13 | this._converter = converter; | |
|
|
14 | } | |
|
|
15 | ||
|
|
16 | writeNewLine() { | |
|
|
17 | this.writeValue("\n"); | |
|
|
18 | } | |
|
|
19 | ||
|
|
20 | write(obj: any): void; | |
|
|
21 | write(format: string, ...args: any[]): void; | |
|
|
22 | write(format: any, ...args: any[]): void { | |
|
|
23 | if (args.length) { | |
|
|
24 | const compiled = compiler.compile(format); | |
|
|
25 | compiled(this, args); | |
|
|
26 | } else { | |
|
|
27 | this.writeValue(format); | |
|
|
28 | } | |
|
|
29 | } | |
|
|
30 | ||
|
|
31 | writeLine(obj?: any): void; | |
|
|
32 | writeLine(format: string, ...args: any[]): void; | |
|
|
33 | writeLine(): void { | |
|
|
34 | if (arguments.length) | |
|
|
35 | this.write.apply(this, arguments); | |
|
|
36 | this.writeNewLine(); | |
|
|
37 | } | |
|
|
38 | ||
|
|
39 | writeValue(value: any, spec?: string): void { | |
|
|
40 | this.writeText( | |
|
|
41 | isString(value) ? | |
|
|
42 | value : | |
|
|
43 | this._converter.convert(value, spec) | |
|
|
44 | ); | |
|
|
45 | } | |
|
|
46 | ||
|
|
47 | abstract writeText(text: string); | |
|
|
48 | } | |
| @@ -0,0 +1,86 | |||
|
|
1 | import { StringBuilder } from "@implab/core/text/StringBuilder"; | |
|
|
2 | import { test } from "./TestTraits"; | |
|
|
3 | import { MockConsole } from "./mock/MockConsole"; | |
|
|
4 | import { ConsoleWriter } from "@implab/core/log/ConsoleWriter"; | |
|
|
5 | ||
|
|
6 | test("String builder", async t => { | |
|
|
7 | const sb = new StringBuilder(); | |
|
|
8 | ||
|
|
9 | sb.write("hello"); | |
|
|
10 | t.equals(sb.toString(), "hello", "Write simple text"); | |
|
|
11 | ||
|
|
12 | sb.write(", "); | |
|
|
13 | sb.write("world!"); | |
|
|
14 | t.equals(sb.toString(), "hello, world!", "Append text"); | |
|
|
15 | ||
|
|
16 | sb.clear(); | |
|
|
17 | t.equals(sb.toString(), "", "Clear"); | |
|
|
18 | ||
|
|
19 | sb.write(1); | |
|
|
20 | t.equals(sb.toString(), "1", "Write number"); | |
|
|
21 | ||
|
|
22 | sb.clear(); | |
|
|
23 | sb.writeValue(0.123); | |
|
|
24 | t.equals(sb.toString(), "0.123", "Format number"); | |
|
|
25 | ||
|
|
26 | sb.clear(); | |
|
|
27 | sb.writeValue(new Date("2019-01-02T00:00:00.000Z")); | |
|
|
28 | t.equals(sb.toString(), "2019-01-02T00:00:00.000Z", "Format date (ISO)"); | |
|
|
29 | ||
|
|
30 | sb.clear(); | |
|
|
31 | sb.write("{0}", "hello"); | |
|
|
32 | t.equals(sb.toString(), "hello", "Simple format text"); | |
|
|
33 | ||
|
|
34 | sb.write(", {0}!", "world"); | |
|
|
35 | t.equals(sb.toString(), "hello, world!", "Append formatted text"); | |
|
|
36 | ||
|
|
37 | sb.clear(); | |
|
|
38 | sb.write("abc: {0:json}; {0.length}; {0.1} {{olo}}", ["a", "b", "c"]); | |
|
|
39 | t.equals(sb.toString(), 'abc: [\n "a",\n "b",\n "c"\n]; 3; b {olo}', "Format string with spec"); | |
|
|
40 | ||
|
|
41 | sb.clear(); | |
|
|
42 | t.throws(() => sb.write("}", 0), "Should die on bad format: '}'"); | |
|
|
43 | t.throws(() => sb.write("{", 0), "Should die on bad format: '{'"); | |
|
|
44 | t.throws(() => sb.write("{}", 0), "Should die on bad format: '{}'"); | |
|
|
45 | t.throws(() => sb.write("{:}", 0), "Should die on bad format: '{:}'"); | |
|
|
46 | t.throws(() => sb.write("{{0}", 0), "Should die on bad format: '{{0}'"); | |
|
|
47 | ||
|
|
48 | }); | |
|
|
49 | ||
|
|
50 | test("ConsoleWriter", t => { | |
|
|
51 | const mockConsole = new MockConsole(); | |
|
|
52 | const writer = new ConsoleWriter(mockConsole); | |
|
|
53 | ||
|
|
54 | writer.setLogLevel("log"); | |
|
|
55 | ||
|
|
56 | writer.writeLine("Hello, world!"); | |
|
|
57 | ||
|
|
58 | t.equals(mockConsole.getBuffer().length, 1, "One line should be written"); | |
|
|
59 | t.equals(mockConsole.getBuffer()[0].level, "log", "LogLevel should be 'log'"); | |
|
|
60 | t.deepEqual(mockConsole.getBuffer()[0].data, ["Hello, world!"], "The buffer should contain single string"); | |
|
|
61 | ||
|
|
62 | mockConsole.clear(); | |
|
|
63 | writer.setLogLevel("debug"); | |
|
|
64 | writer.write("Bring "); | |
|
|
65 | writer.write("the {0}!", "light"); | |
|
|
66 | t.equals(mockConsole.getBuffer().length, 0, "No line should be written"); | |
|
|
67 | writer.writeLine(); | |
|
|
68 | ||
|
|
69 | t.equals(mockConsole.getBuffer().length, 1, "One line should be written"); | |
|
|
70 | t.equals(mockConsole.getBuffer()[0].level, "debug", "LogLevel should be 'log'"); | |
|
|
71 | t.deepEqual(mockConsole.getBuffer()[0].data, ["Bring the light!"], "Should concatenate string parts together"); | |
|
|
72 | ||
|
|
73 | mockConsole.clear(); | |
|
|
74 | writer.writeLine("It's {0} o'clock, lets have some {1}!", { h: 5}, { title: "tee" }); | |
|
|
75 | ||
|
|
76 | t.deepEqual(mockConsole.getBuffer()[0].data, ["It's ", { h: 5}, " o'clock, lets have some ", { title: "tee" }, "!"], "Non string parts should be psassed as is"); | |
|
|
77 | ||
|
|
78 | mockConsole.clear(); | |
|
|
79 | writer.writeLine("{0} or {1} to {2}", {i: 25}, 6, 4); | |
|
|
80 | t.deepEqual(mockConsole.getBuffer()[0].data, [{i: 25}, " or 6 to 4"], "25 or 6 to 4"); | |
|
|
81 | ||
|
|
82 | mockConsole.clear(); | |
|
|
83 | writer.writeLine("{0} or {1} to {2}! Let's have some {3}", 25, 6, 4, { product: "tee" } ); | |
|
|
84 | t.deepEqual(mockConsole.getBuffer()[0].data, ["25 or 6 to 4! Let's have some ", { product: "tee" }], "Should handle many text chunks and object at the end"); | |
|
|
85 | ||
|
|
86 | }); | |
| @@ -0,0 +1,52 | |||
|
|
1 | import {NullConsole} from "@implab/core/log/NullConsole"; | |
|
|
2 | ||
|
|
3 | interface ConsoleLineData { | |
|
|
4 | level: string; | |
|
|
5 | data: any[]; | |
|
|
6 | } | |
|
|
7 | ||
|
|
8 | export class MockConsole extends NullConsole { | |
|
|
9 | _buffer: ConsoleLineData[] = []; | |
|
|
10 | ||
|
|
11 | debug(...args: any[]) { | |
|
|
12 | this._buffer.push({ | |
|
|
13 | level: "debug", | |
|
|
14 | data: args | |
|
|
15 | }); | |
|
|
16 | } | |
|
|
17 | ||
|
|
18 | log(...args: any[]) { | |
|
|
19 | this._buffer.push({ | |
|
|
20 | level: "log", | |
|
|
21 | data: args | |
|
|
22 | }); | |
|
|
23 | } | |
|
|
24 | ||
|
|
25 | warn(...args: any[]) { | |
|
|
26 | this._buffer.push({ | |
|
|
27 | level: "warn", | |
|
|
28 | data: args | |
|
|
29 | }); | |
|
|
30 | } | |
|
|
31 | ||
|
|
32 | error(...args: any[]) { | |
|
|
33 | this._buffer.push({ | |
|
|
34 | level: "error", | |
|
|
35 | data: args | |
|
|
36 | }); | |
|
|
37 | } | |
|
|
38 | ||
|
|
39 | getBuffer() { | |
|
|
40 | return this._buffer; | |
|
|
41 | } | |
|
|
42 | ||
|
|
43 | getLine(i: number) { | |
|
|
44 | if (i >= this._buffer.length) | |
|
|
45 | throw new Error(`Line number ${i} is out of range, buffer.length = ${this._buffer.length}`); | |
|
|
46 | return this._buffer[i].data; | |
|
|
47 | } | |
|
|
48 | ||
|
|
49 | clear() { | |
|
|
50 | this._buffer = []; | |
|
|
51 | } | |
|
|
52 | } | |
| @@ -1,276 +1,278 | |||
|
|
1 | 1 | // если версия явно не заданы вычисляем ее из тэга ревизии v.{num}*** |
|
|
2 | 2 | // результатом будет версия '{num}.{distance}' где distance - расстояние от |
|
|
3 | 3 | // текущей ревизии до ревизии с тэгом |
|
|
4 | 4 | def tagDistance = 0; |
|
|
5 | 5 | def isRelease = false; |
|
|
6 | 6 | |
|
|
7 | 7 | if (!version) { |
|
|
8 | 8 | |
|
|
9 | 9 | def rev = ["hg", "log", "-r", ".", "--template", "{latesttag('re:^v') % '{tag}-{distance}'}"].execute().text.trim(); |
|
|
10 | 10 | |
|
|
11 | 11 | def tagVersion; |
|
|
12 | 12 | |
|
|
13 | 13 | def match = (rev =~ /^v(\d+\.\d+\.\d+).*-(\d+)$/); |
|
|
14 | 14 | |
|
|
15 | 15 | if (match.size()) { |
|
|
16 | 16 | tagVersion = match[0][1]; |
|
|
17 | 17 | tagDistance = match[0][2].toInteger(); |
|
|
18 | 18 | } else { |
|
|
19 | 19 | throw new Exception("A version must be specied"); |
|
|
20 | 20 | } |
|
|
21 | 21 | |
|
|
22 | 22 | version = tagVersion; |
|
|
23 | 23 | |
|
|
24 | 24 | if (tagDistance > 0) |
|
|
25 | 25 | version++; |
|
|
26 | 26 | } else { |
|
|
27 | 27 | println "explicit version: $version"; |
|
|
28 | 28 | } |
|
|
29 | 29 | |
|
|
30 | 30 | if (hasProperty('versionSuffix') && versionSuffix) { |
|
|
31 | 31 | version += "-$versionSuffix" |
|
|
32 | 32 | } |
|
|
33 | 33 | |
|
|
34 | 34 | if(!npmName) |
|
|
35 | 35 | npmName = name; |
|
|
36 | 36 | |
|
|
37 | 37 | if (hasProperty('release')) { |
|
|
38 | 38 | isRelease = (release != 'false') |
|
|
39 | 39 | } else { |
|
|
40 | 40 | isRelease = (tagDistance == 0); |
|
|
41 | 41 | } |
|
|
42 | 42 | |
|
|
43 | 43 | if(!["amd", "commonjs", "system", "umd", "es6", "esnext"].contains(jsmodule)) |
|
|
44 | 44 | throw new Exception("Invalid jsmodule specified: $jsmodule"); |
|
|
45 | 45 | if(!["es3", "es5", "es6", "es2016", "es2017", "esnext"].contains(target)) |
|
|
46 | 46 | throw new Exception("Invalid target specified: $target") |
|
|
47 | 47 | |
|
|
48 | 48 | def targetLibs = [ |
|
|
49 | 49 | "es3" : "es5,es2015.promise,es2015.symbol,dom,scripthost", |
|
|
50 | 50 | "es5" : "es5,es2015.promise,es2015.symbol,dom,scripthost" |
|
|
51 | 51 | ]; |
|
|
52 | 52 | |
|
|
53 | 53 | ext.packageName="@$npmScope/$npmName"; |
|
|
54 | 54 | |
|
|
55 | 55 | def srcDir = "$projectDir/src" |
|
|
56 | 56 | def typingsDir = "$srcDir/typings" |
|
|
57 | 57 | def distDir = "$buildDir/dist" |
|
|
58 | 58 | def testDir = "$buildDir/test" |
|
|
59 | 59 | def lib = targetLibs[target] ?: "${target},dom"; |
|
|
60 | 60 | |
|
|
61 | 61 | println "lib: $lib"; |
|
|
62 | 62 | |
|
|
63 | 63 | def sourceSets = ["main", "amd", "cjs"]; |
|
|
64 | 64 | def testSets = ["test", "testAmd", "testCjs"]; |
|
|
65 | 65 | |
|
|
66 | 66 | task beforeBuild { |
|
|
67 | 67 | } |
|
|
68 | 68 | |
|
|
69 | 69 | def createSoursetTasks = { String name, String outDir -> |
|
|
70 | 70 | def setName = name.capitalize(); |
|
|
71 | 71 | |
|
|
72 | 72 | def compileDir = "$buildDir/compile/$name" |
|
|
73 | 73 | def declDir = "$typingsDir/$name" |
|
|
74 | 74 | def setDir = "$projectDir/src/$name" |
|
|
75 | 75 | def jsDir = outDir; |
|
|
76 | 76 | |
|
|
77 | 77 | def beforeBuildTask = task "beforeBuild$setName"(dependsOn: beforeBuild) { |
|
|
78 | 78 | } |
|
|
79 | 79 | |
|
|
80 | 80 | def copyJsTask = task "copyJs$setName"(dependsOn: beforeBuildTask, type: Copy) { |
|
|
81 | 81 | from "$setDir/js" |
|
|
82 | 82 | into jsDir |
|
|
83 | 83 | } |
|
|
84 | 84 | |
|
|
85 | 85 | def lintJsTask = task "lintJs$setName"(dependsOn: beforeBuildTask, type: Exec) { |
|
|
86 | 86 | inputs.dir("$setDir/js/").skipWhenEmpty(); |
|
|
87 | 87 | commandLine "eslint", '--format', 'stylish', "$setDir/js/" |
|
|
88 | 88 | } |
|
|
89 | 89 | |
|
|
90 | 90 | def compileTsTask = task "compileTs$setName"(dependsOn: beforeBuildTask, type: Exec) { |
|
|
91 | 91 | inputs.dir("$setDir/ts").skipWhenEmpty() |
|
|
92 | 92 | inputs.file("$srcDir/tsconfig.json") |
|
|
93 | 93 | inputs.file("$setDir/tsconfig.json") |
|
|
94 | 94 | outputs.dir(compileDir) |
|
|
95 | 95 | outputs.dir(declDir) |
|
|
96 | 96 | |
|
|
97 | 97 | commandLine 'node_modules/.bin/tsc', |
|
|
98 | 98 | '-p', "$setDir/tsconfig.json", |
|
|
99 | 99 | '-t', target, |
|
|
100 | 100 | '-m', jsmodule, |
|
|
101 | 101 | '-d', |
|
|
102 | '--sourceMap', | |
|
|
103 | '--sourceRoot', "file://$setDir/ts", | |
|
|
102 | 104 | '--outDir', compileDir, |
|
|
103 | 105 | '--declarationDir', declDir |
|
|
104 | 106 | |
|
|
105 | 107 | if (lib) |
|
|
106 | 108 | args '--lib', lib |
|
|
107 | 109 | } |
|
|
108 | 110 | |
|
|
109 | 111 | def copyTsOutputTask = task "copyTsOutput$setName"(dependsOn: compileTsTask, type: Copy) { |
|
|
110 | 112 | from compileDir |
|
|
111 | 113 | into jsDir |
|
|
112 | 114 | } |
|
|
113 | 115 | |
|
|
114 | 116 | def copyTypingsTask = task "copyTypings$setName"(dependsOn: compileTsTask, type: Copy) { |
|
|
115 | 117 | from declDir |
|
|
116 | 118 | into jsDir |
|
|
117 | 119 | } |
|
|
118 | 120 | |
|
|
119 | 121 | def copyResourcesTask = task "copyResources$setName"(dependsOn: beforeBuildTask, type: Copy) { |
|
|
120 | 122 | from "$setDir/resources" |
|
|
121 | 123 | into outDir |
|
|
122 | 124 | } |
|
|
123 | 125 | |
|
|
124 | 126 | task "build$setName" { |
|
|
125 | 127 | dependsOn copyTypingsTask, |
|
|
126 | 128 | copyTsOutputTask, |
|
|
127 | 129 | copyJsTask, |
|
|
128 | 130 | copyResourcesTask, |
|
|
129 | 131 | lintJsTask |
|
|
130 | 132 | } |
|
|
131 | 133 | } |
|
|
132 | 134 | |
|
|
133 | 135 | task printVersion { |
|
|
134 | 136 | doLast { |
|
|
135 | 137 | println "version: $version"; |
|
|
136 | 138 | println "isRelease: $isRelease, tagDistance: $tagDistance"; |
|
|
137 | 139 | println "packageName: $packageName"; |
|
|
138 | 140 | println "bundle: ${pack.outputs.files.join(',')}"; |
|
|
139 | 141 | println "target: $target"; |
|
|
140 | 142 | println "module: $jsmodule"; |
|
|
141 | 143 | } |
|
|
142 | 144 | } |
|
|
143 | 145 | |
|
|
144 | 146 | task clean { |
|
|
145 | 147 | doLast { |
|
|
146 | 148 | delete buildDir |
|
|
147 | 149 | delete typingsDir |
|
|
148 | 150 | } |
|
|
149 | 151 | } |
|
|
150 | 152 | |
|
|
151 | 153 | task _initBuild { |
|
|
152 | 154 | mustRunAfter clean |
|
|
153 | 155 | |
|
|
154 | 156 | def buildInfoFile = "$buildDir/platform"; |
|
|
155 | 157 | inputs.property('target',target); |
|
|
156 | 158 | inputs.property('jsmodule',jsmodule); |
|
|
157 | 159 | outputs.file(buildInfoFile); |
|
|
158 | 160 | |
|
|
159 | 161 | doLast { |
|
|
160 | 162 | delete buildDir |
|
|
161 | 163 | mkdir buildDir |
|
|
162 | 164 | |
|
|
163 | 165 | def f = new File(buildInfoFile); |
|
|
164 | 166 | f << "$target-$jsmodule"; |
|
|
165 | 167 | } |
|
|
166 | 168 | } |
|
|
167 | 169 | |
|
|
168 | 170 | task cleanNpm { |
|
|
169 | 171 | doLast { |
|
|
170 | 172 | delete 'node_modules' |
|
|
171 | 173 | } |
|
|
172 | 174 | } |
|
|
173 | 175 | |
|
|
174 | 176 | task _npmInstall() { |
|
|
175 | 177 | inputs.file("package.json") |
|
|
176 | 178 | outputs.dir("node_modules") |
|
|
177 | 179 | doLast { |
|
|
178 | 180 | exec { |
|
|
179 | 181 | commandLine 'npm', 'install' |
|
|
180 | 182 | } |
|
|
181 | 183 | } |
|
|
182 | 184 | } |
|
|
183 | 185 | |
|
|
184 | 186 | beforeBuild { |
|
|
185 | 187 | dependsOn _initBuild |
|
|
186 | 188 | dependsOn _npmInstall |
|
|
187 | 189 | } |
|
|
188 | 190 | |
|
|
189 | 191 | sourceSets.each { createSoursetTasks(it, distDir) } |
|
|
190 | 192 | |
|
|
191 | 193 | testSets.each { createSoursetTasks(it, testDir) } |
|
|
192 | 194 | |
|
|
193 | 195 | compileTsAmd { |
|
|
194 | 196 | dependsOn compileTsMain |
|
|
195 | 197 | } |
|
|
196 | 198 | |
|
|
197 | 199 | compileTsCjs { |
|
|
198 | 200 | dependsOn compileTsMain |
|
|
199 | 201 | } |
|
|
200 | 202 | |
|
|
201 | 203 | task build(dependsOn: buildMain) { |
|
|
202 | 204 | if (jsmodule == "amd") |
|
|
203 | 205 | dependsOn buildAmd |
|
|
204 | 206 | if (jsmodule == "commonjs") |
|
|
205 | 207 | dependsOn buildCjs |
|
|
206 | 208 | } |
|
|
207 | 209 | |
|
|
208 | 210 | compileTsTest { |
|
|
209 | 211 | dependsOn build |
|
|
210 | 212 | } |
|
|
211 | 213 | |
|
|
212 | 214 | compileTsTestAmd { |
|
|
213 | 215 | dependsOn compileTsTest |
|
|
214 | 216 | } |
|
|
215 | 217 | |
|
|
216 | 218 | compileTsTestCjs { |
|
|
217 | 219 | dependsOn compileTsTest |
|
|
218 | 220 | } |
|
|
219 | 221 | |
|
|
220 | 222 | task _installLocalCjsDependency(dependsOn: [buildTestCjs, "_packageMeta"], type: Exec) { |
|
|
221 | 223 | inputs.file("$distDir/package.json") |
|
|
222 | 224 | outputs.upToDateWhen { |
|
|
223 | 225 | new File("$testDir/$packageName").exists() |
|
|
224 | 226 | } |
|
|
225 | 227 | |
|
|
226 | 228 | workingDir testDir |
|
|
227 | 229 | |
|
|
228 | 230 | commandLine 'npm', 'install', '--no-save', '--force', distDir |
|
|
229 | 231 | } |
|
|
230 | 232 | |
|
|
231 | 233 | task test(dependsOn: [buildTest], type: Exec) { |
|
|
232 | 234 | if (jsmodule == "amd") |
|
|
233 | 235 | dependsOn buildTestAmd |
|
|
234 | 236 | if (jsmodule == "commonjs") { |
|
|
235 | 237 | dependsOn buildTestCjs |
|
|
236 | 238 | dependsOn _installLocalCjsDependency |
|
|
237 | 239 | } |
|
|
238 | 240 | |
|
|
239 | 241 | commandLine 'node', "$testDir/run-tests.js" |
|
|
240 | 242 | } |
|
|
241 | 243 | |
|
|
242 | 244 | task _packageMeta(type: Copy) { |
|
|
243 | 245 | mustRunAfter build |
|
|
244 | 246 | |
|
|
245 | 247 | inputs.property("version", version) |
|
|
246 | 248 | from('.') { |
|
|
247 | 249 | include '.npmignore', 'readme.md', 'license', 'history.md' |
|
|
248 | 250 | } |
|
|
249 | 251 | from("$srcDir/package.${jsmodule}.tmpl.json") { |
|
|
250 | 252 | expand project.properties |
|
|
251 | 253 | rename { "package.json" } |
|
|
252 | 254 | } |
|
|
253 | 255 | into distDir |
|
|
254 | 256 | } |
|
|
255 | 257 | |
|
|
256 | 258 | task pack(dependsOn: [build, _packageMeta], type: Exec) { |
|
|
257 | 259 | workingDir distDir |
|
|
258 | 260 | outputs.file("$npmScope-$npmName-${version}.tgz") |
|
|
259 | 261 | |
|
|
260 | 262 | commandLine 'npm', 'pack' |
|
|
261 | 263 | } |
|
|
262 | 264 | |
|
|
263 | 265 | task publish(dependsOn: [build, _packageMeta], type: Exec) { |
|
|
264 | 266 | doFirst { |
|
|
265 | 267 | if (!isRelease) |
|
|
266 | 268 | throw new Exception("Can't publish an unreleased version"); |
|
|
267 | 269 | } |
|
|
268 | 270 | workingDir distDir |
|
|
269 | 271 | |
|
|
270 | 272 | commandLine 'npm', 'publish', '--access', 'public' |
|
|
271 | 273 | } |
|
|
272 | 274 | |
|
|
273 | 275 | task markRelease(type: Exec) { |
|
|
274 | 276 | onlyIf { tagDistance > 1 } |
|
|
275 | 277 | commandLine "hg", "tag", "v$version"; |
|
|
276 | 278 | } No newline at end of file |
| @@ -1,9 +1,9 | |||
|
|
1 | 1 | version= |
|
|
2 | 2 | author=Implab team |
|
|
3 | 3 | jsmodule=amd |
|
|
4 | 4 | target=es5 |
|
|
5 | 5 | description=Dependency injection, logging, simple and fast text template engine |
|
|
6 | 6 | license=BSD-2-Clause |
|
|
7 | 7 | repository=https://bitbucket.org/implab/implabjs-core |
|
|
8 | 8 | npmScope=implab |
|
|
9 | npmName=core No newline at end of file | |
|
|
9 | npmName=core-amd No newline at end of file | |
| @@ -1,471 +1,471 | |||
|
|
1 | 1 | { |
|
|
2 | 2 | "name": "@implab/core", |
|
|
3 | 3 | "version": "0.0.1-dev", |
|
|
4 | 4 | "lockfileVersion": 1, |
|
|
5 | 5 | "requires": true, |
|
|
6 | 6 | "dependencies": { |
|
|
7 | 7 | "@types/node": { |
|
|
8 | 8 | "version": "10.12.18", |
|
|
9 | 9 | "resolved": "https://registry.npmjs.org/@types/node/-/node-10.12.18.tgz", |
|
|
10 | 10 | "integrity": "sha512-fh+pAqt4xRzPfqA6eh3Z2y6fyZavRIumvjhaCL753+TVkGKGhpPeyrJG2JftD0T9q4GF00KjefsQ+PQNDdWQaQ==", |
|
|
11 | 11 | "dev": true |
|
|
12 | 12 | }, |
|
|
13 | 13 | "@types/requirejs": { |
|
|
14 | 14 | "version": "2.1.31", |
|
|
15 | 15 | "resolved": "https://registry.npmjs.org/@types/requirejs/-/requirejs-2.1.31.tgz", |
|
|
16 | 16 | "integrity": "sha512-b2soeyuU76rMbcRJ4e0hEl0tbMhFwZeTC0VZnfuWlfGlk6BwWNsev6kFu/twKABPX29wkX84wU2o+cEJoXsiTw==", |
|
|
17 | 17 | "dev": true |
|
|
18 | 18 | }, |
|
|
19 | 19 | "@types/tape": { |
|
|
20 | 20 | "version": "4.2.33", |
|
|
21 | 21 | "resolved": "https://registry.npmjs.org/@types/tape/-/tape-4.2.33.tgz", |
|
|
22 | 22 | "integrity": "sha512-ltfyuY5BIkYlGuQfwqzTDT8f0q8Z5DGppvUnWGs39oqDmMd6/UWhNpX3ZMh/VYvfxs3rFGHMrLC/eGRdLiDGuw==", |
|
|
23 | 23 | "dev": true, |
|
|
24 | 24 | "requires": { |
|
|
25 | 25 | "@types/node": "*" |
|
|
26 | 26 | } |
|
|
27 | 27 | }, |
|
|
28 | 28 | "balanced-match": { |
|
|
29 | 29 | "version": "1.0.0", |
|
|
30 | 30 | "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", |
|
|
31 | 31 | "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=", |
|
|
32 | 32 | "dev": true |
|
|
33 | 33 | }, |
|
|
34 | 34 | "brace-expansion": { |
|
|
35 | 35 | "version": "1.1.11", |
|
|
36 | 36 | "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", |
|
|
37 | 37 | "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", |
|
|
38 | 38 | "dev": true, |
|
|
39 | 39 | "requires": { |
|
|
40 | 40 | "balanced-match": "^1.0.0", |
|
|
41 | 41 | "concat-map": "0.0.1" |
|
|
42 | 42 | } |
|
|
43 | 43 | }, |
|
|
44 | 44 | "concat-map": { |
|
|
45 | 45 | "version": "0.0.1", |
|
|
46 | 46 | "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", |
|
|
47 | 47 | "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=", |
|
|
48 | 48 | "dev": true |
|
|
49 | 49 | }, |
|
|
50 | 50 | "core-util-is": { |
|
|
51 | 51 | "version": "1.0.2", |
|
|
52 | 52 | "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz", |
|
|
53 | 53 | "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=", |
|
|
54 | 54 | "dev": true |
|
|
55 | 55 | }, |
|
|
56 | 56 | "deep-equal": { |
|
|
57 | 57 | "version": "0.1.2", |
|
|
58 | 58 | "resolved": "https://registry.npmjs.org/deep-equal/-/deep-equal-0.1.2.tgz", |
|
|
59 | 59 | "integrity": "sha1-skbCuApXCkfBG+HZvRBw7IeLh84=", |
|
|
60 | 60 | "dev": true |
|
|
61 | 61 | }, |
|
|
62 | 62 | "define-properties": { |
|
|
63 | 63 | "version": "1.1.3", |
|
|
64 | 64 | "resolved": "https://registry.npmjs.org/define-properties/-/define-properties-1.1.3.tgz", |
|
|
65 | 65 | "integrity": "sha512-3MqfYKj2lLzdMSf8ZIZE/V+Zuy+BgD6f164e8K2w7dgnpKArBDerGYpM46IYYcjnkdPNMjPk9A6VFB8+3SKlXQ==", |
|
|
66 | 66 | "dev": true, |
|
|
67 | 67 | "requires": { |
|
|
68 | 68 | "object-keys": "^1.0.12" |
|
|
69 | 69 | }, |
|
|
70 | 70 | "dependencies": { |
|
|
71 | 71 | "object-keys": { |
|
|
72 | 72 | "version": "1.0.12", |
|
|
73 | 73 | "resolved": "https://registry.npmjs.org/object-keys/-/object-keys-1.0.12.tgz", |
|
|
74 | 74 | "integrity": "sha512-FTMyFUm2wBcGHnH2eXmz7tC6IwlqQZ6mVZ+6dm6vZ4IQIHjs6FdNsQBuKGPuUUUY6NfJw2PshC08Tn6LzLDOag==", |
|
|
75 | 75 | "dev": true |
|
|
76 | 76 | } |
|
|
77 | 77 | } |
|
|
78 | 78 | }, |
|
|
79 | 79 | "defined": { |
|
|
80 | 80 | "version": "0.0.0", |
|
|
81 | 81 | "resolved": "https://registry.npmjs.org/defined/-/defined-0.0.0.tgz", |
|
|
82 | 82 | "integrity": "sha1-817qfXBekzuvE7LwOz+D2SFAOz4=", |
|
|
83 | 83 | "dev": true |
|
|
84 | 84 | }, |
|
|
85 | 85 | "dojo": { |
|
|
86 | 86 | "version": "1.14.2", |
|
|
87 | 87 | "resolved": "https://registry.npmjs.org/dojo/-/dojo-1.14.2.tgz", |
|
|
88 | 88 | "integrity": "sha512-TI+Ytgfh/VfmHWERp45Jte6NFMdoJTPsvUP/uzJUvAXET8FP2h442LePWWJ/q/xZ4V0V8OtdJhx8It/GB+Zbxg==", |
|
|
89 | 89 | "dev": true |
|
|
90 | 90 | }, |
|
|
91 | 91 | "duplexer": { |
|
|
92 | 92 | "version": "0.1.1", |
|
|
93 |
"resolved": "http |
|
|
|
93 | "resolved": "http://registry.npmjs.org/duplexer/-/duplexer-0.1.1.tgz", | |
|
|
94 | 94 | "integrity": "sha1-rOb/gIwc5mtX0ev5eXessCM0z8E=", |
|
|
95 | 95 | "dev": true |
|
|
96 | 96 | }, |
|
|
97 | 97 | "es-abstract": { |
|
|
98 | 98 | "version": "1.13.0", |
|
|
99 | 99 | "resolved": "https://registry.npmjs.org/es-abstract/-/es-abstract-1.13.0.tgz", |
|
|
100 | 100 | "integrity": "sha512-vDZfg/ykNxQVwup/8E1BZhVzFfBxs9NqMzGcvIJrqg5k2/5Za2bWo40dK2J1pgLngZ7c+Shh8lwYtLGyrwPutg==", |
|
|
101 | 101 | "dev": true, |
|
|
102 | 102 | "requires": { |
|
|
103 | 103 | "es-to-primitive": "^1.2.0", |
|
|
104 | 104 | "function-bind": "^1.1.1", |
|
|
105 | 105 | "has": "^1.0.3", |
|
|
106 | 106 | "is-callable": "^1.1.4", |
|
|
107 | 107 | "is-regex": "^1.0.4", |
|
|
108 | 108 | "object-keys": "^1.0.12" |
|
|
109 | 109 | }, |
|
|
110 | 110 | "dependencies": { |
|
|
111 | 111 | "object-keys": { |
|
|
112 | 112 | "version": "1.0.12", |
|
|
113 | 113 | "resolved": "https://registry.npmjs.org/object-keys/-/object-keys-1.0.12.tgz", |
|
|
114 | 114 | "integrity": "sha512-FTMyFUm2wBcGHnH2eXmz7tC6IwlqQZ6mVZ+6dm6vZ4IQIHjs6FdNsQBuKGPuUUUY6NfJw2PshC08Tn6LzLDOag==", |
|
|
115 | 115 | "dev": true |
|
|
116 | 116 | } |
|
|
117 | 117 | } |
|
|
118 | 118 | }, |
|
|
119 | 119 | "es-to-primitive": { |
|
|
120 | 120 | "version": "1.2.0", |
|
|
121 | 121 | "resolved": "https://registry.npmjs.org/es-to-primitive/-/es-to-primitive-1.2.0.tgz", |
|
|
122 | 122 | "integrity": "sha512-qZryBOJjV//LaxLTV6UC//WewneB3LcXOL9NP++ozKVXsIIIpm/2c13UDiD9Jp2eThsecw9m3jPqDwTyobcdbg==", |
|
|
123 | 123 | "dev": true, |
|
|
124 | 124 | "requires": { |
|
|
125 | 125 | "is-callable": "^1.1.4", |
|
|
126 | 126 | "is-date-object": "^1.0.1", |
|
|
127 | 127 | "is-symbol": "^1.0.2" |
|
|
128 | 128 | } |
|
|
129 | 129 | }, |
|
|
130 | 130 | "faucet": { |
|
|
131 | 131 | "version": "0.0.1", |
|
|
132 | 132 | "resolved": "https://registry.npmjs.org/faucet/-/faucet-0.0.1.tgz", |
|
|
133 | 133 | "integrity": "sha1-WX3PHSGJosBiMhtZHo8VHtIDnZw=", |
|
|
134 | 134 | "dev": true, |
|
|
135 | 135 | "requires": { |
|
|
136 | 136 | "defined": "0.0.0", |
|
|
137 | 137 | "duplexer": "~0.1.1", |
|
|
138 | 138 | "minimist": "0.0.5", |
|
|
139 | 139 | "sprintf": "~0.1.3", |
|
|
140 | 140 | "tap-parser": "~0.4.0", |
|
|
141 | 141 | "tape": "~2.3.2", |
|
|
142 | 142 | "through2": "~0.2.3" |
|
|
143 | 143 | }, |
|
|
144 | 144 | "dependencies": { |
|
|
145 | 145 | "tape": { |
|
|
146 | 146 | "version": "2.3.3", |
|
|
147 |
"resolved": "http |
|
|
|
147 | "resolved": "http://registry.npmjs.org/tape/-/tape-2.3.3.tgz", | |
|
|
148 | 148 | "integrity": "sha1-Lnzgox3wn41oUWZKcYQuDKUFevc=", |
|
|
149 | 149 | "dev": true, |
|
|
150 | 150 | "requires": { |
|
|
151 | 151 | "deep-equal": "~0.1.0", |
|
|
152 | 152 | "defined": "~0.0.0", |
|
|
153 | 153 | "inherits": "~2.0.1", |
|
|
154 | 154 | "jsonify": "~0.0.0", |
|
|
155 | 155 | "resumer": "~0.0.0", |
|
|
156 | 156 | "through": "~2.3.4" |
|
|
157 | 157 | } |
|
|
158 | 158 | } |
|
|
159 | 159 | } |
|
|
160 | 160 | }, |
|
|
161 | 161 | "for-each": { |
|
|
162 | 162 | "version": "0.3.3", |
|
|
163 | 163 | "resolved": "https://registry.npmjs.org/for-each/-/for-each-0.3.3.tgz", |
|
|
164 | 164 | "integrity": "sha512-jqYfLp7mo9vIyQf8ykW2v7A+2N4QjeCeI5+Dz9XraiO1ign81wjiH7Fb9vSOWvQfNtmSa4H2RoQTrrXivdUZmw==", |
|
|
165 | 165 | "dev": true, |
|
|
166 | 166 | "requires": { |
|
|
167 | 167 | "is-callable": "^1.1.3" |
|
|
168 | 168 | } |
|
|
169 | 169 | }, |
|
|
170 | 170 | "fs.realpath": { |
|
|
171 | 171 | "version": "1.0.0", |
|
|
172 | 172 | "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", |
|
|
173 | 173 | "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=", |
|
|
174 | 174 | "dev": true |
|
|
175 | 175 | }, |
|
|
176 | 176 | "function-bind": { |
|
|
177 | 177 | "version": "1.1.1", |
|
|
178 | 178 | "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz", |
|
|
179 | 179 | "integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A==", |
|
|
180 | 180 | "dev": true |
|
|
181 | 181 | }, |
|
|
182 | 182 | "glob": { |
|
|
183 | 183 | "version": "7.1.3", |
|
|
184 | 184 | "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.3.tgz", |
|
|
185 | 185 | "integrity": "sha512-vcfuiIxogLV4DlGBHIUOwI0IbrJ8HWPc4MU7HzviGeNho/UJDfi6B5p3sHeWIQ0KGIU0Jpxi5ZHxemQfLkkAwQ==", |
|
|
186 | 186 | "dev": true, |
|
|
187 | 187 | "requires": { |
|
|
188 | 188 | "fs.realpath": "^1.0.0", |
|
|
189 | 189 | "inflight": "^1.0.4", |
|
|
190 | 190 | "inherits": "2", |
|
|
191 | 191 | "minimatch": "^3.0.4", |
|
|
192 | 192 | "once": "^1.3.0", |
|
|
193 | 193 | "path-is-absolute": "^1.0.0" |
|
|
194 | 194 | } |
|
|
195 | 195 | }, |
|
|
196 | 196 | "has": { |
|
|
197 | 197 | "version": "1.0.3", |
|
|
198 | 198 | "resolved": "https://registry.npmjs.org/has/-/has-1.0.3.tgz", |
|
|
199 | 199 | "integrity": "sha512-f2dvO0VU6Oej7RkWJGrehjbzMAjFp5/VKPp5tTpWIV4JHHZK1/BxbFRtf/siA2SWTe09caDmVtYYzWEIbBS4zw==", |
|
|
200 | 200 | "dev": true, |
|
|
201 | 201 | "requires": { |
|
|
202 | 202 | "function-bind": "^1.1.1" |
|
|
203 | 203 | } |
|
|
204 | 204 | }, |
|
|
205 | 205 | "has-symbols": { |
|
|
206 | 206 | "version": "1.0.0", |
|
|
207 | 207 | "resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.0.0.tgz", |
|
|
208 | 208 | "integrity": "sha1-uhqPGvKg/DllD1yFA2dwQSIGO0Q=", |
|
|
209 | 209 | "dev": true |
|
|
210 | 210 | }, |
|
|
211 | 211 | "inflight": { |
|
|
212 | 212 | "version": "1.0.6", |
|
|
213 | 213 | "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", |
|
|
214 | 214 | "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=", |
|
|
215 | 215 | "dev": true, |
|
|
216 | 216 | "requires": { |
|
|
217 | 217 | "once": "^1.3.0", |
|
|
218 | 218 | "wrappy": "1" |
|
|
219 | 219 | } |
|
|
220 | 220 | }, |
|
|
221 | 221 | "inherits": { |
|
|
222 | 222 | "version": "2.0.3", |
|
|
223 | 223 | "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz", |
|
|
224 | 224 | "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=", |
|
|
225 | 225 | "dev": true |
|
|
226 | 226 | }, |
|
|
227 | 227 | "is-callable": { |
|
|
228 | 228 | "version": "1.1.4", |
|
|
229 | 229 | "resolved": "https://registry.npmjs.org/is-callable/-/is-callable-1.1.4.tgz", |
|
|
230 | 230 | "integrity": "sha512-r5p9sxJjYnArLjObpjA4xu5EKI3CuKHkJXMhT7kwbpUyIFD1n5PMAsoPvWnvtZiNz7LjkYDRZhd7FlI0eMijEA==", |
|
|
231 | 231 | "dev": true |
|
|
232 | 232 | }, |
|
|
233 | 233 | "is-date-object": { |
|
|
234 | 234 | "version": "1.0.1", |
|
|
235 | 235 | "resolved": "https://registry.npmjs.org/is-date-object/-/is-date-object-1.0.1.tgz", |
|
|
236 | 236 | "integrity": "sha1-mqIOtq7rv/d/vTPnTKAbM1gdOhY=", |
|
|
237 | 237 | "dev": true |
|
|
238 | 238 | }, |
|
|
239 | 239 | "is-regex": { |
|
|
240 | 240 | "version": "1.0.4", |
|
|
241 | 241 | "resolved": "https://registry.npmjs.org/is-regex/-/is-regex-1.0.4.tgz", |
|
|
242 | 242 | "integrity": "sha1-VRdIm1RwkbCTDglWVM7SXul+lJE=", |
|
|
243 | 243 | "dev": true, |
|
|
244 | 244 | "requires": { |
|
|
245 | 245 | "has": "^1.0.1" |
|
|
246 | 246 | } |
|
|
247 | 247 | }, |
|
|
248 | 248 | "is-symbol": { |
|
|
249 | 249 | "version": "1.0.2", |
|
|
250 | 250 | "resolved": "https://registry.npmjs.org/is-symbol/-/is-symbol-1.0.2.tgz", |
|
|
251 | 251 | "integrity": "sha512-HS8bZ9ox60yCJLH9snBpIwv9pYUAkcuLhSA1oero1UB5y9aiQpRA8y2ex945AOtCZL1lJDeIk3G5LthswI46Lw==", |
|
|
252 | 252 | "dev": true, |
|
|
253 | 253 | "requires": { |
|
|
254 | 254 | "has-symbols": "^1.0.0" |
|
|
255 | 255 | } |
|
|
256 | 256 | }, |
|
|
257 | 257 | "isarray": { |
|
|
258 | 258 | "version": "0.0.1", |
|
|
259 | 259 | "resolved": "https://registry.npmjs.org/isarray/-/isarray-0.0.1.tgz", |
|
|
260 | 260 | "integrity": "sha1-ihis/Kmo9Bd+Cav8YDiTmwXR7t8=", |
|
|
261 | 261 | "dev": true |
|
|
262 | 262 | }, |
|
|
263 | 263 | "jsonify": { |
|
|
264 | 264 | "version": "0.0.0", |
|
|
265 | 265 | "resolved": "https://registry.npmjs.org/jsonify/-/jsonify-0.0.0.tgz", |
|
|
266 | 266 | "integrity": "sha1-LHS27kHZPKUbe1qu6PUDYx0lKnM=", |
|
|
267 | 267 | "dev": true |
|
|
268 | 268 | }, |
|
|
269 | 269 | "minimatch": { |
|
|
270 | 270 | "version": "3.0.4", |
|
|
271 | 271 | "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", |
|
|
272 | 272 | "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", |
|
|
273 | 273 | "dev": true, |
|
|
274 | 274 | "requires": { |
|
|
275 | 275 | "brace-expansion": "^1.1.7" |
|
|
276 | 276 | } |
|
|
277 | 277 | }, |
|
|
278 | 278 | "minimist": { |
|
|
279 | 279 | "version": "0.0.5", |
|
|
280 |
"resolved": "http |
|
|
|
280 | "resolved": "http://registry.npmjs.org/minimist/-/minimist-0.0.5.tgz", | |
|
|
281 | 281 | "integrity": "sha1-16oye87PUY+RBqxrjwA/o7zqhWY=", |
|
|
282 | 282 | "dev": true |
|
|
283 | 283 | }, |
|
|
284 | 284 | "object-inspect": { |
|
|
285 | 285 | "version": "1.6.0", |
|
|
286 | 286 | "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.6.0.tgz", |
|
|
287 | 287 | "integrity": "sha512-GJzfBZ6DgDAmnuaM3104jR4s1Myxr3Y3zfIyN4z3UdqN69oSRacNK8UhnobDdC+7J2AHCjGwxQubNJfE70SXXQ==", |
|
|
288 | 288 | "dev": true |
|
|
289 | 289 | }, |
|
|
290 | 290 | "object-keys": { |
|
|
291 | 291 | "version": "0.4.0", |
|
|
292 | 292 | "resolved": "https://registry.npmjs.org/object-keys/-/object-keys-0.4.0.tgz", |
|
|
293 | 293 | "integrity": "sha1-KKaq50KN0sOpLz2V8hM13SBOAzY=", |
|
|
294 | 294 | "dev": true |
|
|
295 | 295 | }, |
|
|
296 | 296 | "once": { |
|
|
297 | 297 | "version": "1.4.0", |
|
|
298 | 298 | "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", |
|
|
299 | 299 | "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", |
|
|
300 | 300 | "dev": true, |
|
|
301 | 301 | "requires": { |
|
|
302 | 302 | "wrappy": "1" |
|
|
303 | 303 | } |
|
|
304 | 304 | }, |
|
|
305 | 305 | "path-is-absolute": { |
|
|
306 | 306 | "version": "1.0.1", |
|
|
307 | 307 | "resolved": "http://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", |
|
|
308 | 308 | "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=", |
|
|
309 | 309 | "dev": true |
|
|
310 | 310 | }, |
|
|
311 | 311 | "path-parse": { |
|
|
312 | 312 | "version": "1.0.6", |
|
|
313 | 313 | "resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.6.tgz", |
|
|
314 | 314 | "integrity": "sha512-GSmOT2EbHrINBf9SR7CDELwlJ8AENk3Qn7OikK4nFYAu3Ote2+JYNVvkpAEQm3/TLNEJFD/xZJjzyxg3KBWOzw==", |
|
|
315 | 315 | "dev": true |
|
|
316 | 316 | }, |
|
|
317 | 317 | "readable-stream": { |
|
|
318 | 318 | "version": "1.1.14", |
|
|
319 |
"resolved": "http |
|
|
|
319 | "resolved": "http://registry.npmjs.org/readable-stream/-/readable-stream-1.1.14.tgz", | |
|
|
320 | 320 | "integrity": "sha1-fPTFTvZI44EwhMY23SB54WbAgdk=", |
|
|
321 | 321 | "dev": true, |
|
|
322 | 322 | "requires": { |
|
|
323 | 323 | "core-util-is": "~1.0.0", |
|
|
324 | 324 | "inherits": "~2.0.1", |
|
|
325 | 325 | "isarray": "0.0.1", |
|
|
326 | 326 | "string_decoder": "~0.10.x" |
|
|
327 | 327 | } |
|
|
328 | 328 | }, |
|
|
329 | 329 | "requirejs": { |
|
|
330 | 330 | "version": "2.3.6", |
|
|
331 | 331 | "resolved": "https://registry.npmjs.org/requirejs/-/requirejs-2.3.6.tgz", |
|
|
332 | 332 | "integrity": "sha512-ipEzlWQe6RK3jkzikgCupiTbTvm4S0/CAU5GlgptkN5SO6F3u0UD0K18wy6ErDqiCyP4J4YYe1HuAShvsxePLg==", |
|
|
333 | 333 | "dev": true |
|
|
334 | 334 | }, |
|
|
335 | 335 | "resolve": { |
|
|
336 | 336 | "version": "1.7.1", |
|
|
337 | 337 | "resolved": "http://registry.npmjs.org/resolve/-/resolve-1.7.1.tgz", |
|
|
338 | 338 | "integrity": "sha512-c7rwLofp8g1U+h1KNyHL/jicrKg1Ek4q+Lr33AL65uZTinUZHe30D5HlyN5V9NW0JX1D5dXQ4jqW5l7Sy/kGfw==", |
|
|
339 | 339 | "dev": true, |
|
|
340 | 340 | "requires": { |
|
|
341 | 341 | "path-parse": "^1.0.5" |
|
|
342 | 342 | } |
|
|
343 | 343 | }, |
|
|
344 | 344 | "resumer": { |
|
|
345 | 345 | "version": "0.0.0", |
|
|
346 | 346 | "resolved": "https://registry.npmjs.org/resumer/-/resumer-0.0.0.tgz", |
|
|
347 | 347 | "integrity": "sha1-8ej0YeQGS6Oegq883CqMiT0HZ1k=", |
|
|
348 | 348 | "dev": true, |
|
|
349 | 349 | "requires": { |
|
|
350 | 350 | "through": "~2.3.4" |
|
|
351 | 351 | } |
|
|
352 | 352 | }, |
|
|
353 | 353 | "sprintf": { |
|
|
354 | 354 | "version": "0.1.5", |
|
|
355 | 355 | "resolved": "https://registry.npmjs.org/sprintf/-/sprintf-0.1.5.tgz", |
|
|
356 | 356 | "integrity": "sha1-j4PjmpMXwaUCy324BQ5Rxnn27c8=", |
|
|
357 | 357 | "dev": true |
|
|
358 | 358 | }, |
|
|
359 | 359 | "string.prototype.trim": { |
|
|
360 | 360 | "version": "1.1.2", |
|
|
361 | 361 | "resolved": "https://registry.npmjs.org/string.prototype.trim/-/string.prototype.trim-1.1.2.tgz", |
|
|
362 | 362 | "integrity": "sha1-0E3iyJ4Tf019IG8Ia17S+ua+jOo=", |
|
|
363 | 363 | "dev": true, |
|
|
364 | 364 | "requires": { |
|
|
365 | 365 | "define-properties": "^1.1.2", |
|
|
366 | 366 | "es-abstract": "^1.5.0", |
|
|
367 | 367 | "function-bind": "^1.0.2" |
|
|
368 | 368 | } |
|
|
369 | 369 | }, |
|
|
370 | 370 | "string_decoder": { |
|
|
371 | 371 | "version": "0.10.31", |
|
|
372 |
"resolved": "http |
|
|
|
372 | "resolved": "http://registry.npmjs.org/string_decoder/-/string_decoder-0.10.31.tgz", | |
|
|
373 | 373 | "integrity": "sha1-YuIDvEF2bGwoyfyEMB2rHFMQ+pQ=", |
|
|
374 | 374 | "dev": true |
|
|
375 | 375 | }, |
|
|
376 | 376 | "tap-parser": { |
|
|
377 | 377 | "version": "0.4.3", |
|
|
378 | 378 | "resolved": "https://registry.npmjs.org/tap-parser/-/tap-parser-0.4.3.tgz", |
|
|
379 | 379 | "integrity": "sha1-pOrhkMENdsehEZIf84u+TVjwnuo=", |
|
|
380 | 380 | "dev": true, |
|
|
381 | 381 | "requires": { |
|
|
382 | 382 | "inherits": "~2.0.1", |
|
|
383 | 383 | "readable-stream": "~1.1.11" |
|
|
384 | 384 | } |
|
|
385 | 385 | }, |
|
|
386 | 386 | "tape": { |
|
|
387 | 387 | "version": "4.9.2", |
|
|
388 | 388 | "resolved": "https://registry.npmjs.org/tape/-/tape-4.9.2.tgz", |
|
|
389 | 389 | "integrity": "sha512-lPXKRKILZ1kZaUy5ynWKs8ATGSUO7HAFHCFnBam6FaGSqPdOwMWbxXHq4EXFLE8WRTleo/YOMXkaUTRmTB1Fiw==", |
|
|
390 | 390 | "dev": true, |
|
|
391 | 391 | "requires": { |
|
|
392 | 392 | "deep-equal": "~1.0.1", |
|
|
393 | 393 | "defined": "~1.0.0", |
|
|
394 | 394 | "for-each": "~0.3.3", |
|
|
395 | 395 | "function-bind": "~1.1.1", |
|
|
396 | 396 | "glob": "~7.1.2", |
|
|
397 | 397 | "has": "~1.0.3", |
|
|
398 | 398 | "inherits": "~2.0.3", |
|
|
399 | 399 | "minimist": "~1.2.0", |
|
|
400 | 400 | "object-inspect": "~1.6.0", |
|
|
401 | 401 | "resolve": "~1.7.1", |
|
|
402 | 402 | "resumer": "~0.0.0", |
|
|
403 | 403 | "string.prototype.trim": "~1.1.2", |
|
|
404 | 404 | "through": "~2.3.8" |
|
|
405 | 405 | }, |
|
|
406 | 406 | "dependencies": { |
|
|
407 | 407 | "deep-equal": { |
|
|
408 | 408 | "version": "1.0.1", |
|
|
409 | 409 | "resolved": "https://registry.npmjs.org/deep-equal/-/deep-equal-1.0.1.tgz", |
|
|
410 | 410 | "integrity": "sha1-9dJgKStmDghO/0zbyfCK0yR0SLU=", |
|
|
411 | 411 | "dev": true |
|
|
412 | 412 | }, |
|
|
413 | 413 | "defined": { |
|
|
414 | 414 | "version": "1.0.0", |
|
|
415 | 415 | "resolved": "https://registry.npmjs.org/defined/-/defined-1.0.0.tgz", |
|
|
416 | 416 | "integrity": "sha1-yY2bzvdWdBiOEQlpFRGZ45sfppM=", |
|
|
417 | 417 | "dev": true |
|
|
418 | 418 | }, |
|
|
419 | 419 | "minimist": { |
|
|
420 | 420 | "version": "1.2.0", |
|
|
421 | 421 | "resolved": "http://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz", |
|
|
422 | 422 | "integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=", |
|
|
423 | 423 | "dev": true |
|
|
424 | 424 | } |
|
|
425 | 425 | } |
|
|
426 | 426 | }, |
|
|
427 | 427 | "through": { |
|
|
428 | 428 | "version": "2.3.8", |
|
|
429 | 429 | "resolved": "http://registry.npmjs.org/through/-/through-2.3.8.tgz", |
|
|
430 | 430 | "integrity": "sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU=", |
|
|
431 | 431 | "dev": true |
|
|
432 | 432 | }, |
|
|
433 | 433 | "through2": { |
|
|
434 | 434 | "version": "0.2.3", |
|
|
435 |
"resolved": "http |
|
|
|
435 | "resolved": "http://registry.npmjs.org/through2/-/through2-0.2.3.tgz", | |
|
|
436 | 436 | "integrity": "sha1-6zKE2k6jEbbMis42U3SKUqvyWj8=", |
|
|
437 | 437 | "dev": true, |
|
|
438 | 438 | "requires": { |
|
|
439 | 439 | "readable-stream": "~1.1.9", |
|
|
440 | 440 | "xtend": "~2.1.1" |
|
|
441 | 441 | } |
|
|
442 | 442 | }, |
|
|
443 | 443 | "tslib": { |
|
|
444 | 444 | "version": "1.9.3", |
|
|
445 | 445 | "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.9.3.tgz", |
|
|
446 | 446 | "integrity": "sha512-4krF8scpejhaOgqzBEcGM7yDIEfi0/8+8zDRZhNZZ2kjmHJ4hv3zCbQWxoJGz1iw5U0Jl0nma13xzHXcncMavQ==", |
|
|
447 | 447 | "dev": true |
|
|
448 | 448 | }, |
|
|
449 | 449 | "typescript": { |
|
|
450 | 450 | "version": "3.2.2", |
|
|
451 | 451 | "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.2.2.tgz", |
|
|
452 | 452 | "integrity": "sha512-VCj5UiSyHBjwfYacmDuc/NOk4QQixbE+Wn7MFJuS0nRuPQbof132Pw4u53dm264O8LPc2MVsc7RJNml5szurkg==", |
|
|
453 | 453 | "dev": true |
|
|
454 | 454 | }, |
|
|
455 | 455 | "wrappy": { |
|
|
456 | 456 | "version": "1.0.2", |
|
|
457 | 457 | "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", |
|
|
458 | 458 | "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=", |
|
|
459 | 459 | "dev": true |
|
|
460 | 460 | }, |
|
|
461 | 461 | "xtend": { |
|
|
462 | 462 | "version": "2.1.2", |
|
|
463 | 463 | "resolved": "https://registry.npmjs.org/xtend/-/xtend-2.1.2.tgz", |
|
|
464 | 464 | "integrity": "sha1-bv7MKk2tjmlixJAbM3znuoe10os=", |
|
|
465 | 465 | "dev": true, |
|
|
466 | 466 | "requires": { |
|
|
467 | 467 | "object-keys": "~0.4.0" |
|
|
468 | 468 | } |
|
|
469 | 469 | } |
|
|
470 | 470 | } |
|
|
471 | 471 | } |
| @@ -1,104 +1,135 | |||
|
|
1 | 1 | import * as format from "./format"; |
|
|
2 | 2 | import { TraceSource, DebugLevel } from "../log/TraceSource"; |
|
|
3 | 3 | import { ITemplateParser, TokenType } from "./TemplateParser"; |
|
|
4 | 4 | import m = require("module"); |
|
|
5 | 5 | |
|
|
6 | 6 | const trace = TraceSource.get(m.id); |
|
|
7 | 7 | |
|
|
8 | 8 | type TemplateFn = (obj: object) => string; |
|
|
9 | 9 | |
|
|
10 | const htmlEscapes = { | |
|
|
11 | "&": "&", | |
|
|
12 | "<": "<", | |
|
|
13 | ">": ">", | |
|
|
14 | '"': """, | |
|
|
15 | "'": "'", | |
|
|
16 | "/": "/" | |
|
|
17 | }; | |
|
|
18 | ||
|
|
19 | // Regex containing the keys listed immediately above. | |
|
|
20 | const htmlEscaper = /[&<>"'\/]/g; | |
|
|
21 | ||
|
|
22 | // Escape a string for HTML interpolation. | |
|
|
23 | function escapeHtml(string: any) { | |
|
|
24 | return ("" + string).replace(htmlEscaper, match => htmlEscapes[match]); | |
|
|
25 | } | |
|
|
26 | ||
|
|
10 | 27 | export class TemplateCompiler { |
|
|
11 | 28 | |
|
|
12 | 29 | _data: string[]; |
|
|
13 | 30 | _code: string[]; |
|
|
14 | 31 | _wrapWith = true; |
|
|
15 | 32 | |
|
|
16 | 33 | constructor() { |
|
|
17 | 34 | this._code = []; |
|
|
18 | 35 | this._data = []; |
|
|
19 | 36 | } |
|
|
20 | 37 | |
|
|
21 | 38 | compile(parser: ITemplateParser): TemplateFn { |
|
|
22 | 39 | this.preamble(); |
|
|
23 | 40 | this.visitTemplate(parser); |
|
|
24 | 41 | this.postamble(); |
|
|
25 | 42 | |
|
|
26 | 43 | const text = this._code.join("\n"); |
|
|
27 | 44 | |
|
|
28 | 45 | try { |
|
|
29 | 46 | // tslint:disable-next-line:function-constructor |
|
|
30 | const compiled = new Function("obj, format, $data", text); | |
|
|
47 | const compiled = new Function("obj, format, $data, escapeHtml", text); | |
|
|
31 | 48 | /** |
|
|
32 | 49 | * Функция форматирования по шаблону |
|
|
33 | 50 | * |
|
|
34 | 51 | * @type{Function} |
|
|
35 | 52 | * @param{Object} obj объект с параметрами для подстановки |
|
|
36 | 53 | */ |
|
|
37 | return (obj: object) => compiled(obj || {}, format, this._data); | |
|
|
54 | return (obj: object) => compiled(obj || {}, format, this._data, escapeHtml); | |
|
|
38 | 55 | } catch (e) { |
|
|
39 | 56 | trace.traceEvent(DebugLevel, [e, text, this._data]); |
|
|
40 | 57 | throw e; |
|
|
41 | 58 | } |
|
|
42 | 59 | } |
|
|
43 | 60 | |
|
|
44 | 61 | preamble() { |
|
|
45 | 62 | this._code.push( |
|
|
46 | 63 | "var $p = [];", |
|
|
47 | 64 | "var print = function(){", |
|
|
48 | 65 | " $p.push(format.apply(null,arguments));", |
|
|
49 | 66 | "};" |
|
|
50 | 67 | ); |
|
|
51 | 68 | |
|
|
52 | 69 | if (this._wrapWith) |
|
|
53 | 70 | this._code.push("with(obj){"); |
|
|
54 | 71 | } |
|
|
55 | 72 | |
|
|
56 | 73 | postamble() { |
|
|
57 | 74 | if (this._wrapWith) |
|
|
58 | 75 | this._code.push("}"); |
|
|
59 | 76 | |
|
|
60 | 77 | this._code.push("return $p.join('');"); |
|
|
61 | 78 | } |
|
|
62 | 79 | |
|
|
63 | 80 | visitTemplate(parser: ITemplateParser) { |
|
|
64 | 81 | while (parser.next()) { |
|
|
65 | 82 | switch (parser.token()) { |
|
|
66 | 83 | case TokenType.OpenBlock: |
|
|
67 | 84 | this.visitCode(parser); |
|
|
68 | 85 | break; |
|
|
69 | 86 | case TokenType.OpenInlineBlock: |
|
|
70 | 87 | this.visitInline(parser); |
|
|
71 | 88 | break; |
|
|
89 | case TokenType.OpenFilterBlock: | |
|
|
90 | this.visitFilter(parser); | |
|
|
91 | break; | |
|
|
72 | 92 | default: |
|
|
73 | 93 | this.visitTextFragment(parser); |
|
|
74 | 94 | break; |
|
|
75 | 95 | } |
|
|
76 | 96 | } |
|
|
77 | 97 | } |
|
|
78 | 98 | |
|
|
79 | 99 | visitInline(parser: ITemplateParser) { |
|
|
80 | 100 | const code = ["$p.push("]; |
|
|
81 | 101 | while (parser.next()) { |
|
|
82 | 102 | if (parser.token() === TokenType.CloseBlock) |
|
|
83 | 103 | break; |
|
|
84 | 104 | code.push(parser.value()); |
|
|
85 | 105 | } |
|
|
86 | 106 | code.push(");"); |
|
|
87 | 107 | this._code.push(code.join("")); |
|
|
88 | 108 | } |
|
|
89 | 109 | |
|
|
110 | visitFilter(parser: ITemplateParser) { | |
|
|
111 | const code = ["$p.push(escapeHtml("]; | |
|
|
112 | while (parser.next()) { | |
|
|
113 | if (parser.token() === TokenType.CloseBlock) | |
|
|
114 | break; | |
|
|
115 | code.push(parser.value()); | |
|
|
116 | } | |
|
|
117 | code.push("));"); | |
|
|
118 | this._code.push(code.join("")); | |
|
|
119 | } | |
|
|
120 | ||
|
|
90 | 121 | visitCode(parser: ITemplateParser) { |
|
|
91 | 122 | const code = []; |
|
|
92 | 123 | while (parser.next()) { |
|
|
93 | 124 | if (parser.token() === TokenType.CloseBlock) |
|
|
94 | 125 | break; |
|
|
95 | 126 | code.push(parser.value()); |
|
|
96 | 127 | } |
|
|
97 | 128 | this._code.push(code.join("")); |
|
|
98 | 129 | } |
|
|
99 | 130 | |
|
|
100 | 131 | visitTextFragment(parser: ITemplateParser) { |
|
|
101 | 132 | const i = this._data.push(parser.value()) - 1; |
|
|
102 | 133 | this._code.push("$p.push($data[" + i + "]);"); |
|
|
103 | 134 | } |
|
|
104 | 135 | } |
| @@ -1,69 +1,72 | |||
|
|
1 | 1 | import { argumentNotEmptyString } from "../safe"; |
|
|
2 | 2 | import { MapOf } from "../interfaces"; |
|
|
3 | 3 | import { TraceSource, DebugLevel } from "../log/TraceSource"; |
|
|
4 | 4 | import m = require("module"); |
|
|
5 | 5 | |
|
|
6 | 6 | const trace = TraceSource.get(m.id); |
|
|
7 | 7 | |
|
|
8 | const splitRx = /(<%=|\[%=|<%|\[%|%\]|%>)/; | |
|
|
8 | const splitRx = /(<%=|<%~|\[%~|\[%=|<%|\[%|%\]|%>)/; | |
|
|
9 | 9 | |
|
|
10 | 10 | export enum TokenType { |
|
|
11 | 11 | None, |
|
|
12 | 12 | Text, |
|
|
13 | 13 | OpenInlineBlock, |
|
|
14 | OpenFilterBlock, | |
|
|
14 | 15 | OpenBlock, |
|
|
15 | 16 | CloseBlock |
|
|
16 | 17 | } |
|
|
17 | 18 | |
|
|
18 | 19 | const tokenMap: MapOf<TokenType> = { |
|
|
19 | 20 | "<%": TokenType.OpenBlock, |
|
|
20 | 21 | "[%": TokenType.OpenBlock, |
|
|
21 | 22 | "<%=": TokenType.OpenInlineBlock, |
|
|
22 | 23 | "[%=": TokenType.OpenInlineBlock, |
|
|
24 | "<%~": TokenType.OpenFilterBlock, | |
|
|
25 | "[%~": TokenType.OpenFilterBlock, | |
|
|
23 | 26 | "%>": TokenType.CloseBlock, |
|
|
24 | 27 | "%]": TokenType.CloseBlock |
|
|
25 | 28 | }; |
|
|
26 | 29 | |
|
|
27 | 30 | export interface ITemplateParser { |
|
|
28 | 31 | next(): boolean; |
|
|
29 | 32 | token(): TokenType; |
|
|
30 | 33 | value(): string; |
|
|
31 | 34 | } |
|
|
32 | 35 | |
|
|
33 | 36 | export class TemplateParser implements ITemplateParser { |
|
|
34 | 37 | |
|
|
35 | 38 | _tokens: string[]; |
|
|
36 | 39 | _pos = -1; |
|
|
37 | 40 | _type: TokenType; |
|
|
38 | 41 | _value: string; |
|
|
39 | 42 | |
|
|
40 | 43 | constructor(text: string) { |
|
|
41 | 44 | argumentNotEmptyString(text, "text"); |
|
|
42 | 45 | |
|
|
43 | 46 | this._tokens = text.split(splitRx); |
|
|
44 | 47 | this._type = TokenType.None; |
|
|
45 | 48 | } |
|
|
46 | 49 | |
|
|
47 | 50 | next() { |
|
|
48 | 51 | this._pos++; |
|
|
49 | 52 | if (this._pos < this._tokens.length) { |
|
|
50 | 53 | this._value = this._tokens[this._pos]; |
|
|
51 | 54 | this._type = tokenMap[this._value] || TokenType.Text; |
|
|
52 | 55 | |
|
|
53 | 56 | return true; |
|
|
54 | 57 | } else { |
|
|
55 | 58 | this._type = TokenType.None; |
|
|
56 | 59 | this._value = undefined; |
|
|
57 | 60 | return false; |
|
|
58 | 61 | } |
|
|
59 | 62 | } |
|
|
60 | 63 | |
|
|
61 | 64 | token() { |
|
|
62 | 65 | return this._type; |
|
|
63 | 66 | } |
|
|
64 | 67 | |
|
|
65 | 68 | value() { |
|
|
66 | 69 | return this._value; |
|
|
67 | 70 | } |
|
|
68 | 71 | |
|
|
69 | 72 | } |
| @@ -1,111 +1,111 | |||
|
|
1 | 1 | export interface Constructor<T = {}> { |
|
|
2 | 2 | new(...args: any[]): T; |
|
|
3 | 3 | prototype: T; |
|
|
4 | 4 | } |
|
|
5 | 5 | |
|
|
6 | 6 | export type Factory<T = {}> = (...args: any[]) => T; |
|
|
7 | 7 | |
|
|
8 | 8 | export type Predicate<T = any> = (x: T) => boolean; |
|
|
9 | 9 | |
|
|
10 | 10 | export interface MapOf<T> { |
|
|
11 | 11 | [key: string]: T; |
|
|
12 | 12 | } |
|
|
13 | 13 | |
|
|
14 | 14 | export interface IDestroyable { |
|
|
15 | 15 | destroy(): void; |
|
|
16 | 16 | } |
|
|
17 | 17 | |
|
|
18 | 18 | export interface IRemovable { |
|
|
19 | 19 | remove(): void; |
|
|
20 | 20 | } |
|
|
21 | 21 | |
|
|
22 | 22 | export interface ICancellation { |
|
|
23 | 23 | throwIfRequested(): void; |
|
|
24 | 24 | isRequested(): boolean; |
|
|
25 | 25 | isSupported(): boolean; |
|
|
26 | 26 | register(cb: (e: any) => void): IDestroyable; |
|
|
27 | 27 | } |
|
|
28 | 28 | |
|
|
29 | 29 | /** |
|
|
30 | 30 | * Интерфейс поддерживающий асинхронную активацию |
|
|
31 | 31 | */ |
|
|
32 | 32 | export interface IActivatable { |
|
|
33 | 33 | /** |
|
|
34 | 34 | * @returns Boolean indicates the current state |
|
|
35 | 35 | */ |
|
|
36 | 36 | isActive(): boolean; |
|
|
37 | 37 | |
|
|
38 | 38 | /** |
|
|
39 | 39 | * Starts the component activation |
|
|
40 | 40 | * @param ct cancellation token for this operation |
|
|
41 | 41 | */ |
|
|
42 | 42 | activate(ct?: ICancellation): Promise<void>; |
|
|
43 | 43 | |
|
|
44 | 44 | /** |
|
|
45 | 45 | * Starts the component deactivation |
|
|
46 | 46 | * @param ct cancellation token for this operation |
|
|
47 | 47 | */ |
|
|
48 | 48 | deactivate(ct?: ICancellation): Promise<void>; |
|
|
49 | 49 | |
|
|
50 | 50 | /** |
|
|
51 | 51 | * Sets the activation controller for this component |
|
|
52 | 52 | * @param controller The activation controller |
|
|
53 | 53 | * |
|
|
54 | 54 | * Activation controller checks whether this component |
|
|
55 | 55 | * can be activated and manages the active state of the |
|
|
56 | 56 | * component |
|
|
57 | 57 | */ |
|
|
58 | 58 | setActivationController(controller: IActivationController); |
|
|
59 | 59 | |
|
|
60 | 60 | /** |
|
|
61 | 61 | * Gets the current activation controller for this component |
|
|
62 | 62 | */ |
|
|
63 | 63 | getActivationController(): IActivationController; |
|
|
64 | 64 | } |
|
|
65 | 65 | |
|
|
66 | 66 | export interface IActivationController { |
|
|
67 | 67 | activating(component: IActivatable, ct?: ICancellation): Promise<void>; |
|
|
68 | 68 | |
|
|
69 | 69 | activated(component: IActivatable, ct?: ICancellation): Promise<void>; |
|
|
70 | 70 | |
|
|
71 | 71 | deactivating(component: IActivatable, ct?: ICancellation): Promise<void>; |
|
|
72 | 72 | |
|
|
73 | 73 | deactivated(component: IActivatable, ct?: ICancellation): Promise<void>; |
|
|
74 | 74 | |
|
|
75 | 75 | deactivate(ct?: ICancellation): Promise<void>; |
|
|
76 | 76 | |
|
|
77 | 77 | activate(component: IActivatable, ct?: ICancellation): Promise<void>; |
|
|
78 | 78 | |
|
|
79 | 79 | getActive(): IActivatable; |
|
|
80 | 80 | } |
|
|
81 | 81 | |
|
|
82 | 82 | export interface IAsyncComponent { |
|
|
83 | 83 | getCompletion(): Promise<void>; |
|
|
84 | 84 | } |
|
|
85 | 85 | |
|
|
86 | 86 | export interface ICancellable { |
|
|
87 | 87 | cancel(reason?: any): void; |
|
|
88 | 88 | } |
|
|
89 | 89 | |
|
|
90 | 90 | export interface IObservable<T> { |
|
|
91 | 91 | on(next: (x: T) => void, error?: (e: any) => void, complete?: () => void): IDestroyable; |
|
|
92 | 92 | next(ct?: ICancellation): Promise<T>; |
|
|
93 | 93 | } |
|
|
94 | 94 | |
|
|
95 | 95 | export interface IObserver<T> { |
|
|
96 | 96 | next(event: T): void; |
|
|
97 | 97 | |
|
|
98 | 98 | error(e: any): void; |
|
|
99 | 99 | |
|
|
100 | 100 | complete(): void; |
|
|
101 | 101 | } |
|
|
102 | 102 | |
|
|
103 | 103 | export interface TextWriter { |
|
|
104 |
|
|
|
|
105 |
|
|
|
|
104 | write(obj: any): void; | |
|
|
105 | write(format: string, ...args: any[]): void; | |
|
|
106 | 106 | |
|
|
107 |
|
|
|
|
108 |
|
|
|
|
107 | writeLine(obj?: any): void; | |
|
|
108 | writeLine(format: string, ...args: any[]): void; | |
|
|
109 | 109 | |
|
|
110 |
|
|
|
|
110 | writeValue(value: any, spec?: string): void; | |
|
|
111 | 111 | } |
| @@ -1,134 +1,141 | |||
|
|
1 | 1 | import { Observable } from "../Observable"; |
|
|
2 | 2 | import { Registry } from "./Registry"; |
|
|
3 | import { format as _format } from "../text/StringFormat"; | |
|
|
3 | import { TraceEventData } from "./TraceEventData"; | |
|
|
4 | 4 | |
|
|
5 | 5 | export const DebugLevel = 400; |
|
|
6 | 6 | |
|
|
7 | 7 | export const LogLevel = 300; |
|
|
8 | 8 | |
|
|
9 | 9 | export const WarnLevel = 200; |
|
|
10 | 10 | |
|
|
11 | 11 | export const ErrorLevel = 100; |
|
|
12 | 12 | |
|
|
13 | 13 | export const SilentLevel = 0; |
|
|
14 | 14 | |
|
|
15 | 15 | export interface TraceEvent { |
|
|
16 | 16 | readonly source: TraceSource; |
|
|
17 | 17 | |
|
|
18 | 18 | readonly level: number; |
|
|
19 | 19 | |
|
|
20 |
readonly a |
|
|
|
21 | } | |
|
|
20 | readonly message: any; | |
|
|
22 | 21 | |
|
|
23 | function format(msg) { | |
|
|
24 | if (typeof(msg) !== "string" || arguments.length === 1) | |
|
|
25 | return msg; | |
|
|
26 | return _format.apply(null, arguments); | |
|
|
22 | readonly args?: any[]; | |
|
|
27 | 23 | } |
|
|
28 | 24 | |
|
|
29 | 25 | export class TraceSource { |
|
|
30 | 26 | readonly id: any; |
|
|
31 | 27 | |
|
|
32 | 28 | level: number; |
|
|
33 | 29 | |
|
|
34 | 30 | readonly events: Observable<TraceEvent>; |
|
|
35 | 31 | |
|
|
36 | 32 | _notifyNext: (arg: TraceEvent) => void; |
|
|
37 | 33 | |
|
|
38 | 34 | constructor(id: any) { |
|
|
39 | 35 | |
|
|
40 | 36 | this.id = id || new Object(); |
|
|
41 | 37 | this.events = new Observable(next => { |
|
|
42 | 38 | this._notifyNext = next; |
|
|
43 | 39 | }); |
|
|
44 | 40 | } |
|
|
45 | 41 | |
|
|
46 |
protected emit(level: number, a |
|
|
|
47 |
this._notifyNext( |
|
|
|
42 | protected emit(level: number, message: any, args?: any[]) { | |
|
|
43 | this._notifyNext(new TraceEventData(this, level, message, args)); | |
|
|
48 | 44 | } |
|
|
49 | 45 | |
|
|
50 | 46 | isDebugEnabled() { |
|
|
51 | 47 | return this.level >= DebugLevel; |
|
|
52 | 48 | } |
|
|
53 | 49 | |
|
|
50 | debug(data: any): void; | |
|
|
51 | debug(msg: string, ...args: any[]): void; | |
|
|
54 | 52 | debug(msg: string, ...args: any[]) { |
|
|
55 | 53 | if (this.isEnabled(DebugLevel)) |
|
|
56 |
this.emit(DebugLevel, |
|
|
|
54 | this.emit(DebugLevel, msg, args); | |
|
|
57 | 55 | } |
|
|
58 | 56 | |
|
|
59 | 57 | isLogEnabled() { |
|
|
60 | 58 | return this.level >= LogLevel; |
|
|
61 | 59 | } |
|
|
62 | 60 | |
|
|
61 | log(data: any): void; | |
|
|
62 | log(msg: string, ...args: any[]): void; | |
|
|
63 | 63 | log(msg: string, ...args: any[]) { |
|
|
64 | 64 | if (this.isEnabled(LogLevel)) |
|
|
65 |
this.emit(LogLevel, |
|
|
|
65 | this.emit(LogLevel, msg, args); | |
|
|
66 | 66 | } |
|
|
67 | 67 | |
|
|
68 | 68 | isWarnEnabled() { |
|
|
69 | 69 | return this.level >= WarnLevel; |
|
|
70 | 70 | } |
|
|
71 | 71 | |
|
|
72 | warn(data: any): void; | |
|
|
73 | warn(msg: string, ...args: any[]): void; | |
|
|
72 | 74 | warn(msg: string, ...args: any[]) { |
|
|
73 | 75 | if (this.isEnabled(WarnLevel)) |
|
|
74 |
this.emit(WarnLevel, |
|
|
|
76 | this.emit(WarnLevel, msg, args); | |
|
|
75 | 77 | } |
|
|
76 | 78 | |
|
|
77 | 79 | /** |
|
|
78 | 80 | * returns true if errors will be recorded. |
|
|
79 | 81 | */ |
|
|
80 | 82 | isErrorEnabled() { |
|
|
81 | 83 | return this.level >= ErrorLevel; |
|
|
82 | 84 | } |
|
|
83 | 85 | |
|
|
86 | /** Traces a error | |
|
|
87 | * @param data The object which will be passed to the underlying listeners | |
|
|
88 | */ | |
|
|
89 | error(data: any): void; | |
|
|
84 | 90 | /** |
|
|
85 | 91 | * Traces a error. |
|
|
86 | 92 | * |
|
|
87 | 93 | * @param msg the message. |
|
|
88 | 94 | * @param args parameters which will be substituted in the message. |
|
|
89 | 95 | */ |
|
|
96 | error(msg: string, ...args: any[]): void; | |
|
|
90 | 97 | error(msg: string, ...args: any[]) { |
|
|
91 | 98 | if (this.isEnabled(ErrorLevel)) |
|
|
92 |
this.emit(ErrorLevel, |
|
|
|
99 | this.emit(ErrorLevel, msg, args); | |
|
|
93 | 100 | } |
|
|
94 | 101 | |
|
|
95 | 102 | /** |
|
|
96 | 103 | * Checks whether the specified level is enabled for this |
|
|
97 | 104 | * trace source. |
|
|
98 | 105 | * |
|
|
99 | 106 | * @param level the trace level which should be checked. |
|
|
100 | 107 | */ |
|
|
101 | 108 | isEnabled(level: number) { |
|
|
102 | 109 | return (this.level >= level); |
|
|
103 | 110 | } |
|
|
104 | 111 | |
|
|
105 | 112 | /** |
|
|
106 | 113 | * Traces a raw event, passing data as it is to the underlying listeners |
|
|
107 | 114 | * |
|
|
108 | 115 | * @param level the level of the event |
|
|
109 |
* @param |
|
|
|
116 | * @param msg the data of the event, can be a simple string or any object. | |
|
|
110 | 117 | */ |
|
|
111 |
traceEvent(level: number, |
|
|
|
118 | traceEvent(level: number, msg: any, ...args: any[]) { | |
|
|
112 | 119 | if (this.isEnabled(level)) |
|
|
113 | this.emit(level, arg); | |
|
|
120 | this.emit(level, msg, args); | |
|
|
114 | 121 | } |
|
|
115 | 122 | |
|
|
116 | 123 | /** |
|
|
117 | 124 | * Register the specified handler to be called for every new and already |
|
|
118 | 125 | * created trace source. |
|
|
119 | 126 | * |
|
|
120 | 127 | * @param handler the handler which will be called for each trace source |
|
|
121 | 128 | */ |
|
|
122 | 129 | static on(handler: (source: TraceSource) => void) { |
|
|
123 | 130 | return Registry.instance.on(handler); |
|
|
124 | 131 | } |
|
|
125 | 132 | |
|
|
126 | 133 | /** |
|
|
127 | 134 | * Creates or returns already created trace source for the specified id. |
|
|
128 | 135 | * |
|
|
129 | 136 | * @param id the id for the trace source |
|
|
130 | 137 | */ |
|
|
131 | 138 | static get(id: any) { |
|
|
132 | 139 | return Registry.instance.get(id); |
|
|
133 | 140 | } |
|
|
134 | 141 | } |
| @@ -1,49 +1,1 | |||
|
|
1 | import { IObservable, IDestroyable, ICancellation } from "../../interfaces"; | |
|
|
2 | import { TraceEvent, LogLevel, WarnLevel, DebugLevel } from "../TraceSource"; | |
|
|
3 | import { Cancellation } from "../../Cancellation"; | |
|
|
4 | import { destroy } from "../../safe"; | |
|
|
5 | ||
|
|
6 | function hasConsole() { | |
|
|
7 | try { | |
|
|
8 | // tslint:disable-next-line:no-console | |
|
|
9 | return (typeof console !== "undefined" && typeof console.log === "function"); | |
|
|
10 | } catch { | |
|
|
11 | return false; | |
|
|
12 | } | |
|
|
13 | } | |
|
|
14 | ||
|
|
15 | export class ConsoleWriter implements IDestroyable { | |
|
|
16 | readonly _subscriptions = new Array<IDestroyable>(); | |
|
|
17 | ||
|
|
18 | writeEvents(source: IObservable<TraceEvent>, ct: ICancellation = Cancellation.none) { | |
|
|
19 | const subscription = source.on(this.writeEvent.bind(this)); | |
|
|
20 | if (ct.isSupported()) { | |
|
|
21 | ct.register(subscription.destroy.bind(subscription)); | |
|
|
22 | } | |
|
|
23 | this._subscriptions.push(subscription); | |
|
|
24 | } | |
|
|
25 | ||
|
|
26 | writeEvent(next: TraceEvent) { | |
|
|
27 | // IE will create console only when devepoler tools are activated | |
|
|
28 | if (!hasConsole()) | |
|
|
29 | return; | |
|
|
30 | ||
|
|
31 | if (next.level >= DebugLevel) { | |
|
|
32 | // tslint:disable-next-line:no-console | |
|
|
33 | console.debug(next.source.id.toString(), next.arg); | |
|
|
34 | } else if (next.level >= LogLevel) { | |
|
|
35 | // tslint:disable-next-line:no-console | |
|
|
36 | console.log(next.source.id.toString(), next.arg); | |
|
|
37 | } else if (next.level >= WarnLevel) { | |
|
|
38 | // tslint:disable-next-line:no-console | |
|
|
39 | console.warn(next.source.id.toString(), next.arg); | |
|
|
40 | } else { | |
|
|
41 | // tslint:disable-next-line:no-console | |
|
|
42 | console.error(next.source.id.toString(), next.arg); | |
|
|
43 | } | |
|
|
44 | } | |
|
|
45 | ||
|
|
46 | destroy() { | |
|
|
47 | this._subscriptions.forEach(destroy); | |
|
|
48 | } | |
|
|
49 | } | |
|
|
1 | export { ConsoleLogger as ConsoleWriter } from "./ConsoleLogger"; No newline at end of file | |
| @@ -1,101 +1,126 | |||
|
|
1 | 1 | import { FormatScanner, TokeType } from "./FormatScanner"; |
|
|
2 | import { isNullOrEmptyString } from "../safe"; | |
|
|
3 | import { TextWriter } from "../interfaces"; | |
|
|
2 | import { isNullOrEmptyString, isPrimitive, get } from "../safe"; | |
|
|
3 | import { TextWriter, MapOf } from "../interfaces"; | |
|
|
4 | ||
|
|
5 | type CompiledPattern = (writer: TextWriter, args: any) => void; | |
|
|
4 | 6 | |
|
|
5 | 7 | export class FormatCompiler { |
|
|
6 | 8 | _scanner: FormatScanner; |
|
|
9 | _cache: MapOf<CompiledPattern> = {}; | |
|
|
7 | 10 | |
|
|
8 | _parts: []; | |
|
|
11 | _parts: Array<string | { name: string; format: string; }>; | |
|
|
12 | ||
|
|
13 | compile(pattern: string) { | |
|
|
14 | let compiledPattern = this._cache && this._cache[pattern]; | |
|
|
15 | if (!compiledPattern) { | |
|
|
16 | this._scanner = new FormatScanner(pattern); | |
|
|
17 | this._parts = []; | |
|
|
18 | ||
|
|
19 | this.visitText(); | |
|
|
20 | const parts = this._parts; | |
|
|
9 | 21 | |
|
|
10 | compile() { | |
|
|
11 | return (writer: TextWriter, args: any) => { | |
|
|
12 | this._parts.forEach(x => writer.WriteValue(x)) | |
|
|
22 | compiledPattern = (writer: TextWriter, args: any) => { | |
|
|
23 | parts.forEach(x => { | |
|
|
24 | if (isPrimitive(x)) | |
|
|
25 | writer.writeValue(x); | |
|
|
26 | else | |
|
|
27 | writer.writeValue(get(x.name, args), x.format); | |
|
|
28 | }); | |
|
|
13 | 29 | }; |
|
|
30 | if (this._cache) | |
|
|
31 | this._cache[pattern] = compiledPattern; | |
|
|
32 | } | |
|
|
33 | return compiledPattern; | |
|
|
14 | 34 | } |
|
|
15 | 35 | |
|
|
16 | 36 | visitText() { |
|
|
17 | 37 | while (this._scanner.next()) { |
|
|
38 | // console.log(this._scanner.getTokenType(), this._scanner.getTokenValue()); | |
|
|
18 | 39 | switch (this._scanner.getTokenType()) { |
|
|
19 | 40 | case TokeType.CurlOpen: |
|
|
20 | 41 | this.visitCurlOpen(); |
|
|
21 | 42 | break; |
|
|
22 | 43 | case TokeType.CurlClose: |
|
|
23 | 44 | this.visitCurlClose(); |
|
|
24 | 45 | break; |
|
|
25 | 46 | default: |
|
|
26 | 47 | this.pushText(this._scanner.getTokenValue()); |
|
|
27 | 48 | } |
|
|
28 | if (this._scanner.getTokenType() === TokeType.CurlOpen) | |
|
|
29 | this.visitCurlOpen(); | |
|
|
30 | 49 | } |
|
|
31 | 50 | } |
|
|
32 | 51 | |
|
|
33 | 52 | visitCurlClose() { |
|
|
34 | 53 | if (!this._scanner.next()) |
|
|
35 | 54 | this.dieUnexpectedEnd("}"); |
|
|
36 | 55 | if (this._scanner.getTokenType() !== TokeType.CurlClose) |
|
|
37 | 56 | this.dieUnexpectedToken("}"); |
|
|
38 | 57 | this.pushText("}"); |
|
|
39 | 58 | } |
|
|
40 | 59 | |
|
|
41 | 60 | visitCurlOpen() { |
|
|
42 |
if (this._scanner.next()) |
|
|
|
61 | if (!this._scanner.next()) | |
|
|
62 | this.dieUnexpectedEnd("{ | TEXT"); | |
|
|
63 | ||
|
|
43 | 64 |
|
|
|
44 | 65 |
|
|
|
45 | 66 |
|
|
|
46 | 67 |
|
|
|
47 | } | |
|
|
68 | ||
|
|
48 | 69 | } |
|
|
49 | 70 | |
|
|
50 | 71 | visitTemplateSubst() { |
|
|
51 | 72 | if (this._scanner.getTokenType() !== TokeType.Text) |
|
|
52 | 73 | this.dieUnexpectedToken("TEXT"); |
|
|
53 | 74 | |
|
|
54 | 75 | const fieldName = this._scanner.getTokenValue(); |
|
|
55 |
const filedFormat = this.readColon() |
|
|
|
76 | const filedFormat = this.readColon() ? this.readFieldFormat() : null; | |
|
|
77 | ||
|
|
78 | if (this._scanner.getTokenType() !== TokeType.CurlClose) | |
|
|
79 | this.dieUnexpectedToken("}"); | |
|
|
56 | 80 | |
|
|
57 | 81 | this.pushSubst(fieldName, filedFormat); |
|
|
58 | 82 | } |
|
|
59 | 83 | |
|
|
60 | 84 | readFieldFormat() { |
|
|
61 | 85 | const parts = new Array<string>(); |
|
|
62 | while (this._scanner.next()) { | |
|
|
86 | do { | |
|
|
63 | 87 | if (this._scanner.getTokenType() === TokeType.CurlClose) { |
|
|
64 | 88 | return parts.join(""); |
|
|
65 | 89 | } else { |
|
|
66 | 90 | parts.push(this._scanner.getTokenValue()); |
|
|
67 | 91 | } |
|
|
68 | } | |
|
|
92 | } while (this._scanner.next()); | |
|
|
69 | 93 | |
|
|
70 | 94 | this.dieUnexpectedEnd("}"); |
|
|
71 | 95 | } |
|
|
72 | 96 | |
|
|
73 | 97 | readColon() { |
|
|
74 | 98 | if (!this._scanner.next()) |
|
|
75 | 99 | this.dieUnexpectedEnd(); |
|
|
76 | 100 | if (this._scanner.getTokenType() !== TokeType.Colon) |
|
|
77 | 101 | return false; |
|
|
78 | 102 | if (!this._scanner.next()) |
|
|
79 | 103 | this.dieUnexpectedEnd(); |
|
|
80 | 104 | return true; |
|
|
81 | 105 | } |
|
|
82 | 106 | |
|
|
83 | 107 | pushSubst(fieldName: string, filedFormat: string) { |
|
|
84 | ||
|
|
108 | // console.log("pushSubst ", fieldName, filedFormat); | |
|
|
109 | this._parts.push({ name: fieldName, format: filedFormat }); | |
|
|
85 | 110 | } |
|
|
86 | 111 | |
|
|
87 | 112 | pushText(text: string) { |
|
|
88 | ||
|
|
113 | this._parts.push(text); | |
|
|
89 | 114 | } |
|
|
90 | 115 | |
|
|
91 | 116 | dieUnexpectedToken(expected?: string) { |
|
|
92 | 117 | throw new Error(isNullOrEmptyString(expected) ? |
|
|
93 | 118 | `Unexpected token ${this._scanner.getTokenValue()}` : |
|
|
94 | 119 | `Unexpected token ${this._scanner.getTokenValue()}, expected ${expected}` |
|
|
95 | 120 | ); |
|
|
96 | 121 | } |
|
|
97 | 122 | |
|
|
98 | 123 | dieUnexpectedEnd(expected?: string) { |
|
|
99 | 124 | throw new Error(isNullOrEmptyString(expected) ? "Unexpected end of the string" : `Unexpected end of the string, expected ${expected}`); |
|
|
100 | 125 | } |
|
|
101 | 126 | } |
| @@ -1,48 +1,46 | |||
|
|
1 | 1 | import { argumentNotEmptyString } from "../safe"; |
|
|
2 | 2 | import { MapOf } from "../interfaces"; |
|
|
3 | 3 | |
|
|
4 | 4 | export const enum TokeType { |
|
|
5 | CurlOpen, | |
|
|
6 | CurlClose, | |
|
|
7 | Colon, | |
|
|
8 | Text | |
|
|
5 | CurlOpen = 1, | |
|
|
6 | CurlClose = 2, | |
|
|
7 | Colon = 3, | |
|
|
8 | Text = 4 | |
|
|
9 | 9 | } |
|
|
10 | 10 | |
|
|
11 | 11 | const typeMap = { |
|
|
12 | 12 | "{": TokeType.CurlOpen, |
|
|
13 | 13 | "}": TokeType.CurlClose, |
|
|
14 | 14 | ":": TokeType.Colon |
|
|
15 | 15 | } as MapOf<TokeType>; |
|
|
16 | 16 | |
|
|
17 | 17 | export class FormatScanner { |
|
|
18 | 18 | private _text: string; |
|
|
19 | private _pos: number; | |
|
|
20 | 19 | private _tokenType: TokeType; |
|
|
21 | 20 | private _tokenValue: string; |
|
|
22 | 21 | private _rx = /[^{}:]+|(.)/g; |
|
|
23 | 22 | |
|
|
24 | 23 | constructor(text: string) { |
|
|
25 | 24 | argumentNotEmptyString(text, text); |
|
|
26 | 25 | this._text = text; |
|
|
27 | 26 | } |
|
|
28 | 27 | |
|
|
29 | 28 | next() { |
|
|
30 | 29 | if (this._rx.lastIndex >= this._text.length) |
|
|
31 | 30 | return false; |
|
|
32 | this._pos = this._rx.lastIndex; | |
|
|
33 | 31 | |
|
|
34 | 32 | const match = this._rx.exec(this._text); |
|
|
35 | 33 | this._tokenType = typeMap[match[1]] || TokeType.Text; |
|
|
36 | 34 | this._tokenValue = match[0]; |
|
|
37 | 35 | |
|
|
38 | 36 | return true; |
|
|
39 | 37 | } |
|
|
40 | 38 | |
|
|
41 | 39 | getTokenValue() { |
|
|
42 | 40 | return this._tokenValue; |
|
|
43 | 41 | } |
|
|
44 | 42 | |
|
|
45 | 43 | getTokenType() { |
|
|
46 | 44 | return this._tokenType; |
|
|
47 | 45 | } |
|
|
48 | 46 | } |
| @@ -1,18 +1,31 | |||
|
|
1 | export class StringBuilder { | |
|
|
2 | private _data: string[]; | |
|
|
3 | private _newLine = "\n"; | |
|
|
1 | import { TextWriterBase } from "./TextWriterBase"; | |
|
|
2 | import { Converter } from "./Converter"; | |
|
|
3 | ||
|
|
4 | export class StringBuilder extends TextWriterBase { | |
|
|
5 | private _data = new Array<string>(); | |
|
|
4 | 6 | |
|
|
5 | Write(obj: any); | |
|
|
6 | Write(format: string, ...args: any[]) { | |
|
|
7 | constructor(converter = Converter.default) { | |
|
|
8 | super(converter); | |
|
|
9 | } | |
|
|
7 | 10 | |
|
|
11 | writeText(text: string) { | |
|
|
12 | this._data.push(text); | |
|
|
8 | 13 | } |
|
|
9 | 14 | |
|
|
10 | WriteLine(obj: any); | |
|
|
11 | WriteLine(format: string, ...args: any[]) { | |
|
|
15 | toString() { | |
|
|
16 | return this._data.join(""); | |
|
|
17 | } | |
|
|
12 | 18 | |
|
|
19 | clear() { | |
|
|
20 | this._data.length = 0; | |
|
|
21 | } | |
|
|
13 | 22 | } |
|
|
14 | 23 | |
|
|
15 | WriteValue(value: any, spec?: string) { | |
|
|
24 | const sb = new StringBuilder(); | |
|
|
16 | 25 | |
|
|
26 | export function format(format: string, ...args: any): string; | |
|
|
27 | export function format() { | |
|
|
28 | sb.clear(); | |
|
|
29 | sb.write.apply(sb, arguments); | |
|
|
30 | return sb.toString(); | |
|
|
17 | 31 | } |
|
|
18 | } | |
| @@ -1,60 +1,54 | |||
|
|
1 | import * as tape from "tape"; | |
|
|
2 | 1 |
|
|
|
3 | 2 | import { SimpleActivatable } from "./mock/SimpleActivatable"; |
|
|
3 | import { test } from "./TestTraits"; | |
|
|
4 | 4 | |
|
|
5 |
t |
|
|
|
5 | test("simple activation", async t => { | |
|
|
6 | 6 | |
|
|
7 | 7 | const a = new SimpleActivatable(); |
|
|
8 | 8 | t.false(a.isActive()); |
|
|
9 | 9 | |
|
|
10 | 10 | await a.activate(); |
|
|
11 | 11 | t.true(a.isActive()); |
|
|
12 | 12 | |
|
|
13 | 13 | await a.deactivate(); |
|
|
14 | 14 | t.false(a.isActive()); |
|
|
15 | ||
|
|
16 | t.end(); | |
|
|
17 | 15 | }); |
|
|
18 | 16 | |
|
|
19 |
t |
|
|
|
17 | test("controller activation", async t => { | |
|
|
20 | 18 | |
|
|
21 | 19 | const a = new SimpleActivatable(); |
|
|
22 | 20 | const c = new MockActivationController(); |
|
|
23 | 21 | |
|
|
24 | 22 | t.false(a.isActive(), "the component is not active by default"); |
|
|
25 | 23 | t.assert(c.getActive() == null, "the activation controller doesn't have an active component by default"); |
|
|
26 | 24 | t.assert(a.getActivationController() == null, "the component doesn't have an activation controller by default"); |
|
|
27 | 25 | |
|
|
28 | 26 | t.comment("Active the component through the controller"); |
|
|
29 | 27 | await c.activate(a); |
|
|
30 | 28 | t.true(a.isActive(), "The component should successfully activate"); |
|
|
31 | 29 | t.equal(c.getActive(), a, "The controller should point to the activated component"); |
|
|
32 | 30 | t.equal(a.getActivationController(), c, "The component should point to the controller"); |
|
|
33 | 31 | |
|
|
34 | 32 | t.comment("Deactive the component throug the controller"); |
|
|
35 | 33 | await c.deactivate(); |
|
|
36 | 34 | |
|
|
37 | 35 | t.false(a.isActive(), "The component should successfully deactivate"); |
|
|
38 | 36 | t.equal(c.getActive(), null, "The controller shouldn't point to any component"); |
|
|
39 | 37 | t.equal(a.getActivationController(), c, "The componet should point to it's controller"); |
|
|
40 | ||
|
|
41 | t.end(); | |
|
|
42 | 38 | }); |
|
|
43 | 39 | |
|
|
44 |
t |
|
|
|
40 | test("handle error in onActivating", async t => { | |
|
|
45 | 41 | const a = new SimpleActivatable(); |
|
|
46 | 42 | |
|
|
47 | 43 | a.onActivating = async () => { |
|
|
48 | 44 | throw new Error("Should fail"); |
|
|
49 | 45 | }; |
|
|
50 | 46 | |
|
|
51 | 47 | try { |
|
|
52 | 48 | await a.activate(); |
|
|
53 | 49 | t.fail("activation should fail"); |
|
|
54 | 50 | } catch { |
|
|
55 | 51 | } |
|
|
56 | 52 | |
|
|
57 | 53 | t.false(a.isActive(), "the component should remain inactive"); |
|
|
58 | ||
|
|
59 | t.end(); | |
|
|
60 | 54 | }); |
| @@ -1,96 +1,88 | |||
|
|
1 | import * as tape from "tape"; | |
|
|
2 | 1 |
|
|
|
3 | 2 | import { delay } from "@implab/core/safe"; |
|
|
3 | import { test } from "./TestTraits"; | |
|
|
4 | 4 | |
|
|
5 |
t |
|
|
|
5 | test("standalone cancellation", async t => { | |
|
|
6 | 6 | |
|
|
7 | 7 | let doCancel: (e) => void; |
|
|
8 | 8 | |
|
|
9 | 9 | const ct = new Cancellation(cancel => { |
|
|
10 | 10 | doCancel = cancel; |
|
|
11 | 11 | }); |
|
|
12 | 12 | |
|
|
13 | 13 | let counter = 0; |
|
|
14 | 14 | const reason = "BILL"; |
|
|
15 | 15 | |
|
|
16 | 16 | t.true(ct.isSupported(), "Cancellation must be supported"); |
|
|
17 | 17 | t.false(ct.isRequested(), "Cancellation shouldn't be requested"); |
|
|
18 | 18 | ct.throwIfRequested(); |
|
|
19 | 19 | t.pass("The exception shouldn't be thrown unless the cancellation is requested"); |
|
|
20 | 20 | |
|
|
21 | 21 | ct.register(() => counter++); |
|
|
22 | 22 | t.equals(counter, 0, "counter should be zero"); |
|
|
23 | 23 | |
|
|
24 | 24 | ct.register(() => counter++).destroy(); |
|
|
25 | 25 | |
|
|
26 | 26 | doCancel(reason); |
|
|
27 | 27 | |
|
|
28 | 28 | t.true(ct.isRequested(), "Cancellation should be requested"); |
|
|
29 | 29 | t.equals(counter, 1, "The registered callback should be triggered"); |
|
|
30 | 30 | |
|
|
31 | 31 | ct.register(() => counter++); |
|
|
32 | 32 | t.equals(counter, 2, "The callback should be triggered immediately"); |
|
|
33 | 33 | |
|
|
34 | 34 | let msg; |
|
|
35 | 35 | ct.register(e => msg = e); |
|
|
36 | 36 | t.equals(msg, reason, "The cancellation reason should be passed to callback"); |
|
|
37 | 37 | |
|
|
38 | 38 | try { |
|
|
39 | 39 | msg = null; |
|
|
40 | 40 | ct.throwIfRequested(); |
|
|
41 | 41 | t.fail("The exception should be thrown"); |
|
|
42 | 42 | } catch (e) { |
|
|
43 | 43 | msg = e; |
|
|
44 | 44 | } |
|
|
45 | 45 | t.equals(msg, reason, "The cancellation reason should be catched"); |
|
|
46 | ||
|
|
47 | t.end(); | |
|
|
48 | 46 | }); |
|
|
49 | 47 | |
|
|
50 |
t |
|
|
|
48 | test("async cancellation", async t => { | |
|
|
51 | 49 | |
|
|
52 | 50 | const ct = new Cancellation(cancel => { |
|
|
53 | 51 | cancel("STOP!"); |
|
|
54 | 52 | }); |
|
|
55 | 53 | |
|
|
56 | 54 | try { |
|
|
57 | 55 | await delay(0, ct); |
|
|
58 | 56 | t.fail("Should thow the exception"); |
|
|
59 | 57 | } catch (e) { |
|
|
60 | 58 | t.equals(e, "STOP!", "Should throw the cancellation reason"); |
|
|
61 | 59 | } |
|
|
62 | ||
|
|
63 | t.end(); | |
|
|
64 | 60 | }); |
|
|
65 | 61 | |
|
|
66 |
t |
|
|
|
62 | test("cancel with external event", async t => { | |
|
|
67 | 63 | const ct = new Cancellation(cancel => { |
|
|
68 | 64 | setTimeout(x => cancel("STOP!"), 0); |
|
|
69 | 65 | }); |
|
|
70 | 66 | |
|
|
71 | 67 | try { |
|
|
72 | 68 | await delay(10000, ct); |
|
|
73 | 69 | t.fail("Should thow the exception"); |
|
|
74 | 70 | } catch (e) { |
|
|
75 | 71 | t.equals(e, "STOP!", "Should throw the cancellation reason"); |
|
|
76 | 72 | } |
|
|
77 | ||
|
|
78 | t.end(); | |
|
|
79 | 73 | }); |
|
|
80 | 74 | |
|
|
81 |
t |
|
|
|
75 | test("operation normal flow", async t => { | |
|
|
82 | 76 | |
|
|
83 | 77 | let htimeout; |
|
|
84 | 78 | const ct = new Cancellation(cancel => { |
|
|
85 | 79 | htimeout = setTimeout(() => cancel("STOP!"), 1000); |
|
|
86 | 80 | }); |
|
|
87 | 81 | |
|
|
88 | 82 | try { |
|
|
89 | 83 | await delay(0, ct); |
|
|
90 | 84 | t.pass("Should pass"); |
|
|
91 | 85 | } finally { |
|
|
92 | 86 | clearTimeout(htimeout); |
|
|
93 | 87 | } |
|
|
94 | ||
|
|
95 | t.end(); | |
|
|
96 | 88 | }); |
| @@ -1,73 +1,69 | |||
|
|
1 | 1 | import { TraceSource, DebugLevel } from "@implab/core/log/TraceSource"; |
|
|
2 | import * as tape from "tape"; | |
|
|
3 | 2 | import { Observable } from "@implab/core/Observable"; |
|
|
4 | 3 | import { IObservable } from "@implab/core/interfaces"; |
|
|
5 | 4 | import { delay } from "@implab/core/safe"; |
|
|
5 | import { test } from "./TestTraits"; | |
|
|
6 | 6 | |
|
|
7 | 7 | const trace = TraceSource.get("ObservableTests"); |
|
|
8 | 8 | |
|
|
9 |
t |
|
|
|
9 | test("events sequence example", async t => { | |
|
|
10 | 10 | |
|
|
11 | 11 | let events: IObservable<number>; |
|
|
12 | 12 | |
|
|
13 | 13 | const done = new Promise<void>(resolve => { |
|
|
14 | 14 | events = new Observable<number>(async (notify, fail, finish) => { |
|
|
15 | 15 | for (let i = 0; i < 10; i++) { |
|
|
16 | 16 | await delay(0); |
|
|
17 | 17 | notify(i); |
|
|
18 | 18 | } |
|
|
19 | 19 | finish(); |
|
|
20 | 20 | resolve(); |
|
|
21 | 21 | }); |
|
|
22 | 22 | }); |
|
|
23 | 23 | |
|
|
24 | 24 | let count = 0; |
|
|
25 | 25 | let complete = false; |
|
|
26 | 26 | events.on(x => count = count + x, null, () => complete = true); |
|
|
27 | 27 | |
|
|
28 | 28 | const first = await events.next(); |
|
|
29 | 29 | |
|
|
30 | 30 | t.equals(first, 0, "the first event"); |
|
|
31 | 31 | t.false(complete, "the sequence is not complete"); |
|
|
32 | 32 | |
|
|
33 | 33 | await done; |
|
|
34 | 34 | |
|
|
35 | 35 | t.equals(count, 45, "the summ of the evetns"); |
|
|
36 | 36 | t.true(complete, "the sequence is complete"); |
|
|
37 | ||
|
|
38 | t.end(); | |
|
|
39 | 37 | }); |
|
|
40 | 38 | |
|
|
41 |
t |
|
|
|
39 | test("event sequence termination", async t => { | |
|
|
42 | 40 | let events: IObservable<number>; |
|
|
43 | 41 | |
|
|
44 | 42 | const done = new Promise<void>(resolve => { |
|
|
45 | 43 | events = new Observable<number>(async (notify, fail, complete) => { |
|
|
46 | 44 | await delay(0); |
|
|
47 | 45 | notify(1); |
|
|
48 | 46 | complete(); |
|
|
49 | 47 | notify(2); |
|
|
50 | 48 | complete(); |
|
|
51 | 49 | fail("Sequence terminated"); |
|
|
52 | 50 | resolve(); |
|
|
53 | 51 | }); |
|
|
54 | 52 | }); |
|
|
55 | 53 | |
|
|
56 | 54 | let count = 0; |
|
|
57 | 55 | events.on(() => {}, e => count++, () => count++); |
|
|
58 | 56 | |
|
|
59 | 57 | const first = await events.next(); |
|
|
60 | 58 | t.equals(first, 1, "the first message"); |
|
|
61 | 59 | try { |
|
|
62 | 60 | await events.next(); |
|
|
63 | 61 | t.fail("shoud throw an exception"); |
|
|
64 | 62 | } catch (e) { |
|
|
65 | 63 | t.pass("the sequence is terminated"); |
|
|
66 | 64 | } |
|
|
67 | 65 | |
|
|
68 | 66 | await done; |
|
|
69 | 67 | |
|
|
70 | 68 | t.equals(count, 1, "the sequence must be terminated once"); |
|
|
71 | ||
|
|
72 | t.end(); | |
|
|
73 | 69 | }); |
| @@ -1,99 +1,95 | |||
|
|
1 | import tape = require("tape"); | |
|
|
2 | 1 |
|
|
|
3 | 2 | import { first, isPromise, firstWhere, delay, nowait } from "@implab/core/safe"; |
|
|
3 | import { test } from "./TestTraits"; | |
|
|
4 | 4 | |
|
|
5 |
t |
|
|
|
5 | test("await delay test", async t => { | |
|
|
6 | 6 | // schedule delay |
|
|
7 | 7 | let resolved = false; |
|
|
8 | 8 | let res = delay(0).then(() => resolved = true); |
|
|
9 | 9 | |
|
|
10 | 10 | t.false(resolved, "the delay should be async"); |
|
|
11 | 11 | |
|
|
12 | 12 | await res; |
|
|
13 | 13 | t.pass("await delay"); |
|
|
14 | 14 | |
|
|
15 | 15 | // create cancellation token |
|
|
16 | 16 | let cancel: (e?: any) => void; |
|
|
17 | 17 | const ct = new Cancellation(c => cancel = c); |
|
|
18 | 18 | |
|
|
19 | 19 | // schedule delay |
|
|
20 | 20 | resolved = false; |
|
|
21 | 21 | res = delay(0, ct).then(() => resolved = true); |
|
|
22 | 22 | |
|
|
23 | 23 | t.false(resolved, "created delay with ct"); |
|
|
24 | 24 | |
|
|
25 | 25 | // cancel |
|
|
26 | 26 | cancel(); |
|
|
27 | 27 | |
|
|
28 | 28 | try { |
|
|
29 | 29 | await res; |
|
|
30 | 30 | t.fail("the delay should fail when it is cancelled"); |
|
|
31 | 31 | } catch { |
|
|
32 | 32 | t.pass("the delay is cancelled"); |
|
|
33 | 33 | } |
|
|
34 | 34 | |
|
|
35 | 35 | t.throws(() => { |
|
|
36 | 36 | // try schedule delay after the cancellation is requested |
|
|
37 | 37 | nowait(delay(0, ct)); |
|
|
38 | 38 | }, "Should throw if cancelled before start"); |
|
|
39 | ||
|
|
40 | t.end(); | |
|
|
41 | 39 | }); |
|
|
42 | 40 | |
|
|
43 |
t |
|
|
|
41 | test("sequemce test", async t => { | |
|
|
44 | 42 | const sequence = ["a", "b", "c"]; |
|
|
45 | 43 | const empty = []; |
|
|
46 | 44 | |
|
|
47 | 45 | // synchronous tests |
|
|
48 | 46 | t.equals(first(sequence), "a", "Should return the first element"); |
|
|
49 | 47 | t.equals(firstWhere(sequence, x => x === "b"), "b", "Should get the second element"); |
|
|
50 | 48 | |
|
|
51 | 49 | let v: string; |
|
|
52 | 50 | let e: Error; |
|
|
53 | 51 | first(sequence, x => v = x); |
|
|
54 | 52 | t.equal(v, "a", "The callback should be called for the first element"); |
|
|
55 | 53 | firstWhere(sequence, x => x === "b", x => v = x); |
|
|
56 | 54 | t.equal(v, "b", "The callback should be called for the second element"); |
|
|
57 | 55 | |
|
|
58 | 56 | t.throws(() => { |
|
|
59 | 57 | first(empty); |
|
|
60 | 58 | }, "Should throw when the sequence is empty"); |
|
|
61 | 59 | |
|
|
62 | 60 | t.throws(() => { |
|
|
63 | 61 | firstWhere(empty, x => x === "b"); |
|
|
64 | 62 | }, "Should throw when the sequence is empty"); |
|
|
65 | 63 | |
|
|
66 | 64 | t.throws(() => { |
|
|
67 | 65 | first(empty, x => v = x); |
|
|
68 | 66 | }, "Should throw when the sequence is empty"); |
|
|
69 | 67 | |
|
|
70 | 68 | t.throws(() => { |
|
|
71 | 69 | firstWhere(empty, x => x === "b", x => v = x); |
|
|
72 | 70 | }, "Should throw when the sequence is empty"); |
|
|
73 | 71 | |
|
|
74 | 72 | t.throws(() => { |
|
|
75 | 73 | firstWhere(sequence, x => x === "z"); |
|
|
76 | 74 | }, "Should throw when the element isn't found"); |
|
|
77 | 75 | |
|
|
78 | 76 | t.throws(() => { |
|
|
79 | 77 | firstWhere(sequence, x => x === "z", x => v = x); |
|
|
80 | 78 | }, "Should throw when the element isn't found"); |
|
|
81 | 79 | |
|
|
82 | 80 | first(empty, null, x => e = x); |
|
|
83 | 81 | t.true(e, "The errorback should be called for the empty sequence"); |
|
|
84 | 82 | |
|
|
85 | 83 | // async tests |
|
|
86 | 84 | const asyncSequence = Promise.resolve(sequence); |
|
|
87 | 85 | const asyncEmptySequence = Promise.resolve(empty); |
|
|
88 | 86 | |
|
|
89 | 87 | const promise = first(asyncSequence); |
|
|
90 | 88 | t.true(isPromise(promise), "Should return promise"); |
|
|
91 | 89 | |
|
|
92 | 90 | v = await promise; |
|
|
93 | 91 | t.equal(v, "a", "Should return the first element"); |
|
|
94 | 92 | |
|
|
95 | 93 | v = await new Promise(resolve => first(asyncSequence, resolve)); |
|
|
96 | 94 | t.equal(v, "a", "The callback should be called for the first element"); |
|
|
97 | ||
|
|
98 | t.end(); | |
|
|
99 | 95 | }); |
| @@ -1,65 +1,74 | |||
|
|
1 | 1 | import { IObservable, ICancellation, IDestroyable } from "@implab/core/interfaces"; |
|
|
2 | 2 | import { Cancellation } from "@implab/core/Cancellation"; |
|
|
3 | 3 | import { TraceEvent, LogLevel, WarnLevel, DebugLevel, TraceSource } from "@implab/core/log/TraceSource"; |
|
|
4 | 4 | import * as tape from "tape"; |
|
|
5 | 5 | import { argumentNotNull, destroy } from "@implab/core/safe"; |
|
|
6 | 6 | |
|
|
7 | 7 | export class TapeWriter implements IDestroyable { |
|
|
8 | readonly _tape: tape.Test; | |
|
|
8 | private readonly _tape: tape.Test; | |
|
|
9 | 9 | |
|
|
10 | _subscriptions = new Array<IDestroyable>(); | |
|
|
10 | private readonly _subscriptions = new Array<IDestroyable>(); | |
|
|
11 | private _destroyed; | |
|
|
11 | 12 | |
|
|
12 | 13 | constructor(t: tape.Test) { |
|
|
13 | 14 | argumentNotNull(t, "tape"); |
|
|
14 | 15 | this._tape = t; |
|
|
15 | 16 | } |
|
|
16 | 17 | |
|
|
17 | 18 | writeEvents(source: IObservable<TraceEvent>, ct: ICancellation = Cancellation.none) { |
|
|
19 | if (!this._destroyed) { | |
|
|
18 | 20 | const subscription = source.on(this.writeEvent.bind(this)); |
|
|
19 | 21 | if (ct.isSupported()) { |
|
|
20 | 22 | ct.register(subscription.destroy.bind(subscription)); |
|
|
21 | 23 | } |
|
|
22 | 24 | this._subscriptions.push(subscription); |
|
|
23 | 25 | } |
|
|
26 | } | |
|
|
24 | 27 | |
|
|
25 | 28 | writeEvent(next: TraceEvent) { |
|
|
26 | 29 | if (next.level >= DebugLevel) { |
|
|
27 |
this._tape.comment(`DEBUG ${next.source.id} ${next |
|
|
|
30 | this._tape.comment(`DEBUG ${next.source.id} ${next}`); | |
|
|
28 | 31 | } else if (next.level >= LogLevel) { |
|
|
29 |
this._tape.comment(`LOG ${next.source.id} ${next |
|
|
|
32 | this._tape.comment(`LOG ${next.source.id} ${next}`); | |
|
|
30 | 33 | } else if (next.level >= WarnLevel) { |
|
|
31 |
this._tape.comment(`WARN ${next.source.id} ${next |
|
|
|
34 | this._tape.comment(`WARN ${next.source.id} ${next}`); | |
|
|
32 | 35 | } else { |
|
|
33 |
this._tape.comment(`ERROR ${next.source.id} ${next |
|
|
|
36 | this._tape.comment(`ERROR ${next.source.id} ${next}`); | |
|
|
34 | 37 | } |
|
|
35 | 38 | } |
|
|
36 | 39 | |
|
|
37 | 40 | destroy() { |
|
|
38 | 41 | this._subscriptions.forEach(destroy); |
|
|
39 | 42 | } |
|
|
40 | 43 | } |
|
|
41 | 44 | |
|
|
42 | export function test(name: string, cb: (t: tape.Test) => any) { | |
|
|
45 | export function test(name: string, cb: (t: tape.Test, trace: TraceSource) => any) { | |
|
|
43 | 46 | tape(name, async t => { |
|
|
44 | 47 | const writer = new TapeWriter(t); |
|
|
45 | 48 | |
|
|
46 | TraceSource.on(ts => { | |
|
|
49 | // this trace is not announced through the TraceSource global registry | |
|
|
50 | const trace = new TraceSource(name); | |
|
|
51 | trace.level = DebugLevel; | |
|
|
52 | writer.writeEvents(trace.events); | |
|
|
53 | ||
|
|
54 | const h = TraceSource.on(ts => { | |
|
|
47 | 55 | ts.level = DebugLevel; |
|
|
48 | 56 | writer.writeEvents(ts.events); |
|
|
49 | 57 | }); |
|
|
50 | 58 | |
|
|
51 | 59 | try { |
|
|
52 | await cb(t); | |
|
|
60 | await cb(t, trace); | |
|
|
53 | 61 | } catch (e) { |
|
|
54 | 62 | |
|
|
55 | 63 | // verbose error information |
|
|
56 | 64 | // tslint:disable-next-line |
|
|
57 | 65 | console.error(e); |
|
|
58 | 66 | t.fail(e); |
|
|
59 | 67 | |
|
|
60 | 68 | } finally { |
|
|
61 | 69 | t.end(); |
|
|
62 | 70 | destroy(writer); |
|
|
71 | destroy(h); | |
|
|
63 | 72 | } |
|
|
64 | 73 | }); |
|
|
65 | 74 | } |
| @@ -1,69 +1,86 | |||
|
|
1 | 1 | import { TraceSource, DebugLevel } from "@implab/core/log/TraceSource"; |
|
|
2 | 2 | import * as tape from "tape"; |
|
|
3 | import { TapeWriter } from "./TestTraits"; | |
|
|
3 | import { TapeWriter, test } from "./TestTraits"; | |
|
|
4 | import { MockConsole } from "./mock/MockConsole"; | |
|
|
5 | import { ConsoleLog } from "@implab/core/log/writers/ConsoleLog"; | |
|
|
6 | import { ConsoleWriter } from "@implab/core/log/ConsoleWriter"; | |
|
|
4 | 7 | |
|
|
5 | 8 | const sourceId = "test/TraceSourceTests"; |
|
|
6 | 9 | |
|
|
7 | 10 | tape("trace message", t => { |
|
|
8 | 11 | const trace = TraceSource.get(sourceId); |
|
|
9 | 12 | |
|
|
10 | 13 | trace.level = DebugLevel; |
|
|
11 | 14 | |
|
|
12 | 15 | const h = trace.events.on(ev => { |
|
|
13 | 16 | t.equal(ev.source, trace, "sender should be the current trace source"); |
|
|
14 | 17 | t.equal(ev.level, DebugLevel, "level should be debug level"); |
|
|
15 |
t.equal(ev. |
|
|
|
18 | t.equal(ev.toString(), "Hello, World!", "The message should be a formatted message"); | |
|
|
16 | 19 | |
|
|
17 | 20 | t.end(); |
|
|
18 | 21 | }); |
|
|
19 | 22 | |
|
|
20 | 23 | trace.debug("Hello, {0}!", "World"); |
|
|
21 | 24 | |
|
|
22 | 25 | h.destroy(); |
|
|
23 | 26 | }); |
|
|
24 | 27 | |
|
|
25 | 28 | tape("trace event", t => { |
|
|
26 | 29 | const trace = TraceSource.get(sourceId); |
|
|
27 | 30 | |
|
|
28 | 31 | trace.level = DebugLevel; |
|
|
29 | 32 | |
|
|
30 | 33 | const event = { |
|
|
31 | 34 | name: "custom event" |
|
|
32 | 35 | }; |
|
|
33 | 36 | |
|
|
34 | 37 | const h = trace.events.on(ev => { |
|
|
35 | 38 | t.equal(ev.source, trace, "sender should be the current trace source"); |
|
|
36 | 39 | t.equal(ev.level, DebugLevel, "level should be debug level"); |
|
|
37 |
t.equal(ev.a |
|
|
|
40 | t.equal(ev.message, event, "The message should be the specified object"); | |
|
|
38 | 41 | |
|
|
39 | 42 | t.end(); |
|
|
40 | 43 | }); |
|
|
41 | 44 | |
|
|
42 | 45 | trace.traceEvent(DebugLevel, event); |
|
|
43 | 46 | |
|
|
44 | 47 | h.destroy(); |
|
|
45 | 48 | }); |
|
|
46 | 49 | |
|
|
47 | 50 | tape("tape comment writer", async t => { |
|
|
48 | 51 | const writer = new TapeWriter(t); |
|
|
49 | 52 | |
|
|
50 | 53 | TraceSource.on(ts => { |
|
|
51 | 54 | writer.writeEvents(ts.events); |
|
|
52 | 55 | }); |
|
|
53 | 56 | |
|
|
54 | 57 | const trace = TraceSource.get(sourceId); |
|
|
55 | 58 | trace.level = DebugLevel; |
|
|
56 | 59 | |
|
|
57 | 60 | trace.log("Hello, {0}!", "World"); |
|
|
58 | 61 | trace.log("Multi\n line"); |
|
|
59 | 62 | trace.warn("Look at me!"); |
|
|
60 | 63 | trace.error("DIE!"); |
|
|
61 | 64 | |
|
|
62 | 65 | writer.destroy(); |
|
|
63 | 66 | |
|
|
64 | 67 | trace.log("You shouldn't see it!"); |
|
|
65 | 68 | |
|
|
66 | 69 | t.comment("DONE"); |
|
|
67 | 70 | |
|
|
68 | 71 | t.end(); |
|
|
69 | 72 | }); |
|
|
73 | ||
|
|
74 | test("console writer", (t, trace) => { | |
|
|
75 | ||
|
|
76 | const mockConsole = new MockConsole(); | |
|
|
77 | const writer = new ConsoleWriter(mockConsole); | |
|
|
78 | const consoleLog = new ConsoleLog(writer); | |
|
|
79 | consoleLog.writeEvents(trace.events); | |
|
|
80 | ||
|
|
81 | trace.log("Hello, world!"); | |
|
|
82 | t.deepEqual(mockConsole.getLine(0), ["console writer: Hello, world!"], "Log one string"); | |
|
|
83 | ||
|
|
84 | trace.log({ foo: "bar" }); | |
|
|
85 | t.deepEqual(mockConsole.getLine(1), ["console writer: ", { foo: "bar" }], "Log an object"); | |
|
|
86 | }); | |
General Comments 0
You need to be logged in to leave comments.
Login now
