Skip to content

Commit

Permalink
feat: split @pkgr/utils into @pkgr/core and @pkgr/browser
Browse files Browse the repository at this point in the history
close #352
  • Loading branch information
JounQin committed Dec 26, 2023
1 parent 423567b commit b3e91c3
Show file tree
Hide file tree
Showing 15 changed files with 168 additions and 84 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"workspaces": [
"packages/**"
],
"packageManager": "[email protected].19",
"packageManager": "[email protected].21",
"scripts": {
"build": "run-s build:ts build:r",
"build:r": "tsx packages/rollup/src/cli.ts -f cjs -d false",
Expand Down
File renamed without changes.
File renamed without changes.
36 changes: 36 additions & 0 deletions packages/browser/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "@pkgr/browser",
"version": "0.0.0",
"type": "module",
"description": "Shared browser utils for `@pkgr` packages or any package else",
"repository": "git+https://github.com/un-ts/pkgr.git",
"homepage": "https://github.com/un-ts/pkgr/blob/master/packages/browser",
"author": "JounQin (https://www.1stG.me) <[email protected]>",
"funding": "https://opencollective.com/unts",
"license": "MIT",
"engines": {
"node": "^12.20.0 || ^14.18.0 || >=16.0.0"
},
"main": "./lib/index.cjs",
"module": "./lib/index.js",
"exports": {
"types": "./lib/index.d.ts",
"import": "./lib/index.js",
"require": "./lib/index.cjs"
},
"types": "./lib/index.d.ts",
"files": [
"lib",
"openChrome.applescript"
],
"dependencies": {
"cross-spawn": "^7.0.3",
"open": "^9.1.0",
"picocolors": "^1.0.0",
"tslib": "^2.6.1"
},
"publishConfig": {
"access": "public"
},
"sideEffects": false
}
9 changes: 9 additions & 0 deletions packages/browser/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "@1stg/tsconfig/lib",
"compilerOptions": {
"composite": true,
"outDir": "lib",
"rootDir": "."
},
"exclude": ["lib", "test"]
}
29 changes: 29 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "@pkgr/core",
"version": "0.0.0",
"type": "module",
"description": "Shared core module for `@pkgr` packages or any package else",
"repository": "git+https://github.com/un-ts/pkgr.git",
"homepage": "https://github.com/un-ts/pkgr/blob/master/packages/core",
"author": "JounQin (https://www.1stG.me) <[email protected]>",
"funding": "https://opencollective.com/unts",
"license": "MIT",
"engines": {
"node": "^12.20.0 || ^14.18.0 || >=16.0.0"
},
"main": "./lib/index.cjs",
"module": "./lib/index.js",
"exports": {
"types": "./lib/index.d.ts",
"import": "./lib/index.js",
"require": "./lib/index.cjs"
},
"types": "./lib/index.d.ts",
"files": [
"lib"
],
"publishConfig": {
"access": "public"
},
"sideEffects": false
}
9 changes: 9 additions & 0 deletions packages/core/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { createRequire } from 'node:module'

export const CWD = process.cwd()

export const cjsRequire =
typeof require === 'undefined' ? createRequire(import.meta.url) : require

// eslint-disable-next-line n/no-deprecated-api, sonar/deprecation
export const EXTENSIONS = ['.ts', '.tsx', ...Object.keys(cjsRequire.extensions)]
61 changes: 61 additions & 0 deletions packages/core/src/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import fs from 'node:fs'
import path from 'node:path'

import { CWD, EXTENSIONS, cjsRequire } from './constants.js'

export const tryPkg = (pkg: string) => {
try {
return cjsRequire.resolve(pkg)
} catch {}
}

export const isPkgAvailable = (pkg: string) => !!tryPkg(pkg)

export const tryFile = (filePath?: string[] | string, includeDir = false) => {
if (typeof filePath === 'string') {
return fs.existsSync(filePath) &&
(includeDir || fs.statSync(filePath).isFile())
? filePath
: ''
}

for (const file of filePath ?? []) {
if (tryFile(file, includeDir)) {
return file
}
}

return ''
}

export const tryExtensions = (filepath: string, extensions = EXTENSIONS) => {
const ext = [...extensions, ''].find(ext => tryFile(filepath + ext))
return ext == null ? '' : filepath + ext
}

export const findUp = (searchEntry: string, searchFile = 'package.json') => {
console.assert(path.isAbsolute(searchEntry))

if (
!tryFile(searchEntry, true) ||
(searchEntry !== CWD && !searchEntry.startsWith(CWD + path.sep))
) {
return ''
}

searchEntry = path.resolve(
fs.statSync(searchEntry).isDirectory()
? searchEntry
: path.resolve(searchEntry, '..'),
)

do {
const searched = tryFile(path.resolve(searchEntry, searchFile))
if (searched) {
return searched
}
searchEntry = path.resolve(searchEntry, '..')
} while (searchEntry === CWD || searchEntry.startsWith(CWD + path.sep))

return ''
}
2 changes: 2 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './constants.js'
export * from './helpers.js'
9 changes: 9 additions & 0 deletions packages/core/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "@1stg/tsconfig/lib",
"compilerOptions": {
"composite": true,
"outDir": "lib",
"rootDir": "src"
},
"exclude": ["lib", "test"]
}
10 changes: 3 additions & 7 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,12 @@
},
"types": "./lib/index.d.ts",
"files": [
"lib",
"openChrome.applescript"
"lib"
],
"dependencies": {
"cross-spawn": "^7.0.3",
"@pkgr/core": "^0.0.0",
"fast-glob": "^3.3.1",
"is-glob": "^4.0.3",
"open": "^9.1.0",
"picocolors": "^1.0.0",
"tslib": "^2.6.1"
"is-glob": "^4.0.3"
},
"publishConfig": {
"access": "public"
Expand Down
10 changes: 0 additions & 10 deletions packages/utils/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { createRequire } from 'node:module'

export const DEV = 'development' as const
export const PROD = 'production' as const

Expand All @@ -10,14 +8,6 @@ export const __PROD__ = NODE_ENV === PROD

export const NODE_MODULES_REG = /[/\\]node_modules[/\\]/

export const CWD = process.cwd()

export const cjsRequire =
typeof require === 'undefined' ? createRequire(import.meta.url) : require

// eslint-disable-next-line n/no-deprecated-api, sonar/deprecation
export const EXTENSIONS = ['.ts', '.tsx', ...Object.keys(cjsRequire.extensions)]

export const SCRIPT_RUNNERS = {
npm: 'npx',
pnpm: 'pnpm',
Expand Down
67 changes: 2 additions & 65 deletions packages/utils/src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
import fs from 'node:fs'
import path from 'node:path'

import { CWD, cjsRequire, isPkgAvailable, tryFile } from '@pkgr/core'
import isGlob from 'is-glob'

import {
CWD,
EXTENSIONS,
cjsRequire,
SCRIPT_RUNNERS,
SCRIPT_EXECUTORS,
} from './constants.js'

export const tryPkg = (pkg: string) => {
try {
return cjsRequire.resolve(pkg)
} catch {}
}
import { SCRIPT_RUNNERS, SCRIPT_EXECUTORS } from './constants.js'

export const tryRequirePkg = <T>(pkg: string): T | undefined => {
try {
Expand All @@ -24,8 +12,6 @@ export const tryRequirePkg = <T>(pkg: string): T | undefined => {
} catch {}
}

export const isPkgAvailable = (pkg: string) => !!tryPkg(pkg)

export const isTsAvailable = isPkgAvailable('typescript')

export const isAngularAvailable = isPkgAvailable('@angular/core/package.json')
Expand All @@ -40,28 +26,6 @@ export const isSvelteAvailable = isPkgAvailable('svelte')

export const isVueAvailable = isPkgAvailable('vue')

export const tryFile = (filePath?: string[] | string, includeDir = false) => {
if (typeof filePath === 'string') {
return fs.existsSync(filePath) &&
(includeDir || fs.statSync(filePath).isFile())
? filePath
: ''
}

for (const file of filePath ?? []) {
if (tryFile(file, includeDir)) {
return file
}
}

return ''
}

export const tryExtensions = (filepath: string, extensions = EXTENSIONS) => {
const ext = [...extensions, ''].find(ext => tryFile(filepath + ext))
return ext == null ? '' : filepath + ext
}

export const tryGlob = (
paths: string[],
options:
Expand Down Expand Up @@ -103,33 +67,6 @@ export const identify = <T>(
'' | (T extends boolean ? false : boolean) | null | undefined
> => !!_

export const findUp = (searchEntry: string, searchFile = 'package.json') => {
console.assert(path.isAbsolute(searchEntry))

if (
!tryFile(searchEntry, true) ||
(searchEntry !== CWD && !searchEntry.startsWith(CWD + path.sep))
) {
return ''
}

searchEntry = path.resolve(
fs.statSync(searchEntry).isDirectory()
? searchEntry
: path.resolve(searchEntry, '..'),
)

do {
const searched = tryFile(path.resolve(searchEntry, searchFile))
if (searched) {
return searched
}
searchEntry = path.resolve(searchEntry, '..')
} while (searchEntry === CWD || searchEntry.startsWith(CWD + path.sep))

return ''
}

export const arrayify = <
T,
R = T extends Array<infer S> ? NonNullable<S> : NonNullable<T>,
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './browser.js'
export * from './constants.js'
export * from './helpers.js'
export * from './monorepo.js'
export * from '@pkgr/core'
6 changes: 6 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
{
"path": "packages/es-modules"
},
{
"path": "packages/browser"
},
{
"path": "packages/core"
},
{
"path": "packages/utils"
},
Expand Down

0 comments on commit b3e91c3

Please sign in to comment.