UNPKG

2.98 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright © 2019 Atomist, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.doWithProject = exports.toProjectAwareGoalInvocation = void 0;
19const string_1 = require("@atomist/automation-client/lib/internal/util/string");
20const child_process_1 = require("../misc/child_process");
21/**
22 * Convenience method to create project aware goal invocations with
23 * spawn and exec functions that, by default, use the cloned project
24 * base directory as the current working directory.
25 *
26 * @param project locally cloned project
27 * @param gi SDM goal invocation
28 * @return goal invocation made project aware
29 */
30function toProjectAwareGoalInvocation(project, gi) {
31 const { progressLog } = gi;
32 const spawn = pagiSpawn(project, progressLog);
33 const exec = pagiExec(project);
34 return Object.assign(Object.assign({}, gi), { project, spawn, exec });
35}
36exports.toProjectAwareGoalInvocation = toProjectAwareGoalInvocation;
37/**
38 * Convenience method to create goal implementations that require a local clone of the project.
39 * @param {(pa: ProjectGoalInvocation) => Promise<ExecuteGoalResult>} action
40 * @param {CloneOptions & {readOnly: boolean}} cloneOptions
41 * @returns {ExecuteGoal}
42 */
43function doWithProject(action, cloneOptions = { readOnly: false }) {
44 return gi => {
45 const { context, credentials, id, configuration, progressLog } = gi;
46 return configuration.sdm.projectLoader.doWithProject({
47 context,
48 credentials,
49 id,
50 readOnly: cloneOptions.readOnly,
51 cloneOptions,
52 }, (p) => action(Object.assign(Object.assign({}, gi), { project: p, spawn: pagiSpawn(p, progressLog), exec: pagiExec(p) })));
53 };
54}
55exports.doWithProject = doWithProject;
56/**
57 * Return spawn function for project-aware goal invocations.
58 */
59function pagiSpawn(p, log) {
60 return (cmd, args = [], opts) => {
61 const optsToUse = Object.assign({ cwd: p.baseDir, log }, opts);
62 return child_process_1.spawnLog(cmd, string_1.toStringArray(args), optsToUse);
63 };
64}
65/**
66 * Return exec function for project-aware goal invocations.
67 */
68function pagiExec(p) {
69 return (cmd, args = [], opts = {}) => {
70 const optsToUse = Object.assign({ cwd: p.baseDir }, opts);
71 return child_process_1.execPromise(cmd, string_1.toStringArray(args), optsToUse);
72 };
73}
74//# sourceMappingURL=withProject.js.map
\No newline at end of file