Skip to content

Commit

Permalink
#7 #8 #9 #12 - Fixed multiple issues, and rewrote the task with TypeS…
Browse files Browse the repository at this point in the history
…cript
  • Loading branch information
estruyf committed Oct 8, 2020
1 parent dc745e1 commit e0b778c
Show file tree
Hide file tree
Showing 11,367 changed files with 668,899 additions and 85 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
11 changes: 10 additions & 1 deletion README.md
Expand Up @@ -24,4 +24,13 @@ When this is checked, the task will automatically add a new line at the end the

## Feedback

Feedback is always welcome. Please submit it via creating an issue in the repository [issue list](https://github.com/estruyf/vsts-file-creator/issues).
Feedback is always welcome. Please submit it via creating an issue in the repository [issue list](https://github.com/estruyf/vsts-file-creator/issues).

## Changelog

### [6.0.0] 8/10/2020

- [#7](https://github.com/estruyf/vsts-file-creator/issues/7): Added `verbose` logging setting
- [#8](https://github.com/estruyf/vsts-file-creator/issues/8): Fix for `SyntaxError` in block-scoped declarations
- [#9](https://github.com/estruyf/vsts-file-creator/issues/9): Added support for creating empty files
- [#12](https://github.com/estruyf/vsts-file-creator/issues/12): Switched the `string` setting to `filePath` to be able to select your path
5 changes: 5 additions & 0 deletions buildtask/.gitignore
@@ -0,0 +1,5 @@
node_modules

.env
.taskkey
test.txt
64 changes: 64 additions & 0 deletions buildtask/index-old.js
@@ -0,0 +1,64 @@
// @ts-check

const path = require('path');
const tl = require('vsts-task-lib/task');
const fs = require('fs');

// Set the source path
tl.setResourcePath(path.join(__dirname, 'task.json'));

// Get the two property values
const filepath = tl.getInput('filepath', true);
let filecontent = tl.getInput('filecontent', true);
const fileoverwrite = tl.getInput('fileoverwrite', true);
const skipempty = tl.getInput('skipempty', true);
const endWithNewLine = tl.getInput('endWithNewLine', false);

console.log(`File path: ${filepath}`);
// console.log(`File content: ${filecontent}`);

/**
* Create the folders for the file
* @param {*} filePath
*/
function ensureDirectoryExistence(filePath) {
var dirname = path.dirname(filePath);
// console.log(`Creating dir: ${dirname}`);
if (fs.existsSync(dirname)) {
return true;
}
ensureDirectoryExistence(dirname);
fs.mkdirSync(dirname);
}

/**
* 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";
}
// Create the file
tl.writeFile(filepath, filecontent, 'utf8');
// Check if the file is created
if (tl.exist(filepath)) {
console.log('File created');
} else {
tl.error('File not created / overwritten');
}
} else {
tl.error('File already exists');
}
}
}
160 changes: 103 additions & 57 deletions buildtask/index.js
@@ -1,64 +1,110 @@
// @ts-check

const path = require('path');
const tl = require('vsts-task-lib/task');
const fs = require('fs');

// Set the source path
tl.setResourcePath(path.join(__dirname, 'task.json'));

// Get the two property values
const filepath = tl.getInput('filepath', true);
let filecontent = tl.getInput('filecontent', true);
const fileoverwrite = tl.getInput('fileoverwrite', true);
const skipempty = tl.getInput('skipempty', true);
const endWithNewLine = tl.getInput('endWithNewLine', false);

console.log(`File path: ${filepath}`);
// console.log(`File content: ${filecontent}`);

"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const tl = require("azure-pipelines-task-lib/task");
const path_1 = __importDefault(require("path"));
const fs_1 = __importDefault(require("fs"));
/**
* Create the folders for the file
* @param {*} filePath
* @param {string} filePath
*/
function ensureDirectoryExistence(filePath) {
var dirname = path.dirname(filePath);
// console.log(`Creating dir: ${dirname}`);
if (fs.existsSync(dirname)) {
return true;
}
ensureDirectoryExistence(dirname);
fs.mkdirSync(dirname);
var dirname = path_1.default.dirname(filePath);
// console.log(`Creating dir: ${dirname}`);
if (fs_1.default.existsSync(dirname)) {
return true;
}
ensureDirectoryExistence(dirname);
fs_1.default.mkdirSync(dirname);
}

// Set the source path
// tl.setResourcePath(path.join(__dirname, 'task.json'));
/**
* Check if file needs to be created
* Task function
*/
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";
}
// Create the file
tl.writeFile(filepath, filecontent, 'utf8');
// Check if the file is created
if (tl.exist(filepath)) {
console.log('File created');
} else {
tl.error('File not created / overwritten');
}
} else {
tl.error('File already exists');
}
}
}
function run() {
return __awaiter(this, void 0, void 0, function* () {
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();
100 changes: 100 additions & 0 deletions buildtask/index.ts
@@ -0,0 +1,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();

0 comments on commit e0b778c

Please sign in to comment.