Skip to content

Commit

Permalink
Merge pull request #157 from TNG/add-formatting-and-linting-for-types…
Browse files Browse the repository at this point in the history
…cript

Add formatting and linting for typescript
  • Loading branch information
Nuullll authored Jan 29, 2025
2 parents a662318 + 4435bc4 commit 5ff4298
Show file tree
Hide file tree
Showing 123 changed files with 14,062 additions and 11,128 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/eslint-prettier.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Lint TS
on: [push, pull_request]
jobs:
lint-ts:
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./WebUI
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- run: npm run lint:ci
- run: npm run format:ci
9 changes: 9 additions & 0 deletions WebUI/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[*.{js,jsx,mjs,cjs,ts,tsx,mts,cts,vue}]
charset = utf-8
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

end_of_line = lf
max_line_length = 100
6 changes: 6 additions & 0 deletions WebUI/.prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "https://json.schemastore.org/prettierrc",
"semi": false,
"singleQuote": true,
"printWidth": 100
}
18 changes: 10 additions & 8 deletions WebUI/build/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@

## Fetch remote dependencies for build

1. run ```npm install```
1. run `npm install`
2. aquire windows libuv dlls, e.g. via miniforge:
- Install miniforge: https://github.com/conda-forge/miniforge
- Create a reference conda environment with libuv installed
```
conda create -n cp311_libuv python=3.11 libuv -y
# copy the path to this conda env
conda env list | findstr cp311_libuv
```
3. run ```npm run fetch-build-resources -- --conda_env_dir=$PATH_TO_CONDA_ENV```
```
conda create -n cp311_libuv python=3.11 libuv -y
# copy the path to this conda env
conda env list | findstr cp311_libuv
```
3. run `npm run fetch-build-resources -- --conda_env_dir=$PATH_TO_CONDA_ENV`

## decide for offline or online installer

### online installer

run
run

```
npm run prepare-build
npm run build
Expand All @@ -28,6 +29,7 @@ npm run build
**FIXME: offline scripts are missing**

run

```
npm run prepare-build:${PLATFORM}-offline
npm run build:${PLATFORM}-offline
Expand Down
18 changes: 4 additions & 14 deletions WebUI/build/build-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
{
"from": "external/LlamaCPP",
"to": "LlamaCPP",
"filter": [
"!__pycache__/"
]
"filter": ["!__pycache__/"]
},
{
"from": "external/intel_extension_for_pytorch-2.3.110+xpu-cp311-cp311-win_amd64.whl",
Expand Down Expand Up @@ -77,27 +75,19 @@
"directories": {
"output": "../release"
},
"files": [
"dist",
"dist-electron"
],
"files": ["dist", "dist-electron"],
"win": {
"target": [
{
"target": "nsis",
"arch": [
"x64"
]
"arch": ["x64"]
}
],
"verifyUpdateCodeSignature": false,
"requestedExecutionLevel": "asInvoker",
"icon": "app.ico",
"artifactName": "${productName}-${version}.${ext}",
"electronLanguages": [
"en-US",
"zh-CN"
]
"electronLanguages": ["en-US", "zh-CN"]
},
"nsis": {
"license": "build/license.rtf",
Expand Down
96 changes: 48 additions & 48 deletions WebUI/build/scripts/check-i18n.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,59 @@
// Usage: node check-i18n.js --i18n-dir=$DIR

const fs = require('fs');
const path = require('path');
const argv = require('minimist')(process.argv.slice(2));
const fs = require('fs')
const path = require('path')
const argv = require('minimist')(process.argv.slice(2))

const i18nDirArg = argv.i18n_dir;
const i18nDirArg = argv.i18n_dir
if (!i18nDirArg) {
console.error('Usage: node check-i18n.js --i18n-dir=$DIR\n');
process.exit(1);
console.error('Usage: node check-i18n.js --i18n-dir=$DIR\n')
process.exit(1)
}

const i18nDir = path.resolve(i18nDirArg);
const enData = JSON.parse(fs.readFileSync(path.join(i18nDir, 'en-US.json')));
const keys = Object.keys(enData);
const i18nDir = path.resolve(i18nDirArg)
const enData = JSON.parse(fs.readFileSync(path.join(i18nDir, 'en-US.json')))
const keys = Object.keys(enData)

const langs = fs.readdirSync(i18nDir).filter(f => f.endsWith('.json'));
const missingKeys = {};
const unusedKeys = {};
const langs = fs.readdirSync(i18nDir).filter((f) => f.endsWith('.json'))
const missingKeys = {}
const unusedKeys = {}

langs.forEach(lang => {
const data = JSON.parse(fs.readFileSync(path.join(i18nDir, lang)));
keys.forEach(key => {
if (!data[key]) {
if (!missingKeys[lang]) {
missingKeys[lang] = [];
}
missingKeys[lang].push(key);
}
});

Object.keys(data).forEach(key => {
if (!keys.includes(key)) {
if (!unusedKeys[lang]) {
unusedKeys[lang] = [];
}
unusedKeys[lang].push(key);
}
});
langs.forEach((lang) => {
const data = JSON.parse(fs.readFileSync(path.join(i18nDir, lang)))
keys.forEach((key) => {
if (!data[key]) {
if (!missingKeys[lang]) {
missingKeys[lang] = []
}
missingKeys[lang].push(key)
}
})

if (missingKeys[lang]) {
// print pretty
// 5 entries per row
const entriesPerRow = 5;
const entries = missingKeys[lang];
const rows = [];
for (let i = 0; i < entries.length; i += entriesPerRow) {
rows.push(entries.slice(i, i + entriesPerRow).join(', '));
}
console.log(`${missingKeys[lang].length} missing keys for ${lang}:`);
console.log(` ${rows.join('\n ')}`);
console.log();
Object.keys(data).forEach((key) => {
if (!keys.includes(key)) {
if (!unusedKeys[lang]) {
unusedKeys[lang] = []
}
unusedKeys[lang].push(key)
}
if (unusedKeys[lang]) {
console.log(`${unusedKeys[lang].length} unused keys for ${lang}:`);
console.log(` ${unusedKeys[lang].join(', ')}`);
console.log();
})

if (missingKeys[lang]) {
// print pretty
// 5 entries per row
const entriesPerRow = 5
const entries = missingKeys[lang]
const rows = []
for (let i = 0; i < entries.length; i += entriesPerRow) {
rows.push(entries.slice(i, i + entriesPerRow).join(', '))
}
});
console.log(`${missingKeys[lang].length} missing keys for ${lang}:`)
console.log(` ${rows.join('\n ')}`)
console.log()
}
if (unusedKeys[lang]) {
console.log(`${unusedKeys[lang].length} unused keys for ${lang}:`)
console.log(` ${unusedKeys[lang].join(', ')}`)
console.log()
}
})
124 changes: 65 additions & 59 deletions WebUI/build/scripts/fetch-python-package-resources.js
Original file line number Diff line number Diff line change
@@ -1,93 +1,99 @@
// Usage: node fetch-python-package-resources.js --target-dir=$DIR --conda_env_dir=$DIR
const https = require('https');
const fs = require('fs');
const path = require('path');
const { HttpsProxyAgent } = require('https-proxy-agent');
const https = require('https')
const fs = require('fs')
const path = require('path')
const { HttpsProxyAgent } = require('https-proxy-agent')


const argv = require('minimist')(process.argv.slice(2));
const argv = require('minimist')(process.argv.slice(2))
const targetDirArg = argv.target_dir
const condaEnvDirArg = argv.conda_env_dir

if (!targetDirArg || !condaEnvDirArg) {
console.error('Usage: node fetch-python-package-resources.js --target_dir=$DIR --conda_env_dir=$DIR\n');
process.exit(1);
console.error(
'Usage: node fetch-python-package-resources.js --target_dir=$DIR --conda_env_dir=$DIR\n',
)
process.exit(1)
}

const targetDir = path.resolve(targetDirArg);
const targetDir = path.resolve(targetDirArg)
const condaTargetDir = path.join(targetDir, 'conda-env-lib')
const condaEnvLibraryDir = path.resolve(path.join(condaEnvDirArg, 'Library'));
const condaEnvLibraryDir = path.resolve(path.join(condaEnvDirArg, 'Library'))

const embeddablePythonUrl = 'https://raw.githubusercontent.com/adang1345/PythonWindows/master/3.11.10/python-3.11.10-embed-amd64.zip';
const embeddablePythonUrl =
'https://raw.githubusercontent.com/adang1345/PythonWindows/master/3.11.10/python-3.11.10-embed-amd64.zip'
const getPipScriptUrl = 'https://bootstrap.pypa.io/get-pip.py'
const sevenZrExeUrl = 'https://www.7-zip.org/a/7zr.exe'

function fetchFileIfNotPresent(url) {
const expectedFilePath = path.join(targetDir, getBaseFileName(url))
if (fs.existsSync(expectedFilePath)) {
console.log(`omitting fetching of ${url} as ${expectedFilePath} already exists`)
} else {
fetchFile(url)
}
const expectedFilePath = path.join(targetDir, getBaseFileName(url))
if (fs.existsSync(expectedFilePath)) {
console.log(`omitting fetching of ${url} as ${expectedFilePath} already exists`)
} else {
fetchFile(url)
}
}

function fetchFile(url) {
const proxy = process.env.HTTPS_PROXY || process.env.https_proxy || process.env.HTTP_PROXY || process.env.http_proxy;
const options = proxy ? { agent: new HttpsProxyAgent(proxy) } : {};
https.get(url, options, (response) => {
const filePath = path.join(targetDir, getBaseFileName(url))
const file = fs.createWriteStream(filePath);
response.pipe(file);
const proxy =
process.env.HTTPS_PROXY ||
process.env.https_proxy ||
process.env.HTTP_PROXY ||
process.env.http_proxy
const options = proxy ? { agent: new HttpsProxyAgent(proxy) } : {}
https
.get(url, options, (response) => {
const filePath = path.join(targetDir, getBaseFileName(url))
const file = fs.createWriteStream(filePath)
response.pipe(file)

file.on('finish', () => {
file.close();
console.log(`Downloaded ${filePath} successfully!`);
});
}).on('error', (err) => {
console.error(`Error downloading ${embeddablePythonUrl}: ${err}`);
});
file.on('finish', () => {
file.close()
console.log(`Downloaded ${filePath} successfully!`)
})
})
.on('error', (err) => {
console.error(`Error downloading ${embeddablePythonUrl}: ${err}`)
})
}


function getBaseFileName(url) {
const urlPathSegments = url.split('/');
const baseFileName = urlPathSegments[urlPathSegments.length - 1]
return baseFileName;
const urlPathSegments = url.split('/')
const baseFileName = urlPathSegments[urlPathSegments.length - 1]
return baseFileName
}

function prepareTargetPath() {
if (!fs.existsSync(condaTargetDir)) {
fs.mkdirSync(targetDir, { recursive: true });
}
if (!fs.existsSync(condaTargetDir)) {
fs.mkdirSync(targetDir, { recursive: true })
}
}

function copyLibuvDllsIfNotPresent() {
if (fs.existsSync(path.join(condaTargetDir, 'Library', 'bin', 'uv.dll'))) {
console.log(`omitting fetching copying of libuv DLLs, as they already exist`)
} else {
if (!path.join(condaEnvLibraryDir, 'bin', 'uv.dll')) {
console.log(`provided conda env at ${condaEnvLibraryDir} is missing uv.dll. Aborting`)
process.exit(1);
}
fs.cp(condaEnvLibraryDir, path.join(condaTargetDir, 'Library'), { recursive: true }, (err) => {
if (err) {
console.error(err);
console.log('Failed to copy directory');
process.exit(1)
} else {
console.log('Directory copied successfully');
}
});
if (fs.existsSync(path.join(condaTargetDir, 'Library', 'bin', 'uv.dll'))) {
console.log(`omitting fetching copying of libuv DLLs, as they already exist`)
} else {
if (!path.join(condaEnvLibraryDir, 'bin', 'uv.dll')) {
console.log(`provided conda env at ${condaEnvLibraryDir} is missing uv.dll. Aborting`)
process.exit(1)
}
fs.cp(condaEnvLibraryDir, path.join(condaTargetDir, 'Library'), { recursive: true }, (err) => {
if (err) {
console.error(err)
console.log('Failed to copy directory')
process.exit(1)
} else {
console.log('Directory copied successfully')
}
})
}
}


function main() {
prepareTargetPath()
fetchFileIfNotPresent(embeddablePythonUrl)
fetchFileIfNotPresent(getPipScriptUrl)
fetchFileIfNotPresent(sevenZrExeUrl)
copyLibuvDllsIfNotPresent()
prepareTargetPath()
fetchFileIfNotPresent(embeddablePythonUrl)
fetchFileIfNotPresent(getPipScriptUrl)
fetchFileIfNotPresent(sevenZrExeUrl)
copyLibuvDllsIfNotPresent()
}

main()
Loading

0 comments on commit 5ff4298

Please sign in to comment.