Skip to content
This repository was archived by the owner on May 26, 2022. It is now read-only.

Commit 10c9b46

Browse files
committed
Merge branch 'linting'
2 parents 6fd5d39 + eb345fa commit 10c9b46

12 files changed

+1130
-379
lines changed

.eslintrc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "@sharkcore",
3+
"plugins": ["jest"],
4+
"env": {
5+
"jest/globals": true,
6+
}
7+
}

lib/dependency-map-builder.js

Lines changed: 59 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,14 @@
1-
var _ = require('lodash'),
2-
path = require('path'),
3-
chokidar = require('chokidar'),
4-
Promise = require('bluebird'),
5-
cache = {},
6-
glob = Promise.promisify(require('glob')),
7-
fs = require('graceful-fs'),
8-
lstat = Promise.promisify(fs.lstat),
9-
readFile = Promise.promisify(fs.readFile),
10-
provideRegExp = /goog\.provide\((['"])(([^.)]+)[^)]*)\1\)/g;
1+
const _ = require('lodash');
2+
const path = require('path');
3+
const chokidar = require('chokidar');
4+
const Promise = require('bluebird');
5+
const glob = Promise.promisify(require('glob'));
6+
const fs = require('graceful-fs');
117

12-
/**
13-
* Fulfills in an object wich maps namespaces to file paths found in given
14-
* directories.
15-
*
16-
* @param {string[]} directories Directories to be processed
17-
* @param {boolean} watch Watch for changes is mapped files to invalidate cache
18-
* @param {string} fileExt Searched file extensions
19-
* @returns {Promise}
20-
*/
21-
module.exports = function (directories, watch, fileExt) {
22-
return Promise.map(directories, function(dir) {
23-
return resolveAndCacheDirectory(dir, watch, fileExt)
24-
}).then(function(results) {
25-
return _.assign.apply(_, results);
26-
})
27-
};
8+
const cache = {};
9+
const lstat = Promise.promisify(fs.lstat);
10+
const readFile = Promise.promisify(fs.readFile);
11+
const provideRegExp = /goog\.provide\((['"])(([^.)]+)[^)]*)\1\)/g;
2812

2913
/**
3014
* Promisified watch
@@ -35,14 +19,39 @@ module.exports = function (directories, watch, fileExt) {
3519
* @returns {Promise}
3620
*/
3721
function createWatchPromise(directory) {
38-
return new Promise(function(resolve, reject) {
39-
var watcher = chokidar.watch(directory)
40-
.on('ready', function() {
41-
watcher.on('all', function() {
42-
delete cache[directory];
43-
});
44-
resolve(watcher);
22+
return new Promise(resolve => {
23+
const watcher = chokidar.watch(directory).on('ready', () => {
24+
watcher.on('all', () => {
25+
delete cache[directory];
4526
});
27+
resolve(watcher);
28+
});
29+
});
30+
}
31+
32+
/**
33+
* Scans the given file path for occurences of `goog.provide()` and fulfills
34+
* in an object wich mapps each namespace to the file path
35+
*
36+
* @param {string} filePath
37+
* @returns {Promise}
38+
*/
39+
function findProvideCalls(filePath) {
40+
return lstat(filePath).then(stats => {
41+
if (!stats.isFile()) {
42+
return {};
43+
}
44+
45+
return readFile(filePath).then(fileContent => {
46+
const result = {};
47+
48+
let matches = null;
49+
// eslint-disable-next-line no-cond-assign
50+
while ((matches = provideRegExp.exec(fileContent)) !== null) {
51+
result[matches[2]] = filePath;
52+
}
53+
return result;
54+
});
4655
});
4756
}
4857

@@ -57,44 +66,32 @@ function createWatchPromise(directory) {
5766
* @returns {Promise}
5867
*/
5968
function resolveAndCacheDirectory(directory, watch, fileExt) {
60-
6169
if (cache[directory]) {
6270
return cache[directory];
6371
}
6472

65-
cache[directory] = (watch ? createWatchPromise(directory) : Promise.resolve())
66-
.then(function() {
67-
return glob(path.join(directory, '**', '*' + fileExt));
68-
})
69-
.map(function(filePath) {
70-
return findProvideCalls(filePath);
71-
})
72-
.then(function(results) {
73-
return _.assign.apply(_, results);
74-
});
73+
cache[directory] = (watch
74+
? createWatchPromise(directory)
75+
: Promise.resolve()
76+
)
77+
.then(() => glob(path.join(directory, '**', `*${fileExt}`)))
78+
.map(filePath => findProvideCalls(filePath))
79+
.then(results => _.assign(...results));
7580

7681
return cache[directory];
7782
}
7883

7984
/**
80-
* Scans the given file path for occurences of `goog.provide()` and fulfills
81-
* in an object wich mapps each namespace to the file path
85+
* Fulfills in an object wich maps namespaces to file paths found in given
86+
* directories.
8287
*
83-
* @param {string} filePath
88+
* @param {string[]} directories Directories to be processed
89+
* @param {boolean} watch Watch for changes is mapped files to invalidate cache
90+
* @param {string} fileExt Searched file extensions
8491
* @returns {Promise}
8592
*/
86-
function findProvideCalls(filePath) {
87-
return lstat(filePath).then(function (stats) {
88-
if (!stats.isFile()) {
89-
return {};
90-
}
91-
92-
return readFile(filePath).then(function(fileContent) {
93-
var result = {};
94-
while (matches = provideRegExp.exec(fileContent)) {
95-
result[matches[2]] = filePath;
96-
}
97-
return result;
98-
});
99-
});
100-
}
93+
module.exports = function dependencyMapBuilder(directories, watch, fileExt) {
94+
return Promise.map(directories, dir =>
95+
resolveAndCacheDirectory(dir, watch, fileExt),
96+
).then(results => _.assign(...results));
97+
};

lib/export-path.js

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
/**
2-
* Ensure that a dotted path is defined on a namespace object. Will create
3-
* intermediate namespaces that are not defined as empty objects, and will
4-
* optionally set a value at the end of the path.
5-
*
2+
* Ensure that a dotted path is defined on a namespace object. Will create
3+
* intermediate namespaces that are not defined as empty objects, and will
4+
* optionally set a value at the end of the path.
5+
*
66
* @param {Object} namespace
77
* @param {string} name
88
* @param {*=} value
9-
*/
10-
module.exports = function(namespace, name, value) {
11-
var parts = name.split('.');
12-
var target = namespace;
9+
*/
10+
module.exports = function exportPath(namespace, name, value) {
11+
const parts = name.split('.');
12+
let target = namespace;
1313

14-
for (var i = 0; i < parts.length; i++) {
15-
var part = parts[i];
16-
if (i === parts.length - 1 && value !== undefined) {
17-
target[part] = value;
18-
} else if (target[part]) {
19-
target = target[part];
20-
} else {
21-
target = target[part] = {};
14+
for (let i = 0; i < parts.length; i += 1) {
15+
const part = parts[i];
16+
if (i === parts.length - 1 && value !== undefined) {
17+
target[part] = value;
18+
} else if (target[part]) {
19+
target = target[part];
20+
} else {
21+
target[part] = {};
22+
target = target[part];
23+
}
2224
}
23-
}
2425
};

0 commit comments

Comments
 (0)