Skip to content

Commit

Permalink
refactor build scripts to use ts-node
Browse files Browse the repository at this point in the history
  • Loading branch information
eagleoflqj committed Aug 4, 2023
1 parent c2a6abe commit 341920b
Show file tree
Hide file tree
Showing 13 changed files with 69 additions and 56 deletions.
File renamed without changes.
18 changes: 10 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"files": [
"dist"
],
"type": "module",
"appName": "My RIME",
"homepage": "https://github.com/LibreService/my_rime",
"author": {
Expand All @@ -14,18 +15,18 @@
"license": "AGPL-3.0-or-later",
"scripts": {
"submodule": "bash scripts/update_submodule.sh",
"worker": "rollup -c rollup.worker-config.mjs",
"font": "node scripts/download_font.mjs",
"worker": "rollup -c rollup.worker-config.js",
"font": "node --loader ts-node/esm scripts/download_font.ts",
"native": "bash scripts/build_native.sh",
"schema": "node scripts/install_schemas.mjs",
"schema": "node --loader ts-node/esm scripts/install_schemas.ts",
"lib": "bash scripts/build_lib.sh",
"wasm": "node scripts/build_wasm.mjs",
"checksum": "node scripts/checksum.mjs",
"wasm": "node --loader ts-node/esm scripts/build_wasm.ts",
"checksum": "node --loader ts-node/esm scripts/checksum.ts",
"dev": "vite --host",
"lint": "eslint --ext .ts,.js,.mjs,.vue scripts src test && clang-format -Werror -dry-run wasm/api.cpp",
"lint:fix": "eslint --ext .ts,.js,.mjs,.vue --fix scripts src test && clang-format -i wasm/api.cpp",
"lint": "eslint --ext .ts,.js,.vue scripts src test && clang-format -Werror -dry-run wasm/api.cpp",
"lint:fix": "eslint --ext .ts,.js,.vue --fix scripts src test && clang-format -i wasm/api.cpp",
"check": "vue-tsc --noEmit",
"build": "pnpm run check && rm -rf public/*.js.map && node scripts/clean_ime.mjs && NODE_ENV=production pnpm run worker && vite build && node scripts/clean_font.mjs",
"build": "pnpm run check && rm -rf public/*.js.map && node --loader ts-node/esm scripts/clean_ime.ts && NODE_ENV=production pnpm run worker && vite build && node --loader ts-node/esm scripts/clean_font.ts",
"test": "playwright test --browser all --fully-parallel test/ && playwright test --fully-parallel test-device/",
"test:chromium": "playwright test --browser chromium --fully-parallel test/",
"test:firefox": "playwright test --browser firefox --fully-parallel test/",
Expand Down Expand Up @@ -67,6 +68,7 @@
"rollup-plugin-esbuild": "^5.0.0",
"rppi": "^0.1.0",
"textarea-caret": "^3.1.0",
"ts-node": "^10.9.1",
"tslib": "^2.6.1",
"typescript": "^5.1.6",
"vite": "^4.4.7",
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion scripts/build_wasm.mjs → scripts/build_wasm.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { spawnSync } from 'child_process'
import { exit } from 'process'
import { existsSync } from 'fs'
import { ensure } from './util.mjs'
import { ensure } from './util.js'

const OPENCC_TARGET = '/usr/share/opencc'
const LIB_PATH = 'build/sysroot/usr/lib'
Expand Down
4 changes: 2 additions & 2 deletions scripts/checksum.mjs → scripts/checksum.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { readFileSync } from 'fs'
import { exit } from 'process'
import { md5sum } from './util.mjs'
import { md5sum } from './util.js'

const content = readFileSync('checksum', { encoding: 'utf-8' })
const iter = content.matchAll(/(\S+)\s+(\S+)/g)

let match
let match: ReturnType<typeof iter.next>
while (match = iter.next(), match.value) { // eslint-disable-line no-sequences
const expected = match.value[1]
const path = match.value[2]
Expand Down
9 changes: 0 additions & 9 deletions scripts/clean_font.mjs

This file was deleted.

11 changes: 11 additions & 0 deletions scripts/clean_font.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { rmSync } from 'fs'
import { rf } from './util.js'
import fonts from '../fonts.json' assert { type: 'json' }

if (process.env.LIBRESERVICE_CDN) {
for (const { files } of fonts) {
for (const file of files) {
rmSync(`dist/${file}`, rf)
}
}
}
10 changes: 5 additions & 5 deletions scripts/clean_ime.mjs → scripts/clean_ime.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { rmSync, readFileSync, readdirSync } from 'fs'
import { rmSync, readdirSync } from 'fs'
import { exit } from 'process'
import { rf } from './util.mjs'
import { rf } from './util.js'
import targetFiles from '../target-files.json' assert { type: 'json' }

const imeDir = 'public/ime'

Expand All @@ -9,11 +10,11 @@ if (process.env.LIBRESERVICE_CDN) {
exit(0)
}

function keepPackage (dir, patterns) {
function keepPackage (dir: string, patterns: string[]) {
for (const name of readdirSync(dir)) {
const subDir = `${dir}/${name}`
let keep = false
const subPatterns = []
const subPatterns: string[] = []
for (const pattern of patterns) {
const i = pattern.indexOf('/')
const base = i >= 0 ? pattern.substring(0, i) : pattern
Expand All @@ -32,5 +33,4 @@ function keepPackage (dir, patterns) {
}
}

const targetFiles = JSON.parse(readFileSync('target-files.json'))
keepPackage(imeDir, Object.keys(targetFiles))
File renamed without changes.
51 changes: 26 additions & 25 deletions scripts/install_schemas.mjs → scripts/install_schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,30 @@ import { readFileSync, writeFileSync, mkdirSync, copyFileSync, readdirSync, cpSy
import { cwd, chdir, exit } from 'process'
import yaml from 'js-yaml'
import { Recipe } from '@libreservice/micro-plum'
import { rf, utf8, ensure, md5sum } from './util.mjs'
import { rf, utf8, ensure, md5sum } from './util.js'
import packageJson from '../package.json' assert { type: 'json' }
import schemas from '../schemas.json' assert { type: 'json' }

const root = cwd()
const { version } = JSON.parse(readFileSync('package.json'))
const { version } = packageJson
const RIME_DIR = 'build/librime_native/bin'
const defaultPath = `${RIME_DIR}/default.yaml`

// input file
const schemas = JSON.parse(readFileSync('schemas.json'))

// output files
const schemaName = {} // maps schema to names
const schemaFiles = {} // maps schema to dict and prism
const schemaTarget = {} // maps schema to target
const targetFiles = {} // maps target to files with hash
const targetVersion = {} // maps target to npm package version
const dependencyMap = {} // maps schema to dependent schemas
const schemaName: { [key: string]: string } = {} // maps schema to names
const schemaFiles: { [key: string]: { dict?: string, prism?: string } } = {} // maps schema to dict and prism
const schemaTarget: { [key: string]: string } = {} // maps schema to target
const targetFiles: { [key: string]: { name: string, md5: string }[] } = {} // maps target to files with hash
const targetVersion: { [key: string]: string } = {} // maps target to npm package version
const dependencyMap: { [key: string]: string[] } = {} // maps schema to dependent schemas

// temp data structures
const targetManifest = {} // maps target to files downloaded from it
const targetLicense = {}
const targetManifest: { [key: string]: string[] } = {} // maps target to files downloaded from it
const targetLicense: { [key: string]: string } = {}
const ids = []
const disabledIds = []
const disabledIds: string[] = []

async function install (recipe, target) {
async function install (recipe: Recipe, target?: string) {
const manifest = await recipe.load()
for (const { file, content } of manifest) {
if (content) {
Expand All @@ -42,11 +41,11 @@ async function install (recipe, target) {
}
}

function parseYaml (schemaId) {
const content = yaml.load(readFileSync(`${RIME_DIR}/build/${schemaId}.schema.yaml`, utf8))
function parseYaml (schemaId: string) {
const content = yaml.load(readFileSync(`${RIME_DIR}/build/${schemaId}.schema.yaml`, utf8)) as { [key: string]: any }
for (const [key, value] of Object.entries(content)) {
if (key === 'translator') {
const { dictionary, prism } = value
const { dictionary, prism } = value as { dictionary: string, prism?: string }
schemaFiles[schemaId] = {}
// By default, dictionary equals to schemaId, and prism equals to dictionary (not schemaId, see luna_pinyin_fluency)
if (dictionary !== schemaId) {
Expand All @@ -59,19 +58,19 @@ function parseYaml (schemaId) {
}
}

function getPackageDir (target) {
function getPackageDir (target: string) {
return `public/ime/${target}`
}

function readJson (path, defaultValue) {
function readJson (path: string, defaultValue: any) {
try {
return JSON.parse(readFileSync(path, 'utf-8'))
} catch (e) {
return defaultValue
}
}

function bumpVersion (oldVersion) {
function bumpVersion (oldVersion: string | undefined) {
if (!oldVersion) {
return version
}
Expand All @@ -96,19 +95,20 @@ const emojiJson = `${RIME_DIR}/opencc/emoji.json`
const emojiCategory = `${RIME_DIR}/opencc/emoji_category.txt`
const emojiContent = JSON.parse(readFileSync(emojiJson, utf8))
const emojiDict = emojiContent.conversion_chain[0].dict
emojiDict.dicts = emojiDict.dicts.filter(({ file }) => file !== 'emoji_category.txt')
emojiDict.dicts = emojiDict.dicts.filter(({ file }: { file: string }) => file !== 'emoji_category.txt')
writeFileSync(emojiJson, JSON.stringify(emojiContent))
rmSync(emojiCategory, rf)

for (const schema of schemas) {
const recipe = new Recipe(schema.target, { schemaIds: [schema.id] })
const target = recipe.repo.match(/(rime\/rime-)?(.*)/)[2]
const target = recipe.repo.match(/(rime\/rime-)?(.*)/)![2]
if (!(target in targetManifest)) {
targetManifest[target] = []
targetFiles[target] = []
targetLicense[target] = schema.license
}
ids.push(schema.id)
// @ts-ignore
if (schema.disabled) {
disabledIds.push(schema.id)
} else {
Expand All @@ -119,6 +119,7 @@ for (const schema of schemas) {
dependencyMap[schema.id] = schema.dependencies
}
if (schema.family) {
// @ts-ignore
for (const { id, name, disabled } of schema.family) {
recipe.schemaIds.push(id)
ids.push(id)
Expand Down Expand Up @@ -203,13 +204,13 @@ for (const [target, manifest] of Object.entries(targetManifest)) {
}
}

const oldTargetFiles = readJson('target-files.json', {})
const oldTargetFiles = readJson('target-files.json', {}) as typeof targetFiles

const updatedTargets = []
for (const [target, files] of Object.entries(targetFiles)) {
const packageDir = getPackageDir(target)
const packageJsonPath = `${packageDir}/package.json`
const { version: oldVersion } = readJson(packageJsonPath, {})
const { version: oldVersion } = readJson(packageJsonPath, {}) as { version?: string }
let newVersion = oldVersion || version
if (JSON.stringify(files) !== JSON.stringify(oldTargetFiles[target])) {
updatedTargets.push(target)
Expand Down
8 changes: 4 additions & 4 deletions scripts/util.mjs → scripts/util.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { spawnSync } from 'child_process'

const utf8 = { encoding: 'utf-8' }
const utf8: { encoding: 'utf-8' } = { encoding: 'utf-8' }
const rf = { recursive: true, force: true }

function ensure (result) {
function ensure (result: ReturnType<typeof spawnSync>) {
if (result.status !== 0) {
throw new Error('Command fails.')
}
return result
}

function md5sum (path) {
return ensure(spawnSync('md5sum', [path], utf8)).stdout.slice(0, 32)
function md5sum (path: string) {
return ensure(spawnSync('md5sum', [path], utf8)).stdout.slice(0, 32) as string
}

export {
Expand Down
2 changes: 1 addition & 1 deletion test/test-font.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
init,
callOnDownload
} from './util'
import fonts from '../fonts.json'
import fonts from '../fonts.json' assert { type: 'json' }

async function expectFirstFont (page: Page, fontFamily: string) {
while (!await page.evaluate(() => document.fonts.keys().next().value));
Expand Down
10 changes: 9 additions & 1 deletion tsconfig.node.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@
"composite": true,
"module": "ESNext",
"moduleResolution": "Node",
"target": "ESNext",
"resolveJsonModule": true,
"allowSyntheticDefaultImports": true
},
"include": ["vite.config.ts", "package.json"]
"include": [
"vite.config.ts",
"scripts/**/*.ts",
"fonts.json",
"schemas.json",
"target-files.json",
"package.json"
]
}

0 comments on commit 341920b

Please sign in to comment.