This repository has been archived by the owner on Jun 20, 2023. It is now read-only.
/
css-astdiff.ts
75 lines (58 loc) · 1.87 KB
/
css-astdiff.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
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
#! /usr/bin/env node
/// <reference path="../typings/bundle.d.ts" />
/// <reference path="../typings/css/css.d.ts" />
import * as commander from "commander";
import * as css from "css";
import {parseFiles, createDummyStyleSheet} from "../css_utils";
import {astDiff} from "../ast_diff";
import {isEmpty} from "../collection_utils";
import {getVersion} from "../cli_utils";
enum StatusCode {
NOT_CHANGED = 0,
ERROR = 1,
CHANGED = 2,
}
const cli = commander
.version(getVersion())
.usage("[options] <file ...>")
.option("-V, --verbose", "Display verbose diff")
.parse(process.argv);
type Options = {
verbose: boolean;
};
function astDiffByFiles(filePathA: string, filePathB: string, options?: Options): void {
parseFiles(filePathA, filePathB)
.then((tuple) => astDiff(tuple[0], tuple[1]))
.then((result) => {
const {changed, extra, missing} = result;
if (changed) {
if (options.verbose) {
if (!isEmpty(extra)) {
console.log(formatNodes(extra, "extra"));
}
if (!isEmpty(missing)) {
console.log(formatNodes(missing, "missing"));
}
}
console.log(`${extra.length} extra rules and ${missing.length} missing rules ` +
`between ${filePathA} and ${filePathB}`);
}
return changed ? StatusCode.CHANGED : StatusCode.NOT_CHANGED;
}, (err) => {
return StatusCode.ERROR;
})
.then((statusCode) => process.exit(statusCode));
}
function formatNodes(nodes: css.Node[], msg: string): string {
return nodes
.map(createDummyStyleSheet)
.map((styleSheet) => css.stringify(styleSheet))
.map((cssString) => `${msg}:\n${indent(cssString)}`)
.join("\n\n");
}
function indent(str: string): string {
return str.split("\n").map((line) => `\t${line}`).join("\n");
}
astDiffByFiles(cli.args[0], cli.args[1], {
verbose: cli.opts().verbose,
});