Skip to content

Commit

Permalink
feature - task for replace files
Browse files Browse the repository at this point in the history
  • Loading branch information
renatodans committed Sep 3, 2021
1 parent b88e8e9 commit 0c2da6e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
1 change: 0 additions & 1 deletion tasks/stack-board-replaces/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
},
"devDependencies": {
"@types/node": "^12.0.0",
"@types/shelljs": "^0.8.9",
"typescript": "^4.1.2"
}
}
38 changes: 26 additions & 12 deletions tasks/stack-board-replaces/stackboardreplaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,31 @@ function setFirstNode(obj: any, path: string, value: any): any {

async function main(): Promise<void> {
try {
tl.setResourcePath(path.join(__dirname, "task.json"));
tl.setResourcePath(path.join(__dirname, "task.json"));

const folderPath = tl.getPathInput("folderPath", true) ?? "";
const fileType = tl.getPathInput("fileType", true) ?? "yaml";
const targetFiles = tl.getPathInput("targetFiles", true) ?? "";
const targetFiles = tl.getDelimitedInput("targetFiles", "\n", false);

//const workingDirectory = tl.getVariable("System.DefaultWorkingDirectory");
const useKeyVault = tl.getBoolInput("useKeyVault", false);
const keyVaultVariables = tl.getDelimitedInput("keyVaultVariables", "\n", false);

let variables: any[] = [];

const pipelineVariables = tl.getVariables();
for (let pipelineVariable of pipelineVariables) {
variables.push({ key: pipelineVariable.name, value: pipelineVariable.value })
}

if (useKeyVault) {
console.log("Use Key Vault...", keyVaultVariables);

for (let keyVaultVariable of keyVaultVariables) {
let secret = tl.getVariable(keyVaultVariable);
variables.push({ key: keyVaultVariable.replace("--", "."), value: secret })
}
}

const variables = [
{ key: "", value: "" },
];
const keys = variables.map((k) => k.key);
const values = variables.map((k) => k.value);

Expand All @@ -103,12 +117,12 @@ async function main(): Promise<void> {
let jsonfy = JSON.stringify(content.replace(/(?:\r\n|\r|\n|\s)/g, ""));

for (let variable of variables) {
const nodeTo = setFirstNode(app, variable.key, variable.value);
const nodeFrom = getFirstNode(app, variable.key);
const valueNode = setFirstNode(app, variable.key, variable.value);
const currentNode = getFirstNode(app, variable.key);

if (nodeTo != undefined && nodeFrom != undefined) {
const from = JSON.stringify(nodeTo).replace(/(^\"+|\"+$)/gm, "");
const to = JSON.stringify(nodeFrom).replace(/(^\"+|\"+$)/gm, "");
if (valueNode != undefined && currentNode != undefined) {
const from = JSON.stringify(valueNode).replace(/(^\"+|\"+$)/gm, "");
const to = JSON.stringify(currentNode).replace(/(^\"+|\"+$)/gm, "");

jsonfy = jsonfy.replace(from, to);
}
Expand All @@ -119,7 +133,7 @@ async function main(): Promise<void> {
}

tl.setResult(tl.TaskResult.Succeeded, "Task completed!");
} catch (err) {
} catch (err: any) {
tl.setResult(tl.TaskResult.Failed, err);
}
}
Expand Down
2 changes: 1 addition & 1 deletion tasks/stack-board-replaces/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"name": "keyVaultVariables",
"type": "multiLine",
"label": "Key Vault variables",
"helpMarkDown": "Provide new line separated list of Key Vault variables to substitute the values",
"helpMarkDown": "Provide new line separated list of Key Vault variables to substitute the values. e.g. StackBoard--Secret--Connection",
"required": false,
"defaultValue": "",
"visibleRule": "useKeyVault == true"
Expand Down

0 comments on commit 0c2da6e

Please sign in to comment.