UNPKG

@atomist/atomist-sdm

Version:

Atomist SDM to deliver our own projects

74 lines 3.25 kB
"use strict"; /* * Copyright © 2018 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"); /** * Change the version of NPM that gets installed into our Docker images * @type {{name: string; pushTest: PredicatePushTest; transform: (p) => Promise<Project>}} */ function npmDockerfileFix(...modules) { return { name: "Dockerfile NPM install", pushTest: sdm_1.allSatisfied(sdm_1.hasFile("Dockerfile"), sdm_1.ToDefaultBranch), transform: (p) => __awaiter(this, void 0, void 0, function* () { const df = yield p.getFile("Dockerfile"); let dfc = yield df.getContent(); for (const m of modules) { dfc = yield updateToLatestVersion(m, dfc); } yield df.setContent(dfc); return p; }), }; } exports.npmDockerfileFix = npmDockerfileFix; function updateToLatestVersion(module, content) { return __awaiter(this, void 0, void 0, function* () { const log = new sdm_1.StringCapturingProgressLog(); const result = yield sdm_1.spawnLog("npm", ["show", module, "version"], { logCommand: false, log, }); if (result.code !== 0) { return content; } automation_client_1.logger.info(`Updating ${module} install to version '${log.log.trim()}'`); return updateNpmInstall(content, module, log.log.trim()); }); } exports.updateToLatestVersion = updateToLatestVersion; /** * Replace the version in an NPM install command. * * @param module module being installed * @param version desired version * @return content with installation of new version */ function updateNpmInstall(content, module, version) { return content.replace(new RegExp(`npm(\\s+(?:.*\\s+)?)(?:i|install|add)(\\s+(?:.*\\s+)?)${module}(?:@\\S+)?(.*)`), `npm\$1install\$2${module}@${version}\$3`); } exports.updateNpmInstall = updateNpmInstall; //# sourceMappingURL=dockerfileFix.js.map