forked from w3c/webref
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapply-patches.js
111 lines (97 loc) · 3.47 KB
/
apply-patches.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/**
* Apply patches
*
* The output folder gets created if it does not exist yet.
*
* node tools/apply-patches.js [raw folder] [output folder] [type]
*
* ... where:
* - [raw folder] is the name of the folder that contains the raw data and the
* patches (default is "ed");
* - [output folder] the name of the folder that is to contain the results of
* applying the patches (default is "curated");
* - [type] is the type of patches to apply: "css", "elements", "idl", or "all"
* (default is "all").
*/
const fs = require('fs').promises;
const path = require('path');
const util = require('util');
const execFile = util.promisify(require('child_process').execFile);
const { createFolderIfNeeded } = require('./utils');
async function applyPatches(rawFolder, outputFolder, type) {
type = (type === 'all') ? ['css', 'elements', 'idl'] : [type];
const packages = [
{
name: 'css',
srcDir: path.join(rawFolder, 'css'),
dstDir: path.join(outputFolder, 'css'),
dstDirForCli: [outputFolder, 'css'].join('/'),
patchDir: path.join(rawFolder, 'csspatches'),
fileExt: 'json'
},
{
name: 'elements',
srcDir: path.join(rawFolder, 'elements'),
dstDir: path.join(outputFolder, 'elements'),
dstDirForCli: [outputFolder, 'elements'].join('/'),
patchDir: path.join(rawFolder, 'elementspatches'),
fileExt: 'json'
},
{
name: 'idl',
srcDir: path.join(rawFolder, 'idl'),
dstDir: path.join(outputFolder, 'idl'),
dstDirForCli: [outputFolder, 'idl'].join('/'),
patchDir: path.join(rawFolder, 'idlpatches'),
fileExt: 'idl'
}
];
await createFolderIfNeeded(outputFolder);
for (const { name, srcDir, dstDir, patchDir, fileExt } of packages) {
if (!type.includes(name)) {
continue;
}
await createFolderIfNeeded(dstDir);
// rm dstDir/*.${fileExt}
const dstFiles = await fs.readdir(dstDir);
for (const file of dstFiles) {
if (file.endsWith(`.${fileExt}`) && file !== 'package.json') {
await fs.unlink(path.join(dstDir, file));
}
}
// cp srcDir/*.${fileExt} dstDir/
const srcFiles = await fs.readdir(srcDir);
for (const file of srcFiles) {
if (file.endsWith('.' + fileExt)) {
await fs.copyFile(path.join(srcDir, file), path.join(dstDir, file));
}
}
// The patches are against srcDir and can be applied there using `git am`,
// but apply them in dstDir instead using `git apply`.
// See ed/idlpatches/README.md for how to maintain these patches.
const patchFiles = await fs.readdir(patchDir);
for (const file of patchFiles) {
if (file.endsWith('.patch')) {
const patch = path.join(patchDir, file);
console.log(`Applying ${path.relative(rawFolder, patch)}`);
await execFile('git', ['apply', `--directory=${outputFolder}/${name}`, '-p3', patch]);
}
}
}
}
/**************************************************
Export methods for use as module
**************************************************/
module.exports.applyPatches = applyPatches;
/**************************************************
Code run if the code is run as a stand-alone module
**************************************************/
if (require.main === module) {
const rawFolder = process.argv[2] ?? 'ed';
const outputFolder = process.argv[3] ?? 'curated';
const type = process.argv[4] ?? 'all';
applyPatches(rawFolder, outputFolder, type).catch(e => {
console.error(e);
process.exit(1);
});
}