Add max parallelism as input option
This commit is contained in:
parent
31301c4429
commit
4245b3de99
|
|
@ -2,11 +2,35 @@ import fs from 'fs';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import {
|
import {
|
||||||
getFiles,
|
getFiles,
|
||||||
|
isPositiveInteger,
|
||||||
isValidEncoding,
|
isValidEncoding,
|
||||||
processInChunks,
|
processInChunks,
|
||||||
replaceTextInFile,
|
replaceTextInFile,
|
||||||
} from '../src/utils';
|
} from '../src/utils';
|
||||||
|
|
||||||
|
describe('isPositiveInteger', () => {
|
||||||
|
it('should return true for positive integers', () => {
|
||||||
|
expect(isPositiveInteger('1')).toBe(true);
|
||||||
|
expect(isPositiveInteger('123')).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return false for zero', () => {
|
||||||
|
expect(isPositiveInteger('0')).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return false for negative integers', () => {
|
||||||
|
expect(isPositiveInteger('-1')).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return false for non-integer numbers', () => {
|
||||||
|
expect(isPositiveInteger('1.5')).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return false for non-numeric strings', () => {
|
||||||
|
expect(isPositiveInteger('abc')).toBe(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('isValidEncoding', () => {
|
describe('isValidEncoding', () => {
|
||||||
it('should return true for valid encodings', () => {
|
it('should return true for valid encodings', () => {
|
||||||
expect(isValidEncoding('ascii')).toBe(true);
|
expect(isValidEncoding('ascii')).toBe(true);
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,12 @@ inputs:
|
||||||
It can be the path to a file or a glob pattern (e.g. `**/*.md`).
|
It can be the path to a file or a glob pattern (e.g. `**/*.md`).
|
||||||
default: ""
|
default: ""
|
||||||
required: false
|
required: false
|
||||||
|
max-parallelism:
|
||||||
|
description: |-
|
||||||
|
(Optional) The maximum number of files to process in parallel.
|
||||||
|
Defaults to `10`.
|
||||||
|
default: "10"
|
||||||
|
required: false
|
||||||
|
|
||||||
branding:
|
branding:
|
||||||
icon: "edit"
|
icon: "edit"
|
||||||
|
|
|
||||||
|
|
@ -28,10 +28,15 @@ function run() {
|
||||||
const replaceText = (0, core_1.getInput)('replacement-text');
|
const replaceText = (0, core_1.getInput)('replacement-text');
|
||||||
const excludePattern = (0, core_1.getInput)('exclude');
|
const excludePattern = (0, core_1.getInput)('exclude');
|
||||||
const inputEncoding = (0, core_1.getInput)('encoding');
|
const inputEncoding = (0, core_1.getInput)('encoding');
|
||||||
|
const maxParallelism = (0, core_1.getInput)('max-parallelism');
|
||||||
// Validate the encoding
|
// Validate the encoding
|
||||||
if (!(0, utils_1.isValidEncoding)(inputEncoding)) {
|
if (!(0, utils_1.isValidEncoding)(inputEncoding)) {
|
||||||
throw new Error(`Invalid encoding: ${inputEncoding}`);
|
throw new Error(`Invalid encoding: ${inputEncoding}`);
|
||||||
}
|
}
|
||||||
|
// Validate that maxParallelism is a positive integer
|
||||||
|
if (!(0, utils_1.isPositiveInteger)(maxParallelism)) {
|
||||||
|
throw new Error(`Invalid max-parallelism: ${maxParallelism}`);
|
||||||
|
}
|
||||||
// Get the file paths that match the files pattern and do not match the exclude pattern
|
// Get the file paths that match the files pattern and do not match the exclude pattern
|
||||||
const filePaths = yield (0, utils_1.getFiles)(filesPattern, excludePattern);
|
const filePaths = yield (0, utils_1.getFiles)(filesPattern, excludePattern);
|
||||||
// If no file paths were found, log a warning and exit
|
// If no file paths were found, log a warning and exit
|
||||||
|
|
@ -41,10 +46,10 @@ function run() {
|
||||||
}
|
}
|
||||||
(0, core_1.info)(`Found ${filePaths.length} files for the given pattern.`);
|
(0, core_1.info)(`Found ${filePaths.length} files for the given pattern.`);
|
||||||
(0, core_1.info)(`Replacing "${searchText}" with "${replaceText}".`);
|
(0, core_1.info)(`Replacing "${searchText}" with "${replaceText}".`);
|
||||||
const encoding = inputEncoding;
|
|
||||||
// Process the file paths in chunks, replacing the search text with the replace text in each file
|
// Process the file paths in chunks, replacing the search text with the replace text in each file
|
||||||
// This is done to avoid opening too many files at once
|
// This is done to avoid opening too many files at once
|
||||||
const chunkSize = 10;
|
const encoding = inputEncoding;
|
||||||
|
const chunkSize = parseInt(maxParallelism);
|
||||||
yield (0, utils_1.processInChunks)(filePaths, (filePath) => __awaiter(this, void 0, void 0, function* () {
|
yield (0, utils_1.processInChunks)(filePaths, (filePath) => __awaiter(this, void 0, void 0, function* () {
|
||||||
(0, core_1.info)(`Replacing text in file ${filePath}`);
|
(0, core_1.info)(`Replacing text in file ${filePath}`);
|
||||||
yield (0, utils_1.replaceTextInFile)(filePath, searchText, replaceText, encoding);
|
yield (0, utils_1.replaceTextInFile)(filePath, searchText, replaceText, encoding);
|
||||||
|
|
@ -86,7 +91,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||||
};
|
};
|
||||||
Object.defineProperty(exports, "__esModule", ({ value: true }));
|
Object.defineProperty(exports, "__esModule", ({ value: true }));
|
||||||
exports.replaceTextInFile = exports.processInChunks = exports.getFiles = exports.isValidEncoding = void 0;
|
exports.replaceTextInFile = exports.processInChunks = exports.getFiles = exports.isValidEncoding = exports.isPositiveInteger = void 0;
|
||||||
const fs_1 = __importDefault(__nccwpck_require__(7147));
|
const fs_1 = __importDefault(__nccwpck_require__(7147));
|
||||||
const glob_1 = __nccwpck_require__(8211);
|
const glob_1 = __nccwpck_require__(8211);
|
||||||
const encodings = [
|
const encodings = [
|
||||||
|
|
@ -97,6 +102,16 @@ const encodings = [
|
||||||
'base64',
|
'base64',
|
||||||
'latin1',
|
'latin1',
|
||||||
];
|
];
|
||||||
|
/**
|
||||||
|
* Checks if a given string represents a positive integer.
|
||||||
|
*
|
||||||
|
* @param value - The string to check.
|
||||||
|
* @returns True if the string represents a positive integer, false otherwise.
|
||||||
|
*/
|
||||||
|
function isPositiveInteger(value) {
|
||||||
|
return /^[1-9]\d*$/.test(value);
|
||||||
|
}
|
||||||
|
exports.isPositiveInteger = isPositiveInteger;
|
||||||
/**
|
/**
|
||||||
* Checks if the given encoding is supported.
|
* Checks if the given encoding is supported.
|
||||||
* @param encoding The encoding to check.
|
* @param encoding The encoding to check.
|
||||||
|
|
|
||||||
File diff suppressed because one or more lines are too long
12
src/main.ts
12
src/main.ts
|
|
@ -2,6 +2,7 @@ import { debug, getInput, info, setFailed, warning } from '@actions/core';
|
||||||
import {
|
import {
|
||||||
Encoding,
|
Encoding,
|
||||||
getFiles,
|
getFiles,
|
||||||
|
isPositiveInteger,
|
||||||
isValidEncoding,
|
isValidEncoding,
|
||||||
processInChunks,
|
processInChunks,
|
||||||
replaceTextInFile,
|
replaceTextInFile,
|
||||||
|
|
@ -16,12 +17,18 @@ async function run(): Promise<void> {
|
||||||
const replaceText = getInput('replacement-text');
|
const replaceText = getInput('replacement-text');
|
||||||
const excludePattern = getInput('exclude');
|
const excludePattern = getInput('exclude');
|
||||||
const inputEncoding = getInput('encoding');
|
const inputEncoding = getInput('encoding');
|
||||||
|
const maxParallelism = getInput('max-parallelism');
|
||||||
|
|
||||||
// Validate the encoding
|
// Validate the encoding
|
||||||
if (!isValidEncoding(inputEncoding)) {
|
if (!isValidEncoding(inputEncoding)) {
|
||||||
throw new Error(`Invalid encoding: ${inputEncoding}`);
|
throw new Error(`Invalid encoding: ${inputEncoding}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Validate that maxParallelism is a positive integer
|
||||||
|
if (!isPositiveInteger(maxParallelism)) {
|
||||||
|
throw new Error(`Invalid max-parallelism: ${maxParallelism}`);
|
||||||
|
}
|
||||||
|
|
||||||
// Get the file paths that match the files pattern and do not match the exclude pattern
|
// Get the file paths that match the files pattern and do not match the exclude pattern
|
||||||
const filePaths = await getFiles(filesPattern, excludePattern);
|
const filePaths = await getFiles(filesPattern, excludePattern);
|
||||||
|
|
||||||
|
|
@ -34,11 +41,10 @@ async function run(): Promise<void> {
|
||||||
info(`Found ${filePaths.length} files for the given pattern.`);
|
info(`Found ${filePaths.length} files for the given pattern.`);
|
||||||
info(`Replacing "${searchText}" with "${replaceText}".`);
|
info(`Replacing "${searchText}" with "${replaceText}".`);
|
||||||
|
|
||||||
const encoding = inputEncoding as Encoding;
|
|
||||||
|
|
||||||
// Process the file paths in chunks, replacing the search text with the replace text in each file
|
// Process the file paths in chunks, replacing the search text with the replace text in each file
|
||||||
// This is done to avoid opening too many files at once
|
// This is done to avoid opening too many files at once
|
||||||
const chunkSize = 10;
|
const encoding = inputEncoding as Encoding;
|
||||||
|
const chunkSize = parseInt(maxParallelism);
|
||||||
await processInChunks(
|
await processInChunks(
|
||||||
filePaths,
|
filePaths,
|
||||||
async (filePath: string) => {
|
async (filePath: string) => {
|
||||||
|
|
|
||||||
10
src/utils.ts
10
src/utils.ts
|
|
@ -12,6 +12,16 @@ const encodings = [
|
||||||
|
|
||||||
export type Encoding = (typeof encodings)[number];
|
export type Encoding = (typeof encodings)[number];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if a given string represents a positive integer.
|
||||||
|
*
|
||||||
|
* @param value - The string to check.
|
||||||
|
* @returns True if the string represents a positive integer, false otherwise.
|
||||||
|
*/
|
||||||
|
export function isPositiveInteger(value: string): boolean {
|
||||||
|
return /^[1-9]\d*$/.test(value);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if the given encoding is supported.
|
* Checks if the given encoding is supported.
|
||||||
* @param encoding The encoding to check.
|
* @param encoding The encoding to check.
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue