Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use TS CommonJS modules #51

Merged
merged 5 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ module.exports = {
'no-restricted-globals': ['warn'],
'no-restricted-properties': ['warn'],
'no-restricted-syntax': ['warn'],
'import/no-unresolved': ['error', { commonjs: true }],
'import/extensions': [
'error',
'ignorePackages',
Expand All @@ -46,5 +47,6 @@ module.exports = {
tsx: 'never',
},
],
'@typescript-eslint/no-var-requires': 'off',
},
}
2 changes: 1 addition & 1 deletion packages/abbr/src/abbr.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default function abbr(str: string, maxLength = 55, divider = `[...]`): string {
export = function abbr(str: string, maxLength = 55, divider = `[...]`): string {
if (str !== `${str}`) {
return str
}
Expand Down
1 change: 0 additions & 1 deletion packages/analyze-step/src/analyzeStep.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import { describe, test } from 'node:test'
import assert from 'node:assert'
import analyzeStep from './analyzeStep'
// const util = require('util')

const ROBOTS = {
'/image/resize': {
Expand Down
17 changes: 10 additions & 7 deletions packages/analyze-step/src/analyzeStep.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
/* eslint-disable quote-props */
/* eslint-disable no-template-curly-in-string */
import formatDurationMs from '@transloadit/format-duration-ms'
import prettierBytes from '@transloadit/prettier-bytes'
import * as inflect from 'inflection'
import { JSONPath } from 'jsonpath-plus'
import { clone, countBy, get, has } from 'lodash'
import formatDurationMs = require('@transloadit/format-duration-ms')
import prettierBytes = require('@transloadit/prettier-bytes')
import inflect = require('inflection')
import JSONPath = require('jsonpath-plus')
import clone = require('lodash/clone')
import countBy = require('lodash/countBy')
import get = require('lodash/get')
import has = require('lodash/has')

function humanJoin(array: string[], reduce = true, glueword = 'and'): string {
let countedArray = array
Expand Down Expand Up @@ -69,7 +72,7 @@
if (typeof step[type] === 'string') {
collection[type]?.push(`Filter by code evaluation`)
} else if (step[type] && Array.isArray(step[type])) {
for (const [key, operator, val] of Object.values(step[type]!)) {

Check warning on line 75 in packages/analyze-step/src/analyzeStep.ts

View workflow job for this annotation

GitHub Actions / test

Forbidden non-null assertion
const template = clone(templates[operator])
if (!template) {
throw new Error(
Expand Down Expand Up @@ -306,10 +309,10 @@
Partial<PresetStep> &
Partial<FormatStep> & {
// using any until we can put some effort into Assembly/Template/Robot types
[key: string]: any

Check warning on line 312 in packages/analyze-step/src/analyzeStep.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
}

export default function humanize(step: Step, robots: Robots, extrameta: ExtraMeta = {}): string {
export = function humanize(step: Step, robots: Robots, extrameta: ExtraMeta = {}): string {
let str = ``

const robot = robots[step.robot]
Expand Down Expand Up @@ -395,7 +398,7 @@
}

if (robot?.rname === '/video/merge') {
const types = JSONPath({ path: '$..as', json: step })
const types = JSONPath.JSONPath({ path: '$..as', json: step })
if (types.length) {
str = `Merge ${humanJoin(types)} to create a new video`
} else if (get(step, 'ffmpeg.f') === 'gif') {
Expand Down
13 changes: 5 additions & 8 deletions packages/enrich-tweet/src/enrichTweet.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import twttr from 'twitter-text'
import { tall } from 'tall'
import twttr = require('twitter-text')
import tall = require('tall')

import getUrls from 'get-urls'
import getUrls = require('get-urls')

async function tryUnshorten(url: string, unshorten: boolean): Promise<string> {
if (!unshorten) return url
try {
return await tall(url)
return await tall.tall(url)
} catch (err) {
return url
}
Expand All @@ -32,10 +32,7 @@ type Tweet = {
}
}

export default async function enrichTweet(
tweet: Tweet,
unshorten = true
): Promise<string | undefined> {
export = async function enrichTweet(tweet: Tweet, unshorten = true): Promise<string | undefined> {
if (!tweet) return

let text = tweet.full_text ?? ''
Expand Down
4 changes: 2 additions & 2 deletions packages/file-exists/src/fileExists.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from 'fs'
import fs = require('fs')

export default function fileExists(path: string): Promise<boolean> {
export = function fileExists(path: string): Promise<boolean> {
return new Promise((resolve) => {
fs.access(path, fs.constants.F_OK, (err) => {
resolve(!err)
Expand Down
4 changes: 2 additions & 2 deletions packages/format-duration-ms/src/formatDurationMs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import prettyMS from 'pretty-ms'
import prettyMS = require('pretty-ms')

export default function formatDurationMs(ms: number): string {
export = function formatDurationMs(ms: number): string {
let human = prettyMS(ms)

human = human.replace(/(\d+)\.\d+s/g, '$1s')
Expand Down
2 changes: 1 addition & 1 deletion packages/has-property/src/has-property.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { test } from 'node:test'
import assert from 'node:assert'
import { hasProperty } from './has-property'
import hasProperty from './has-property'

test('hasProperty', () => {
assert.ok(hasProperty({ foo: 'bar' }, 'foo'))
Expand Down
2 changes: 1 addition & 1 deletion packages/has-property/src/has-property.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// eslint-disable-next-line import/prefer-default-export
export function hasProperty<K extends PropertyKey>(
export = function hasProperty<K extends PropertyKey>(
obj: unknown,
key: K | null | undefined
): obj is Record<K, unknown> {
Expand Down
12 changes: 6 additions & 6 deletions packages/post/src/post.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#!/usr/bin/env node
/* eslint-disable no-console */
import { promises as fs } from 'fs'
import inquirer from 'inquirer'
import fs = require('fs/promises')
import inquirer = require('inquirer')

import openInEditor from 'open-in-editor'
import fileExists from '@transloadit/file-exists'
import slugify from '@transloadit/slugify'
import title from 'title'
import openInEditor = require('open-in-editor')
import fileExists = require('@transloadit/file-exists')
import slugify = require('@transloadit/slugify')
import title = require('title')

async function post(): Promise<void> {
console.log(`Welcome to @transloadit/post.`)
Expand Down
4 changes: 2 additions & 2 deletions packages/pr/src/pr.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as util from 'util'
import util = require('util')

export default function pr<T>(...args: T[]): T[] {
export = function pr<T>(...args: T[]): T[] {
for (const arg of args) {
console.log(util.inspect(arg, false, null, true))
}
Expand Down
4 changes: 2 additions & 2 deletions packages/prd/src/prd.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pr from '@transloadit/pr'
import pr = require('@transloadit/pr')

export default function prd<T>(...args: T[]): void {
export = function prd<T>(...args: T[]): void {
pr(...args)
const err = new Error('Halt via prd')
console.error(err)
Expand Down
8 changes: 6 additions & 2 deletions packages/prd/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "dist/",
"rootDir": "src/"
"rootDir": "src/",
"paths": {
"@transloadit/pr": ["../pr"]
}
},
"include": ["src/"]
"include": ["src/"],
"references": [{ "path": "../pr" }]
}
2 changes: 1 addition & 1 deletion packages/prettier-bytes/src/prettierBytes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Adapted from https://github.com/Flet/prettier-bytes/
// Changing 1000 bytes to 1024, so we can keep uppercase KB vs kB
// ISC License (c) Dan Flettre https://github.com/Flet/prettier-bytes/blob/master/LICENSE
export default function prettierBytes(num: number): string {
export = function prettierBytes(num: number): string {
if (typeof num !== 'number' || Number.isNaN(num)) {
throw new TypeError(`Expected a number, got ${typeof num}`)
}
Expand All @@ -10,7 +10,7 @@
const units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']

if (neg) {
num = -num

Check warning on line 13 in packages/prettier-bytes/src/prettierBytes.ts

View workflow job for this annotation

GitHub Actions / test

Assignment to function parameter 'num'
}

if (num < 1) {
Expand All @@ -18,7 +18,7 @@
}

const exponent = Math.min(Math.floor(Math.log(num) / Math.log(1024)), units.length - 1)
num = Number(num / 1024 ** exponent)

Check warning on line 21 in packages/prettier-bytes/src/prettierBytes.ts

View workflow job for this annotation

GitHub Actions / test

Assignment to function parameter 'num'
const unit = units[exponent]

if (num >= 10 || num % 1 === 0) {
Expand Down
2 changes: 1 addition & 1 deletion packages/slugify/src/slugify.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default function slugify(str: string): string {
export = function slugify(str: string): string {
if (!str || str !== `${str}`) return str

return str
Expand Down
8 changes: 4 additions & 4 deletions packages/sort-assembly/src/sortAssembly.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import sortObjectByPrio from '@transloadit/sort-object-by-prio'
import sortResult from '@transloadit/sort-result'
import { hasProperty } from '@transloadit/has-property'
import sortObjectByPrio = require('@transloadit/sort-object-by-prio')
import sortResult = require('@transloadit/sort-result')
import hasProperty = require('@transloadit/has-property')

export default function sortAssembly<T extends Record<string, unknown>>(assembly: T): T {
export = function sortAssembly<T extends Record<string, unknown>>(assembly: T): T {
const sorted = sortObjectByPrio(assembly, {
_: ['assembly_id', 'ok', 'message', 'warnings', 'error'],
z: ['uploads', 'results'],
Expand Down
4 changes: 2 additions & 2 deletions packages/sort-object-by-prio/src/sortObjectByPrio.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import sortObject from '@transloadit/sort-object'
import sortObject = require('@transloadit/sort-object')

type Prefixes = {
[prefix: string]: Array<string | RegExp>
}

export default function sortObjectByPrio<T extends Record<string, unknown>>(
export = function sortObjectByPrio<T extends Record<string, unknown>>(
obj: T,
prefixes: Prefixes
): T {
Expand Down
2 changes: 1 addition & 1 deletion packages/sort-object/src/sortObject.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default function sortObject<T extends Record<string, unknown>>(
export = function sortObject<T extends Record<string, unknown>>(
obj: T,
// eslint-disable-next-line no-unused-vars
sortFunc?: (a: string, b: string) => number
Expand All @@ -7,7 +7,7 @@
return Object.keys(obj)
.sort(sortFunc)
.reduce((result, key: keyof T) => {
result[key] = obj[key]

Check warning on line 10 in packages/sort-object/src/sortObject.ts

View workflow job for this annotation

GitHub Actions / test

Assignment to property of function parameter 'result'
return result
}, {} as T)
}
4 changes: 2 additions & 2 deletions packages/sort-result-meta/src/sortResultMeta.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import sortObjectByPrio from '@transloadit/sort-object-by-prio'
import sortObjectByPrio = require('@transloadit/sort-object-by-prio')

type Meta = {
faces?: Record<string, unknown>[]
Expand All @@ -10,14 +10,14 @@
)
}

export default function sortResultMeta<T extends Meta>(meta: T): T {
export = function sortResultMeta<T extends Meta>(meta: T): T {
if (meta.faces) {
for (let i = 0; i < meta.faces.length; i++) {
const el = meta.faces[i]
if (!isObject(el)) continue
if (!meta.faces) continue

meta.faces[i] = sortObjectByPrio(el, {

Check warning on line 20 in packages/sort-result-meta/src/sortResultMeta.ts

View workflow job for this annotation

GitHub Actions / test

Assignment to property of function parameter 'meta'
_: [],
z: ['x1', 'y1', 'x2', 'y2'],
})
Expand Down
6 changes: 3 additions & 3 deletions packages/sort-result/src/sortResult.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sortObjectByPrio from '@transloadit/sort-object-by-prio'
import sortResultMeta from '@transloadit/sort-result-meta'
import sortObjectByPrio = require('@transloadit/sort-object-by-prio')
import sortResultMeta = require('@transloadit/sort-result-meta')

export default function sortResult<T extends { meta?: unknown }>(result: T): T {
export = function sortResult<T extends { meta?: unknown }>(result: T): T {
const sorted = sortObjectByPrio(result, {
_: ['id'],
z: ['meta'],
Expand Down
2 changes: 1 addition & 1 deletion packages/trigger-pager/src/triggerPager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ mockRequire('@pagerduty/pdjs', () => {
return { api: () => ({ post: mockPost }) }
})

const { default: triggerPager } = require('./triggerPager')
const triggerPager = require('./triggerPager')

const LOREM_LONG = `Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Expand Down
8 changes: 4 additions & 4 deletions packages/trigger-pager/src/triggerPager.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { api } from '@pagerduty/pdjs'
import pagerduty = require('@pagerduty/pdjs')

const PRIORITY_P1 = 'PUTY3A1'
const DUPLICATE_INCIDENT_MESSAGE = 'matching dedup key already exists'

export type TriggerPagerOptions = {
type TriggerPagerOptions = {
description: string
from?: string
incidentKey: string
Expand All @@ -22,7 +22,7 @@ const triggerPager = async ({
token,
urgency = 'high',
}: TriggerPagerOptions): Promise<void> => {
const res = await api({ token }).post('/incidents', {
const res = await pagerduty.api({ token }).post('/incidents', {
headers: {
from,
},
Expand Down Expand Up @@ -58,4 +58,4 @@ const triggerPager = async ({
}
}

export default triggerPager
export = triggerPager
5 changes: 3 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"compilerOptions": {
"esModuleInterop": true,
"lib": ["es2022"],
"lib": ["ES2023"],
"target": "es2022",
"module": "commonjs",
"module": "nodenext",
"verbatimModuleSyntax": true,

"forceConsistentCasingInFileNames": true,
"skipLibCheck": true,
Expand Down