-
Notifications
You must be signed in to change notification settings - Fork 133
Expand file tree
/
Copy pathupdateRolesContext.ts
More file actions
101 lines (92 loc) · 2.85 KB
/
updateRolesContext.ts
File metadata and controls
101 lines (92 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import * as path from "path";
import * as fs from "fs";
import {
IVarsContext,
IWorkSpaceRolesContext,
VarType,
} from "@src/interfaces/lightspeed";
import { readVarFiles } from "@src/features/lightspeed/utils/readVarFiles";
async function getVarsFromRoles(
rolePath: string,
varType: VarType,
): Promise<IVarsContext | undefined> {
const varsRootPath = path.join(rolePath, varType);
let dirContent: string[];
try {
dirContent = await fs.promises.readdir(varsRootPath);
} catch {
return;
}
const varsFiles = dirContent
.filter((name) => [".yml", ".yaml"].includes(path.extname(name)))
.map((name) => path.join(varsRootPath, name));
for (const varsFile of varsFiles) {
try {
const varsContext: IVarsContext = {};
const varsFileName = path.basename(varsFile);
const varsFileContent = await readVarFiles(varsFile);
if (varsFileContent) {
varsContext[varsFileName] = varsFileContent;
return varsContext;
}
} catch (err) {
console.error(`Failed to read ${varsFile} with error ${err}`);
}
}
return;
}
export async function updateRolesContext(
ansibleRolesCache: IWorkSpaceRolesContext,
rolesRootPath: string,
workSpaceRoot: string,
): Promise<IWorkSpaceRolesContext | undefined> {
if (!rolesRootPath.endsWith("roles")) {
return;
}
let dirContent: string[];
try {
dirContent = await fs.promises.readdir(rolesRootPath);
} catch (error) {
console.error(`Cannot read the directory: ${error}`);
return;
}
for (const name of dirContent) {
try {
const stat = await fs.promises.stat(path.join(rolesRootPath, name));
if (stat.isDirectory()) {
const rolePath = path.join(rolesRootPath, name);
await updateRoleContext(ansibleRolesCache, rolePath, workSpaceRoot);
}
} catch {
// skip entries that can't be stat'd
}
}
}
export async function updateRoleContext(
ansibleRolesCache: IWorkSpaceRolesContext,
rolePath: string,
workSpaceRoot: string,
): Promise<void> {
if (!ansibleRolesCache[workSpaceRoot]) {
ansibleRolesCache[workSpaceRoot] = {};
}
const rolesContext = ansibleRolesCache[workSpaceRoot];
rolesContext[rolePath] = {
name: rolePath.split(path.sep).pop(),
};
// Get all the task files in the tasks directory
const tasksPath = path.join(rolePath, "tasks");
try {
const dirContent = await fs.promises.readdir(tasksPath);
const taskNames = dirContent
.filter((name) => [".yml", ".yaml"].includes(path.extname(name)))
.map((name) => path.basename(name, path.extname(name)));
rolesContext[rolePath]["tasks"] = taskNames;
} catch {
// tasks directory doesn't exist or can't be read
}
rolesContext[rolePath]["roleVars"] = {
defaults: (await getVarsFromRoles(rolePath, "defaults")) || {},
vars: (await getVarsFromRoles(rolePath, "vars")) || {},
};
}