forked from darkreader/darkreader
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcode-style.js
More file actions
68 lines (61 loc) · 1.83 KB
/
Copy pathcode-style.js
File metadata and controls
68 lines (61 loc) · 1.83 KB
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
// @ts-check
import {format} from 'prettier';
import {getDestDir} from './paths.js';
import {PLATFORM} from './platform.js';
import {createTask} from './task.js';
import {readFile, writeFile, getPaths} from './utils.js';
/** @type {import('prettier').Options} */
const options = {
arrowParens: 'always',
bracketSpacing: false,
endOfLine: 'crlf',
printWidth: 80,
quoteProps: 'consistent',
singleQuote: false,
tabWidth: 4,
trailingComma: 'none',
};
const extensions = ['html', 'css', 'js'];
async function processAPIBuild() {
const filepath = 'darkreader.js';
const code = await readFile(filepath);
const formatted = await format(code, {
...options,
filepath,
});
if (code !== formatted) {
await writeFile(filepath, formatted);
}
}
async function processExtensionPlatform(platform) {
const dir = getDestDir({debug: false, platform});
const files = await getPaths(extensions.map((ext) => `${dir}/**/*.${ext}`));
for (const file of files) {
const code = await readFile(file);
const formatted = await format(code, {
...options,
filepath: file,
});
if (code !== formatted) {
await writeFile(file, formatted);
}
}
}
async function codeStyle({platforms, debug}) {
if (debug) {
throw new Error('code-style task does not support debug builds');
}
const promisses = [];
if (platforms[PLATFORM.API]) {
promisses.push(processAPIBuild());
}
Object.values(PLATFORM)
.filter((platform) => platform !== PLATFORM.API && platforms[platform])
.forEach((platform) => promisses.push(processExtensionPlatform(platform)));
await Promise.all(promisses);
}
const codeStyleTask = createTask(
'code-style',
codeStyle,
);
export default codeStyleTask;