Add project-dir

This commit is contained in:
Sergey Dolin 2023-04-10 16:52:10 +02:00
parent 869f4dd0c7
commit 02aa3290a3
4 changed files with 1385 additions and 1328 deletions

View file

@ -25,6 +25,8 @@ inputs:
description: 'Used to specify a package manager for caching in the default directory. Supported values: npm, yarn, pnpm.'
cache-dependency-path:
description: 'Used to specify the path to a dependency file: package-lock.json, yarn.lock, etc. Supports wildcards or a list of file names for caching multiple dependencies.'
project-dir:
description: 'Optional path used as a current working directory during executing package manager commands. Important if project directory is not the same as root.'
# TODO: add input to control forcing to pull from cloud or dist.
# escape valve for someone having issues or needing the absolute latest which isn't cached yet
outputs:

View file

@ -59244,11 +59244,15 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.isCacheFeatureAvailable = exports.isGhes = exports.getCacheDirectoryPath = exports.getPackageManagerInfo = exports.getCommandOutput = exports.supportedPackageManagers = void 0;
const core = __importStar(__nccwpck_require__(2186));
const exec = __importStar(__nccwpck_require__(1514));
const cache = __importStar(__nccwpck_require__(7799));
const path_1 = __importDefault(__nccwpck_require__(1017));
exports.supportedPackageManagers = {
npm: {
lockFilePatterns: ['package-lock.json', 'npm-shrinkwrap.json', 'yarn.lock'],
@ -59267,8 +59271,8 @@ exports.supportedPackageManagers = {
getCacheFolderCommand: 'yarn config get cacheFolder'
}
};
const getCommandOutput = (toolCommand) => __awaiter(void 0, void 0, void 0, function* () {
let { stdout, stderr, exitCode } = yield exec.getExecOutput(toolCommand, undefined, { ignoreReturnCode: true });
const getCommandOutput = (toolCommand, cwd) => __awaiter(void 0, void 0, void 0, function* () {
let { stdout, stderr, exitCode } = yield exec.getExecOutput(toolCommand, undefined, Object.assign({ ignoreReturnCode: true }, (cwd !== null && { cwd })));
if (exitCode) {
stderr = !stderr.trim()
? `The '${toolCommand}' command failed with exit code: ${exitCode}`
@ -59278,8 +59282,20 @@ const getCommandOutput = (toolCommand) => __awaiter(void 0, void 0, void 0, func
return stdout.trim();
});
exports.getCommandOutput = getCommandOutput;
const getPackageManagerWorkingDir = () => {
const projectDir = core.getInput('project-dir');
if (projectDir) {
return projectDir;
}
const cache = core.getInput('cache');
if (cache !== 'yarn') {
return null;
}
const cacheDependencyPath = core.getInput('cache-dependency-path');
return cacheDependencyPath ? path_1.default.dirname(cacheDependencyPath) : null;
};
const getPackageManagerVersion = (packageManager, command) => __awaiter(void 0, void 0, void 0, function* () {
const stdOut = yield exports.getCommandOutput(`${packageManager} ${command}`);
const stdOut = yield exports.getCommandOutput(`${packageManager} ${command}`, getPackageManagerWorkingDir());
if (!stdOut) {
throw new Error(`Could not retrieve version of ${packageManager}`);
}
@ -59308,7 +59324,7 @@ const getPackageManagerInfo = (packageManager) => __awaiter(void 0, void 0, void
});
exports.getPackageManagerInfo = getPackageManagerInfo;
const getCacheDirectoryPath = (packageManagerInfo, packageManager) => __awaiter(void 0, void 0, void 0, function* () {
const stdOut = yield exports.getCommandOutput(packageManagerInfo.getCacheFolderCommand);
const stdOut = yield exports.getCommandOutput(packageManagerInfo.getCacheFolderCommand, getPackageManagerWorkingDir());
if (!stdOut) {
throw new Error(`Could not get cache folder path for ${packageManager}`);
}

24
dist/setup/index.js vendored
View file

@ -71212,11 +71212,15 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.isCacheFeatureAvailable = exports.isGhes = exports.getCacheDirectoryPath = exports.getPackageManagerInfo = exports.getCommandOutput = exports.supportedPackageManagers = void 0;
const core = __importStar(__nccwpck_require__(2186));
const exec = __importStar(__nccwpck_require__(1514));
const cache = __importStar(__nccwpck_require__(7799));
const path_1 = __importDefault(__nccwpck_require__(1017));
exports.supportedPackageManagers = {
npm: {
lockFilePatterns: ['package-lock.json', 'npm-shrinkwrap.json', 'yarn.lock'],
@ -71235,8 +71239,8 @@ exports.supportedPackageManagers = {
getCacheFolderCommand: 'yarn config get cacheFolder'
}
};
const getCommandOutput = (toolCommand) => __awaiter(void 0, void 0, void 0, function* () {
let { stdout, stderr, exitCode } = yield exec.getExecOutput(toolCommand, undefined, { ignoreReturnCode: true });
const getCommandOutput = (toolCommand, cwd) => __awaiter(void 0, void 0, void 0, function* () {
let { stdout, stderr, exitCode } = yield exec.getExecOutput(toolCommand, undefined, Object.assign({ ignoreReturnCode: true }, (cwd !== null && { cwd })));
if (exitCode) {
stderr = !stderr.trim()
? `The '${toolCommand}' command failed with exit code: ${exitCode}`
@ -71246,8 +71250,20 @@ const getCommandOutput = (toolCommand) => __awaiter(void 0, void 0, void 0, func
return stdout.trim();
});
exports.getCommandOutput = getCommandOutput;
const getPackageManagerWorkingDir = () => {
const projectDir = core.getInput('project-dir');
if (projectDir) {
return projectDir;
}
const cache = core.getInput('cache');
if (cache !== 'yarn') {
return null;
}
const cacheDependencyPath = core.getInput('cache-dependency-path');
return cacheDependencyPath ? path_1.default.dirname(cacheDependencyPath) : null;
};
const getPackageManagerVersion = (packageManager, command) => __awaiter(void 0, void 0, void 0, function* () {
const stdOut = yield exports.getCommandOutput(`${packageManager} ${command}`);
const stdOut = yield exports.getCommandOutput(`${packageManager} ${command}`, getPackageManagerWorkingDir());
if (!stdOut) {
throw new Error(`Could not retrieve version of ${packageManager}`);
}
@ -71276,7 +71292,7 @@ const getPackageManagerInfo = (packageManager) => __awaiter(void 0, void 0, void
});
exports.getPackageManagerInfo = getPackageManagerInfo;
const getCacheDirectoryPath = (packageManagerInfo, packageManager) => __awaiter(void 0, void 0, void 0, function* () {
const stdOut = yield exports.getCommandOutput(packageManagerInfo.getCacheFolderCommand);
const stdOut = yield exports.getCommandOutput(packageManagerInfo.getCacheFolderCommand, getPackageManagerWorkingDir());
if (!stdOut) {
throw new Error(`Could not get cache folder path for ${packageManager}`);
}

View file

@ -1,6 +1,7 @@
import * as core from '@actions/core';
import * as exec from '@actions/exec';
import * as cache from '@actions/cache';
import path from 'path';
type SupportedPackageManagers = {
[prop: string]: PackageManagerInfo;
@ -30,11 +31,14 @@ export const supportedPackageManagers: SupportedPackageManagers = {
}
};
export const getCommandOutput = async (toolCommand: string) => {
export const getCommandOutput = async (
toolCommand: string,
cwd: string | null
) => {
let {stdout, stderr, exitCode} = await exec.getExecOutput(
toolCommand,
undefined,
{ignoreReturnCode: true}
{ignoreReturnCode: true, ...(cwd !== null && {cwd})}
);
if (exitCode) {
@ -47,11 +51,29 @@ export const getCommandOutput = async (toolCommand: string) => {
return stdout.trim();
};
const getPackageManagerWorkingDir = (): string | null => {
const projectDir = core.getInput('project-dir');
if (projectDir) {
return projectDir;
}
const cache = core.getInput('cache');
if (cache !== 'yarn') {
return null;
}
const cacheDependencyPath = core.getInput('cache-dependency-path');
return cacheDependencyPath ? path.dirname(cacheDependencyPath) : null;
};
const getPackageManagerVersion = async (
packageManager: string,
command: string
) => {
const stdOut = await getCommandOutput(`${packageManager} ${command}`);
const stdOut = await getCommandOutput(
`${packageManager} ${command}`,
getPackageManagerWorkingDir()
);
if (!stdOut) {
throw new Error(`Could not retrieve version of ${packageManager}`);
@ -85,7 +107,8 @@ export const getCacheDirectoryPath = async (
packageManager: string
) => {
const stdOut = await getCommandOutput(
packageManagerInfo.getCacheFolderCommand
packageManagerInfo.getCacheFolderCommand,
getPackageManagerWorkingDir()
);
if (!stdOut) {