js-green-licenses
Version: 
JavaScript package.json license checker
94 lines • 3.59 kB
JavaScript
;
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
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;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getGitHubConfig = exports.getLocalConfig = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const strip_json_comments_1 = __importDefault(require("strip-json-comments"));
const util_1 = require("util");
const fsReadFile = (0, util_1.promisify)(fs.readFile);
const CONFIG_FILE_NAME = 'js-green-licenses.json';
function isConfig(obj) {
    const config = obj;
    const isStringArray = (obj) => {
        return (!obj || (Array.isArray(obj) && obj.every(x => typeof x === 'string')));
    };
    return (isStringArray(config.greenLicenses) &&
        isStringArray(config.packageAllowlist));
}
function ensureConfig(obj) {
    if (!isConfig(obj)) {
        throw new Error('Invalid config contents');
    }
    return obj;
}
function parseJson(input) {
    return JSON.parse((0, strip_json_comments_1.default)(input));
}
async function getLocalConfig(directory) {
    try {
        const content = await fsReadFile(path.join(directory, CONFIG_FILE_NAME), 'utf8');
        return ensureConfig(parseJson(content));
    }
    catch (e) {
        const err = e;
        if (err.code !== 'ENOENT') {
            console.error('[js-green-licenses] Error while reading config file:', err);
        }
        return null;
    }
}
exports.getLocalConfig = getLocalConfig;
async function getGitHubConfig(repo, commitSha) {
    const content = await repo.getFileContent(commitSha, CONFIG_FILE_NAME);
    if (!content) {
        return null;
    }
    try {
        return ensureConfig(parseJson(content));
    }
    catch (err) {
        console.error('[js-green-licenses] Error while reading config file:', err);
        return null;
    }
}
exports.getGitHubConfig = getGitHubConfig;
//# sourceMappingURL=config.js.map