unimported
Version:
Scans your nodejs project folder and shows obsolete files and modules
154 lines (153 loc) • 7.74 kB
JavaScript
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPeerDependencies = exports.getDependencies = exports.getAliases = exports.readTsconfig = exports.typedBoolean = exports.hasPackage = void 0;
const path_1 = __importStar(require("path"));
const typescript_1 = require("typescript");
const config_1 = require("./config");
const ensureArray_1 = require("./ensureArray");
const fs = __importStar(require("./fs"));
const log_1 = require("./log");
function hasPackage(packageJson, name) {
var _a, _b, _c;
return Boolean(((_a = packageJson.dependencies) === null || _a === void 0 ? void 0 : _a[name]) ||
((_b = packageJson.devDependencies) === null || _b === void 0 ? void 0 : _b[name]) ||
((_c = packageJson.peerDependencies) === null || _c === void 0 ? void 0 : _c[name]));
}
exports.hasPackage = hasPackage;
function typedBoolean(value) {
return Boolean(value);
}
exports.typedBoolean = typedBoolean;
const readTsconfig = () => {
const resolvedPathname = (0, typescript_1.findConfigFile)(process.cwd(), typescript_1.sys.fileExists, 'tsconfig.json');
if (!resolvedPathname) {
return undefined;
}
const { config } = (0, typescript_1.readConfigFile)(resolvedPathname, typescript_1.sys.readFile);
if (!config) {
return undefined;
}
const { options } = (0, typescript_1.parseJsonConfigFileContent)(config, typescript_1.sys, process.cwd());
return {
compilerOptions: options,
};
};
exports.readTsconfig = readTsconfig;
function getAliases(entryFile) {
var _a, _b, _c, _d, _e, _f, _g, _h;
return __awaiter(this, void 0, void 0, function* () {
const [packageJson, jsconfig] = yield Promise.all([
fs.readJson('package.json'),
fs.readJson('jsconfig.json'),
]);
const tsconfig = (0, exports.readTsconfig)();
const config = yield (0, config_1.getConfig)();
let aliases = {};
let baseUrl = (_e = (_c = (_a = config === null || config === void 0 ? void 0 : config.rootDir) !== null && _a !== void 0 ? _a : (_b = tsconfig === null || tsconfig === void 0 ? void 0 : tsconfig.compilerOptions) === null || _b === void 0 ? void 0 : _b.baseUrl) !== null && _c !== void 0 ? _c : (_d = jsconfig === null || jsconfig === void 0 ? void 0 : jsconfig.compilerOptions) === null || _d === void 0 ? void 0 : _d.baseUrl) !== null && _e !== void 0 ? _e : '.';
// '/' doesn't resolve
if (baseUrl === '/') {
baseUrl = '.';
}
const root = path_1.default.resolve(baseUrl);
// add support for root slash import
aliases['/'] = [`${root}/`];
// add support for mono-repos
if ((_f = packageJson === null || packageJson === void 0 ? void 0 : packageJson.repository) === null || _f === void 0 ? void 0 : _f.directory) {
const root = path_1.default.resolve('../');
const packages = yield fs.list('*/', root, { realpath: false });
for (const alias of packages) {
aliases[alias] = [(0, path_1.join)(root, alias)];
}
}
// add support for typescript path aliases
if ((_g = tsconfig === null || tsconfig === void 0 ? void 0 : tsconfig.compilerOptions) === null || _g === void 0 ? void 0 : _g.paths) {
const root = path_1.default.resolve(tsconfig.compilerOptions.baseUrl || '.');
aliases = Object.assign(aliases, normalizeAliases(root, tsconfig.compilerOptions.paths));
}
// add support for jsconfig path aliases
if ((_h = jsconfig === null || jsconfig === void 0 ? void 0 : jsconfig.compilerOptions) === null || _h === void 0 ? void 0 : _h.paths) {
const root = path_1.default.resolve(jsconfig.compilerOptions.baseUrl || '.');
aliases = Object.assign(aliases, normalizeAliases(root, jsconfig.compilerOptions.paths));
}
// add support for additional path aliases (in typescript compiler path like setup)
if (entryFile.aliases) {
aliases = Object.assign(aliases, normalizeAliases(root, entryFile.aliases));
}
log_1.log.info(`aliases for %s %O`, entryFile !== null && entryFile !== void 0 ? entryFile : '*', aliases);
return aliases;
});
}
exports.getAliases = getAliases;
// normalize the aliases. The keys maintain trailing '/' to ease path comparison,
// in: { '@components/*': ['src/components/*'] }
// out: { '@components/': ['src/components/'] }
function normalizeAliases(root, paths) {
const aliases = {};
for (const key of Object.keys(paths)) {
const alias = key.replace(/\*$/, '');
aliases[alias] = (0, ensureArray_1.ensureArray)(paths[key]).map((x) => {
const path = (0, path_1.join)(root, x.replace(/\*$/, ''));
return alias.endsWith('/') && !path.endsWith('/') ? `${path}/` : path;
});
// only keep uniqs
aliases[alias] = Array.from(new Set(aliases[alias]));
}
return aliases;
}
function getDependencies(projectPath) {
return __awaiter(this, void 0, void 0, function* () {
const packageJson = yield fs.readJson('package.json', projectPath);
if (!packageJson) {
return {};
}
return packageJson.dependencies || {};
});
}
exports.getDependencies = getDependencies;
function getPeerDependencies(projectPath) {
return __awaiter(this, void 0, void 0, function* () {
const packageJson = yield fs.readJson('package.json', projectPath);
if (!packageJson) {
return {};
}
const peerDependencies = {};
for (const dep of Object.keys(packageJson.dependencies || {})) {
const json = yield fs.readJson((0, path_1.join)('node_modules', dep, 'package.json'), projectPath);
Object.assign(peerDependencies, json === null || json === void 0 ? void 0 : json.peerDependencies);
}
return peerDependencies;
});
}
exports.getPeerDependencies = getPeerDependencies;
;