Skip to content

Commit

Permalink
chore: format, lint, fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
kentcdodds committed Sep 14, 2023
1 parent 4019cee commit 8779eae
Show file tree
Hide file tree
Showing 61 changed files with 726 additions and 643 deletions.
5 changes: 4 additions & 1 deletion .github/PULL_REQUEST_TEMPLATE.md
Expand Up @@ -15,16 +15,19 @@ merge of your pull request!
-->

**What**:

<!-- What changes are being made? (What feature/bug is being fixed here?) -->

**Why**:

<!-- Why are these changes necessary? -->

**How**:
<!-- How were these changes implemented? -->

<!-- How were these changes implemented? -->

**Checklist**:

<!-- Have you done all of these things? -->

<!-- To check an item, place an "x" in the box like so: "- [x] Documentation" -->
Expand Down
4 changes: 1 addition & 3 deletions jest.config.js
Expand Up @@ -10,9 +10,7 @@ config.moduleNameMapper = {

config.testEnvironment = 'jsdom'

config.setupFilesAfterEnv = [
'<rootDir>/testenv/jest.js',
]
config.setupFilesAfterEnv = ['<rootDir>/testenv/jest.js']

config.testMatch.push('<rootDir>/tests/**/*.+(js|jsx|ts|tsx)')

Expand Down
4 changes: 3 additions & 1 deletion scripts/package.json
@@ -1 +1,3 @@
{"type": "module"}
{
"type": "module"
}
214 changes: 115 additions & 99 deletions scripts/setup.js
@@ -1,7 +1,7 @@
import fs from 'fs/promises'
import path from 'path'
import { createBundleBuilder } from '@ph.fritsche/toolbox/dist/builder/index.js'
import { spawn } from 'child_process'
import {createBundleBuilder} from '@ph.fritsche/toolbox/dist/builder/index.js'
import {spawn} from 'child_process'

const dirname = path.dirname(new URL(import.meta.url).pathname)
const indexDirLib = path.join(dirname, '../testenv/libs')
Expand All @@ -13,124 +13,140 @@ const cmd = process.argv[2]
const names = process.argv.length > 3 ? process.argv.slice(3) : undefined

if (cmd === 'install-lib') {
await Promise.all(
(await getLibDirs(names))
.map(([name, dir]) => installLib(name, dir))
)
await Promise.all(
(await getLibDirs(names)).map(([name, dir]) => installLib(name, dir)),
)
} else if (cmd === 'bundle-lib') {
await Promise.all(
(await getLibDirs(names))
.map(([name, dir]) => buildLib(name, dir))
)
await Promise.all(
(await getLibDirs(names)).map(([name, dir]) => buildLib(name, dir)),
)
} else if (cmd === 'bundle-env') {
await Promise.all(
(await getEnvFiles(names))
.map(([name, file]) => buildEnv(name, file))
)
await Promise.all(
(await getEnvFiles(names)).map(([name, file]) => buildEnv(name, file)),
)
} else if (!cmd) {
await Promise.all([
...(await getLibDirs()).map(([name, dir]) => installLib(name, dir).then(() => buildLib(name, dir))),
...(await getEnvFiles()).map(([name, file]) => buildEnv(name, file)),
])
await Promise.all([
...(
await getLibDirs()
).map(([name, dir]) =>
installLib(name, dir).then(() => buildLib(name, dir)),
),
...(await getEnvFiles()).map(([name, file]) => buildEnv(name, file)),
])
}

async function getLibDirs(names) {
names ??= (await fs.readdir(indexDirLib)).filter(n => !n.startsWith('.'))

return await Promise.all(names.map(name => {
const dir = `${indexDirLib}/${name}`

return fs.stat(`${dir}/index.js`).then(
() => [name, dir],
() => {throw new Error(`${dir}/index.js could not be found.`)}
)
}))
names ??= (await fs.readdir(indexDirLib)).filter(n => !n.startsWith('.'))

return await Promise.all(
names.map(name => {
const dir = `${indexDirLib}/${name}`

return fs.stat(`${dir}/index.js`).then(
() => [name, dir],
() => {
throw new Error(`${dir}/index.js could not be found.`)
},
)
}),
)
}

async function getEnvFiles(names) {
names ??= (await fs.readdir(indexDirEnv))
.filter(n => /^\w+\.js$/.test(n))
.filter(n => !ignoreEnv.includes(n))
.map(f => f.slice(0, f.length - 3))

return await Promise.all(names.map(async name => {
const file = `${indexDirEnv}/${name}.js`

return fs.stat(file).then(
() => [name, file],
() => { throw new Error(`${file} could not be found.`)}
)
}))
names ??= (await fs.readdir(indexDirEnv))
.filter(n => /^\w+\.js$/.test(n))
.filter(n => !ignoreEnv.includes(n))
.map(f => f.slice(0, f.length - 3))

return await Promise.all(
names.map(async name => {
const file = `${indexDirEnv}/${name}.js`

return fs.stat(file).then(
() => [name, file],
() => {
throw new Error(`${file} could not be found.`)
},
)
}),
)
}

async function installLib(name, dir) {
return new Promise((res, rej) => {
const child = spawn('npm', ['i'], {cwd: dir})
return new Promise((res, rej) => {
const child = spawn('npm', ['i'], {cwd: dir})

process.stdout.write(`Installing library "${name}" at ${dir}\n`)
process.stdout.write(`Installing library "${name}" at ${dir}\n`)

child.on('error', e => {
process.stdout.write(`${e.stack ?? String(e)}\n`)
})
child.on('exit', (code, signal) => {
(code || signal ? rej(code) : res())
})
child.on('error', e => {
process.stdout.write(`${e.stack ?? String(e)}\n`)
})
child.on('exit', (code, signal) => {
code || signal ? rej(code) : res()
})
})
}

async function buildLib(name, dir) {
const { globals } = JSON.parse(await fs.readFile(`${dir}/package.json`))

process.stdout.write(`Bundling library "${name}" at ${dir}/index.js\n`)

const builder = createBundleBuilder({
basePath: `${dir}/`,
globals,
})
builder.inputFiles.set(`${dir}/index.js`, undefined)

builder.emitter.addListener('complete', e => {
const content = String(e.outputFiles.get('index.js')?.content)
fs.writeFile(`${dir}/index.bundle.js`, content)
.then(() => process.stdout.write([
'<<<',
`Wrote ${dir}/index.bundle.js`,
`[${content.length} bytes]`,
...((globals && Object.keys(globals).length)
? [
` Depending on:`,
...Object.entries(globals).map(([module, name]) => ` ${name} => ${module}`),
]
: []),
'>>>',
'',
].join('\n')))
})
const {globals} = JSON.parse(await fs.readFile(`${dir}/package.json`))

process.stdout.write(`Bundling library "${name}" at ${dir}/index.js\n`)

const builder = createBundleBuilder({
basePath: `${dir}/`,
globals,
})
builder.inputFiles.set(`${dir}/index.js`, undefined)

builder.emitter.addListener('complete', e => {
const content = String(e.outputFiles.get('index.js')?.content)
fs.writeFile(`${dir}/index.bundle.js`, content).then(() =>
process.stdout.write(
[
'<<<',
`Wrote ${dir}/index.bundle.js`,
`[${content.length} bytes]`,
...(globals && Object.keys(globals).length
? [
` Depending on:`,
...Object.entries(globals).map(
([module, name]) => ` ${name} => ${module}`,
),
]
: []),
'>>>',
'',
].join('\n'),
),
)
})

builder.build()
builder.build()
}

async function buildEnv(name, file) {
process.stdout.write(`Bundling environment "${name}" at ${file}\n`)

const builder = createBundleBuilder({
basePath: `${indexDirEnv}/`,
})
const basename = path.basename(file, '.js')
builder.inputFiles.set(file, undefined)

builder.emitter.addListener('complete', e => {
const content = String(e.outputFiles.get(`${basename}.js`)?.content)
fs.writeFile(`${indexDirEnv}/${basename}.bundle.js`, content)
.then(() => process.stdout.write([
'<<<',
`Wrote ${indexDirEnv}/${basename}.bundle.js`,
`[${content.length} bytes]`,
'>>>',
'',
].join('\n')))
})
process.stdout.write(`Bundling environment "${name}" at ${file}\n`)

const builder = createBundleBuilder({
basePath: `${indexDirEnv}/`,
})
const basename = path.basename(file, '.js')
builder.inputFiles.set(file, undefined)

builder.emitter.addListener('complete', e => {
const content = String(e.outputFiles.get(`${basename}.js`)?.content)
fs.writeFile(`${indexDirEnv}/${basename}.bundle.js`, content).then(() =>
process.stdout.write(
[
'<<<',
`Wrote ${indexDirEnv}/${basename}.bundle.js`,
`[${content.length} bytes]`,
'>>>',
'',
].join('\n'),
),
)
})

builder.build()
builder.build()
}

0 comments on commit 8779eae

Please sign in to comment.