-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor build process from Python to JavaScript (#78)
- Replace Python build script with JavaScript - The `選項` argument of `from字頭` now accepts both objects and arrays. (Allowing specifying objects is purely for backward compatibility.) Results are outputted as both array elements and named properties. Closes #77. - Check coverage during building and testing (Fixes #71) - Separate type definitions to the dedicated file such that they are recognised when called in a TypeScript file - Move generated artifacts to a `dist` directory and make the npm package include only this directory --- * Refactor: Replace Python build script with JS - Check coverage during building & testing - Generate & Bundle type definition file; Move JSDoc to there - Update workflow * Move artifacts into `dist` directory * Only include `dist` directory in the npm package * Fix incorrect type definition * Exclude `dist` directory from being linted - `index.js` artifact is now located in `dist`. - Files to ignore are now specified by CLI flag instead of inside config to get rid of `Unused eslint-disable directive` warning in `index.js`. * Fix: `dist` directory isn’t created automatically * Fix indentation * Fix ignore pattern --------- Co-authored-by: Sêkai Zhou <[email protected]>
- Loading branch information
1 parent
cdd9e9d
commit 4b4441d
Showing
8 changed files
with
129 additions
and
87 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
node_modules | ||
/index.js | ||
dist |
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,90 @@ | ||
import { appendFileSync, existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from 'node:fs'; | ||
|
||
mkdirSync('./dist', { recursive: true }); | ||
|
||
writeFileSync('./dist/index.js', /* js */ `\ | ||
import TshetUinh from 'tshet-uinh'; | ||
import { 推導方案 } from 'tshet-uinh-deriver-tools'; | ||
export function from字頭(schema, 字頭, 選項) { | ||
return TshetUinh.資料.query字頭(字頭).map(條目 => { | ||
if (Array.isArray(schema)) (條目.推導結果 = schema.map((schema, i) => _schemata[schema](Array.isArray(選項) && 選項[i] || 選項?.[schema])(條目.音韻地位, 條目.字頭))).forEach((推導結果, i) => 條目.推導結果[schema[i]] = 推導結果); | ||
else 條目.推導結果 = _schemata[schema](選項)(條目.音韻地位, 條目.字頭); | ||
return 條目; | ||
}); | ||
} | ||
`); | ||
|
||
writeFileSync('./dist/index.d.ts', /* ts */`\ | ||
import TshetUinh from 'tshet-uinh'; | ||
import { 推導方案 } from 'tshet-uinh-deriver-tools'; | ||
type 推導選項 = Readonly<Record<string, unknown>> | undefined; | ||
interface 字頭檢索及推導結果<T extends Schema | readonly Schema[]> extends TshetUinh.資料.檢索結果 { | ||
推導結果: T extends readonly Schema[] ? { -readonly [K in keyof T]: string } & { -readonly [K in T[number]]: string } : string; | ||
} | ||
/** | ||
* 查詢字頭的擬音。 | ||
* @param schema - 推導方案或推導方案陣列 | ||
* @param 字頭 - 要查詢的字頭 | ||
* @param 選項 - 選項(可選) | ||
* - 若 \`schema\` 為字串(單個方案),則該引數為其選項 | ||
* - 若 \`schema\` 為字串列表(多個方案),則該引數亦為列表,元素為相應方案的選項 | ||
* @return 由字頭檢索到的各條目及相應推導結果 | ||
* - 若 \`schema\` 為字串,傳回結果中的 \`推導結果\` 屬性為字串 | ||
* - 若 \`schema\` 為字串列表,傳回結果中的 \`推導結果\` 屬性亦為字串列表 | ||
*/ | ||
export function from字頭<const T extends Schema | readonly Schema[]>( | ||
schema: T, | ||
字頭: string, | ||
選項?: T extends readonly Schema[] ? { readonly [K in keyof T]?: 推導選項 } | { readonly [K in T[number]]?: 推導選項 } : 推導選項, | ||
): 字頭檢索及推導結果<T>[]; | ||
`); | ||
|
||
const directoryFiles = new Set(readdirSync('.').filter(file => file.endsWith('.js') && !file.endsWith('.config.js'))); | ||
const nonExistentFiles = new Set(); | ||
|
||
const readmeContent = readFileSync('README.md', 'utf-8'); | ||
const files = readmeContent.matchAll(/`(([a-z0-9_]+)\.js)`/g); | ||
const schemata = []; | ||
|
||
for (const [, file, schema] of files) { | ||
if (!existsSync(file)) { | ||
nonExistentFiles.add(file); | ||
continue; | ||
} | ||
|
||
const content = readFileSync(file, 'utf-8'); | ||
const [, description, code] = content.match(/^\/\*(.+?)\*\/(.+)$/s); | ||
|
||
appendFileSync('./dist/index.js', /* js */ ` | ||
export const ${schema} = new 推導方案(function (選項, 音韻地位, 字頭) { | ||
${code.trim()} | ||
}); | ||
`); | ||
|
||
appendFileSync('./dist/index.d.ts', /* ts */ ` | ||
/** | ||
* ${description.trim()} | ||
*/ | ||
export const ${schema}: 推導方案<string>; | ||
`); | ||
|
||
schemata.push(schema); | ||
directoryFiles.delete(file); | ||
} | ||
|
||
appendFileSync('./dist/index.js', /* js */ ` | ||
const _schemata = { ${schemata.join(', ')} }; | ||
`); | ||
|
||
appendFileSync('./dist/index.d.ts', /* ts */ ` | ||
type Schema = '${schemata.join("' | '")}'; | ||
`); | ||
|
||
if (directoryFiles.size || nonExistentFiles.size) { | ||
if (directoryFiles.size) console.error('The following files are missing from README.md:', directoryFiles); | ||
if (nonExistentFiles.size) console.error('The following files are listed in README.md but do not exist:', nonExistentFiles); | ||
process.exit(1); | ||
} |
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