appcenter-cli
Version:
Command line tool for Visual Studio App Center
112 lines (111 loc) • 4.86 kB
JavaScript
;
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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.filterIncludedFiles = exports.parseIncludedFiles = exports.copyIncludedFiles = exports.processIncludedFiles = void 0;
const path = require("path");
const pfs = require("../../../util/misc/promisfied-fs");
const xml_util_1 = require("./xml-util");
const interaction_1 = require("../../../util/interaction");
const _ = require("lodash");
const invalidCharactersRegexp = /['"!#$%&+^<=>`|]/;
function processIncludedFiles(manifest, include, destinationDir, sourceRootDir) {
return __awaiter(this, void 0, void 0, function* () {
if (!include) {
return;
}
const filteredFiles = this.filterIncludedFiles(manifest.files, include);
const includedFiles = this.parseIncludedFiles(filteredFiles, sourceRootDir);
yield this.copyIncludedFiles(manifest, includedFiles, destinationDir);
});
}
exports.processIncludedFiles = processIncludedFiles;
function copyIncludedFiles(manifest, includedFiles, destinationDir) {
return __awaiter(this, void 0, void 0, function* () {
for (const includedFile of includedFiles) {
const copyTarget = path.join(destinationDir, includedFile.targetPath);
yield pfs.cp(includedFile.sourcePath, copyTarget);
manifest.files.push(includedFile.targetPath);
}
});
}
exports.copyIncludedFiles = copyIncludedFiles;
function parseIncludedFiles(includedFiles, rootDir) {
return includedFiles.map((f) => parseIncludedFile(f, rootDir));
}
exports.parseIncludedFiles = parseIncludedFiles;
function filterIncludedFiles(manifestFiles, include) {
if (!include) {
return [];
}
const allFiles = manifestFiles.concat(include);
return include.filter((f) => validFile(f, allFiles));
}
exports.filterIncludedFiles = filterIncludedFiles;
function validFile(fileName, allIncludedFiles) {
if (_.endsWith(fileName, ".dll.config")) {
const assemblyName = fileName.slice(0, -7);
const hasCorrespondingAssembly = allIncludedFiles.indexOf(assemblyName) > -1;
if (hasCorrespondingAssembly && !xml_util_1.validXmlFile(fileName)) {
interaction_1.out.text(`Warning: The XML config file ${fileName} was not a valid XML file. This file will not be uploaded.`);
return false;
}
}
return true;
}
function parseIncludedFile(includedFile, rootDir) {
const separatorIndex = includedFile.indexOf("=");
if (separatorIndex === -1) {
return parseIncludedFileFromSinglePath(includedFile, rootDir);
}
else {
return parseIncludedFileFromPathsPair(includedFile, rootDir, separatorIndex);
}
}
function parseIncludedFileFromSinglePath(includedFile, rootDir) {
validatePath(includedFile);
if (path.isAbsolute(includedFile)) {
const targetPath = path.relative(rootDir, includedFile);
if (targetPath.indexOf("..") !== -1) {
throw new Error(`Invalid included file: "${includedFile}". ` + `If only a single path is used, it must be inside directory "${rootDir}"`);
}
return {
targetPath: path.relative(rootDir, includedFile),
sourcePath: includedFile,
};
}
else {
return {
targetPath: includedFile,
sourcePath: path.join(rootDir, includedFile),
};
}
}
function parseIncludedFileFromPathsPair(includedFile, rootDir, separatorIndex) {
const targetPath = includedFile.substr(0, separatorIndex);
let sourcePath = includedFile.substr(separatorIndex + 1, includedFile.length - separatorIndex - 1);
validatePath(targetPath);
validatePath(sourcePath);
if (path.isAbsolute(targetPath)) {
throw new Error(`Invalid included file: "${includedFile}". Target path must be relative`);
}
if (!path.isAbsolute(sourcePath)) {
sourcePath = path.join(rootDir, sourcePath);
}
return {
targetPath: targetPath,
sourcePath: sourcePath,
};
}
function validatePath(possiblePath) {
if (invalidCharactersRegexp.test(possiblePath)) {
throw new Error(`Invalid path: "${possiblePath}"`);
}
}