/
index.ts
100 lines (89 loc) · 2.89 KB
/
index.ts
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
import tl = require('azure-pipelines-task-lib/task');
import path from 'path';
import fs from 'fs';
/**
* Create the folders for the file
* @param {string} filePath
*/
function ensureDirectoryExistence(filePath: string) {
var dirname = path.dirname(filePath);
// console.log(`Creating dir: ${dirname}`);
if (fs.existsSync(dirname)) {
return true;
}
ensureDirectoryExistence(dirname);
fs.mkdirSync(dirname);
}
// Set the source path
// tl.setResourcePath(path.join(__dirname, 'task.json'));
/**
* Task function
*/
async function run() {
try {
// Get all string values
const filepath = tl.getInput('filepath', true);
let filecontent = tl.getInput('filecontent', false) || "";
// Get all boolean values
const fileoverwrite = tl.getInput('fileoverwrite', true) === "true";
const skipempty = tl.getInput('skipempty', true) === "true";
const endWithNewLine = tl.getInput('endWithNewLine', false) === "true";
const verbose = tl.getInput('verbose', false) === "true";
if (verbose) {
console.log(`VERBOSE LOGGING`);
console.log(`----------------`);
console.log(`'filepath': ${filepath}`);
console.log(`'fileoverwrite': ${fileoverwrite}`);
console.log(`'skipempty': ${skipempty}`);
console.log(`'endWithNewLine': ${endWithNewLine}`);
console.log(`'filecontent': ${filecontent}`);
console.log(`'filecontent.length': ${filecontent.length}`);
console.log(``);
} else {
console.log(`File path: ${filepath}`);
}
/**
* Check if file needs to be created
*/
if (filecontent || (!filecontent && !skipempty)) {
filecontent = filecontent || "";
/**
* Start processing the file
*/
if (filepath) {
// Check if file exists
if (!tl.exist(filepath) || fileoverwrite) {
// Create the folder, if needed
ensureDirectoryExistence(filepath);
// Check if new line needs to be added at the end of the file
if (endWithNewLine) {
filecontent = filecontent + "\n";
}
if (verbose) {
console.log(`Writing file with the following contents:`);
console.log(filecontent);
console.log(`----------------`);
}
// Create the file
tl.writeFile(filepath, filecontent, 'utf8');
// Check if the file is created
if (tl.exist(filepath)) {
tl.setResult(tl.TaskResult.Succeeded, 'File created');
} else {
tl.setResult(tl.TaskResult.Failed, 'File not created / overwritten');
}
} else {
tl.setResult(tl.TaskResult.Failed, 'File already exists');
}
}
} else {
if (verbose) {
console.log(`Skipped file creation.`);
console.log(`----------------`);
}
}
} catch (err) {
tl.setResult(tl.TaskResult.Failed, err.message);
}
}
run();