UNPKG

2.1 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const instruction_1 = require("../instruction");
4class Workdir extends instruction_1.Instruction {
5 constructor(document, range, dockerfile, escapeChar, instruction, instructionRange) {
6 super(document, range, dockerfile, escapeChar, instruction, instructionRange);
7 }
8 /**
9 * Returns the path that has been defined. Note that this path may
10 * be absolute or relative depending on what was written in the
11 * instruction.
12 *
13 * @return the working directory's path, or null if this
14 * instruction has no arguments
15 */
16 getPath() {
17 return this.getArgumentsContent();
18 }
19 /**
20 * Returns the absolute path that this instruction resolves to. The
21 * function will inspect prior WORKDIR instructions in the current
22 * image or another build stage in the Dockerfile to try to
23 * determine this.
24 *
25 * @return the absolute path of the working directory, or null if
26 * this instruction has no arguments, or undefined if it
27 * cannot be determined because only relative paths could be
28 * found
29 */
30 getAbsolutePath() {
31 const path = this.getPath();
32 if (path === null || path.startsWith("/")) {
33 return path;
34 }
35 const startLine = this.getRange().start.line;
36 const hierarchy = this.dockerfile.getStageHierarchy(startLine);
37 for (let i = hierarchy.length - 1; i >= 0; i--) {
38 const workdirs = hierarchy[i].getWORKDIRs();
39 for (let j = workdirs.length - 1; j >= 0; j--) {
40 if (workdirs[j].getRange().start.line < startLine) {
41 const parent = workdirs[j].getAbsolutePath();
42 if (parent === undefined || parent === null) {
43 return undefined;
44 }
45 return parent.endsWith("/") ? parent + path : parent + "/" + path;
46 }
47 }
48 }
49 return undefined;
50 }
51}
52exports.Workdir = Workdir;