forked from date-fns/date-fns
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build script: FP and Indices conversion to TypeScript (date-fns#2880)
- Loading branch information
Showing
8 changed files
with
128 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')} | ||
` | ||
} |