Best JavaScript code snippet using fast-check-monorepo
npm.ts
Source:npm.ts
1import chalk from "chalk";2import Debug from "debug";3import path from "path";4import { Configuration, Package } from "./interfaces";5import { getLicense } from "./license";6import { getRepository } from "./repository";7import * as util from "./util";8const debug = Debug("license-compliance:npm");9const PACKAGE_JSON = "package.json";10const NODE_MODULES = "node_modules";11/**12 * Gets an array of all installed packages, regardless of license being valid.13 *14 * @export15 * @returns {Promise<Array<Package>>}16 */17export async function getInstalledPackages(configuration: Pick<Configuration, "direct" | "development" | "production">, rootPath = ""): Promise<Array<Package>> {18 const packages: Array<Package> = new Array<Package>();19 // Paths20 const rootNodeModulesPath = path.join(rootPath, NODE_MODULES);21 const packPath = path.join(rootPath, PACKAGE_JSON);22 const pack = await util.readPackageJson(packPath);23 if (!pack) {24 return new Array<Package>();25 }26 if (pack.dependencies && !configuration.development) {27 debug("Analyzing production dependencies at", rootNodeModulesPath);28 await readPackages(pack.name, pack.dependencies, 0, rootNodeModulesPath, configuration, rootNodeModulesPath, packages);29 }30 if (pack.devDependencies && !configuration.production) {31 debug("Analyzing development dependencies at", rootNodeModulesPath);32 await readPackages(pack.name, pack.devDependencies, 0, rootNodeModulesPath, configuration, rootNodeModulesPath, packages);33 }34 return packages;35}36/**37 * Verifies if the package was analyzed.38 * A combination of name and version is used; different versions of the same package might have different licenses.39 *40 * @param {Package} pack Package to verify.41 * @returns {boolean} true if it was analyzed; otherwise, false.42 */43function alreadyAnalyzed(packages: Array<Package>, pack: Package): boolean {44 return packages.find((value): boolean => value.name === pack.name && value.version === pack.version) !== undefined;45}46/**47 * Asynchronously gets the package's location.48 * First, it verifies if there is a specific package version installed within the parent's node_module folder.49 * If not found, then it verifies if the package is installed in the root node_modules folder.50 *51 * @param {string} parentName Name of the parent package. Required to analyze packages with sub-folders.52 * @param {string} packageName Name of the package.53 * @param {(string | undefined)} parentNodeModulesPath Path of the parent package.54 * @returns {(Promise<string | undefined>)} Promise with the path where the package was found; undefined if not found.55 */56async function getInstalledPath(parentName: string, packageName: string, parentNodeModulesPath: string, rootNodeModulesPath: string): Promise<string | undefined> {57 // Verify if present in parent's node_modules58 let packagePath = path.join(parentNodeModulesPath, packageName);59 if (await util.fileExists(packagePath)) {60 return packagePath;61 }62 // Verify if present in sibling node_modules63 const pathComposite = [parentNodeModulesPath, "..", ".."];64 for (let i = 0; i < parentName.split("/").length - 1; i++) {65 pathComposite.push("..");66 }67 pathComposite.push(packageName);68 packagePath = path.join(...pathComposite);69 if (await util.fileExists(packagePath)) {70 return packagePath;71 }72 // Verify if present in root node_module73 packagePath = path.join(rootNodeModulesPath, packageName);74 if (await util.fileExists(packagePath)) {75 return packagePath;76 }77 return undefined;78}79/**80 * Asynchronously reads recursively all dependencies.81 * If arg --direct was provided, it only reads direct installed packages82 *83 * @param {Array<[string, string]>} dependencies Dependencies (package names) to read.84 * @param {number} depth Depth counter; root level is zero.85 * @param {string} parentNodeModulesPath Parent package's node_module path.86 * @returns {Promise<void>}87 */88async function readPackages(parentName: string, dependencies: Array<[string, string]>, depth: number,89 parentNodeModulesPath: string, configuration: Pick<Configuration, "direct">,90 rootNodeModulesPath: string, packages: Array<Package>): Promise<void> {91 if (depth > 0 && configuration.direct) {92 return;93 }94 const getPackagePromises = new Array<Promise<void>>();95 for (const dependency of Object.keys(dependencies)) {96 getPackagePromises.push(getPackage(parentName, dependency, parentNodeModulesPath, configuration, depth, rootNodeModulesPath, packages));97 }98 await Promise.all(getPackagePromises);99}100async function getPackage(parentName: string, dependency: string, parentNodeModulesPath: string,101 configuration: Pick<Configuration, "direct">, depth: number, rootNodeModulesPath: string, packages: Array<Package>): Promise<void> {102 const packagePath = await getInstalledPath(parentName, dependency, parentNodeModulesPath, rootNodeModulesPath);103 if (packagePath === undefined) {104 console.error(chalk.red(`Package "${dependency}" was not found. Confirm that all modules are installed.`));105 return;106 }107 const file = await util.readPackageJson(path.join(packagePath, PACKAGE_JSON));108 if (!file) {109 console.error(chalk.red(`Package "${dependency}" is empty and cannot be analyzed.`));110 return;111 }112 const license = await getLicense(file, packagePath);113 const pack: Package = {114 license: license.name,115 licenseFile: license.path,116 name: dependency,117 path: packagePath,118 repository: getRepository(file.repository),119 version: file.version,120 };121 if (alreadyAnalyzed(packages, pack)) {122 return;123 }124 packages.push(pack);125 if (file.dependencies) {126 await readPackages(dependency, file.dependencies, depth + 1, path.join(packagePath, NODE_MODULES), configuration, rootNodeModulesPath, packages);127 }...
node-modules-dependencies-builder.js
Source:node-modules-dependencies-builder.js
1"use strict";2Object.defineProperty(exports, "__esModule", { value: true });3const path = require("path");4const constants_1 = require("../../constants");5class NodeModulesDependenciesBuilder {6 constructor($fs) {7 this.$fs = $fs;8 }9 getProductionDependencies(projectPath) {10 const rootNodeModulesPath = path.join(projectPath, constants_1.NODE_MODULES_FOLDER_NAME);11 const projectPackageJsonPath = path.join(projectPath, constants_1.PACKAGE_JSON_FILE_NAME);12 const packageJsonContent = this.$fs.readJson(projectPackageJsonPath);13 const dependencies = packageJsonContent && packageJsonContent.dependencies;14 const resolvedDependencies = [];15 const queue = _.keys(dependencies)16 .map(dependencyName => ({17 parent: null,18 parentDir: projectPath,19 name: dependencyName,20 depth: 021 }));22 while (queue.length) {23 const currentModule = queue.shift();24 const resolvedDependency = this.findModule(rootNodeModulesPath, currentModule, resolvedDependencies);25 if (resolvedDependency && !_.some(resolvedDependencies, r => r.directory === resolvedDependency.directory)) {26 _.each(resolvedDependency.dependencies, d => {27 const dependency = { parent: currentModule, name: d, parentDir: resolvedDependency.directory, depth: resolvedDependency.depth + 1 };28 const shouldAdd = !_.some(queue, element => element.parent === dependency.parent &&29 element.name === dependency.name &&30 element.parentDir === dependency.parentDir &&31 element.depth === dependency.depth);32 if (shouldAdd) {33 queue.push(dependency);34 }35 });36 resolvedDependencies.push(resolvedDependency);37 }38 }39 return resolvedDependencies;40 }41 findModule(rootNodeModulesPath, depDescription, resolvedDependencies) {42 let modulePath = path.join(depDescription.parentDir, constants_1.NODE_MODULES_FOLDER_NAME, depDescription.name);43 const rootModulesPath = path.join(rootNodeModulesPath, depDescription.name);44 let depthInNodeModules = depDescription.depth;45 if (!this.moduleExists(modulePath)) {46 let moduleExists = false;47 let parent = depDescription.parent;48 while (parent && !moduleExists) {49 modulePath = path.join(depDescription.parent.parentDir, constants_1.NODE_MODULES_FOLDER_NAME, depDescription.name);50 moduleExists = this.moduleExists(modulePath);51 if (!moduleExists) {52 parent = parent.parent;53 }54 }55 if (!moduleExists) {56 modulePath = rootModulesPath;57 if (!this.moduleExists(modulePath)) {58 return null;59 }60 }61 depthInNodeModules = 0;62 }63 if (_.some(resolvedDependencies, r => r.name === depDescription.name && r.directory === modulePath)) {64 return null;65 }66 return this.getDependencyData(depDescription.name, modulePath, depthInNodeModules);67 }68 getDependencyData(name, directory, depth) {69 const dependency = {70 name,71 directory,72 depth73 };74 const packageJsonPath = path.join(directory, constants_1.PACKAGE_JSON_FILE_NAME);75 const packageJsonExists = this.$fs.getLsStats(packageJsonPath).isFile();76 if (packageJsonExists) {77 const packageJsonContents = this.$fs.readJson(packageJsonPath);78 if (!!packageJsonContents.nativescript) {79 dependency.nativescript = packageJsonContents.nativescript;80 }81 dependency.dependencies = _.keys(packageJsonContents.dependencies);82 return dependency;83 }84 return null;85 }86 moduleExists(modulePath) {87 try {88 let modulePathLsStat = this.$fs.getLsStats(modulePath);89 if (modulePathLsStat.isSymbolicLink()) {90 modulePathLsStat = this.$fs.getLsStats(this.$fs.realpath(modulePath));91 }92 return modulePathLsStat.isDirectory();93 }94 catch (e) {95 return false;96 }97 }98}99exports.NodeModulesDependenciesBuilder = NodeModulesDependenciesBuilder;...
Using AI Code Generation
1const path = require('path');2const { rootNodeModulesPath } = require('fast-check-monorepo-helper');3const nodeModulesPath = rootNodeModulesPath();4const jestPath = path.join(nodeModulesPath, '.bin', 'jest');5console.log(jestPath);6{7 "scripts": {8 }9}
Using AI Code Generation
1const path = require('path');2const fc = require('fast-check');3const { rootNodeModulesPath } = require('fast-check-monorepo');4const rootPath = path.join(rootNodeModulesPath(), 'fast-check');5const fastCheckPath = path.join(rootPath, 'lib', 'fast-check-default.js');6const fastCheck = require(fastCheckPath);7fastCheck.check(fastCheck.property(fastCheck.integer(), (i) => {8 return i === i;9}));
Using AI Code Generation
1const { rootNodeModulesPath } = require('fast-check-monorepo');2const path = require('path');3const fastCheckPath = path.join(rootNodeModulesPath(), 'fast-check');4const { rootNodeModulesPath } = require('fast-check-monorepo');5const fastCheckPath = path.join(rootNodeModulesPath, 'fast-check');6const path = require('path');7const fastCheckPath = path.join(path, 'fast-check');8const { rootNodeModulesPath } = require('fast-check-monorepo');9const fastCheckPath = path.join(rootNodeModulesPath(), 'fast-check');10const { rootNodeModulesPath } = require('fast-check-monorepo');11const fastCheckPath = path.join(rootNodeModulesPath, 'fast-check');
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!