Skip to content

Commit c0ed4ff

Browse files
committed
refactor(main): functionize
1 parent b9cd72b commit c0ed4ff

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

intelli-espower-loader.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"use strict";
2-
var assert = require("assert");
32
var fs = require("fs");
3+
var assert = require("assert");
44
var pather = require("path");
5+
var normalizeDir = require("./lib/normalize-dir");
56
var packageName = require("./package.json").name;
67

78
function findPackageDir(paths) {
@@ -21,11 +22,15 @@ function getPackageJSON() {
2122
assert(dir, "package.json is not found");
2223
return require(pather.resolve(dir, "package.json"));
2324
}
24-
var directories = getPackageJSON().directories;
25-
assert.equal(typeof directories, "object", 'You should setting `directories : { test : "test/" }`');
26-
assert.equal(typeof directories.test, "string", 'You should setting `directories : { test : "test/" }`');
27-
var testDirectory = directories.test;
25+
function getTestDirFromPkg(pkg) {
26+
var directories = pkg.directories;
27+
assert.equal(typeof directories, "object", 'You should setting `directories : { test : "test/" }`');
28+
assert.equal(typeof directories.test, "string", 'You should setting `directories : { test : "test/" }`');
29+
return directories.test;
30+
}
31+
var pkg = getPackageJSON();
32+
var testDirectory = getTestDirFromPkg(pkg);
2833
require('espower-loader')({
2934
cwd: process.cwd(),
30-
pattern: testDirectory + ((testDirectory.lastIndexOf(pather.sep, 0) === 0) ? '' : pather.sep) + "**" + pather.sep + "*.js"
35+
pattern: normalizeDir(testDirectory) + "**" + pather.sep + "*.js"
3136
});

lib/normalize-dir.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"use strict";
2+
var pather = require("path");
3+
function isEndedWithSeparator(filePath) {
4+
return filePath[filePath.length - 1] === pather.sep;
5+
}
6+
function normalizeDirPath(filePath) {
7+
if (isEndedWithSeparator(filePath)) {
8+
return filePath;
9+
} else {
10+
return filePath + pather.sep;
11+
}
12+
}
13+
module.exports = normalizeDirPath;

0 commit comments

Comments
 (0)