Skip to content

Commit d849667

Browse files
committed
Add "core" module for shared functionality between loaders/processors.
1 parent 52aab35 commit d849667

File tree

8 files changed

+40
-0
lines changed

8 files changed

+40
-0
lines changed

core.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
var fs = require('fs');
2+
var path = require('path');
3+
4+
var CONFIG_FILE_NAME = '.component-css-ns';
5+
6+
// Looks for a CONFIG_FILE_NAME next to referenceFile, or along its parent chain, returning either its contents or null
7+
// TODO: Add lookup cache based on dirname
8+
function loadConfigFile(referenceFile) {
9+
var dirname = path.dirname(path.resolve(referenceFile));
10+
if (dirname === '/') return null; // can't ascend anymore -> call it quits
11+
var candidate = path.join(dirname, CONFIG_FILE_NAME);
12+
try {
13+
return require(candidate);
14+
} catch (e) {
15+
return loadConfigFile(dirname); // ascend one path level and keep looking
16+
}
17+
}
18+
19+
module.exports = {
20+
loadConfigFile: loadConfigFile
21+
};

test/cases/core/.component-css-ns.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
hola: 123
3+
};

test/cases/core/deep/path/hierarchy/.gitkeep

Whitespace-only changes.

test/cases/core/expected.out

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pwd: /deep/path/hierarchy
2+
configFile: { hola: 123 }

test/cases/core/node_modules/.bin

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/cases/core/node_modules/component-css-ns

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/cases/core/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"scripts": {
3+
"test": "cd deep/path/hierarchy && node ../../../test.js"
4+
}
5+
}

test/cases/core/test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
var core = require('component-css-ns/core');
2+
3+
// Make sure we're testing the path ascension properly
4+
console.log('pwd:', process.cwd().substr(__dirname.length));
5+
6+
// Test loading config file
7+
console.log('configFile:', core.loadConfigFile('whatever.css'));

0 commit comments

Comments
 (0)