forked from reworkcss/css
-
Notifications
You must be signed in to change notification settings - Fork 10
/
generate-tests.ts
48 lines (41 loc) · 1.48 KB
/
generate-tests.ts
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
// Generates missing output source and AST files for the test cases
// IMPORTANT: Always verify the generated files when using this!
const fs = require('fs');
const path = require('path');
const parse = require('./').parse;
const stringify = require('./').stringify;
const casesDir = path.join(__dirname, 'test', 'cases');
const cases = fs.readdirSync(casesDir).map((f: string) => {
return path.join(casesDir, f);
});
cases.forEach((dir: string) => {
const inputFile = path.join(dir, 'input.css');
if (!fs.existsSync(inputFile))
throw new Error('Missing input file ' + inputFile);
const input = fs.readFileSync(inputFile, 'utf8');
let parsed;
try {
parsed = parse(input, {source: 'input.css'});
} catch (e) {
console.log('Failed to parse', inputFile);
throw e;
}
const outputFile = path.join(dir, 'output.css');
if (!fs.existsSync(outputFile)) {
console.log('Generating', outputFile);
const output = stringify(parsed);
fs.writeFileSync(outputFile, output, 'utf8');
}
const compressedFile = path.join(dir, 'compressed.css');
if (!fs.existsSync(compressedFile)) {
console.log('Generating', compressedFile);
const compressed = stringify(parsed, {compress: true});
fs.writeFileSync(compressedFile, compressed, 'utf8');
}
const astFile = path.join(dir, 'ast.json');
if (!fs.existsSync(astFile)) {
console.log('Generating', astFile);
const ast = JSON.stringify(parsed, null, ' ');
fs.writeFileSync(astFile, ast, 'utf8');
}
});