UNPKG

@atomist/sdm-pack-fingerprints

Version:

an Atomist SDM Extension Pack for fingerprinting code

181 lines 7 kB
"use strict"; /* * Copyright © 2019 Atomist, Inc. * * 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 * * http://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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { 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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const automation_client_1 = require("@atomist/automation-client"); const sdm_1 = require("@atomist/sdm"); const slack_messages_1 = require("@atomist/slack-messages"); const _ = require("lodash"); const __1 = require("../.."); const updateTarget_1 = require("../handlers/commands/updateTarget"); const fingerprintSupport_1 = require("../machine/fingerprintSupport"); /** * Construct an npmdep fingerprint from the given library and version * @param {string} lib * @param {string} version * @return {FP} */ function createNpmDepFingerprint(lib, version) { const data = [lib, version]; return { type: NpmDepsName, name: `${constructNpmDepsFingerprintName(lib)}`, abbreviation: "npmdeps", version: "0.0.1", data, sha: __1.sha256(JSON.stringify(data)), }; } exports.createNpmDepFingerprint = createNpmDepFingerprint; function constructNpmDepsFingerprintName(lib) { return `${lib.replace("@", "").replace("/", "::")}`; } exports.constructNpmDepsFingerprintName = constructNpmDepsFingerprintName; /** * Return the library name in its natural form - e.g. "lodash" or "@types/lodash" or "@atomist/sdm" * @param {string} fingerprintName * @return {string | undefined} */ function deconstructNpmDepsFingerprintName(fingerprintName) { const regex = /^([^:]+)(::.*)?$/; const match = regex.exec(fingerprintName); if (!match) { return undefined; } if (match[2] !== undefined) { const lib = match[2].replace("::", ""); const owner = match[1]; return `@${owner}/${lib}`; } else { const lib = match[1]; return lib; } } exports.deconstructNpmDepsFingerprintName = deconstructNpmDepsFingerprintName; exports.createNpmDepsFingerprints = (p) => __awaiter(this, void 0, void 0, function* () { const file = yield p.getFile("package.json"); if (file) { const jsonData = JSON.parse(yield file.getContent()); const dependencies = _.merge(jsonData.dependencies || {}, jsonData.devDependencies || {}); const fingerprints = []; for (const [lib, version] of Object.entries(dependencies)) { fingerprints.push(createNpmDepFingerprint(lib, version)); } return fingerprints; } else { return undefined; } }); exports.createNpmCoordinatesFingerprint = (p) => __awaiter(this, void 0, void 0, function* () { const file = yield p.getFile("package.json"); if (file) { const jsonData = JSON.parse(yield file.getContent()); const fingerprints = []; const coords = { name: jsonData.name, version: jsonData.version }; fingerprints.push({ name: exports.NpmCoordinates.name, abbreviation: exports.NpmCoordinates.name, version: "0.0.1", data: coords, sha: __1.sha256(JSON.stringify(coords)), }); return fingerprints; } else { return undefined; } }); exports.applyNpmDepsFingerprint = (p, fp) => __awaiter(this, void 0, void 0, function* () { const pckage = fp.data[0]; const version = fp.data[1]; const file = yield p.getFile("package.json"); if (!!file) { const pj = (yield file.getContent()) .replace(new RegExp(`"${pckage}":\\s*".*"`, "g"), `"${pckage}": "${version}"`); yield file.setContent(pj); const log = new sdm_1.LoggingProgressLog("npm install"); const result = yield sdm_1.spawnLog("npm", ["install"], { cwd: p.baseDir, log, logCommand: true, }); automation_client_1.logger.info(log.log); return result.code === 0; } else { return false; } }); /* tslint:disable:max-line-length */ exports.diffNpmDepsFingerprints = (diff, target) => { return { title: "New NPM Package Policy", description: `Policy version for NPM package ${slack_messages_1.bold(diff.from.data[0])} is ${slack_messages_1.codeLine(target.data[1])}.\nProject ${slack_messages_1.bold(`${diff.owner}/${diff.repo}/${diff.branch}`)} is currently configured to use version ${slack_messages_1.codeLine(diff.to.data[1])}.`, }; }; /* tslint:disable:max-line-length */ exports.diffNpmCoordinatesFingerprints = (diff, target) => { return { title: "New Package Coordinate Updated", description: `from ${diff.from.data} to ${diff.to.data}`, }; }; const NpmDepsName = "npm-project-deps"; /** * Aspect emitting 0 or more npm dependencies fingerprints. */ exports.NpmDeps = { displayName: "NPM dependencies", name: NpmDepsName, extract: exports.createNpmDepsFingerprints, apply: exports.applyNpmDepsFingerprint, summary: exports.diffNpmDepsFingerprints, toDisplayableFingerprint: fp => fp.data[1], toDisplayableFingerprintName: deconstructNpmDepsFingerprintName, workflows: [ fingerprintSupport_1.DefaultTargetDiffHandler, ], }; exports.NpmCoordinates = { displayName: "NPM coordinates", name: "npm-project-coordinates", extract: exports.createNpmCoordinatesFingerprint, summary: exports.diffNpmCoordinatesFingerprints, toDisplayableFingerprint: fp => fp.data, workflows: [ fingerprintSupport_1.diffOnlyHandler((ctx, diff) => { if (diff.channel) { return updateTarget_1.setNewTargetFingerprint(ctx.context, exports.NpmDeps, createNpmDepFingerprint(diff.to.data.name, diff.to.data.version), diff.channel); } else { return new Promise((resolve, reject) => { resolve({ abstain: true }); }); } }), ], }; //# sourceMappingURL=npmDeps.js.map