Skip to content

Commit

Permalink
feat: split @pkgr/utils into @pkgr/core and @pkgr/browser (#354)
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin authored Dec 26, 2023
1 parent 423567b commit 686238a
Show file tree
Hide file tree
Showing 23 changed files with 973 additions and 915 deletions.
7 changes: 7 additions & 0 deletions .changeset/fluffy-jars-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@pkgr/browser": minor
"@pkgr/core": minor
"@pkgr/utils": minor
---

feat: split `@pkgr/utils` into `@pkgr/core` and `@pkgr/browser`
2 changes: 1 addition & 1 deletion .codesandbox/ci.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"node": "16",
"node": "18",
"packages": [
"packages/*"
],
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ jobs:
runs-on: ${{ matrix.os }}
steps:
- name: Checkout Repo
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup Node.js ${{ matrix.node }}
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
cache: yarn

- name: Install Dependencies
run: yarn --frozen-lockfile
run: yarn --frozen-lockfile --ignore-engines

- name: Build, Lint and Test
run: yarn run-s lint typecov test
Expand Down
13 changes: 8 additions & 5 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
name: 'CodeQL'
name: CodeQL

on:
push:
branches: ['master']
branches:
- master
pull_request:
branches: ['master']
branches:
- master
schedule:
- cron: '4 21 * * 3'

Expand All @@ -20,11 +22,12 @@ jobs:
strategy:
fail-fast: false
matrix:
language: [javascript]
language:
- javascript

steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Initialize CodeQL
uses: github/codeql-action/init@v2
Expand Down
12 changes: 5 additions & 7 deletions .github/workflows/pkg-size.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
name: Package Size Report

on:
pull_request:
branches:
- master
- pull_request

jobs:
pkg-size-report:
Expand All @@ -12,12 +10,12 @@ jobs:

steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v3
- name: Setup Node.js LTS
uses: actions/setup-node@v4
with:
node-version: 16
node-version: lts/*
cache: yarn

- name: Package Size Report
Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
# This makes Actions fetch all Git history so that Changesets can generate changelogs with the correct commits
fetch-depth: 0

- name: Setup Node.js 16
uses: actions/setup-node@v3
- name: Setup Node.js LTS
uses: actions/setup-node@v4
with:
node-version: 16
node-version: lts/*
cache: yarn

- name: Install Dependencies
run: yarn --frozen-lockfile

# `@pkgr/utils` is used by `@1stg/lint-staged`
- name: Build prerelease
- name: Build
run: yarn build

- name: Create Release Pull Request or Publish to npm
Expand Down
34 changes: 17 additions & 17 deletions 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 All @@ -27,24 +27,24 @@
"typecov": "type-coverage"
},
"devDependencies": {
"@1stg/common-config": "^9.0.0",
"@changesets/changelog-github": "^0.4.8",
"@changesets/cli": "^2.26.2",
"@types/cross-spawn": "^6.0.2",
"@types/debug": "^4.1.8",
"@types/imagemin": "^8.0.1",
"@types/is-glob": "^4.0.2",
"@types/jest": "^29.5.3",
"@types/lodash-es": "^4.17.8",
"@types/micromatch": "^4.0.2",
"@types/node": "^20.4.6",
"jest": "^29.6.2",
"@1stg/common-config": "^9.0.1",
"@changesets/changelog-github": "^0.5.0",
"@changesets/cli": "^2.27.1",
"@types/cross-spawn": "^6.0.6",
"@types/debug": "^4.1.12",
"@types/imagemin": "^8.0.5",
"@types/is-glob": "^4.0.4",
"@types/jest": "^29.5.11",
"@types/lodash-es": "^4.17.12",
"@types/micromatch": "^4.0.6",
"@types/node": "^20.10.5",
"jest": "^29.7.0",
"patch-package": "^8.0.0",
"rimraf": "^5.0.1",
"rimraf": "^5.0.5",
"ts-jest": "^29.1.1",
"tsx": "^3.12.7",
"type-coverage": "^2.26.0",
"typescript": "^5.1.6",
"tsx": "^4.7.0",
"type-coverage": "^2.27.1",
"typescript": "^5.3.3",
"yarn-deduplicate": "^6.0.2"
},
"resolutions": {
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": "^8.4.2 || ^9.1.0 || ^10.0.1",
"picocolors": "^1.0.0",
"tslib": "^2.6.2"
},
"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"]
}
12 changes: 4 additions & 8 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",
"fast-glob": "^3.3.1",
"is-glob": "^4.0.3",
"open": "^9.1.0",
"picocolors": "^1.0.0",
"tslib": "^2.6.1"
"@pkgr/core": "^0.0.0",
"fast-glob": "^3.3.2",
"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
Loading

0 comments on commit 686238a

Please sign in to comment.