@dev-build-deploy/dep5-it
Version:
Debian Copyright (dep5) management library
176 lines (175 loc) • 6.09 kB
JavaScript
;
/*
SPDX-FileCopyrightText: 2023 Kevin de Jong <monkaii@hotmail.com>
SPDX-License-Identifier: MIT
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DebianCopyright = void 0;
const fs = __importStar(require("fs"));
const parser_1 = require("./parser");
const wildcard_1 = require("./wildcard");
/**
* Debian Copyright
* @class DebianCopyright
*/
class DebianCopyright {
constructor(header = {
format: "https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/",
}, files = []) {
this.header = header;
this.files = files;
}
/**
* Parse a debian/copyright file (.dep5)
* @param file Path to the file
* @returns Debian Copyright object
*/
static fromFile(file) {
const content = fs.readFileSync(file, "utf-8");
const paragraphs = content.split("\n\n").filter(paragraph => paragraph.trim().length > 0);
// First paragraph MUST be the header.
return new DebianCopyright(DebianHeader.fromParagraph(paragraphs[0]), paragraphs.splice(1).map(paragraph => DebianFile.fromParagraph(paragraph)));
}
/**
* Returns the File stanza which matches the given file
* @param file File to match
* @returns File stanza which matches the given file
*/
getFileStanza(file) {
for (const stanza of this.files) {
if (stanza.files.some(pattern => (0, wildcard_1.isWildcardMatch)(file, pattern))) {
return stanza;
}
}
}
}
exports.DebianCopyright = DebianCopyright;
/**
* Debian Copyright Header
* @class DebianHeader
*/
class DebianHeader {
constructor(format) {
this.format = format;
}
/**
* Converts a paragraph to a Debian Copyright Header object
* @param paragraph Paragraph to convert
* @returns Debian Copyright Header object
*/
static fromParagraph(paragraph) {
const header = new DebianHeader("");
for (const token of (0, parser_1.extractData)(paragraph)) {
switch (token.type) {
case "format":
if (typeof token.data === "string") {
header.format = token.data;
}
break;
case "upstreamName":
if (typeof token.data === "string") {
header.upstreamName = token.data;
}
break;
case "upstreamContact":
if (Array.isArray(token.data)) {
header.upstreamContact = token.data;
}
break;
case "source":
if (typeof token.data === "string") {
header.source = token.data;
}
break;
case "disclaimer":
if (typeof token.data === "string") {
header.disclaimer = token.data;
}
break;
case "comment":
if (typeof token.data === "string") {
header.comment = token.data;
}
break;
case "license":
if (typeof token.data === "string") {
header.license = token.data;
}
break;
case "copyright":
if (typeof token.data === "string") {
header.copyright = token.data;
}
break;
}
}
return header;
}
}
/**
* Debian File stanza
* @class DebianFile
*/
class DebianFile {
constructor(files, copyright, license) {
this.files = files;
this.copyright = copyright;
this.license = license;
}
/**
* Converts a paragraph to a Debian File Stanza object
* @param paragraph Paragraph to convert
* @returns Debian File Stanza object
*/
static fromParagraph(paragraph) {
const file = new DebianFile([], "", "");
for (const token of (0, parser_1.extractData)(paragraph)) {
switch (token.type) {
case "files":
if (Array.isArray(token.data)) {
file.files = token.data;
}
break;
case "copyright":
if (typeof token.data === "string") {
file.copyright = token.data;
}
break;
case "comment":
if (typeof token.data === "string") {
file.comment = token.data;
}
break;
case "license":
if (typeof token.data === "string") {
file.license = token.data;
}
break;
}
}
return file;
}
}