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

peterjwest / unlinted / 0d7d72eb-711b-4c7a-a710-2ae23bfff973

17 Aug 2023 12:35PM UTC coverage: 6.254% (-0.4%) from 6.7%
0d7d72eb-711b-4c7a-a710-2ae23bfff973

push

circleci

peterjwest
Improve readme description

2 of 18 branches covered (11.11%)

Branch coverage included in aggregate %.

93 of 1501 relevant lines covered (6.2%)

0.06 hits per line

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

0.0
/src/getConfig.ts
1
import { join, resolve } from 'path';
×
2
import z from 'zod';
×
3
import { fromZodError } from 'zod-validation-error';
×
4

×
5
import { DEFAULT_CONFIG, ExtendedConfig, UserConfig } from './config';
×
6
import checkConfig from './checkConfig';
×
7
import combineConfig from './combineConfig';
×
8
import extendConfig from './extendConfig';
×
9
import { fileReadable, ErrorWithFailures } from './util';
×
10

×
11
/** Return an array of (slightly) more user friendly errors from ZodError */
×
12
function getZodErrors(error: z.ZodError) {
×
13
  return fromZodError(error, { issueSeparator: '\n', prefix: null }).message.split('\n');
×
14
}
×
15

×
16
/** Parses a Config with zod and wraps any ZodErrors */
×
17
function parseConfig(configObject: unknown): UserConfig {
×
18
  try {
×
19
    return UserConfig.parse(configObject);
×
20
  } catch (error: unknown) {
×
21
    if (error instanceof z.ZodError) {
×
22
      throw new ErrorWithFailures('Config invalid', getZodErrors(error));
×
23
    }
×
24
    throw error;
×
25
  }
×
26
}
×
27

×
28
/** Get full configuration, combining default with user config */
×
29
export default async function getConfig(projectDir: string, userConfigPath?: string): Promise<[ExtendedConfig, string | undefined]> {
×
30
  const DEFAULT_TS_CONFIG = join(projectDir, 'unlinted.config.ts');
×
31
  const DEFAULT_JS_CONFIG = join(projectDir, 'unlinted.config.js');
×
32
  const DEFAULT_JSON_CONFIG = join(projectDir, 'unlinted.config.json');
×
33

×
34
  let configPath: string | undefined;
×
35

×
36
  if (userConfigPath) configPath = resolve(process.cwd(), userConfigPath);
×
37
  else if (await fileReadable(DEFAULT_TS_CONFIG)) configPath = DEFAULT_TS_CONFIG;
×
38
  else if (await fileReadable(DEFAULT_JS_CONFIG)) configPath = DEFAULT_JS_CONFIG;
×
39
  else if (await fileReadable(DEFAULT_JSON_CONFIG)) configPath = DEFAULT_JSON_CONFIG;
×
40

×
41
  const configModule: unknown = configPath ? await import(configPath) : undefined;
×
42
  // Unwrap default export if it exists
×
43
  const configEntity = configModule && typeof configModule === 'object' && 'default' in configModule ? configModule.default : configModule;
×
44
  // If the config is a function, run it with the default config
×
45
  const configObject: unknown = typeof configEntity === 'function' ? configEntity(DEFAULT_CONFIG) : configEntity;
×
46
  // Parse with zod to ensure the type is correct
×
47
  const userConfig: UserConfig | undefined = configObject ? parseConfig(configObject) : undefined;
×
48
  const config = userConfig ? combineConfig(DEFAULT_CONFIG, userConfig) : DEFAULT_CONFIG;
×
49

×
50
  const errors = checkConfig(config);
×
51
  if (errors.length) {
×
52
    throw new ErrorWithFailures('Config invalid', errors);
×
53
  }
×
54

×
55
  return [extendConfig(config), userConfigPath ? userConfigPath : configPath];
×
56
}
×
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

© 2026 Coveralls, Inc