New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@wixc3/resolve-directory-context

Package Overview
Dependencies
Maintainers
75
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@wixc3/resolve-directory-context - npm Package Compare versions

Comparing version 1.0.4 to 2.0.0

9

dist/directory-context.d.ts

@@ -1,2 +0,4 @@

import { INpmPackage } from './npm-package';
import { ResolveWorkspacePackagesHost } from './yarn-workspaces';
import { INpmPackage, ResolveLinkedPackagesHost } from './npm-package';
import { FindUpHost } from './find-up';
export interface SinglePackageContext {

@@ -11,3 +13,6 @@ type: 'single';

}
export declare function resolveDirectoryContext(basePath: string): SinglePackageContext | MultiPackageContext;
export interface DirectoryContextHost extends FindUpHost, ResolveWorkspacePackagesHost, ResolveLinkedPackagesHost {
existsSync(path: string): boolean;
}
export declare function resolveDirectoryContext(basePath: string, host: DirectoryContextHost): SinglePackageContext | MultiPackageContext;
export declare function childPackagesFromContext(context: SinglePackageContext | MultiPackageContext): INpmPackage[];

@@ -14,0 +19,0 @@ export declare function allPackagesFromContext(context: SinglePackageContext | MultiPackageContext): INpmPackage[];

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getRootPackage = exports.allPackagesFromContext = exports.childPackagesFromContext = exports.resolveDirectoryContext = void 0;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const yarn_workspaces_1 = require("./yarn-workspaces");

@@ -13,9 +8,9 @@ const language_helpers_1 = require("./language-helpers");

const find_up_1 = require("./find-up");
function resolveDirectoryContext(basePath) {
const packageJsonPath = (0, find_up_1.findFileUpSync)(basePath, npm_package_1.PACKAGE_JSON);
function resolveDirectoryContext(basePath, host) {
const packageJsonPath = (0, find_up_1.findFileUpSync)(basePath, npm_package_1.PACKAGE_JSON, host);
if (!(0, language_helpers_1.isString)(packageJsonPath)) {
throw new Error(`Cannot find ${npm_package_1.PACKAGE_JSON} for ${basePath}`);
}
const directoryPath = path_1.default.dirname(packageJsonPath);
const packageJsonContent = fs_1.default.readFileSync(packageJsonPath, 'utf8');
const directoryPath = host.dirname(packageJsonPath);
const packageJsonContent = host.readFileSync(packageJsonPath, 'utf8');
const packageJson = JSON.parse(packageJsonContent);

@@ -38,8 +33,8 @@ if (!(0, language_helpers_1.isPlainObject)(packageJson)) {

rootPackage,
packages: (0, npm_package_1.sortPackagesByDepth)((0, yarn_workspaces_1.resolveWorkspacePackages)(directoryPath, (0, yarn_workspaces_1.extractPackageLocations)(packageJson.workspaces))),
packages: (0, npm_package_1.sortPackagesByDepth)((0, yarn_workspaces_1.resolveWorkspacePackages)(directoryPath, (0, yarn_workspaces_1.extractPackageLocations)(packageJson.workspaces), host)),
};
}
const lernaJsonPath = path_1.default.join(directoryPath, 'lerna.json');
if (fs_1.default.existsSync(lernaJsonPath)) {
const lernaJsonContents = fs_1.default.readFileSync(lernaJsonPath, 'utf8');
const lernaJsonPath = host.join(directoryPath, 'lerna.json');
if (host.existsSync(lernaJsonPath)) {
const lernaJsonContents = host.readFileSync(lernaJsonPath, 'utf8');
const lernaJson = JSON.parse(lernaJsonContents);

@@ -50,7 +45,7 @@ if ((0, language_helpers_1.isPlainObject)(packageJson) && Array.isArray(lernaJson.packages)) {

rootPackage,
packages: (0, npm_package_1.sortPackagesByDepth)((0, yarn_workspaces_1.resolveWorkspacePackages)(directoryPath, (0, yarn_workspaces_1.extractPackageLocations)(lernaJson.packages))),
packages: (0, npm_package_1.sortPackagesByDepth)((0, yarn_workspaces_1.resolveWorkspacePackages)(directoryPath, (0, yarn_workspaces_1.extractPackageLocations)(lernaJson.packages), host)),
};
}
}
const linkedPackages = (0, npm_package_1.resolveLinkedPackages)(rootPackage);
const linkedPackages = (0, npm_package_1.resolveLinkedPackages)(rootPackage, host);
if (linkedPackages.length) {

@@ -57,0 +52,0 @@ return {

@@ -1,2 +0,9 @@

export declare function findFileUpSync(directoryPath: string, fileName: string): string | undefined;
export interface FindUpHost {
statSync(path: string): {
isFile(): boolean;
};
dirname(path: string): string;
join(...segments: string[]): string;
}
export declare function findFileUpSync(directoryPath: string, fileName: string, host: FindUpHost): string | undefined;
//# sourceMappingURL=find-up.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.findFileUpSync = void 0;
const fs_1 = require("fs");
const path_1 = require("path");
function findFileUpSync(directoryPath, fileName) {
for (const directoryInChain of pathChainToRoot(directoryPath)) {
const filePath = (0, path_1.join)(directoryInChain, fileName);
function findFileUpSync(directoryPath, fileName, host) {
for (const directoryInChain of pathChainToRoot(directoryPath, host.dirname)) {
const filePath = host.join(directoryInChain, fileName);
try {
if ((0, fs_1.statSync)(filePath).isFile()) {
if (host.statSync(filePath).isFile()) {
return filePath;

@@ -19,3 +17,3 @@ }

exports.findFileUpSync = findFileUpSync;
function* pathChainToRoot(currentPath) {
function* pathChainToRoot(currentPath, dirname) {
let lastPath;

@@ -25,5 +23,5 @@ while (lastPath !== currentPath) {

lastPath = currentPath;
currentPath = (0, path_1.dirname)(currentPath);
currentPath = dirname(currentPath);
}
}
//# sourceMappingURL=find-up.js.map

@@ -10,4 +10,8 @@ import type { PackageJson } from 'type-fest';

}
export declare function resolveLinkedPackages(rootPackage: INpmPackage): INpmPackage[];
export interface ResolveLinkedPackagesHost {
readFileSync(filePath: string, encoding: 'utf8'): string;
join(...segments: string[]): string;
}
export declare function resolveLinkedPackages(rootPackage: INpmPackage, host: ResolveLinkedPackagesHost): INpmPackage[];
export declare function sortPackagesByDepth(packages: INpmPackage[]): INpmPackage[];
//# sourceMappingURL=npm-package.d.ts.map
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.sortPackagesByDepth = exports.resolveLinkedPackages = exports.PACKAGE_JSON = void 0;
const path_1 = __importDefault(require("path"));
const fs_1 = __importDefault(require("fs"));
const language_helpers_1 = require("./language-helpers");
exports.PACKAGE_JSON = 'package.json';
function resolveLinkedPackages(rootPackage) {
function resolveLinkedPackages(rootPackage, host) {
const { dependencies = {}, devDependencies = {} } = rootPackage.packageJson;

@@ -17,5 +12,5 @@ const linkedPackages = [];

const linkTarget = request.slice(5);
const directoryPath = path_1.default.join(rootPackage.directoryPath, linkTarget);
const packageJsonPath = path_1.default.join(directoryPath, exports.PACKAGE_JSON);
const packageJsonContent = fs_1.default.readFileSync(packageJsonPath, 'utf8');
const directoryPath = host.join(rootPackage.directoryPath, linkTarget);
const packageJsonPath = host.join(directoryPath, exports.PACKAGE_JSON);
const packageJsonContent = host.readFileSync(packageJsonPath, 'utf8');
const packageJson = JSON.parse(packageJsonContent);

@@ -22,0 +17,0 @@ if (!(0, language_helpers_1.isPlainObject)(packageJson)) {

import type { PackageJson } from 'type-fest';
import { INpmPackage } from './npm-package';
export declare function resolveWorkspacePackages(basePath: string, workspaces: string[]): INpmPackage[];
export interface ResolveWorkspacePackagesHost {
readFileSync(filePath: string, encoding: 'utf8'): string;
readdirSync(directoryPath: string, options: {
withFileTypes: true;
}): Iterable<{
name: string;
isFile(): boolean;
isDirectory(): boolean;
}>;
dirname(path: string): string;
relative(from: string, to: string): string;
join(...segments: string[]): string;
}
export declare function resolveWorkspacePackages(basePath: string, workspaces: string[], host: ResolveWorkspacePackagesHost): INpmPackage[];
export declare function extractPackageLocations(workspaces: PackageJson.YarnConfiguration['workspaces']): string[];
export declare function deepFindFilesSync(directoryPath: string, filterFile: ((fileName: string, filePath: string) => boolean) | undefined, filterDirectory: ((directoryName: string, directoryPath: string) => boolean) | undefined, host: ResolveWorkspacePackagesHost): Generator<string>;
//# sourceMappingURL=yarn-workspaces.d.ts.map

@@ -6,23 +6,17 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.extractPackageLocations = exports.resolveWorkspacePackages = void 0;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const glob_1 = __importDefault(require("glob"));
exports.deepFindFilesSync = exports.extractPackageLocations = exports.resolveWorkspacePackages = void 0;
const minimatch_1 = __importDefault(require("minimatch"));
const language_helpers_1 = require("./language-helpers");
const npm_package_1 = require("./npm-package");
function resolveWorkspacePackages(basePath, workspaces) {
function resolveWorkspacePackages(basePath, workspaces, host) {
const packages = new Map();
const globOptions = {
cwd: basePath,
absolute: true,
ignore: '**/node_modules/**',
};
const packageJsonFilePaths = Array.from(deepFindFilesSync(basePath, (fileName) => fileName === npm_package_1.PACKAGE_JSON, (directoryName) => !directoryName.startsWith('.') && directoryName !== 'node_modules', host));
for (const packageDirGlob of workspaces) {
const packageJsonGlob = path_1.default.posix.join(packageDirGlob, npm_package_1.PACKAGE_JSON);
const packageJsonPaths = glob_1.default.sync(packageJsonGlob, globOptions);
for (const packageJsonPath of packageJsonPaths.map(path_1.default.normalize)) {
const packageJsonGlob = ensureEndsWithPackageJson(packageDirGlob);
const packageJsonPaths = packageJsonFilePaths.filter((packageJsonPath) => (0, minimatch_1.default)(host.relative(basePath, packageJsonPath), packageJsonGlob));
for (const packageJsonPath of packageJsonPaths) {
if (packages.has(packageJsonPath)) {
continue;
}
const packageJsonContent = fs_1.default.readFileSync(packageJsonPath, 'utf8');
const packageJsonContent = host.readFileSync(packageJsonPath, 'utf8');
const packageJson = JSON.parse(packageJsonContent);

@@ -37,3 +31,3 @@ if (!(0, language_helpers_1.isPlainObject)(packageJson)) {

packageJson,
directoryPath: path_1.default.dirname(packageJsonPath),
directoryPath: host.dirname(packageJsonPath),
packageJsonContent,

@@ -67,2 +61,25 @@ });

exports.extractPackageLocations = extractPackageLocations;
function* deepFindFilesSync(directoryPath, filterFile = () => true, filterDirectory = () => true, host) {
for (const item of host.readdirSync(directoryPath, { withFileTypes: true })) {
const itemPath = host.join(directoryPath, item.name);
if (item.isFile() && filterFile(item.name, itemPath)) {
yield host.join(directoryPath, item.name);
}
else if (item.isDirectory() && filterDirectory(item.name, itemPath)) {
yield* deepFindFilesSync(itemPath, filterFile, filterDirectory, host);
}
}
}
exports.deepFindFilesSync = deepFindFilesSync;
function ensureEndsWithPackageJson(workspace) {
if (workspace.endsWith(`/${npm_package_1.PACKAGE_JSON}`)) {
return workspace;
}
else if (workspace.endsWith('/')) {
return workspace + npm_package_1.PACKAGE_JSON;
}
else {
return `${workspace}/${npm_package_1.PACKAGE_JSON}`;
}
}
//# sourceMappingURL=yarn-workspaces.js.map
{
"name": "@wixc3/resolve-directory-context",
"description": "Helpers to get information about single/multi-package contexts",
"version": "1.0.4",
"version": "2.0.0",
"main": "./dist/index.js",

@@ -11,4 +11,5 @@ "scripts": {

"pretest": "npm run typecheck",
"test": "npm run test:spec",
"test:spec": "mocha \"./test/**/*.spec.ts\"",
"test": "npm run test:node && npm run test:browser",
"test:node": "mocha \"./test/**/*.{spec,nodespec}.ts\"",
"test:browser": "mocha-play \"./test/**/*.spec.ts\"",
"lint": "eslint . -f codeframe",

@@ -18,15 +19,21 @@ "typecheck": "tsc --noEmit"

"dependencies": {
"glob": "^7.2.0",
"type-fest": "^2.9.0"
"minimatch": "^4.1.1",
"type-fest": "^2.11.2"
},
"devDependencies": {
"@file-services/memory": "^5.7.1",
"@file-services/path": "^5.7.1",
"@ts-tools/node": "^3.0.1",
"@ts-tools/webpack-loader": "^3.0.2",
"@types/chai": "^4.3.0",
"@types/glob": "^7.2.0",
"@types/mocha": "^9.0.0",
"@types/minimatch": "^3.0.5",
"@types/mocha": "^9.1.0",
"@types/node": "14",
"chai": "^4.3.4",
"mocha": "^9.1.3",
"chai": "^4.3.6",
"mocha": "^9.2.0",
"mocha-play": "^3.0.2",
"playwright-chromium": "^1.19.0",
"rimraf": "^3.0.2",
"typescript": "~4.5.4"
"typescript": "~4.5.5",
"webpack": "^5.68.0"
},

@@ -33,0 +40,0 @@ "files": [

@@ -1,8 +0,12 @@

import fs from 'fs';
import path from 'path';
import type { PackageJson } from 'type-fest';
import { resolveWorkspacePackages, extractPackageLocations } from './yarn-workspaces';
import { resolveWorkspacePackages, ResolveWorkspacePackagesHost, extractPackageLocations } from './yarn-workspaces';
import { isPlainObject, isString } from './language-helpers';
import { INpmPackage, PACKAGE_JSON, resolveLinkedPackages, sortPackagesByDepth } from './npm-package';
import { findFileUpSync } from './find-up';
import {
INpmPackage,
PACKAGE_JSON,
resolveLinkedPackages,
ResolveLinkedPackagesHost,
sortPackagesByDepth,
} from './npm-package';
import { findFileUpSync, FindUpHost } from './find-up';

@@ -20,5 +24,12 @@ export interface SinglePackageContext {

export function resolveDirectoryContext(basePath: string): SinglePackageContext | MultiPackageContext {
const packageJsonPath = findFileUpSync(basePath, PACKAGE_JSON);
export interface DirectoryContextHost extends FindUpHost, ResolveWorkspacePackagesHost, ResolveLinkedPackagesHost {
existsSync(path: string): boolean;
}
export function resolveDirectoryContext(
basePath: string,
host: DirectoryContextHost
): SinglePackageContext | MultiPackageContext {
const packageJsonPath = findFileUpSync(basePath, PACKAGE_JSON, host);
if (!isString(packageJsonPath)) {

@@ -28,5 +39,5 @@ throw new Error(`Cannot find ${PACKAGE_JSON} for ${basePath}`);

const directoryPath = path.dirname(packageJsonPath);
const directoryPath = host.dirname(packageJsonPath);
const packageJsonContent = fs.readFileSync(packageJsonPath, 'utf8');
const packageJsonContent = host.readFileSync(packageJsonPath, 'utf8');
const packageJson = JSON.parse(packageJsonContent) as PackageJson;

@@ -53,3 +64,3 @@ if (!isPlainObject(packageJson)) {

packages: sortPackagesByDepth(
resolveWorkspacePackages(directoryPath, extractPackageLocations(packageJson.workspaces))
resolveWorkspacePackages(directoryPath, extractPackageLocations(packageJson.workspaces), host)
),

@@ -59,5 +70,5 @@ };

const lernaJsonPath = path.join(directoryPath, 'lerna.json');
if (fs.existsSync(lernaJsonPath)) {
const lernaJsonContents = fs.readFileSync(lernaJsonPath, 'utf8');
const lernaJsonPath = host.join(directoryPath, 'lerna.json');
if (host.existsSync(lernaJsonPath)) {
const lernaJsonContents = host.readFileSync(lernaJsonPath, 'utf8');
const lernaJson = JSON.parse(lernaJsonContents) as { packages?: string[] };

@@ -69,3 +80,3 @@ if (isPlainObject(packageJson) && Array.isArray(lernaJson.packages)) {

packages: sortPackagesByDepth(
resolveWorkspacePackages(directoryPath, extractPackageLocations(lernaJson.packages))
resolveWorkspacePackages(directoryPath, extractPackageLocations(lernaJson.packages), host)
),

@@ -76,3 +87,3 @@ };

const linkedPackages = resolveLinkedPackages(rootPackage);
const linkedPackages = resolveLinkedPackages(rootPackage, host);
if (linkedPackages.length) {

@@ -79,0 +90,0 @@ return {

@@ -1,9 +0,12 @@

import { statSync } from 'fs';
import { dirname, join } from 'path';
export interface FindUpHost {
statSync(path: string): { isFile(): boolean };
dirname(path: string): string;
join(...segments: string[]): string;
}
export function findFileUpSync(directoryPath: string, fileName: string) {
for (const directoryInChain of pathChainToRoot(directoryPath)) {
const filePath = join(directoryInChain, fileName);
export function findFileUpSync(directoryPath: string, fileName: string, host: FindUpHost) {
for (const directoryInChain of pathChainToRoot(directoryPath, host.dirname)) {
const filePath = host.join(directoryInChain, fileName);
try {
if (statSync(filePath).isFile()) {
if (host.statSync(filePath).isFile()) {
return filePath;

@@ -16,3 +19,3 @@ }

function* pathChainToRoot(currentPath: string) {
function* pathChainToRoot(currentPath: string, dirname: (path: string) => string) {
let lastPath: string | undefined;

@@ -19,0 +22,0 @@ while (lastPath !== currentPath) {

@@ -1,3 +0,1 @@

import path from 'path';
import fs from 'fs';
import type { PackageJson } from 'type-fest';

@@ -16,3 +14,8 @@ import { flattenTree, isString, concatIterables, isPlainObject } from './language-helpers';

export function resolveLinkedPackages(rootPackage: INpmPackage): INpmPackage[] {
export interface ResolveLinkedPackagesHost {
readFileSync(filePath: string, encoding: 'utf8'): string;
join(...segments: string[]): string;
}
export function resolveLinkedPackages(rootPackage: INpmPackage, host: ResolveLinkedPackagesHost): INpmPackage[] {
const { dependencies = {}, devDependencies = {} } = rootPackage.packageJson;

@@ -23,5 +26,5 @@ const linkedPackages: INpmPackage[] = [];

const linkTarget = request.slice(5);
const directoryPath = path.join(rootPackage.directoryPath, linkTarget);
const packageJsonPath = path.join(directoryPath, PACKAGE_JSON);
const packageJsonContent = fs.readFileSync(packageJsonPath, 'utf8');
const directoryPath = host.join(rootPackage.directoryPath, linkTarget);
const packageJsonPath = host.join(directoryPath, PACKAGE_JSON);
const packageJsonContent = host.readFileSync(packageJsonPath, 'utf8');
const packageJson = JSON.parse(packageJsonContent) as PackageJson;

@@ -28,0 +31,0 @@

@@ -1,4 +0,2 @@

import fs from 'fs';
import path from 'path';
import glob from 'glob';
import minimatch from 'minimatch';
import type { PackageJson } from 'type-fest';

@@ -8,17 +6,39 @@ import { isString, isPlainObject } from './language-helpers';

export function resolveWorkspacePackages(basePath: string, workspaces: string[]): INpmPackage[] {
export interface ResolveWorkspacePackagesHost {
readFileSync(filePath: string, encoding: 'utf8'): string;
readdirSync(
directoryPath: string,
options: { withFileTypes: true }
): Iterable<{ name: string; isFile(): boolean; isDirectory(): boolean }>;
dirname(path: string): string;
relative(from: string, to: string): string;
join(...segments: string[]): string;
}
export function resolveWorkspacePackages(
basePath: string,
workspaces: string[],
host: ResolveWorkspacePackagesHost
): INpmPackage[] {
const packages = new Map<string, INpmPackage>();
const globOptions: glob.IOptions = {
cwd: basePath,
absolute: true,
ignore: '**/node_modules/**',
};
const packageJsonFilePaths = Array.from(
deepFindFilesSync(
basePath,
(fileName) => fileName === PACKAGE_JSON,
(directoryName) => !directoryName.startsWith('.') && directoryName !== 'node_modules',
host
)
);
for (const packageDirGlob of workspaces) {
const packageJsonGlob = path.posix.join(packageDirGlob, PACKAGE_JSON);
const packageJsonPaths = glob.sync(packageJsonGlob, globOptions);
for (const packageJsonPath of packageJsonPaths.map(path.normalize)) {
const packageJsonGlob = ensureEndsWithPackageJson(packageDirGlob);
const packageJsonPaths = packageJsonFilePaths.filter((packageJsonPath) =>
minimatch(host.relative(basePath, packageJsonPath), packageJsonGlob)
);
for (const packageJsonPath of packageJsonPaths) {
if (packages.has(packageJsonPath)) {
continue;
}
const packageJsonContent = fs.readFileSync(packageJsonPath, 'utf8');
const packageJsonContent = host.readFileSync(packageJsonPath, 'utf8');
const packageJson = JSON.parse(packageJsonContent) as PackageJson;

@@ -35,3 +55,3 @@ if (!isPlainObject(packageJson)) {

packageJson,
directoryPath: path.dirname(packageJsonPath),
directoryPath: host.dirname(packageJsonPath),
packageJsonContent,

@@ -62,1 +82,27 @@ });

}
export function* deepFindFilesSync(
directoryPath: string,
filterFile: (fileName: string, filePath: string) => boolean = () => true,
filterDirectory: (directoryName: string, directoryPath: string) => boolean = () => true,
host: ResolveWorkspacePackagesHost
): Generator<string> {
for (const item of host.readdirSync(directoryPath, { withFileTypes: true })) {
const itemPath = host.join(directoryPath, item.name);
if (item.isFile() && filterFile(item.name, itemPath)) {
yield host.join(directoryPath, item.name);
} else if (item.isDirectory() && filterDirectory(item.name, itemPath)) {
yield* deepFindFilesSync(itemPath, filterFile, filterDirectory, host);
}
}
}
function ensureEndsWithPackageJson(workspace: string) {
if (workspace.endsWith(`/${PACKAGE_JSON}`)) {
return workspace;
} else if (workspace.endsWith('/')) {
return workspace + PACKAGE_JSON;
} else {
return `${workspace}/${PACKAGE_JSON}`;
}
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc