Skip to content

Commit

Permalink
Change to ES6 (#2)
Browse files Browse the repository at this point in the history
* feat: bump to es6 and disable side effects

BREAKING CHANGE: Now targets es6 with the build to hopefully improve tree shaking.

* chore: path and minor updates

* chore: update kcd-scripts

* feat: remove chalk

* feat: update read-pkg-up

* fix: setup jest to support esm

* feat: set type to module in package.json

* fix: move package functions out of index

BREAKING CHANGE: this moves package functions which require native node functions to work, which messes up remix etc...
  • Loading branch information
Arcath authored Apr 4, 2022
1 parent 1cbc1f6 commit 453c388
Show file tree
Hide file tree
Showing 14 changed files with 4,843 additions and 4,229 deletions.
14 changes: 11 additions & 3 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
const configs = require('kcd-scripts/config')
import configs from 'kcd-scripts/config.js'

module.exports = Object.assign(configs.jest, {
const config = Object.assign(configs.jest, {
testMatch: ['**/__tests__/**/*.[jt]s?(x)', '**/?(*.)+(spec|test).[jt]s?(x)'],
transform: {
'^.+\\.tsx?$': 'ts-jest'
},
collectCoverage: true
collectCoverage: true,
extensionsToTreatAsEsm: ['.ts'],
globals: {
'ts-jest': {
useESM: true
}
}
})

export default config
8,957 changes: 4,783 additions & 4,174 deletions package-lock.json

Large diffs are not rendered by default.

22 changes: 12 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,33 @@
"name": "@arcath/utils",
"version": "0.0.0-semantically-released",
"main": "lib/index.js",
"sideEffects": false,
"scripts": {
"test": "kcd-scripts test --runInBand --detectOpenHandles",
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules kcd-scripts test --runInBand --detectOpenHandles",
"lint": "kcd-scripts lint",
"build": "tsc",
"document": "typedoc --out ./docs --entryPoints src/index.ts --name Utils --readme Readme.md && echo utils.arcath.net > ./docs/CNAME",
"validate": "kcd-scripts validate",
"format": "kcd-scripts format"
},
"type": "module",
"author": "Adam Laycock <adam@arcath.net>",
"license": "MIT",
"devDependencies": {
"@types/chalk": "^2.2.0",
"@types/jest": "^27.0.3",
"@types/jest": "^27.4.1",
"@types/left-pad": "^1.2.0",
"@types/node": "^16.11.11",
"jest": "^27.4.3",
"kcd-scripts": "^11.2.2",
"prettier": "^2.5.1",
"ts-jest": "^27.1.0",
"@types/node": "^17.0.23",
"cross-env": "^7.0.3",
"jest": "^27.5.1",
"kcd-scripts": "^12.1.0",
"prettier": "^2.6.2",
"ts-jest": "^27.1.4",
"typedoc": "^0.22.13",
"typescript": "^4.5.2"
"typescript": "^4.6.3"
},
"dependencies": {
"chalk": "^4.1.2",
"read-pkg-up": "^7.0.1"
"read-pkg-up": "^9.1.0"
},
"eslintConfig": {
"extends": "./node_modules/kcd-scripts/eslint.js",
Expand Down
3 changes: 3 additions & 0 deletions src/classes/logger.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
/* eslint no-control-regex:off */
// eslint-disable-next-line
import {jest} from '@jest/globals'

import {Logger} from '../'

describe('Logger', () => {
Expand Down
22 changes: 10 additions & 12 deletions src/classes/logger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import chalk from 'chalk'

import {defaults} from '../functions/defaults'

interface LoggerOptions {
Expand All @@ -24,20 +22,18 @@ export class Logger {
this.time = process.hrtime()
}

private message(message: string, color: chalk.Chalk, timed: boolean) {
let m = color(`[${this.timeString()}]`)
private message(message: string, timed: boolean) {
let m = `[${this.timeString()}]`

if (this.serviceName !== '') {
m += color(`[${this.serviceName}]`)
m += `[${this.serviceName}]`
}

m += ` ${message}`

if (timed) {
const diff = process.hrtime(this.time)
m += chalk.yellow(
` +${((diff[0] * 1e9 + diff[1]) / 1e9).toLocaleString('en-GB')}s`
)
m += ` +${((diff[0] * 1e9 + diff[1]) / 1e9).toLocaleString('en-GB')}s`

this.time = process.hrtime()
}
Expand All @@ -46,13 +42,15 @@ export class Logger {
}

log(message: string, timed = false) {
if (this.options.output)
console.log(this.message(message, chalk.green, timed))
if (this.options.output) {
console.log(this.message(message, timed))
}
}

error(message: string, timed = false) {
if (this.options.output)
console.log(this.message(message, chalk.red, timed))
if (this.options.output) {
console.log(this.message(message, timed))
}
}

private timeString() {
Expand Down
3 changes: 3 additions & 0 deletions src/functions/cache-for.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// eslint-disable-next-line
import {jest} from '@jest/globals'

import {
cacheForSync,
cacheFor,
Expand Down
3 changes: 2 additions & 1 deletion src/functions/if-fn.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {ifFn, hasDevDependency} from '../index'
import {ifFn} from '../index'
import {hasDevDependency} from './package'

describe('If FN', () => {
it('should return the correct result', () => {
Expand Down
3 changes: 2 additions & 1 deletion src/functions/indexed-by.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ export const indexedBy = <T extends {}, K extends keyof T>(

return array.reduce<IndexedArray<T>>((indexedArray, value) => {
//eslint-disable-next-line
if (!o.collide && indexedArray[value[key] as any])
if (!o.collide && indexedArray[value[key] as any]) {
throw new Error(`Key ${value[key]} occurs more than once`)
}

//eslint-disable-next-line
indexedArray[value[key] as any] = value
Expand Down
2 changes: 1 addition & 1 deletion src/functions/package.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
hasPeerDependency,
hasAnyDependency,
hasScript
} from '../index'
} from './package'

describe('Package Fns', () => {
it('should get the package', () => {
Expand Down
19 changes: 7 additions & 12 deletions src/functions/package.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import readPkg from 'read-pkg-up'
import {readPackageUpSync} from 'read-pkg-up'
import type {NormalizedPackageJson} from 'read-pkg-up'

import {defaults} from './defaults'
import {ifFn} from './if-fn'
Expand All @@ -20,7 +21,7 @@ export type IfDependency = <Truthy, Falsy>(
) => Falsy | Truthy

export interface Package {
pkg: readPkg.NormalizedPackageJson
pkg: NormalizedPackageJson
pkgPath: string
hasDependency: HasDependency
hasDevDependency: HasDependency
Expand All @@ -41,7 +42,7 @@ export interface Package {
const dependencyFunction = (
key: 'dependencies' | 'devDependencies' | 'peerDependencies'
) => {
return (pkg: readPkg.NormalizedPackageJson, dep: string) => {
return (pkg: NormalizedPackageJson, dep: string) => {
if (!pkg[key]) {
return false
}
Expand All @@ -55,21 +56,15 @@ export const hasDependency = dependencyFunction('dependencies')
export const hasDevDependency = dependencyFunction('devDependencies')
export const hasPeerDependency = dependencyFunction('peerDependencies')

export const hasAnyDependency = (
pkg: readPkg.NormalizedPackageJson,
dep: string
) => {
export const hasAnyDependency = (pkg: NormalizedPackageJson, dep: string) => {
return [
hasDependency(pkg, dep),
hasDevDependency(pkg, dep),
hasPeerDependency(pkg, dep)
].some(r => r)
}

export const hasScript = (
pkg: readPkg.NormalizedPackageJson,
script: string
) => {
export const hasScript = (pkg: NormalizedPackageJson, script: string) => {
if (!pkg.scripts) {
return false
}
Expand All @@ -88,7 +83,7 @@ export const getPackage = (options?: Partial<PackageOptions>): Package => {
cwd: process.cwd()
})

const result = readPkg.sync({cwd})
const result = readPackageUpSync({cwd})

if (typeof result === 'undefined') {
throw new Error('Could not find package.json')
Expand Down
9 changes: 7 additions & 2 deletions src/functions/reduce.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// eslint-disable-next-line
import {jest} from '@jest/globals'

import {reduceTruthy, reduceFalsy, reducio} from '../index'

describe('Reduce Functions', () => {
Expand All @@ -8,7 +11,8 @@ describe('Reduce Functions', () => {
})

it('should only call the check function until it hits a false', () => {
const check = jest.fn(e => e)
// eslint-disable-next-line
const check = jest.fn<boolean, any>(e => e)

reduceTruthy([true, true, false, true, true, true, false, true], check)

Expand All @@ -27,7 +31,8 @@ describe('Reduce Functions', () => {
reducio([true, true, false, false, true], e => e, {initial: true})
).toBeFalsy()

const check = jest.fn(e => e)
// eslint-disable-next-line
const check = jest.fn<boolean, any>(e => e)

expect(reducio([true, true, false, false, true], check)).toBeTruthy()

Expand Down
5 changes: 1 addition & 4 deletions src/functions/wait-for.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ describe('waitFor', () => {

const [result, error] = await waitFor(truthy())

if (error) {
throw error
}

expect(error).toBeNull()
expect(result).toBe(true)
})

Expand Down
8 changes: 0 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,6 @@ export {
export {replaceProperty} from './functions/replace-property'
export {propIs, propIsNot} from './functions/selectors'
export {nl2br} from './functions/nl2br'
export {
getPackage,
hasDependency,
hasDevDependency,
hasPeerDependency,
hasAnyDependency,
hasScript
} from './functions/package'
export {parameterize} from './functions/parameterize'
export {pick, omit} from './functions/pick'
export {
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"isolatedModules": false,
"outDir": "./lib",
"noEmit": false,
"target": "es5",
"target": "es6",
"declaration": true
},
"include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx"],
Expand Down

0 comments on commit 453c388

Please sign in to comment.