@tunnckocore/utils
Advanced tools
Comparing version 0.4.1 to 0.5.0
{ | ||
"version": "0.4.1", | ||
"version": "0.5.0", | ||
"name": "@tunnckocore/utils", | ||
"description": "Utility functions and helpers for internal usage", | ||
"description": "Thin layer on top of `execa` that allows executing multiple commands in parallel or in sequence with control for concurrency", | ||
"author": "Charlike Mike Reagent <opensource@tunnckocore.com>", | ||
"homepage": "https://github.com/tunnckocorehq/hela", | ||
"homepage": "https://github.com/tunnckoCore/opensource", | ||
"license": "MPL-2.0", | ||
"licenseStart": 2019, | ||
"main": "src/index.js", | ||
"module": "src/index.js", | ||
"types": "dist/types/index.d.ts", | ||
"scripts": {}, | ||
@@ -14,21 +17,38 @@ "publishConfig": { | ||
}, | ||
"engines": { | ||
"node": ">=8.11" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/tunnckoCoreHQ/hela.git", | ||
"url": "https://github.com/tunnckoCore/opensource.git", | ||
"directory": "@tunnckocore/utils" | ||
}, | ||
"files": [ | ||
"src" | ||
"dist" | ||
], | ||
"keywords": [ | ||
"tunnckocorehq", | ||
"devest", | ||
"hela", | ||
"jest", | ||
"env", | ||
"develop", | ||
"utils" | ||
"parallel", | ||
"series", | ||
"concurrency", | ||
"sequence", | ||
"exec", | ||
"child", | ||
"process", | ||
"execute", | ||
"fork", | ||
"execfile", | ||
"execa", | ||
"spawn", | ||
"file", | ||
"shell", | ||
"bin", | ||
"binary", | ||
"binaries", | ||
"npm", | ||
"path", | ||
"local" | ||
], | ||
"dependencies": {}, | ||
"devDependencies": {} | ||
"gitHead": "cdd74cf426c386836697113bf2865b3eabccf886" | ||
} |
@@ -1,10 +0,18 @@ | ||
'use strict'; | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const Module = require('module'); | ||
module.exports = { createAliases, getWorkspacesAndExtensions }; | ||
// eslint-disable-next-line no-underscore-dangle | ||
const EXTENSIONS = Object.keys(Module._extensions).filter( | ||
(x) => x !== '.json' && x !== '.node', | ||
); | ||
/* eslint-disable global-require, import/no-dynamic-require */ | ||
module.exports = { createAliases, getWorkspacesAndExtensions, isMonorepo }; | ||
function isMonorepo(cwd = process.cwd()) { | ||
const { workspaces } = getWorkspacesAndExtensions(cwd); | ||
return workspaces.length > 0; | ||
} | ||
/** | ||
@@ -19,5 +27,7 @@ * Create explicit alias key/value pair from the current | ||
*/ | ||
function createAliases(cwd, sourceDir) { | ||
const { workspaces, extensions, exts } = getWorkspacesAndExtensions(cwd); | ||
const alias = workspaces | ||
function createAliases(cwd = process.cwd(), sourceDirectory) { | ||
// const { workspaces, extensions, exts } = getWorkspacesAndExtensions(cwd); | ||
const result = getWorkspacesAndExtensions(cwd); | ||
const alias = result.workspaces | ||
.filter(Boolean) | ||
@@ -29,12 +39,19 @@ .reduce((acc, ws) => { | ||
.readdirSync(workspace) | ||
.map((dir) => { | ||
const pkgDir = path.join(workspace, dir); | ||
const pkgJsonPath = path.join(pkgDir, 'package.json'); | ||
.map((directory) => { | ||
const pkgDirectory = path.join(workspace, directory); | ||
const pkgJsonPath = path.join(pkgDirectory, 'package.json'); | ||
return { pkgDir, pkgJsonPath }; | ||
return { pkgDirectory, pkgJsonPath }; | ||
}) | ||
.map(({ pkgDir, pkgJsonPath }) => { | ||
.map(({ pkgDirectory, pkgJsonPath }) => { | ||
// package specific package.json | ||
const pkgJson = require(pkgJsonPath); | ||
return [pkgDir, pkgJson]; | ||
const packageJson = parseJson(pkgJsonPath, 'utf8'); | ||
/* istanbul ignore next */ | ||
if (Object.keys(packageJson).length === 0) { | ||
throw new Error( | ||
`Cannot find package.json or cannot parse it: ${pkgJsonPath}`, | ||
); | ||
} | ||
return [pkgDirectory, packageJson]; | ||
}); | ||
@@ -44,19 +61,28 @@ | ||
}, []) | ||
.reduce((acc, [pkgDir, pkgJson]) => { | ||
acc[pkgJson.name] = path.join(pkgDir, sourceDir || 'src'); | ||
.reduce((acc, [pkgDirectory, packageJson]) => { | ||
if (typeof sourceDirectory === 'string') { | ||
acc[packageJson.name] = path.join(pkgDirectory, sourceDirectory); | ||
} else { | ||
const source = path.join(pkgDirectory, 'src'); | ||
acc[packageJson.name] = fs.existsSync(source) ? source : pkgDirectory; | ||
} | ||
return acc; | ||
}, {}); | ||
return { cwd, extensions, exts, alias }; | ||
return { ...result, alias }; | ||
} | ||
function getWorkspacesAndExtensions(cwd) { | ||
function parseJson(fp) { | ||
return fs.existsSync(fp) ? JSON.parse(fs.readFileSync(fp, 'utf8')) : {}; | ||
} | ||
function getWorkspacesAndExtensions(cwd = process.cwd()) { | ||
const fromRoot = (...x) => path.resolve(cwd, ...x); | ||
const rootPkg = require(fromRoot('package.json')); | ||
const rootLerna = fs.existsSync(fromRoot('lerna.json')) | ||
? require(fromRoot('lerna.json')) | ||
: {}; | ||
const packagePath = fromRoot('package.json'); | ||
const lernaPath = fromRoot('lerna.json'); | ||
const pkg = parseJson(packagePath); | ||
const lerna = parseJson(lernaPath); | ||
const workspaces = [] | ||
.concat(rootLerna.packages || (rootPkg.workspaces || [])) | ||
.concat(lerna.packages || (pkg.workspaces || [])) | ||
.filter((x) => typeof x === 'string') | ||
@@ -67,9 +93,14 @@ .filter(Boolean) | ||
let exts = [].concat(rootPkg.extensions).filter(Boolean); | ||
exts = exts.length > 0 ? exts : ['js', 'jsx', 'ts', 'tsx', 'mjs']; | ||
exts = exts.map((ext) => (ext.startsWith('.') ? ext.slice(1) : ext)); | ||
let exts = [].concat(pkg.extensions).filter(Boolean); | ||
if (exts.length === 0) { | ||
exts = ['tsx', 'ts', 'jsx', ...EXTENSIONS]; | ||
} | ||
exts = exts.map((extension) => | ||
extension.startsWith('.') ? extension.slice(1) : extension, | ||
); | ||
const extensions = exts.map((x) => `.${x}`); | ||
return { workspaces, extensions, exts }; | ||
return { workspaces, extensions, exts, lerna, lernaPath, pkg, packagePath }; | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
12396
245
5
2
0
6
1