UNPKG

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