• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

FullHuman / purgecss / 7742080500

01 Feb 2024 01:33PM CUT coverage: 92.031%. Remained the same
7742080500

Pull #1209

github

web-flow
build(deps-dev): bump lint-staged from 14.0.1 to 15.2.1

Bumps [lint-staged](https://github.com/okonet/lint-staged) from 14.0.1 to 15.2.1.
- [Release notes](https://github.com/okonet/lint-staged/releases)
- [Changelog](https://github.com/lint-staged/lint-staged/blob/master/CHANGELOG.md)
- [Commits](https://github.com/okonet/lint-staged/compare/v14.0.1...v15.2.1)

---
updated-dependencies:
- dependency-name: lint-staged
  dependency-type: direct:development
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
Pull Request #1209: build(deps-dev): bump lint-staged from 14.0.1 to 15.2.1

317 of 372 branches covered (0.0%)

Branch coverage included in aggregate %.

503 of 519 relevant lines covered (96.92%)

10603.71 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

75.76
/packages/purgecss/src/bin.ts
1
import { Command } from "commander";
8✔
2
import * as fs from "fs";
8✔
3
import packageJson from "./../package.json";
8✔
4
import {
8✔
5
  defaultOptions,
6
  PurgeCSS,
7
  setOptions,
8
  standardizeSafelist,
9
} from "./index";
10
import { Options } from "./types";
11

12
async function writeCSSToFile(filePath: string, css: string) {
13
  try {
6✔
14
    await fs.promises.writeFile(filePath, css);
6✔
15
  } catch (err: unknown) {
16
    if (err instanceof Error) {
×
17
      console.error(err.message);
×
18
    }
19
  }
20
}
21

22
async function read(stream: NodeJS.ReadStream) {
23
  const chunks = [];
×
24
  for await (const chunk of stream) chunks.push(chunk);
×
25
  return Buffer.concat(chunks).toString("utf8");
×
26
}
27

28
type CommandOptions = {
29
  config?: string;
30
  css?: string[];
31
  content?: string[];
32
  output?: string;
33
  fontFace?: boolean;
34
  keyframes?: boolean;
35
  variables?: boolean;
36
  rejected?: boolean;
37
  rejectedCss?: boolean;
38
  safelist?: string[];
39
  blocklist?: string[];
40
  skippedContentGlobs: string[];
41
};
42

43
export function parseCommandOptions(program: Command): Command {
8✔
44
  program
8✔
45
    .description(packageJson.description)
46
    .version(packageJson.version)
47
    .usage("--css <css...> --content <content...> [options]");
48

49
  program
8✔
50
    .option("-con, --content <files...>", "glob of content files")
51
    .option("-css, --css <files...>", "glob of css files")
52
    .option("-c, --config <path>", "path to the configuration file")
53
    .option(
54
      "-o, --output <path>",
55
      "file path directory to write purged css files to"
56
    )
57
    .option("-font, --font-face", "option to remove unused font-faces")
58
    .option("-keyframes, --keyframes", "option to remove unused keyframes")
59
    .option("-v, --variables", "option to remove unused variables")
60
    .option("-rejected, --rejected", "option to output rejected selectors")
61
    .option("-rejected-css, --rejected-css", "option to output rejected css")
62
    .option(
63
      "-s, --safelist <list...>",
64
      "list of classes that should not be removed"
65
    )
66
    .option(
67
      "-b, --blocklist <list...>",
68
      "list of selectors that should be removed"
69
    )
70
    .option(
71
      "-k, --skippedContentGlobs <list...>",
72
      "list of glob patterns for folders/files that should not be scanned"
73
    );
74

75
  return program;
8✔
76
}
77

78
export async function getOptions(program: Command): Promise<Options> {
8✔
79
  const {
80
    config,
81
    css,
82
    content,
83
    output,
84
    fontFace,
85
    keyframes,
86
    variables,
87
    rejected,
88
    rejectedCss,
89
    safelist,
90
    blocklist,
91
    skippedContentGlobs,
92
  } = program.opts<CommandOptions>();
8✔
93
  // config file is not specified or the content and css are not,
94
  // PurgeCSS will not run
95
  if (!config && !(content && css)) {
8!
96
    program.help();
×
97
  }
98

99
  // if the config file is present, use it
100
  // other options specified will override
101
  let options = defaultOptions;
8✔
102
  if (config) {
8!
103
    options = await setOptions(config);
×
104
  }
105
  if (content) {
8✔
106
    if (content.length === 1 && content[0] === "-") {
8!
107
      options.content = [
×
108
        {
109
          raw: await read(process.stdin),
110
          extension: "",
111
        },
112
      ];
113
    } else {
114
      options.content = content;
8✔
115
    }
116
  }
117
  if (css) {
8✔
118
    if (css.length === 1 && css[0] === "-") {
8!
119
      options.css = [
×
120
        {
121
          raw: await read(process.stdin),
122
        },
123
      ];
124
    } else {
125
      options.css = css;
8✔
126
    }
127
  }
128
  if (fontFace) options.fontFace = fontFace;
8✔
129
  if (keyframes) options.keyframes = keyframes;
8✔
130
  if (rejected) options.rejected = rejected;
8✔
131
  if (rejectedCss) options.rejectedCss = rejectedCss;
8✔
132
  if (variables) options.variables = variables;
8✔
133
  if (safelist) options.safelist = standardizeSafelist(safelist);
8✔
134
  if (blocklist) options.blocklist = blocklist;
8✔
135
  if (skippedContentGlobs) options.skippedContentGlobs = skippedContentGlobs;
8✔
136
  if (output) options.output = output;
8✔
137
  return options;
8✔
138
}
139

140
export async function run(program: Command) {
8✔
141
  const options = await getOptions(program);
6✔
142
  const purged = await new PurgeCSS().purge(options);
6✔
143

144
  // output results in specified directory
145
  if (options.output) {
6✔
146
    if (purged.length === 1 && options.output.endsWith(".css")) {
4✔
147
      await writeCSSToFile(options.output, purged[0].css);
2✔
148
      return;
2✔
149
    }
150

151
    for (const purgedResult of purged) {
2✔
152
      const fileName = purgedResult?.file?.split("/").pop();
4!
153
      await writeCSSToFile(`${options.output}/${fileName}`, purgedResult.css);
4✔
154
    }
155
  } else {
156
    console.log(JSON.stringify(purged));
2✔
157
  }
158
}
159

160
export async function main() {
8✔
161
  try {
×
162
    const program = parseCommandOptions(new Command());
×
163
    program.parse(process.argv);
×
164
    run(program);
×
165
  } catch (error: unknown) {
166
    if (error instanceof Error) {
×
167
      console.error(error.message);
×
168
    }
169
    process.exit(1);
×
170
  }
171
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc