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

devpow112 / codify-images / 4851918122

01 May 2023 02:21PM UTC coverage: 100.0%. Remained the same
4851918122

push

github

Devon Powell
chore(deps-dev): bump markdownlint-cli from 0.33.0 to 0.34.0

62 of 62 branches covered (100.0%)

Branch coverage included in aggregate %.

185 of 185 relevant lines covered (100.0%)

113.38 hits per line

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

100.0
/src/coa.js
1
import { name as cliName, version as cliVersion } from '../package.json';
3✔
2
import { InvalidArgumentError, Option } from 'commander';
1✔
3
import { statSync, writeFileSync } from 'fs';
1✔
4
import chalk from 'chalk';
1✔
5
import { codifyImagesSync } from './codify-images.js';
1✔
6
import { mkdirp } from 'mkdirp';
1✔
7
import { resolve } from 'path';
1✔
8

9
const { blue, green, red, yellow } = chalk;
1✔
10
const mkdirpSync = mkdirp.sync;
1✔
11

12
const generateStart = options => {
1✔
13
  let output = options.banner === true ? '// auto-generated\n' : '';
66✔
14

15
  if (options.es === 5) {
66✔
16
    output += 'module.exports = {\n';
32✔
17
  }
18

19
  return output;
66✔
20
};
21

22
const generateLine = (name, data, options) => {
1✔
23
  const adjustedData = options.doubleQuotes !== true ?
390✔
24
    `'${data.replace(/'/g, '\\\'')}'` :
25
    `"${data}"`;
26
  const indentType = options.indentType === 'tab' ? '\t' : ' ';
390✔
27
  const indent = indentType.repeat(options.indentCount);
390✔
28

29
  return options.es === 6 ?
390✔
30
    `export const ${name} = ${adjustedData};` :
31
    `${indent}${name}: ${adjustedData}`;
32
};
33

34
const generateLineEnding = (options, last) => {
1✔
35
  return options.es !== 6 && !last ? ',\n' : '\n';
390✔
36
};
37

38
const generateEnd = options => {
1✔
39
  return options.es !== 6 ? '};\n' : '';
65✔
40
};
41

42
const writeOutput = (outputPath, output, options) => {
1✔
43
  mkdirpSync(options.output);
65✔
44
  writeFileSync(outputPath, output, { encoding: 'utf-8' });
65✔
45
};
46

47
const logStart = message => {
1✔
48
  console.log(yellow(message));
66✔
49
  console.group();
66✔
50
};
51

52
const logInfo = message => {
1✔
53
  console.log(blue(message));
780✔
54
};
55

56
const logError = message => {
1✔
57
  console.groupEnd();
1✔
58
  console.log(red(`error: ${message}`));
1✔
59
};
60

61
const logProcessed = (path, name) => {
1✔
62
  logInfo(`processed image (${path} => ${name})`);
390✔
63
};
64

65
const logEnd = message => {
1✔
66
  console.groupEnd();
65✔
67
  console.log(green(message));
65✔
68
};
69

70
const generate = options => {
1✔
71
  logStart(yellow(`generating exports (${options.input}) ...`));
66✔
72

73
  let output = generateStart(options);
66✔
74
  const images = codifyImagesSync(
66✔
75
    options.input,
76
    { log: logProcessed, svgMode: options.svgMode }
77
  );
78

79
  if (Object.keys(images).length === 0) {
66✔
80
    throw new Error('no images available at input path.');
1✔
81
  }
82

83
  const keys = Object.keys(images);
65✔
84

85
  for (const index in keys) {
65✔
86
    const key = keys[index];
390✔
87
    const data = images[key];
390✔
88

89
    output += generateLine(key, data, options);
390✔
90
    output += generateLineEnding(options, index === keys.length - 1);
390✔
91

92
    logInfo(`writing export (${key})`);
390✔
93
  }
94

95
  output += generateEnd(options);
65✔
96

97
  const outputPath = resolve(options.output, 'images.js');
65✔
98

99
  writeOutput(outputPath, output, options);
65✔
100
  logEnd(`exports generated (${outputPath})`);
65✔
101
};
102

103
const customParseInt = value => {
1✔
104
  const parsedValue = parseInt(value, 10);
71✔
105

106
  if (isNaN(parsedValue)) {
71✔
107
    throw new InvalidArgumentError('Must be valid integer.');
4✔
108
  } else if (parsedValue <= 0) {
67✔
109
    throw new InvalidArgumentError('Must be positive integer.');
2✔
110
  }
111

112
  return parsedValue;
65✔
113
};
114

115
export const setUpProgram = program => {
1✔
116
  program
75✔
117
    .name(cliName)
118
    .version(cliVersion)
119
    .showHelpAfterError()
120
    .configureOutput({ outputError: (message, write) => write(red(message)) })
9✔
121
    .argument(
122
      '<input path>',
123
      'path to where image files reside',
124
      value => {
125
        try {
68✔
126
          const path = resolve(value);
68✔
127
          const stat = statSync(path);
68✔
128

129
          if (!stat.isDirectory()) {
67✔
130
            throw new InvalidArgumentError('Must be directory.');
1✔
131
          }
132

133
          return path;
66✔
134
        } catch (_) {
135
          throw new InvalidArgumentError('Must exist.');
2✔
136
        }
137
      }
138
    )
139
    .option(
140
      '-d, --double-quotes',
141
      'Use double quotes for output instead of single quotes',
142
      false
143
    )
144
    .option(
145
      '-o, --output <path>',
146
      'path to write generated files',
147
      value => resolve(value),
65✔
148
      'generated'
149
    )
150
    .option(
151
      '-e, --es <version>',
152
      'version of ESM to generate',
153
      value => {
154
        const parsedValue = customParseInt(value);
68✔
155
        const choicesEs = [5, 6];
65✔
156

157
        if (!choicesEs.includes(parsedValue)) {
65✔
158
          const choices = choicesEs.join(', ');
1✔
159

160
          throw new InvalidArgumentError(`Allowed choices are ${choices}.`);
1✔
161
        }
162

163
        return parsedValue;
64✔
164
      },
165
      6
166
    )
167
    .option(
168
      '-c, --indent-count <count>',
169
      'number of indent elements to output',
170
      value => customParseInt(value),
3✔
171
      1
172
    )
173
    .option(
174
      '-B, --no-banner',
175
      'do not include banner comment at top of generated file'
176
    )
177
    .addOption(
178
      new Option('-t, --indent-type <type>', 'type of indent to output')
179
        .choices(['tab', 'space'])
180
        .default('tab')
181
    )
182
    .addOption(
183
      new Option('-s, --svg-mode <mode>', 'output mode to use for SVG images')
184
        .choices(['base64', 'uri', 'mini', 'mini-srcset'])
185
        .default('base64')
186
    )
187
    .action((input, opts) => {
188
      if (opts.output === 'generated') {
66✔
189
        opts.output = resolve(opts.output);
1✔
190
      }
191

192
      try {
66✔
193
        generate({ input, ...opts });
66✔
194
      } catch (err) {
195
        logError(err.message);
1✔
196

197
        throw err;
1✔
198
      }
199
    });
200
};
1✔
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