UNPKG

2.14 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright © 2018 Atomist, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.DefaultGoalNameGenerator = exports.SourceLocationGoalNameGenerator = void 0;
19const path = require("path");
20const trace = require("stack-trace");
21/**
22 * Generates goal names based on source code location.
23 * This is stable enough to survive SDM restarts and also supports the cluster and goal forking mode.
24 *
25 * This implementation has to used directly inside the body of the goal you want to name.
26 * Otherwise the source code location might not get captured correctly.
27 */
28class SourceLocationGoalNameGenerator {
29 generateName(prefix) {
30 let stack;
31 try {
32 // Just throw an error so that we can capture the stack
33 throw new Error();
34 }
35 catch (err) {
36 stack = trace.parse(err);
37 }
38 // 0 = this, 1 = the caller of generateName, 2 = the creator of the goal
39 const goal = stack[1];
40 const goalName = path.basename(goal.getFileName()).split(".")[0];
41 const creator = stack[2];
42 const creatorFileName = path.basename(creator.getFileName());
43 const creatorLineNumber = creator.getLineNumber();
44 const name = `${creatorFileName}:${creatorLineNumber}`;
45 return `${prefix ? prefix : goalName}#${name}`;
46 }
47}
48exports.SourceLocationGoalNameGenerator = SourceLocationGoalNameGenerator;
49// Default GoalNameGenerator
50exports.DefaultGoalNameGenerator = new SourceLocationGoalNameGenerator();
51//# sourceMappingURL=GoalNameGenerator.js.map
\No newline at end of file