lint: src/static/js/pluginfw/read-installed.js

pull/4675/head^2
Richard Hansen 2021-02-03 18:31:42 -05:00 committed by John McLear
parent 2b32bc1840
commit 2c80c1f2da
1 changed files with 23 additions and 25 deletions

View File

@ -1,3 +1,5 @@
'use strict';
// A copy of npm/lib/utils/read-installed.js // A copy of npm/lib/utils/read-installed.js
// that is hacked to not cache everything :) // that is hacked to not cache everything :)
@ -88,7 +90,6 @@ as far as the left-most node_modules folder.
*/ */
const npm = require('npm/lib/npm.js'); const npm = require('npm/lib/npm.js');
const fs = require('graceful-fs'); const fs = require('graceful-fs');
const path = require('path'); const path = require('path');
@ -97,8 +98,10 @@ const semver = require('semver');
const log = require('log4js').getLogger('pluginfw'); const log = require('log4js').getLogger('pluginfw');
let fuSeen = []; let fuSeen = [];
let riSeen = [];
let rpSeen = {};
function readJson(file, callback) { const readJson = (file, callback) => {
fs.readFile(file, (er, buf) => { fs.readFile(file, (er, buf) => {
if (er) { if (er) {
callback(er); callback(er);
@ -110,11 +113,9 @@ function readJson(file, callback) {
callback(er); callback(er);
} }
}); });
} };
module.exports = readInstalled; const readInstalled = (folder, cb) => {
function readInstalled(folder, cb) {
/* This is where we clear the cache, these three lines are all the /* This is where we clear the cache, these three lines are all the
* new code there is */ * new code there is */
fuSeen = []; fuSeen = [];
@ -129,10 +130,11 @@ function readInstalled(folder, cb) {
resolveInheritance(obj); resolveInheritance(obj);
cb(null, obj); cb(null, obj);
}); });
} };
var rpSeen = {}; module.exports = readInstalled;
function readInstalled_(folder, parent, name, reqver, depth, maxDepth, cb) {
const readInstalled_ = (folder, parent, name, reqver, depth, maxDepth, cb) => {
let installed, let installed,
obj, obj,
real, real,
@ -229,11 +231,10 @@ function readInstalled_(folder, parent, name, reqver, depth, maxDepth, cb) {
return cb(null, obj); return cb(null, obj);
}); });
} }
} };
// starting from a root object, call findUnmet on each layer of children // starting from a root object, call findUnmet on each layer of children
var riSeen = []; const resolveInheritance = (obj) => {
function resolveInheritance(obj) {
if (typeof obj !== 'object') return; if (typeof obj !== 'object') return;
if (riSeen.indexOf(obj) !== -1) return; if (riSeen.indexOf(obj) !== -1) return;
riSeen.push(obj); riSeen.push(obj);
@ -246,11 +247,11 @@ function resolveInheritance(obj) {
Object.keys(obj.dependencies).forEach((dep) => { Object.keys(obj.dependencies).forEach((dep) => {
resolveInheritance(obj.dependencies[dep]); resolveInheritance(obj.dependencies[dep]);
}); });
} };
// find unmet deps by walking up the tree object. // find unmet deps by walking up the tree object.
// No I/O // No I/O
function findUnmet(obj) { const findUnmet = (obj) => {
if (typeof obj !== 'object') return; if (typeof obj !== 'object') return;
if (fuSeen.indexOf(obj) !== -1) return; if (fuSeen.indexOf(obj) !== -1) return;
fuSeen.push(obj); fuSeen.push(obj);
@ -282,16 +283,16 @@ function findUnmet(obj) {
} }
}); });
return obj; return obj;
} };
function copy(obj) { const copy = (obj) => {
if (!obj || typeof obj !== 'object') return obj; if (!obj || typeof obj !== 'object') return obj;
if (Array.isArray(obj)) return obj.map(copy); if (Array.isArray(obj)) return obj.map(copy);
const o = {}; const o = {};
for (const i in obj) o[i] = copy(obj[i]); for (const [i, v] of Object.entries(obj)) o[i] = copy(v);
return o; return o;
} };
if (module === require.main) { if (module === require.main) {
const util = require('util'); const util = require('util');
@ -312,7 +313,7 @@ if (module === require.main) {
function cleanup(map) { function cleanup(map) {
if (seen.indexOf(map) !== -1) return; if (seen.indexOf(map) !== -1) return;
seen.push(map); seen.push(map);
for (var i in map) { for (const i of Object.keys(map)) {
switch (i) { switch (i) {
case '_id': case '_id':
case 'path': case 'path':
@ -322,12 +323,9 @@ if (module === require.main) {
default: delete map[i]; default: delete map[i];
} }
} }
const dep = map.dependencies; for (const dep of Object.values(map.dependencies || {})) {
if (dep) { if (typeof dep === 'object') {
for (var i in dep) { cleanup(dep);
if (typeof dep[i] === 'object') {
cleanup(dep[i]);
}
} }
} }
return map; return map;