Skip to content

Commit

Permalink
build script: FP and Indices conversion to TypeScript (date-fns#2880)
Browse files Browse the repository at this point in the history
  • Loading branch information
fturmel authored Jan 7, 2022
1 parent 9730ce8 commit faac81f
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 147 deletions.
12 changes: 3 additions & 9 deletions scripts/_lib/listFPFns.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,13 @@ const fs = require('fs')

module.exports = listFPFns

const ignoredFiles = [
'index.js',
'index.ts',
'test.js',
'test.ts',
'index.js.flow',
'package.json',
]
const ignorePattern = /^_|\./ // can't start with `_` or have a `.` in it

function listFPFns() {
const files = fs.readdirSync(path.join(process.cwd(), 'src', 'fp'))

return files
.filter((file) => /^[^._]/.test(file) && !ignoredFiles.includes(file))
.filter((file) => !ignorePattern.test(file))
.map((file) => ({
name: file,
path: `./${file}`,
Expand Down
20 changes: 6 additions & 14 deletions scripts/_lib/listFns.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,21 @@ const readDir = promisify(fs.readdir)

module.exports = listFns

const ignoredFiles = [
'locale',
'esm',
'fp',
'constants',
'index.js',
'index.ts',
'test.js',
'test.ts',
'index.js.flow',
'package.json',
'types.ts',
]
const ignorePattern = /^_|\./ // can't start with `_` or have a `.` in it
const ignoredDirs = ['locale', 'esm', 'fp', 'constants']

async function listFns() {
const srcPath = path.join(process.cwd(), 'src')
const files = await readDir(srcPath)

return Promise.all(
files
.filter((file) => /^[^._]/.test(file) && !ignoredFiles.includes(file))
.filter(
(file) => !ignorePattern.test(file) && !ignoredDirs.includes(file)
)
.map(async (file) => {
const isTs = await exists(path.join(srcPath, file, 'index.ts'))

return {
name: file,
path: `./${file}`,
Expand Down
15 changes: 4 additions & 11 deletions scripts/_lib/listLocales.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,20 @@ const { promisify } = require('util')
const exists = promisify(fs.exists)
const readDir = promisify(fs.readdir)

const ignoredFiles = [
'index.js',
'index.ts',
'test.js',
'test.ts',
'index.js.flow',
'package.json',
'types.ts',
]

module.exports = listLocales

const ignorePattern = /^_|\./ // can't start with `_` or have a `.` in it

async function listLocales() {
const localesPath = path.resolve(process.cwd(), 'src/locale')
const locales = await readDir(localesPath)

return Promise.all(
locales
.filter((file) => /^[^._]/.test(file) && !ignoredFiles.includes(file))
.filter((file) => !ignorePattern.test(file))
.map(async (locale) => {
const isTs = await exists(path.join(localesPath, locale, 'index.ts'))

return {
name: locale.replace(/-/g, ''),
code: locale,
Expand Down
4 changes: 2 additions & 2 deletions scripts/build/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
set -ex

./scripts/build/docs.js
./scripts/build/fp.js
./scripts/build/fp.ts
./scripts/build/typings.js
./scripts/build/indices.js
./scripts/build/indices.ts
49 changes: 0 additions & 49 deletions scripts/build/fp.js

This file was deleted.

59 changes: 59 additions & 0 deletions scripts/build/fp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/env ts-node

/**
* @file
* The script generates the FP functions using the docs JSON file.
*
* It's a part of the build process.
*/

import { existsSync } from 'fs'
import { mkdir, readFile, unlink, writeFile } from 'fs/promises'

interface DocFn {
kind: string
isFPFn: boolean

title: string
generatedFrom: string
args: {
length: number
}
}

;(async () => {
const jsDocs = JSON.parse((await readFile('./tmp/docs.json')).toString())

const fpFns: DocFn[] = Object.keys(jsDocs)
.map((category) => jsDocs[category])
.reduce((previousValue, newValue) => [...previousValue, ...newValue], [])
.filter((doc: DocFn) => doc.kind === 'function' && doc.isFPFn)

fpFns.forEach(buildFPFn)
})()

function getFPFn(initialFnName: string, arity: number): string {
return `// This file is generated automatically by \`scripts/build/fp.ts\`. Please, don't change it.
import fn from '../../${initialFnName}/index'
import convertToFP from '../_lib/convertToFP/index'
export default convertToFP(fn, ${arity})
`
}

async function buildFPFn({
title,
generatedFrom,
args: { length },
}: DocFn): Promise<void> {
const source = getFPFn(generatedFrom, length)
const dir = `./src/fp/${title}`

if (!existsSync(dir)) await mkdir(dir)
writeFile(`${dir}/index.ts`, source)

// remove legacy index.js (if any)
const jsPath = `${dir}/index.js`
if (existsSync(jsPath)) unlink(jsPath)
}
62 changes: 0 additions & 62 deletions scripts/build/indices.js

This file was deleted.

54 changes: 54 additions & 0 deletions scripts/build/indices.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env ts-node

/**
* @file
* The script generates index files for submodules.
*
* It's a part of the build process.
*/

import { readFile, writeFile } from 'fs/promises'
import listFns from '../_lib/listFns'
import listFPFns from '../_lib/listFPFns'
import listLocales from '../_lib/listLocales'

interface File {
name: string
path: string
fullPath: string
}

;(async () => {
const outdatedLocales: string[] = JSON.parse(
(await readFile('./outdatedLocales.json')).toString()
)

const locales = (await listLocales()).filter(
({ code }) => !outdatedLocales.includes(code)
)

const fns = await listFns()
const fpFns = listFPFns()

writeFile('src/index.ts', generateIndex(fns, false, true))
writeFile('src/fp/index.ts', generateIndex(fpFns, true, true))
writeFile('src/locale/index.ts', generateIndex(locales, false, false))
})()

function generateIndex(
files: File[],
isFP: boolean,
includeConstants: boolean
): string {
const lines = files.map(
(file) => `export { default as ${file.name} } from '${file.path}/index'`
)

if (includeConstants)
lines.push(`export * from '${isFP ? '..' : '.'}/constants/index'`)

return `// This file is generated automatically by \`scripts/build/indices.ts\`. Please, don't change it.
${lines.join('\n')}
`
}

0 comments on commit faac81f

Please sign in to comment.