Skip to content

Commit a30b785

Browse files
committed
Add suggested changes, fix removeFromCuration for CDDL
This generalizes the logic that parses property values of a crawn index to gather extract files, and applies it throughout. This actually fixes the `removeFromCuration` logic that was broken for CDDL.
1 parent b850ee3 commit a30b785

File tree

3 files changed

+40
-35
lines changed

3 files changed

+40
-35
lines changed

tools/apply-patches.js

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import path from 'node:path';
1919
import util from 'node:util';
2020
import { fileURLToPath } from 'node:url';
2121
import { execFile as execCb } from 'node:child_process';
22-
import { createFolderIfNeeded, loadJSON } from './utils.js';
22+
import { createFolderIfNeeded, loadJSON, getTargetedExtracts } from './utils.js';
2323
const execFile = util.promisify(execCb);
2424

2525
async function applyPatches(rawFolder, outputFolder, type) {
@@ -121,7 +121,6 @@ async function applyFreezePatches(rawFolder, outputFolder) {
121121
}
122122

123123
const shortname = file.replace(/\.json$/, '');
124-
const branchName = `freezepatch-${shortname}`;
125124
const patch = path.join(patchDir, file);
126125
const json = await loadJSON(patch);
127126

@@ -131,28 +130,14 @@ async function applyFreezePatches(rawFolder, outputFolder) {
131130
// Get back to the patch commit
132131
// (note this does not touch the `curated` folder because it is in
133132
// the `.gitignore` file)
134-
await execFile('git', ['checkout', '-B', branchName, json.commit]);
133+
await execFile('git', ['checkout', json.commit]);
135134

136135
const crawlIndex = await loadJSON(path.join(rawFolder, 'index.json'));
137136
const crawlSpec = crawlIndex.results.find(spec => spec.shortname === shortname);
138137

139-
for (const [extractType, extractFile] of Object.entries(crawlSpec)) {
140-
if (extractType === 'cddl') {
141-
// Handle CDDL extracts separately, it's an array of extracts
142-
for (const { file: cddlFile } of extractFile) {
143-
await fs.copyFile(
144-
path.join(rawFolder, cddlFile),
145-
path.join(outputFolder, cddlFile)
146-
);
147-
}
148-
}
149-
else if (!extractFile ||
150-
(typeof extractFile !== 'string') ||
151-
!extractFile.match(/^[^\/]+\/[^\/]+\.(json|idl)$/)) {
152-
// Skip properties that do not link to an extract
153-
continue;
154-
}
155-
else {
138+
for (const propValue of Object.values(crawlSpec)) {
139+
const extractFiles = getTargetedExtracts(propValue);
140+
for (const extractFile of extracFiles) {
156141
await fs.copyFile(
157142
path.join(rawFolder, extractFile),
158143
path.join(outputFolder, extractFile)
@@ -162,7 +147,6 @@ async function applyFreezePatches(rawFolder, outputFolder) {
162147
}
163148

164149
await execFile('git', ['checkout', 'main']);
165-
await execFile('git', ['branch', '-D', branchName]);
166150
patchApplied = true;
167151
}
168152

tools/prepare-curated.js

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ import { rimraf } from 'rimraf';
2323
import {
2424
createFolderIfNeeded,
2525
loadJSON,
26-
copyFolder } from './utils.js';
26+
copyFolder,
27+
getTargetedExtracts } from './utils.js';
2728
import { applyPatches } from './apply-patches.js';
2829
import { dropCSSPropertyDuplicates } from './drop-css-property-duplicates.js';
2930
import { curateEvents } from './amend-event-data.js';
@@ -35,10 +36,9 @@ import { crawlSpecs } from 'reffy';
3536
*/
3637
async function removeFromCuration(spec, curatedFolder) {
3738
for (const property of ['cddl', 'css', 'elements', 'events', 'idl']) {
38-
if (spec[property] &&
39-
(typeof spec[property] === 'string') &&
40-
spec[property].match(/^[^\/]+\/[^\/]+\.(json|idl|cddl)$/)) {
41-
const filename = path.join(curatedFolder, spec[property]);
39+
const extractFiles = getTargetedExtracts(spec[property]);
40+
for (const extractFile of extractFiles) {
41+
const filename = path.join(curatedFolder, extractFile);
4242
console.log(`Removing ${spec.standing} ${spec.title} from curation: del ${filename}`);
4343
await fs.unlink(filename);
4444
}
@@ -56,16 +56,15 @@ async function removeFromCuration(spec, curatedFolder) {
5656
async function cleanCrawlOutcome(spec) {
5757
for (const property of Object.keys(spec)) {
5858
// Only consider properties that link to an extract
59-
if (spec[property] &&
60-
(typeof spec[property] === 'string') &&
61-
spec[property].match(/^[^\/]+\/[^\/]+\.(json|idl|cddl)$/)) {
62-
try {
63-
await fs.lstat(path.join(curatedFolder, spec[property]));
64-
}
65-
catch (err) {
66-
delete spec[property];
59+
const extractFiles = getTargetedExtracts(spec[property]);
60+
try {
61+
for (const extractFile of extractFiles) {
62+
await fs.lstat(path.join(curatedFolder, extractFile));
6763
}
6864
}
65+
catch (err) {
66+
delete spec[property];
67+
}
6968
}
7069
}
7170

tools/utils.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,30 @@ async function copyFolder(source, target, { excludeRoot = false } = {}) {
6868
};
6969

7070

71+
/**
72+
* Return the list of extract files that the given value targets.
73+
*
74+
* Note: The `cddl` property value targets an array of extracts, the actual
75+
* extract being under the `file` key each time.
76+
*/
77+
function getTargetedExtracts(value) {
78+
const reExtractFile = /^[^\/]+\/[^\/]+\.(json|idl|cddl)$/;
79+
if (Array.isArray(value)) {
80+
return value
81+
.filter(v => typeof v.file === 'string' && v.file.match(reExtractFile))
82+
.map(v => v.file);
83+
}
84+
else if (typeof value === 'string') {
85+
return [value];
86+
}
87+
else {
88+
return [];
89+
}
90+
}
91+
7192
export {
7293
createFolderIfNeeded,
7394
loadJSON,
74-
copyFolder
95+
copyFolder,
96+
getTargetedExtracts
7597
};

0 commit comments

Comments
 (0)