Skip to content

Commit 13e47f8

Browse files
committed
Type options and add --force option
1 parent 5f152be commit 13e47f8

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

pr-checks/sync_back.test.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import * as path from "node:path";
1111
import { afterEach, beforeEach, describe, it } from "node:test";
1212

1313
import {
14+
Options,
1415
scanGeneratedWorkflows,
1516
updateSyncTs,
1617
updateTemplateFiles,
@@ -21,6 +22,8 @@ let workflowDir: string;
2122
let checksDir: string;
2223
let syncTsPath: string;
2324

25+
const defaultOptions: Options = { verbose: false, force: false };
26+
2427
beforeEach(() => {
2528
/** Set up temporary directories and files for testing */
2629
testDir = fs.mkdtempSync(path.join(os.tmpdir(), "sync-back-test-"));
@@ -150,7 +153,7 @@ const steps = [
150153
"actions/setup-go": { version: "v6" },
151154
};
152155

153-
const result = updateSyncTs(syncTsPath, actionVersions);
156+
const result = updateSyncTs(defaultOptions, syncTsPath, actionVersions);
154157
assert.equal(result, true);
155158

156159
const updatedContent = fs.readFileSync(syncTsPath, "utf8");
@@ -176,7 +179,7 @@ const steps = [
176179
"actions/setup-node": { version: "v5", comment: " Latest version" },
177180
};
178181

179-
const result = updateSyncTs(syncTsPath, actionVersions);
182+
const result = updateSyncTs(defaultOptions, syncTsPath, actionVersions);
180183
assert.equal(result, true);
181184

182185
const updatedContent = fs.readFileSync(syncTsPath, "utf8");
@@ -203,7 +206,7 @@ const steps = [
203206
"actions/setup-node": { version: "v5" },
204207
};
205208

206-
const result = updateSyncTs(syncTsPath, actionVersions);
209+
const result = updateSyncTs(defaultOptions, syncTsPath, actionVersions);
207210
assert.equal(result, false);
208211
});
209212
});
@@ -228,7 +231,11 @@ steps:
228231
"actions/setup-node": { version: "v5", comment: " Latest" },
229232
};
230233

231-
const result = updateTemplateFiles(checksDir, actionVersions);
234+
const result = updateTemplateFiles(
235+
defaultOptions,
236+
checksDir,
237+
actionVersions,
238+
);
232239
assert.equal(result.length, 1);
233240
assert.ok(result.includes(templatePath));
234241

@@ -256,7 +263,11 @@ steps:
256263
},
257264
};
258265

259-
const result = updateTemplateFiles(checksDir, actionVersions);
266+
const result = updateTemplateFiles(
267+
defaultOptions,
268+
checksDir,
269+
actionVersions,
270+
);
260271
assert.equal(result.length, 1);
261272

262273
const updatedContent = fs.readFileSync(templatePath, "utf8");

pr-checks/sync_back.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ const CHECKS_DIR = path.join(THIS_DIR, "checks");
2929
const WORKFLOW_DIR = path.join(THIS_DIR, "..", ".github", "workflows");
3030
const SYNC_TS_PATH = path.join(THIS_DIR, "sync.ts");
3131

32+
/** Command-line options for this program. */
33+
export type Options = {
34+
verbose: boolean;
35+
force: boolean;
36+
};
37+
3238
/** Records information about the version of an Action with an optional comment. */
3339
type ActionVersion = { version: string; comment?: string };
3440

@@ -122,11 +128,13 @@ export function scanGeneratedWorkflows(
122128
/**
123129
* Update hardcoded action versions in pr-checks/sync.ts
124130
*
131+
* @param options - The command-line options.
125132
* @param syncTsPath - Path to sync.ts file
126133
* @param actionVersions - Map of action names to versions (may include comments)
127134
* @returns True if the file was modified, false otherwise
128135
*/
129136
export function updateSyncTs(
137+
options: Options,
130138
syncTsPath: string,
131139
actionVersions: Record<string, ActionVersion>,
132140
): boolean {
@@ -150,7 +158,7 @@ export function updateSyncTs(
150158
);
151159
}
152160

153-
if (content !== originalContent) {
161+
if (content !== originalContent || options.force) {
154162
fs.writeFileSync(syncTsPath, content, "utf8");
155163
console.info(`Updated ${syncTsPath}`);
156164
return true;
@@ -163,11 +171,13 @@ export function updateSyncTs(
163171
/**
164172
* Update action versions in template files in pr-checks/checks/
165173
*
174+
* @param options - The command-line options.
166175
* @param checksDir - Path to pr-checks/checks directory
167176
* @param actionVersions - Map of action names to versions (may include comments)
168177
* @returns List of files that were modified
169178
*/
170179
export function updateTemplateFiles(
180+
options: Options,
171181
checksDir: string,
172182
actionVersions: Record<string, ActionVersion>,
173183
): string[] {
@@ -200,7 +210,7 @@ export function updateTemplateFiles(
200210
);
201211

202212
// Write the YAML document back to the file if we made changes.
203-
if (modified) {
213+
if (modified || options.force) {
204214
fs.writeFileSync(
205215
filePath,
206216
yaml.stringify(doc, { lineWidth: 0, flowCollectionPadding: false }),
@@ -222,6 +232,11 @@ function main(): number {
222232
short: "v",
223233
default: false,
224234
},
235+
force: {
236+
type: "boolean",
237+
short: "f",
238+
default: false,
239+
},
225240
},
226241
strict: true,
227242
});
@@ -248,12 +263,16 @@ function main(): number {
248263
const modifiedFiles: string[] = [];
249264

250265
// Update sync.ts
251-
if (updateSyncTs(SYNC_TS_PATH, actionVersions)) {
266+
if (updateSyncTs(values, SYNC_TS_PATH, actionVersions)) {
252267
modifiedFiles.push(SYNC_TS_PATH);
253268
}
254269

255270
// Update template files
256-
const templateModified = updateTemplateFiles(CHECKS_DIR, actionVersions);
271+
const templateModified = updateTemplateFiles(
272+
values,
273+
CHECKS_DIR,
274+
actionVersions,
275+
);
257276
modifiedFiles.push(...templateModified);
258277

259278
if (modifiedFiles.length > 0) {

0 commit comments

Comments
 (0)