1 4
import fs from 'fs';
2 4
import path from 'path';
3 4
import { getDefaultOptions } from './defaultOptions';
4
import { Options } from '@remax/types';
5 4
import validateOptions from 'schema-utils';
6

7 4
const schema = require('../OptionsSchema.json');
8

9
export interface CliOptions {
10
  target: string;
11
}
12

13 4
function readJavascriptConfig(path: string) {
14 4
  delete require.cache[path];
15 4
  const config = require(path);
16 4
  return config || {};
17
}
18

19 4
function normalizeConfigPath(options: Options): Options {
20 4
  const pathKeys: Array<keyof Options> = ['cwd', 'rootDir', 'output'];
21 4
  pathKeys.forEach(key => {
22 4
    const value = options[key];
23
    // @ts-ignore string-type
24 4
    options[key] = path.normalize(value).replace(/(\\|\/)$/, '');
25
  });
26 4
  return options;
27
}
28

29 4
export default function getConfig(validate = true): Options {
30 4
  const configPath: string = path.join(process.cwd(), './remax.config');
31

32 4
  let options = {} as Options;
33

34 4
  if (fs.existsSync(configPath + '.js')) {
35 4
    options = readJavascriptConfig(configPath + '.js');
36
  }
37

38 4
  if (validate) {
39 4
    validateOptions(schema as any, options, {
40
      name: 'remax',
41
    });
42
  }
43

44 4
  const remaxConfig = {
45
    ...getDefaultOptions(),
46
    ...options,
47
  };
48

49 4
  return normalizeConfigPath(remaxConfig);
50
}

Read our documentation on viewing source code .

Loading