@atomist/sdm-pack-node
Version:
Extension pack for an Atomist SDM to work with Node.js projects
138 lines • 5.67 kB
JavaScript
;
/*
* 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 sdm_core_1 = require("@atomist/sdm-core");
const fs = require("fs-extra");
const p = require("path");
/**
* Execute npm publish
*
* Tags with branch-name unless the `tag` option is specified. If the branch === the repo's default branch
* also the next tags is being set
*
* @param {ProjectLoader} projectLoader
* @param {ProjectIdentifier} projectIdentifier
* @param {PrepareForGoalExecution[]} preparations
* @return {ExecuteGoal}
*/
function executePublish(projectIdentifier, options) {
return sdm_1.doWithProject((goalInvocation) => __awaiter(this, void 0, void 0, function* () {
const { credentials, id, project, goalEvent } = goalInvocation;
if (!(yield sdm_1.projectConfigurationValue("npm.publish.enabled", project, true))) {
return {
code: 0,
description: "Publish disabled",
state: sdm_1.SdmGoalState.success,
};
}
yield configureNpmRc(options, project);
const args = [
p.join(__dirname, "..", "..", "assets", "scripts", "npm-publish.bash"),
];
if (!!options.registry) {
args.push("--registry", options.registry);
}
const access = yield sdm_1.projectConfigurationValue("npm.publish.access", project, options.access);
if (access) {
args.push("--access", access);
}
if (!!options.tag) {
args.push("--tag", options.tag);
}
else {
args.push("--tag", gitBranchToNpmTag(id.branch));
}
let result = yield goalInvocation.spawn("bash", args);
if (result.code === 0) {
const pi = yield projectIdentifier(project);
// Additionally publish the next tag
if (goalEvent.push.repo.defaultBranch === goalEvent.branch && options.nextTag !== false) {
const nextArgs = ["dist-tag", "add", `${pi.name}@${pi.version}`, "next"];
if (!!options.registry) {
nextArgs.push("--registry", options.registry);
}
result = yield goalInvocation.spawn("npm", nextArgs);
if (result.code !== 0) {
return result;
}
}
const url = `${options.registry}/${pi.name}/-/${pi.name}-${pi.version}.tgz`;
result.externalUrls = [{
label: "NPM package",
url,
}];
if (options.status) {
yield sdm_core_1.github.createStatus(credentials, id, {
context: "npm/atomist/package",
description: "NPM package",
target_url: url,
state: "success",
});
}
}
return result;
}));
}
exports.executePublish = executePublish;
function deleteBranchTag(branch, project, options) {
return __awaiter(this, void 0, void 0, function* () {
const pj = yield project.getFile("package.json");
if (pj) {
const tag = gitBranchToNpmTag(branch);
const name = JSON.parse(yield pj.getContent()).name;
yield configureNpmRc(options, project);
const result = yield sdm_1.spawnLog("npm", ["dist-tags", "rm", name, tag], {
cwd: project.baseDir,
log: new sdm_1.LoggingProgressLog("npm dist-tag rm"),
});
return result;
}
return automation_client_1.Success;
});
}
exports.deleteBranchTag = deleteBranchTag;
/**
* Create an npmrc file for the package.
*/
function configureNpmRc(options, project) {
return __awaiter(this, void 0, void 0, function* () {
yield fs.writeFile(p.join(project.baseDir, ".npmrc"), options.npmrc, { mode: 0o600 });
return options;
});
}
exports.configureNpmRc = configureNpmRc;
function gitBranchToNpmTag(branchName) {
return `branch-${gitBranchToNpmVersion(branchName)}`;
}
exports.gitBranchToNpmTag = gitBranchToNpmTag;
function gitBranchToNpmVersion(branchName) {
return branchName.replace(/\//g, "-")
.replace(/_/g, "-")
.replace(/@/g, "");
}
exports.gitBranchToNpmVersion = gitBranchToNpmVersion;
//# sourceMappingURL=executePublish.js.map