Skip to content

Commit

Permalink
Update internal tools
Browse files Browse the repository at this point in the history
  • Loading branch information
mobily committed Dec 14, 2021
1 parent 2daeb36 commit 1da7374
Show file tree
Hide file tree
Showing 27 changed files with 1,043 additions and 533 deletions.
20 changes: 8 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,14 @@
"index.js.flow"
],
"scripts": {
"build": "bash ./scripts/build.sh",
"esbuild": "node esbuild.config.js",
"build": "yarn foy -c ./scripts/build.foyfile.ts",
"clean": "rm -rf ./dist && rm -f ./tsconfig.tsbuildinfo",
"test": "yarn jest --coverage",
"test:watch": "yarn jest --watch",
"test": "yarn foy -c ./scripts/test.foyfile.ts",
"re:clean": "yarn bsb -clean-world",
"re:build": "yarn bsb -make-world",
"re:dev": "yarn bsb -make-world -w",
"generate:flow": "node ./scripts/generate-flow.js",
"generate:docs": "bash ./scripts/generate-docs.sh",
"transform:types": "bash ./scripts/transform-types.sh",
"transform:javascript": "bash ./scripts/transform-javascript.sh",
"generate:contributors": "yarn all-contributors generate"
"transform": "yarn foy -c ./scripts/transform.foyfile.ts",
"generate": "yarn foy -c ./scripts/generate.foyfile.ts"
},
"engines": {
"node": ">= 10.*"
Expand All @@ -42,6 +37,7 @@
"license": "MIT",
"keywords": [
"typescript",
"flow",
"functional-programming",
"fp",
"monad",
Expand Down Expand Up @@ -69,16 +65,16 @@
"esbuild": "^0.14.0",
"flow-bin": "^0.165.1",
"flowgen": "^1.16.0",
"foy": "^0.2.15",
"gentype": "^4.3.0",
"globby": "^11.0.3",
"jest": "^27.3.1",
"jscodeshift": "^0.13.0",
"minimatch": "^3.0.4",
"prettier": "^2.5.0",
"ts-expect": "^1.3.0",
"ts-jest": "^27.0.7",
"typescript": "^4.5.2",
"wonka": "^4.0.15"
"ts-node": "^10.4.0",
"typescript": "^4.5.2"
},
"cacheDirectories": [
"node_modules"
Expand Down
68 changes: 68 additions & 0 deletions scripts/build.foyfile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { task, desc, option, strict } from 'foy'
import * as globby from 'globby'

type Options = {
readonly runTests: boolean
}

type DevOptions = {
readonly namespace: string
readonly test: string
}

desc('Build dist')
option('-t, --run-tests', 'run tests')
strict()
task<Options>('dist', async ctx => {
await ctx.exec([
'yarn clean',
'yarn re:clean',
'yarn re:build',
'yarn transform all',
'yarn generate docs',
'node esbuild.config.js',
])

const files = await globby('dist/**/index.js')
const js = files.join(' ')

await ctx.exec(
`node node_modules/.bin/jscodeshift --run-in-band -t tools/javascript-codemods/post/index.ts ${js}`,
)

await ctx.exec(['yarn generate tsc --esm --cjs', 'yarn generate flow'])

if (ctx.options.runTests) {
await ctx.exec('yarn test run -c')
}
})

desc('Build for development purposes')
option('-t, --test <name>', 'run tests for a single file')
option('-n, --namespace <name>', 'namespace')
strict()
task<DevOptions>('dev', async ctx => {
if (!ctx.options.namespace) {
throw new Error('-n is required')
}

await ctx.exec([
`yarn transform all -r -n ${ctx.options.namespace}`,
'node esbuild.config.js',
])

const files = await globby(`dist/**/${ctx.options.namespace}/index.js`)
const js = files.join(' ')

await ctx.exec(
`node node_modules/.bin/jscodeshift --run-in-band -t tools/javascript-codemods/post/index.ts ${js}`,
)

await ctx.exec('yarn generate tsc --cjs')

if (ctx.options.test) {
await ctx.exec(
`yarn test run -n ${ctx.options.namespace} -f ${ctx.options.test}`,
)
}
})
13 changes: 0 additions & 13 deletions scripts/build.sh

This file was deleted.

4 changes: 0 additions & 4 deletions scripts/generate-docs.sh

This file was deleted.

56 changes: 0 additions & 56 deletions scripts/generate-flow.js

This file was deleted.

81 changes: 81 additions & 0 deletions scripts/generate.foyfile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { task, desc, option, strict } from 'foy'
import { compiler, beautify } from 'flowgen'

import * as globby from 'globby'
import * as path from 'path'

type Options = {
readonly rebuild: boolean
}
type TSCOptions = {
readonly esm: boolean
readonly cjs: boolean
}

const defaultEncoding = { encoding: 'utf8' } as const

desc('Generate docs')
option('-r, --rebuild', 'rebuild rescript files')
strict()
task<Options>('docs', async ctx => {
const files = await globby('src/**/index.ts')
const ts = files.join(' ')

ctx.fs.mkdirp('docs/api/generated')

if (ctx.options.rebuild) {
await ctx.exec('yarn transform typescript -r')
}

await ctx.exec(
`node node_modules/.bin/jscodeshift --run-in-band --extensions=ts --parser=ts -t tools/generate-docs/index.ts ${ts}`,
)
})

desc('Generate Flow types')
task('flow', async ctx => {
const entryFile = await ctx.fs.readFile('index.js.flow', defaultEncoding)
const content = entryFile.replace(/.\/dist\//g, './')
const files = await globby('dist/esm/**/*.d.ts')

await ctx.fs.writeFile('dist/index.js.flow', content, defaultEncoding)
await ctx.fs.writeFile('dist/index.mjs.flow', content, defaultEncoding)

const defs = files.map(filename => {
const fullpath = path.resolve(__dirname, '..', filename)
const flowdef = beautify(compiler.compileDefinitionFile(fullpath))
const basename = path.basename(fullpath, '.d.ts')
const filepath = path.dirname(fullpath)
const definition = flowdef.replace(/import\s/g, 'import type ')

return ctx.fs.writeFile(
path.join(filepath, `${basename}.js.flow`),
['// @flow', definition].join('\n\n'),
defaultEncoding,
)
})

await Promise.all(defs)
})

desc('Generate contributors in README.md')
task('contributors', async ctx => {
await ctx.exec('yarn all-contributors generate')
})

desc('Generate tsc')
option('--esm', 'esm')
option('--cjs', 'cjs')
strict()
task<TSCOptions>('tsc', async ctx => {
if (!ctx.options.esm && !ctx.options.cjs) {
throw new Error('at least one param is required: --esm or --cjs')
}

await ctx.exec(
[
ctx.options.cjs && 'yarn tsc --outDir ./dist/cjs',
ctx.options.esm && 'yarn tsc --outDir ./dist/esm',
].filter(Boolean),
)
})
33 changes: 33 additions & 0 deletions scripts/test.foyfile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { task, desc, option, strict } from 'foy'
import * as globby from 'globby'

type Options = {
readonly namespace: string
readonly file: string
readonly coverage: boolean
}

desc('Run tests')
option('-n, --namespace <name>', 'namespace')
option('-f, --file <name>', 'file')
option('-c, --coverage', 'coverage')
strict()
task<Options>('run', async ctx => {
const coverage = ctx.options.coverage ? '--coverage' : ''

const cmd = ['yarn', 'jest', coverage]

if (ctx.options.namespace || ctx.options.file) {
const namespace = ctx.options.namespace || '**'
const file = ctx.options.file ? `${ctx.options.file}.test.ts` : '*.test.ts'

const files = await globby(`__tests__/${namespace}/${file}`)

cmd.push(...files)

await ctx.exec(cmd.join(' '))
return
}

await ctx.exec(cmd.join(' '))
})
6 changes: 0 additions & 6 deletions scripts/transform-javascript.sh

This file was deleted.

4 changes: 0 additions & 4 deletions scripts/transform-types.sh

This file was deleted.

Loading

0 comments on commit 1da7374

Please sign in to comment.