Skip to content

Commit

Permalink
Merge branch 'main' into c8
Browse files Browse the repository at this point in the history
# Conflicts:
#	.gitignore
#	.prettierignore
#	package-lock.json
#	package.json
  • Loading branch information
fisker committed Mar 4, 2025
2 parents ece6e12 + 24d9da5 commit e85ebba
Show file tree
Hide file tree
Showing 20 changed files with 8,003 additions and 9,606 deletions.
1 change: 0 additions & 1 deletion .eslintignore

This file was deleted.

1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
16 changes: 5 additions & 11 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,24 @@ jobs:
# - 'macos-latest'
# - 'windows-latest'
node_version:
- '18'
- '16'
- '14'
- '12'
- '22'
- '20'
name: Node.js ${{ matrix.node_version }} on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

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

# https://github.com/bahmutov/npm-install/issues/103#issuecomment-931226602
- name: Update NPM
run: npm install --global npm@8

- name: Install Dependencies
run: npm ci

- name: Lint
if: matrix.node_version == '18'
if: matrix.node_version == '22'
run: npm run lint

- name: Run Tests
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v3
uses: actions/setup-node@v4

- name: Install dependencies
run: npm i
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
node_modules
/cjs
/index.cjs
/coverage
2 changes: 0 additions & 2 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
coverage/
cjs/
tests/snapshots/
18 changes: 18 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import globals from 'globals'
import eslintJs from '@eslint/js'
import eslintConfigPrettier from 'eslint-config-prettier'
import eslintPluginN from 'eslint-plugin-n'
import eslintPluginPromise from 'eslint-plugin-promise'

export default [
eslintJs.configs.recommended,
eslintPluginN.configs['flat/recommended'],
eslintPluginPromise.configs['flat/recommended'],
eslintConfigPrettier,
{
languageOptions: {
globals: { ...globals.builtin, ...globals.node },
},
},
{ ignores: ['index.cjs'] },
]
44 changes: 12 additions & 32 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import isPlainObject from 'is-plain-obj'
import semver from 'semver'

const hasOwn =
// eslint-disable-next-line n/no-unsupported-features/es-builtins, n/no-unsupported-features/es-syntax -- will enable later
Object.hasOwn ||
// TODO: Remove this when we drop supported for Node.js v14
((object, property) => Object.prototype.hasOwnProperty.call(object, property))
const pipe =
(fns) =>
(x, ...args) =>
fns.reduce((result, fn) => fn(result, ...args), x)
const onArray = (fn) => (x) => Array.isArray(x) ? fn(x) : x
const onArray = (fn) => (x) => (Array.isArray(x) ? fn(x) : x)
const onStringArray = (fn) => (x) =>
Array.isArray(x) && x.every((item) => typeof item === 'string') ? fn(x) : x
const uniq = onStringArray((xs) => [...new Set(xs)])
Expand Down Expand Up @@ -89,23 +90,17 @@ const sortObjectBySemver = sortObjectBy((a, b) => {
})

const getPackageName = (ident) => {
const parts = ident.split('@')

if (ident.startsWith('@')) {
// Handle cases where package name starts with '@'
return parts.length > 2 ? parts.slice(0, -1).join('@') : ident
}

// Handle cases where package name doesn't start with '@'
return parts.length > 1 ? parts.slice(0, -1).join('@') : ident
const index = ident.indexOf('@', ident.startsWith('@') ? 1 : 0)
// This should not happen, unless user manually edit the package.json file
return index === -1 ? ident : ident.slice(0, index)
}

const sortObjectByIdent = (a, b) => {
const PackageNameA = getPackageName(a)
const PackageNameB = getPackageName(b)
const packageNameA = getPackageName(a)
const packageNameB = getPackageName(b)

if (PackageNameA < PackageNameB) return -1
if (PackageNameA > PackageNameB) return 1
if (packageNameA < packageNameB) return -1
if (packageNameA > packageNameB) return 1
return 0
}

Expand Down Expand Up @@ -248,24 +243,9 @@ const sortScripts = onObject((scripts, packageJson) => {
keys.sort()
}

const scriptsKeyMap = new Map()

keys
.flatMap((key) =>
prefixable.has(key) ? [`pre${key}`, key, `post${key}`] : [key],
)
.forEach((key) => {
const [prefix] = key.split(':')
const keySet = scriptsKeyMap.has(prefix)
? scriptsKeyMap.get(prefix)
: new Set()
scriptsKeyMap.set(prefix, keySet.add(key))
})

const order = [...scriptsKeyMap.values()].flat().reduce((keys, keySet) => {
keys.push(...keySet)
return keys
}, [])
const order = keys.flatMap((key) =>
prefixable.has(key) ? [`pre${key}`, key, `post${key}`] : [key],
)

return sortObjectKeys(scripts, order)
})
Expand Down
Loading

0 comments on commit e85ebba

Please sign in to comment.