-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathreplace.js
31 lines (25 loc) · 984 Bytes
/
replace.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
Chomp.registerTemplate('replace', function (task) {
if (task.engine || task.run)
throw new Error('"engine", "run" not configurable for Replace template.');
const { replacements = [], throwUnmatched } = task.templateOptions;
return [{
name: task.name,
targets: task.targets,
deps: task.deps,
engine: 'node',
run: ` import { readFileSync, writeFileSync } from 'fs';
let source = readFileSync(process.env.DEP, 'utf8');
const replacements = [${replacements.map(([from, to], idx) => `\n [${
from.match(/^\/.+\/[gus]*$/) ? from : JSON.stringify(from)
}, ${JSON.stringify(to)}]${idx === replacements.length - 1 ? '\n ' : ','}`).join('')}];
for (const [from, to] of replacements) {
${throwUnmatched ? ` if (!source.match(from)) {
console.log(source);
throw new Error(\`Match not found for \${from} -> \${to}\`);
}
` : ''} source = source.replace(from, to);
}
writeFileSync(process.env.TARGET, source);
`
}]
});