mirror of https://github.com/gorhill/uBlock.git
[mv3] Salvage rule ids for all rulesets
This commit is contained in:
parent
8d47eac6e6
commit
7e00046b8e
|
@ -55,51 +55,60 @@ if ( beforeDir === '' || afterDir === '' ) {
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
const afterFiles = await fs.readdir(`${afterDir}/rulesets/main`);
|
const folders = [
|
||||||
|
'main',
|
||||||
|
'modify-headers',
|
||||||
|
'redirect',
|
||||||
|
'regex',
|
||||||
|
'removeparam',
|
||||||
|
];
|
||||||
const writePromises = [];
|
const writePromises = [];
|
||||||
for ( const file of afterFiles ) {
|
for ( const folder of folders ) {
|
||||||
let raw = await fs.readFile(`${beforeDir}/rulesets/main/${file}`, 'utf-8').catch(( ) => '');
|
const afterFiles = await fs.readdir(`${afterDir}/rulesets/${folder}`);
|
||||||
let beforeRules;
|
for ( const file of afterFiles ) {
|
||||||
try { beforeRules = JSON.parse(raw); } catch(_) { }
|
let raw = await fs.readFile(`${beforeDir}/rulesets/${folder}/${file}`, 'utf-8').catch(( ) => '');
|
||||||
if ( Array.isArray(beforeRules) === false ) { continue; }
|
let beforeRules;
|
||||||
raw = await fs.readFile(`${afterDir}/rulesets/main/${file}`, 'utf-8').catch(( ) => '');
|
try { beforeRules = JSON.parse(raw); } catch(_) { }
|
||||||
let afterRules;
|
if ( Array.isArray(beforeRules) === false ) { continue; }
|
||||||
try { afterRules = JSON.parse(raw); } catch(_) { }
|
raw = await fs.readFile(`${afterDir}/rulesets/${folder}/${file}`, 'utf-8').catch(( ) => '');
|
||||||
if ( Array.isArray(afterRules) === false ) { continue; }
|
let afterRules;
|
||||||
const beforeMap = new Map(beforeRules.map(a => {
|
try { afterRules = JSON.parse(raw); } catch(_) { }
|
||||||
const id = a.id;
|
if ( Array.isArray(afterRules) === false ) { continue; }
|
||||||
a.id = 0;
|
const beforeMap = new Map(beforeRules.map(a => {
|
||||||
return [ JSON.stringify(a), id ];
|
const id = a.id;
|
||||||
}));
|
a.id = 0;
|
||||||
const usedIds = new Set();
|
return [ JSON.stringify(a), id ];
|
||||||
for ( const afterRule of afterRules ) {
|
}));
|
||||||
afterRule.id = 0;
|
const usedIds = new Set();
|
||||||
const key = JSON.stringify(afterRule);
|
for ( const afterRule of afterRules ) {
|
||||||
const beforeId = beforeMap.get(key);
|
afterRule.id = 0;
|
||||||
if ( beforeId === undefined ) { continue; }
|
const key = JSON.stringify(afterRule);
|
||||||
if ( usedIds.has(beforeId) ) { continue; }
|
const beforeId = beforeMap.get(key);
|
||||||
afterRule.id = beforeId;
|
if ( beforeId === undefined ) { continue; }
|
||||||
usedIds.add(beforeId);
|
if ( usedIds.has(beforeId) ) { continue; }
|
||||||
|
afterRule.id = beforeId;
|
||||||
|
usedIds.add(beforeId);
|
||||||
|
}
|
||||||
|
// Assign new ids to unmatched rules
|
||||||
|
let ruleIdGenerator = 1;
|
||||||
|
for ( const afterRule of afterRules ) {
|
||||||
|
if ( afterRule.id !== 0 ) { continue; }
|
||||||
|
while ( usedIds.has(ruleIdGenerator) ) { ruleIdGenerator += 1; }
|
||||||
|
afterRule.id = ruleIdGenerator++;
|
||||||
|
}
|
||||||
|
afterRules.sort((a, b) => a.id - b.id);
|
||||||
|
const indent = afterRules.length > 10 ? undefined : 1;
|
||||||
|
const lines = [];
|
||||||
|
for ( const afterRule of afterRules ) {
|
||||||
|
lines.push(JSON.stringify(afterRule, null, indent));
|
||||||
|
}
|
||||||
|
writePromises.push(
|
||||||
|
fs.writeFile(
|
||||||
|
`${afterDir}/rulesets/${folder}/${file}`,
|
||||||
|
`[\n${lines.join(',\n')}\n]\n`
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
// Assign new ids to unmatched rules
|
|
||||||
let ruleIdGenerator = 1;
|
|
||||||
for ( const afterRule of afterRules ) {
|
|
||||||
if ( afterRule.id !== 0 ) { continue; }
|
|
||||||
while ( usedIds.has(ruleIdGenerator) ) { ruleIdGenerator += 1; }
|
|
||||||
afterRule.id = ruleIdGenerator++;
|
|
||||||
}
|
|
||||||
afterRules.sort((a, b) => a.id - b.id);
|
|
||||||
const indent = afterRules.length > 10 ? undefined : 1;
|
|
||||||
const lines = [];
|
|
||||||
for ( const afterRule of afterRules ) {
|
|
||||||
lines.push(JSON.stringify(afterRule, null, indent));
|
|
||||||
}
|
|
||||||
writePromises.push(
|
|
||||||
fs.writeFile(
|
|
||||||
`${afterDir}/rulesets/main/${file}`,
|
|
||||||
`[\n${lines.join(',\n')}\n]\n`
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
await Promise.all(writePromises);
|
await Promise.all(writePromises);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue