@everyonesoftware/common 1.0.0 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/publish.yml +54 -38
- package/package.json +9 -2
- package/sources/asyncIterator.ts +436 -436
- package/sources/asyncIteratorToJavascriptAsyncIteratorAdapter.ts +47 -47
- package/sources/asyncResult.ts +95 -95
- package/sources/byteList.ts +201 -201
- package/sources/byteListStream.ts +120 -120
- package/sources/byteReadStream.ts +23 -23
- package/sources/byteWriteStream.ts +15 -15
- package/sources/characterList.ts +194 -194
- package/sources/characterListStream.ts +150 -150
- package/sources/characterReadStream.ts +80 -80
- package/sources/characterReadStreamIterator.ts +127 -127
- package/sources/concatenateIterable.ts +118 -118
- package/sources/concatenateIterator.ts +164 -164
- package/sources/currentProcess.ts +157 -157
- package/sources/dateTime.ts +129 -129
- package/sources/depthFirstSearch.ts +229 -229
- package/sources/flatMapIterable.ts +103 -103
- package/sources/flatMapIterator.ts +151 -151
- package/sources/generator.ts +250 -250
- package/sources/index.ts +1 -1
- package/sources/iterator.ts +480 -480
- package/sources/javascriptAsyncIteratorToAsyncIteratorAdapter.ts +123 -123
- package/sources/javascriptSetSet.ts +133 -133
- package/sources/listQueue.ts +61 -61
- package/sources/listStack.ts +61 -61
- package/sources/luxonDateTime.ts +108 -108
- package/sources/mapAsyncIterator.ts +140 -140
- package/sources/mutableMap.ts +291 -291
- package/sources/node.ts +36 -36
- package/sources/promiseAsyncResult.ts +173 -173
- package/sources/queue.ts +48 -48
- package/sources/recreationDotGovClient.ts +258 -258
- package/sources/searchControl.ts +41 -41
- package/sources/set.ts +243 -243
- package/sources/skipAsyncIterator.ts +144 -144
- package/sources/stack.ts +47 -47
- package/sources/syncResult.ts +299 -299
- package/sources/takeAsyncIterator.ts +140 -140
- package/sources/whereAsyncIterator.ts +142 -142
- package/sources/wonderlandTrailClient.ts +1502 -1502
- package/tests/assertTestTests.ts +74 -74
- package/tests/byteListStreamTests.ts +389 -389
- package/tests/byteListTests.ts +26 -26
- package/tests/characterListStreamTests.ts +390 -390
- package/tests/characterListTests.ts +249 -249
- package/tests/dateTimeTests.ts +29 -29
- package/tests/depthFirstSearchTests.ts +105 -105
- package/tests/generatorTests.ts +85 -85
- package/tests/mutableMapTests.ts +153 -153
- package/tests/promiseAsyncResultTests.ts +687 -687
- package/tests/queueTests.ts +28 -28
- package/tests/recreationDotGovClientTests.ts +190 -190
- package/tests/setTests.ts +139 -139
- package/tests/stackTests.ts +65 -65
- package/tests/syncResultTests.ts +1250 -1250
- package/tests/wonderlandTrailClientTests.ts +451 -451
- package/tsconfig.json +3 -0
- package/tsup.config.ts +12 -12
|
@@ -1,165 +1,165 @@
|
|
|
1
|
-
import { Iterator } from "./iterator";
|
|
2
|
-
import { JavascriptIterable, JavascriptIterator } from "./javascript";
|
|
3
|
-
import { List } from "./list";
|
|
4
|
-
import { PreCondition } from "./preCondition";
|
|
5
|
-
import { SyncResult } from "./syncResult";
|
|
6
|
-
import { Type } from "./types";
|
|
7
|
-
|
|
8
|
-
export class ConcatenateIterator<T> implements Iterator<T>
|
|
9
|
-
{
|
|
10
|
-
private readonly innerIterators: Iterator<Iterator<T>>;
|
|
11
|
-
|
|
12
|
-
private constructor(innerIterators: Iterator<Iterator<T>>)
|
|
13
|
-
{
|
|
14
|
-
PreCondition.assertNotUndefinedAndNotNull(innerIterators, "innerIterators");
|
|
15
|
-
|
|
16
|
-
this.innerIterators = innerIterators;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
public static create<T>(...toConcatenate: JavascriptIterable<T>[]): ConcatenateIterator<T>
|
|
20
|
-
{
|
|
21
|
-
PreCondition.assertNotUndefinedAndNotNull(toConcatenate, "toConcatenate");
|
|
22
|
-
|
|
23
|
-
const innerIterators: List<Iterator<T>> = List.create();
|
|
24
|
-
for (const value of toConcatenate)
|
|
25
|
-
{
|
|
26
|
-
innerIterators.add(Iterator.create(value));
|
|
27
|
-
}
|
|
28
|
-
return new ConcatenateIterator<T>(innerIterators.iterate());
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
public next(): SyncResult<boolean>
|
|
32
|
-
{
|
|
33
|
-
return SyncResult.create(() =>
|
|
34
|
-
{
|
|
35
|
-
let innerIteratorAtBeginning: boolean = false;
|
|
36
|
-
if (!this.innerIterators.hasStarted())
|
|
37
|
-
{
|
|
38
|
-
this.innerIterators.next().await();
|
|
39
|
-
innerIteratorAtBeginning = true;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
while (this.innerIterators.hasCurrent())
|
|
43
|
-
{
|
|
44
|
-
const innerIterator: Iterator<T> = this.innerIterators.getCurrent();
|
|
45
|
-
if (innerIteratorAtBeginning)
|
|
46
|
-
{
|
|
47
|
-
innerIterator.start().await();
|
|
48
|
-
}
|
|
49
|
-
else
|
|
50
|
-
{
|
|
51
|
-
innerIterator.next().await();
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
if (innerIterator.hasCurrent())
|
|
55
|
-
{
|
|
56
|
-
break;
|
|
57
|
-
}
|
|
58
|
-
else
|
|
59
|
-
{
|
|
60
|
-
this.innerIterators.next().await();
|
|
61
|
-
innerIteratorAtBeginning = true;
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
return this.hasCurrent();
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
public hasStarted(): boolean
|
|
70
|
-
{
|
|
71
|
-
return this.innerIterators.hasStarted();
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
public hasCurrent(): boolean
|
|
75
|
-
{
|
|
76
|
-
return this.innerIterators.hasCurrent();
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
public getCurrent(): T
|
|
80
|
-
{
|
|
81
|
-
PreCondition.assertTrue(this.hasCurrent(), "this.hasCurrent()");
|
|
82
|
-
|
|
83
|
-
return this.innerIterators.getCurrent().getCurrent();
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
public start(): SyncResult<this>
|
|
87
|
-
{
|
|
88
|
-
return Iterator.start<T, this>(this);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
public takeCurrent(): SyncResult<T>
|
|
92
|
-
{
|
|
93
|
-
return Iterator.takeCurrent(this);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
public any(): SyncResult<boolean>
|
|
97
|
-
{
|
|
98
|
-
return Iterator.any(this);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
public getCount(): SyncResult<number>
|
|
102
|
-
{
|
|
103
|
-
return Iterator.getCount(this);
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
public toArray(): SyncResult<T[]>
|
|
107
|
-
{
|
|
108
|
-
return Iterator.toArray(this);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
public concatenate(...toConcatenate: JavascriptIterable<T>[]): Iterator<T>
|
|
112
|
-
{
|
|
113
|
-
return Iterator.concatenate(this, ...toConcatenate);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
public where(condition: (value: T) => (boolean | SyncResult<boolean>)): Iterator<T>
|
|
117
|
-
{
|
|
118
|
-
return Iterator.where(this, condition);
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
public map<TOutput>(mapping: (value: T) => TOutput | SyncResult<TOutput>): Iterator<TOutput>
|
|
122
|
-
{
|
|
123
|
-
return Iterator.map(this, mapping);
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
public flatMap<TOutput>(mapping: (value: T) => JavascriptIterable<TOutput>): Iterator<TOutput>
|
|
127
|
-
{
|
|
128
|
-
return Iterator.flatMap(this, mapping);
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
public whereInstanceOf<U extends T>(typeCheck: (value: T) => value is U): Iterator<U>
|
|
132
|
-
{
|
|
133
|
-
return Iterator.whereInstanceOf(this, typeCheck);
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
public whereInstanceOfType<U extends T>(type: Type<U>): Iterator<U>
|
|
137
|
-
{
|
|
138
|
-
return Iterator.whereInstanceOfType(this, type);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
public first(condition?: ((value: T) => (boolean | SyncResult<boolean>)) | undefined): SyncResult<T>
|
|
142
|
-
{
|
|
143
|
-
return Iterator.first(this, condition);
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
public last(condition?: ((value: T) => (boolean | SyncResult<boolean>)) | undefined): SyncResult<T>
|
|
147
|
-
{
|
|
148
|
-
return Iterator.last(this, condition);
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
public take(maximumToTake: number): Iterator<T>
|
|
152
|
-
{
|
|
153
|
-
return Iterator.take(this, maximumToTake);
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
public skip(maximumToSkip: number): Iterator<T>
|
|
157
|
-
{
|
|
158
|
-
return Iterator.skip(this, maximumToSkip);
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
public [Symbol.iterator](): JavascriptIterator<T>
|
|
162
|
-
{
|
|
163
|
-
return Iterator[Symbol.iterator](this);
|
|
164
|
-
}
|
|
1
|
+
import { Iterator } from "./iterator";
|
|
2
|
+
import { JavascriptIterable, JavascriptIterator } from "./javascript";
|
|
3
|
+
import { List } from "./list";
|
|
4
|
+
import { PreCondition } from "./preCondition";
|
|
5
|
+
import { SyncResult } from "./syncResult";
|
|
6
|
+
import { Type } from "./types";
|
|
7
|
+
|
|
8
|
+
export class ConcatenateIterator<T> implements Iterator<T>
|
|
9
|
+
{
|
|
10
|
+
private readonly innerIterators: Iterator<Iterator<T>>;
|
|
11
|
+
|
|
12
|
+
private constructor(innerIterators: Iterator<Iterator<T>>)
|
|
13
|
+
{
|
|
14
|
+
PreCondition.assertNotUndefinedAndNotNull(innerIterators, "innerIterators");
|
|
15
|
+
|
|
16
|
+
this.innerIterators = innerIterators;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
public static create<T>(...toConcatenate: JavascriptIterable<T>[]): ConcatenateIterator<T>
|
|
20
|
+
{
|
|
21
|
+
PreCondition.assertNotUndefinedAndNotNull(toConcatenate, "toConcatenate");
|
|
22
|
+
|
|
23
|
+
const innerIterators: List<Iterator<T>> = List.create();
|
|
24
|
+
for (const value of toConcatenate)
|
|
25
|
+
{
|
|
26
|
+
innerIterators.add(Iterator.create(value));
|
|
27
|
+
}
|
|
28
|
+
return new ConcatenateIterator<T>(innerIterators.iterate());
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
public next(): SyncResult<boolean>
|
|
32
|
+
{
|
|
33
|
+
return SyncResult.create(() =>
|
|
34
|
+
{
|
|
35
|
+
let innerIteratorAtBeginning: boolean = false;
|
|
36
|
+
if (!this.innerIterators.hasStarted())
|
|
37
|
+
{
|
|
38
|
+
this.innerIterators.next().await();
|
|
39
|
+
innerIteratorAtBeginning = true;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
while (this.innerIterators.hasCurrent())
|
|
43
|
+
{
|
|
44
|
+
const innerIterator: Iterator<T> = this.innerIterators.getCurrent();
|
|
45
|
+
if (innerIteratorAtBeginning)
|
|
46
|
+
{
|
|
47
|
+
innerIterator.start().await();
|
|
48
|
+
}
|
|
49
|
+
else
|
|
50
|
+
{
|
|
51
|
+
innerIterator.next().await();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (innerIterator.hasCurrent())
|
|
55
|
+
{
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
else
|
|
59
|
+
{
|
|
60
|
+
this.innerIterators.next().await();
|
|
61
|
+
innerIteratorAtBeginning = true;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return this.hasCurrent();
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
public hasStarted(): boolean
|
|
70
|
+
{
|
|
71
|
+
return this.innerIterators.hasStarted();
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
public hasCurrent(): boolean
|
|
75
|
+
{
|
|
76
|
+
return this.innerIterators.hasCurrent();
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
public getCurrent(): T
|
|
80
|
+
{
|
|
81
|
+
PreCondition.assertTrue(this.hasCurrent(), "this.hasCurrent()");
|
|
82
|
+
|
|
83
|
+
return this.innerIterators.getCurrent().getCurrent();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
public start(): SyncResult<this>
|
|
87
|
+
{
|
|
88
|
+
return Iterator.start<T, this>(this);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
public takeCurrent(): SyncResult<T>
|
|
92
|
+
{
|
|
93
|
+
return Iterator.takeCurrent(this);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
public any(): SyncResult<boolean>
|
|
97
|
+
{
|
|
98
|
+
return Iterator.any(this);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
public getCount(): SyncResult<number>
|
|
102
|
+
{
|
|
103
|
+
return Iterator.getCount(this);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
public toArray(): SyncResult<T[]>
|
|
107
|
+
{
|
|
108
|
+
return Iterator.toArray(this);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
public concatenate(...toConcatenate: JavascriptIterable<T>[]): Iterator<T>
|
|
112
|
+
{
|
|
113
|
+
return Iterator.concatenate(this, ...toConcatenate);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
public where(condition: (value: T) => (boolean | SyncResult<boolean>)): Iterator<T>
|
|
117
|
+
{
|
|
118
|
+
return Iterator.where(this, condition);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
public map<TOutput>(mapping: (value: T) => TOutput | SyncResult<TOutput>): Iterator<TOutput>
|
|
122
|
+
{
|
|
123
|
+
return Iterator.map(this, mapping);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
public flatMap<TOutput>(mapping: (value: T) => JavascriptIterable<TOutput>): Iterator<TOutput>
|
|
127
|
+
{
|
|
128
|
+
return Iterator.flatMap(this, mapping);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
public whereInstanceOf<U extends T>(typeCheck: (value: T) => value is U): Iterator<U>
|
|
132
|
+
{
|
|
133
|
+
return Iterator.whereInstanceOf(this, typeCheck);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
public whereInstanceOfType<U extends T>(type: Type<U>): Iterator<U>
|
|
137
|
+
{
|
|
138
|
+
return Iterator.whereInstanceOfType(this, type);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
public first(condition?: ((value: T) => (boolean | SyncResult<boolean>)) | undefined): SyncResult<T>
|
|
142
|
+
{
|
|
143
|
+
return Iterator.first(this, condition);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
public last(condition?: ((value: T) => (boolean | SyncResult<boolean>)) | undefined): SyncResult<T>
|
|
147
|
+
{
|
|
148
|
+
return Iterator.last(this, condition);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
public take(maximumToTake: number): Iterator<T>
|
|
152
|
+
{
|
|
153
|
+
return Iterator.take(this, maximumToTake);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
public skip(maximumToSkip: number): Iterator<T>
|
|
157
|
+
{
|
|
158
|
+
return Iterator.skip(this, maximumToSkip);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
public [Symbol.iterator](): JavascriptIterator<T>
|
|
162
|
+
{
|
|
163
|
+
return Iterator[Symbol.iterator](this);
|
|
164
|
+
}
|
|
165
165
|
}
|
|
@@ -1,158 +1,158 @@
|
|
|
1
|
-
import { CharacterWriteStream } from "./characterWriteStream";
|
|
2
|
-
import { CommandLineParameters } from "./commandLineParameters";
|
|
3
|
-
import { JavascriptIterable } from "./javascript";
|
|
4
|
-
import { Network } from "./network";
|
|
5
|
-
import { NodeJSCharacterWriteStream } from "./nodeJSCharacterWriteStream";
|
|
6
|
-
import { PreCondition } from "./preCondition";
|
|
7
|
-
import { Property } from "./property";
|
|
8
|
-
import { RealNetwork } from "./realNetwork";
|
|
9
|
-
import { isNumber } from "./types";
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* An object that provides all of the resources that are available to the current process.
|
|
13
|
-
*/
|
|
14
|
-
export class CurrentProcess
|
|
15
|
-
{
|
|
16
|
-
private args: JavascriptIterable<string> | undefined
|
|
17
|
-
private parameters: CommandLineParameters | undefined;
|
|
18
|
-
private outputWriteStream: CharacterWriteStream | undefined;
|
|
19
|
-
private exitCodeProperty: Property<number> | undefined;
|
|
20
|
-
private network: Network | undefined;
|
|
21
|
-
|
|
22
|
-
private constructor()
|
|
23
|
-
{
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
public static create(): CurrentProcess
|
|
27
|
-
{
|
|
28
|
-
return new CurrentProcess();
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
public static async run(action: (currentProcess: CurrentProcess) => void | number | Promise<void | number>): Promise<void>
|
|
32
|
-
{
|
|
33
|
-
PreCondition.assertNotUndefinedAndNotNull(action, "action");
|
|
34
|
-
|
|
35
|
-
const currentProcess: CurrentProcess = CurrentProcess.create();
|
|
36
|
-
try
|
|
37
|
-
{
|
|
38
|
-
const result: void | number = await action(currentProcess);
|
|
39
|
-
if (isNumber(result))
|
|
40
|
-
{
|
|
41
|
-
currentProcess.setExitCode(result);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
catch (error)
|
|
45
|
-
{
|
|
46
|
-
currentProcess.setExitCode(-1);
|
|
47
|
-
const writeStream: CharacterWriteStream = currentProcess.getOutputWriteStream();
|
|
48
|
-
if (error instanceof Error && error.stack)
|
|
49
|
-
{
|
|
50
|
-
writeStream.writeLine(error.stack);
|
|
51
|
-
}
|
|
52
|
-
else
|
|
53
|
-
{
|
|
54
|
-
writeStream.writeLine(`${error}`);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
public getArguments(): JavascriptIterable<string>
|
|
60
|
-
{
|
|
61
|
-
if (!this.args)
|
|
62
|
-
{
|
|
63
|
-
this.args = process.argv;
|
|
64
|
-
}
|
|
65
|
-
return this.args;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
public setArguments(args: JavascriptIterable<string>): this
|
|
69
|
-
{
|
|
70
|
-
PreCondition.assertNotUndefinedAndNotNull(args, "args");
|
|
71
|
-
PreCondition.assertUndefined(this.parameters, "this.parameters");
|
|
72
|
-
|
|
73
|
-
this.args = args;
|
|
74
|
-
return this;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
public getParameters(): CommandLineParameters
|
|
78
|
-
{
|
|
79
|
-
if (!this.parameters)
|
|
80
|
-
{
|
|
81
|
-
this.parameters = CommandLineParameters.create(this.getArguments());
|
|
82
|
-
}
|
|
83
|
-
return this.parameters;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
public getOutputWriteStream(): CharacterWriteStream
|
|
87
|
-
{
|
|
88
|
-
if (!this.outputWriteStream)
|
|
89
|
-
{
|
|
90
|
-
this.outputWriteStream = NodeJSCharacterWriteStream.create(process.stdout);
|
|
91
|
-
}
|
|
92
|
-
return this.outputWriteStream;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
public setOutputWriteStream(outputWriteStream: CharacterWriteStream): this
|
|
96
|
-
{
|
|
97
|
-
PreCondition.assertNotUndefinedAndNotNull(outputWriteStream, "outputWriteStream");
|
|
98
|
-
|
|
99
|
-
this.outputWriteStream = outputWriteStream;
|
|
100
|
-
|
|
101
|
-
return this;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
public getExitCode(): number
|
|
105
|
-
{
|
|
106
|
-
return this.getExitCodeProperty().getValue();
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
public setExitCode(exitCode: number): this
|
|
110
|
-
{
|
|
111
|
-
PreCondition.assertNotUndefinedAndNotNull(exitCode, "exitCode");
|
|
112
|
-
|
|
113
|
-
this.getExitCodeProperty().setValue(exitCode);
|
|
114
|
-
|
|
115
|
-
return this;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
public getExitCodeProperty(): Property<number>
|
|
119
|
-
{
|
|
120
|
-
if (!this.exitCodeProperty)
|
|
121
|
-
{
|
|
122
|
-
this.exitCodeProperty = Property.create({
|
|
123
|
-
getter: () => process.exitCode as number,
|
|
124
|
-
setter: (value: number) => { process.exitCode = value; },
|
|
125
|
-
});
|
|
126
|
-
}
|
|
127
|
-
return this.exitCodeProperty;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
public setExitCodeProperty(exitCodeProperty: Property<number>): this
|
|
131
|
-
{
|
|
132
|
-
PreCondition.assertNotUndefinedAndNotNull(exitCodeProperty, "exitCodeProperty");
|
|
133
|
-
|
|
134
|
-
this.exitCodeProperty = exitCodeProperty;
|
|
135
|
-
|
|
136
|
-
return this;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
public getNetwork(): Network
|
|
140
|
-
{
|
|
141
|
-
if (!this.network)
|
|
142
|
-
{
|
|
143
|
-
this.network = RealNetwork.create();
|
|
144
|
-
}
|
|
145
|
-
return this.network;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
public setNetwork(network: Network): this
|
|
149
|
-
{
|
|
150
|
-
PreCondition.assertUndefined(this.network, "this.network");
|
|
151
|
-
PreCondition.assertNotUndefinedAndNotNull(network, "network");
|
|
152
|
-
|
|
153
|
-
this.network = network;
|
|
154
|
-
|
|
155
|
-
return this;
|
|
156
|
-
|
|
157
|
-
}
|
|
1
|
+
import { CharacterWriteStream } from "./characterWriteStream";
|
|
2
|
+
import { CommandLineParameters } from "./commandLineParameters";
|
|
3
|
+
import { JavascriptIterable } from "./javascript";
|
|
4
|
+
import { Network } from "./network";
|
|
5
|
+
import { NodeJSCharacterWriteStream } from "./nodeJSCharacterWriteStream";
|
|
6
|
+
import { PreCondition } from "./preCondition";
|
|
7
|
+
import { Property } from "./property";
|
|
8
|
+
import { RealNetwork } from "./realNetwork";
|
|
9
|
+
import { isNumber } from "./types";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* An object that provides all of the resources that are available to the current process.
|
|
13
|
+
*/
|
|
14
|
+
export class CurrentProcess
|
|
15
|
+
{
|
|
16
|
+
private args: JavascriptIterable<string> | undefined
|
|
17
|
+
private parameters: CommandLineParameters | undefined;
|
|
18
|
+
private outputWriteStream: CharacterWriteStream | undefined;
|
|
19
|
+
private exitCodeProperty: Property<number> | undefined;
|
|
20
|
+
private network: Network | undefined;
|
|
21
|
+
|
|
22
|
+
private constructor()
|
|
23
|
+
{
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
public static create(): CurrentProcess
|
|
27
|
+
{
|
|
28
|
+
return new CurrentProcess();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
public static async run(action: (currentProcess: CurrentProcess) => void | number | Promise<void | number>): Promise<void>
|
|
32
|
+
{
|
|
33
|
+
PreCondition.assertNotUndefinedAndNotNull(action, "action");
|
|
34
|
+
|
|
35
|
+
const currentProcess: CurrentProcess = CurrentProcess.create();
|
|
36
|
+
try
|
|
37
|
+
{
|
|
38
|
+
const result: void | number = await action(currentProcess);
|
|
39
|
+
if (isNumber(result))
|
|
40
|
+
{
|
|
41
|
+
currentProcess.setExitCode(result);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
catch (error)
|
|
45
|
+
{
|
|
46
|
+
currentProcess.setExitCode(-1);
|
|
47
|
+
const writeStream: CharacterWriteStream = currentProcess.getOutputWriteStream();
|
|
48
|
+
if (error instanceof Error && error.stack)
|
|
49
|
+
{
|
|
50
|
+
writeStream.writeLine(error.stack);
|
|
51
|
+
}
|
|
52
|
+
else
|
|
53
|
+
{
|
|
54
|
+
writeStream.writeLine(`${error}`);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
public getArguments(): JavascriptIterable<string>
|
|
60
|
+
{
|
|
61
|
+
if (!this.args)
|
|
62
|
+
{
|
|
63
|
+
this.args = process.argv;
|
|
64
|
+
}
|
|
65
|
+
return this.args;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
public setArguments(args: JavascriptIterable<string>): this
|
|
69
|
+
{
|
|
70
|
+
PreCondition.assertNotUndefinedAndNotNull(args, "args");
|
|
71
|
+
PreCondition.assertUndefined(this.parameters, "this.parameters");
|
|
72
|
+
|
|
73
|
+
this.args = args;
|
|
74
|
+
return this;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
public getParameters(): CommandLineParameters
|
|
78
|
+
{
|
|
79
|
+
if (!this.parameters)
|
|
80
|
+
{
|
|
81
|
+
this.parameters = CommandLineParameters.create(this.getArguments());
|
|
82
|
+
}
|
|
83
|
+
return this.parameters;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
public getOutputWriteStream(): CharacterWriteStream
|
|
87
|
+
{
|
|
88
|
+
if (!this.outputWriteStream)
|
|
89
|
+
{
|
|
90
|
+
this.outputWriteStream = NodeJSCharacterWriteStream.create(process.stdout);
|
|
91
|
+
}
|
|
92
|
+
return this.outputWriteStream;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
public setOutputWriteStream(outputWriteStream: CharacterWriteStream): this
|
|
96
|
+
{
|
|
97
|
+
PreCondition.assertNotUndefinedAndNotNull(outputWriteStream, "outputWriteStream");
|
|
98
|
+
|
|
99
|
+
this.outputWriteStream = outputWriteStream;
|
|
100
|
+
|
|
101
|
+
return this;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
public getExitCode(): number
|
|
105
|
+
{
|
|
106
|
+
return this.getExitCodeProperty().getValue();
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
public setExitCode(exitCode: number): this
|
|
110
|
+
{
|
|
111
|
+
PreCondition.assertNotUndefinedAndNotNull(exitCode, "exitCode");
|
|
112
|
+
|
|
113
|
+
this.getExitCodeProperty().setValue(exitCode);
|
|
114
|
+
|
|
115
|
+
return this;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
public getExitCodeProperty(): Property<number>
|
|
119
|
+
{
|
|
120
|
+
if (!this.exitCodeProperty)
|
|
121
|
+
{
|
|
122
|
+
this.exitCodeProperty = Property.create({
|
|
123
|
+
getter: () => process.exitCode as number,
|
|
124
|
+
setter: (value: number) => { process.exitCode = value; },
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
return this.exitCodeProperty;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
public setExitCodeProperty(exitCodeProperty: Property<number>): this
|
|
131
|
+
{
|
|
132
|
+
PreCondition.assertNotUndefinedAndNotNull(exitCodeProperty, "exitCodeProperty");
|
|
133
|
+
|
|
134
|
+
this.exitCodeProperty = exitCodeProperty;
|
|
135
|
+
|
|
136
|
+
return this;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
public getNetwork(): Network
|
|
140
|
+
{
|
|
141
|
+
if (!this.network)
|
|
142
|
+
{
|
|
143
|
+
this.network = RealNetwork.create();
|
|
144
|
+
}
|
|
145
|
+
return this.network;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
public setNetwork(network: Network): this
|
|
149
|
+
{
|
|
150
|
+
PreCondition.assertUndefined(this.network, "this.network");
|
|
151
|
+
PreCondition.assertNotUndefinedAndNotNull(network, "network");
|
|
152
|
+
|
|
153
|
+
this.network = network;
|
|
154
|
+
|
|
155
|
+
return this;
|
|
156
|
+
|
|
157
|
+
}
|
|
158
158
|
}
|