UNPKG

1.37 kBJavaScriptView Raw
1/* eslint-env node */
2/* eslint-disable no-console */
3const assert = require('assert');
4const findUp = require('find-up');
5const path = require('path');
6
7// Traverse up the folder tree, trying to find config files
8module.exports = function(cwd = process.cwd()) {
9 const creunaRcPath = findUp.sync('.creunarc.json', { cwd });
10 const eslintRcPath = findUp.sync('.eslintrc.json', { cwd });
11
12 const eslintConfig =
13 eslintRcPath && require(path.relative(__dirname, eslintRcPath));
14
15 if (!creunaRcPath) {
16 return [{ componentsPath: cwd, eslintConfig, staticSitePath: cwd }];
17 }
18
19 const projectRoot = path.dirname(creunaRcPath);
20 const {
21 componentsPath,
22 staticSitePath,
23 dataFileExtension,
24 dataFileContent
25 } = require(path.relative(__dirname, creunaRcPath));
26
27 try {
28 const errorFooter = `\nThis property is required when using a '.creunarc.json' file.`;
29 assert(
30 componentsPath,
31 `No 'componentsPath' found in ${creunaRcPath}${errorFooter}`
32 );
33 assert(
34 staticSitePath,
35 `No 'staticSitePath' found in ${creunaRcPath}${errorFooter}`
36 );
37 } catch (error) {
38 return [{}, error];
39 }
40
41 return [
42 {
43 componentsPath: path.join(projectRoot, componentsPath),
44 staticSitePath: path.join(projectRoot, staticSitePath),
45 dataFileContent,
46 dataFileExtension,
47 eslintConfig
48 }
49 ];
50};