UNPKG

2.89 kBJavaScriptView Raw
1/*! *****************************************************************************
2Copyright (c) Microsoft Corporation. All rights reserved.
3Licensed under the Apache License, Version 2.0 (the "License"); you may not use
4this file except in compliance with the License. You may obtain a copy of the
5License at http://www.apache.org/licenses/LICENSE-2.0
6
7THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
8KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
9WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
10MERCHANTABLITY OR NON-INFRINGEMENT.
11
12See the Apache Version 2.0 License for specific language governing permissions
13and limitations under the License.
14***************************************************************************** */
15
16
17"use strict";
18var fs = require("fs");
19function pipeExists(name) {
20 return fs.existsSync(name);
21}
22function createCancellationToken(args) {
23 var cancellationPipeName;
24 for (var i = 0; i < args.length - 1; i++) {
25 if (args[i] === "--cancellationPipeName") {
26 cancellationPipeName = args[i + 1];
27 break;
28 }
29 }
30 if (!cancellationPipeName) {
31 return {
32 isCancellationRequested: function () { return false; },
33 setRequest: function (_requestId) { return void 0; },
34 resetRequest: function (_requestId) { return void 0; }
35 };
36 }
37 if (cancellationPipeName.charAt(cancellationPipeName.length - 1) === "*") {
38 var namePrefix_1 = cancellationPipeName.slice(0, -1);
39 if (namePrefix_1.length === 0 || namePrefix_1.indexOf("*") >= 0) {
40 throw new Error("Invalid name for template cancellation pipe: it should have length greater than 2 characters and contain only one '*'.");
41 }
42 var perRequestPipeName_1;
43 var currentRequestId_1;
44 return {
45 isCancellationRequested: function () { return perRequestPipeName_1 !== undefined && pipeExists(perRequestPipeName_1); },
46 setRequest: function (requestId) {
47 currentRequestId_1 = requestId;
48 perRequestPipeName_1 = namePrefix_1 + requestId;
49 },
50 resetRequest: function (requestId) {
51 if (currentRequestId_1 !== requestId) {
52 throw new Error("Mismatched request id, expected " + currentRequestId_1 + ", actual " + requestId);
53 }
54 perRequestPipeName_1 = undefined;
55 }
56 };
57 }
58 else {
59 return {
60 isCancellationRequested: function () { return pipeExists(cancellationPipeName); },
61 setRequest: function (_requestId) { return void 0; },
62 resetRequest: function (_requestId) { return void 0; }
63 };
64 }
65}
66module.exports = createCancellationToken;
67//# sourceMappingURL=cancellationToken.js.map
\No newline at end of file