From 0d5d0f0102d2b0324c1962c2db1cf46f5bfb0229 Mon Sep 17 00:00:00 2001 From: Izuco Date: Tue, 25 Jan 2022 21:04:57 +0100 Subject: [PATCH] Build application --- .github/workflows/release-matrix.yml | 4 +- .gitignore | 3 +- gui/electron/src/index.ts | 14 +++- gui/react/src/App.tsx | 17 ++-- package.json | 6 +- tsc.ts | 111 +++++++++++++++++---------- tsconfig.json | 3 +- 7 files changed, 103 insertions(+), 55 deletions(-) diff --git a/.github/workflows/release-matrix.yml b/.github/workflows/release-matrix.yml index 5813fcb..18701b6 100644 --- a/.github/workflows/release-matrix.yml +++ b/.github/workflows/release-matrix.yml @@ -24,7 +24,9 @@ jobs: check-latest: true - name: Install Node modules - run: npm install + run: | + npm install + cd gui/react && npm install - name: Get name and version from package.json run: | diff --git a/.gitignore b/.gitignore index 8d3e9e5..f6bffc4 100644 --- a/.gitignore +++ b/.gitignore @@ -25,4 +25,5 @@ archive.json fonts .webpack/ out/ -dist/ \ No newline at end of file +dist/ +build/ \ No newline at end of file diff --git a/gui/electron/src/index.ts b/gui/electron/src/index.ts index f2f852a..4e86ac9 100644 --- a/gui/electron/src/index.ts +++ b/gui/electron/src/index.ts @@ -1,11 +1,15 @@ import { app, BrowserWindow } from 'electron'; +import path from 'path/posix'; import json from '../../../package.json'; import registerMessageHandler from './messageHandler'; +import fs from "fs"; if (require('electron-squirrel-startup')) { app.quit(); } +console.log(process.argv, process.env); + const createWindow = (): void => { registerMessageHandler(); // Create the browser window. @@ -15,11 +19,17 @@ const createWindow = (): void => { title: json.name, webPreferences: { nodeIntegration: true, - preload: 'preload.js' + preload: path.join(__dirname, 'preload.js') }, }); - mainWindow.loadURL('http://localhost:3000'); + const htmlFile = path.join(__dirname, '..', 'build', 'index.html'); + + if (fs.existsSync(htmlFile)) { + mainWindow.loadFile(htmlFile); + } else { + mainWindow.loadURL('http://localhost:3000'); + } mainWindow.webContents.openDevTools(); }; diff --git a/gui/react/src/App.tsx b/gui/react/src/App.tsx index e42906f..1d6061d 100644 --- a/gui/react/src/App.tsx +++ b/gui/react/src/App.tsx @@ -1,16 +1,21 @@ import React from 'react'; -import { Button } from '@mui/material'; +import { Button, TextField, Box } from '@mui/material'; import { messageChannelContext } from './provider/MessageChannel'; function App() { const channel = React.useContext(messageChannelContext); + const [data, setData] = React.useState<{username: string|undefined, password: string|undefined}>({ username: undefined, password: undefined }); return ( - + + setData({ password: data.password, username: e.target.value })} /> + setData({ password: e.target.value, username: data.username })} /> + + ); } diff --git a/package.json b/package.json index e555946..ffa1f3f 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ "utility", "cli" ], - "author": "AniDL/Izu-co", + "author": "Izu-co", "homepage": "https://github.com/anidl/multi-downloader-nx", "repository": { "type": "git", @@ -24,7 +24,7 @@ "url": "https://github.com/anidl/multi-downloader-nx/issues" }, "license": "MIT", - "main": "lib/gui/electron/src/index.js", + "main": "gui/electron/src/index.js", "dependencies": { "cheerio": "^1.0.0-rc.10", "electron-squirrel-startup": "^1.0.0", @@ -69,7 +69,7 @@ }, "scripts": { "prestart": "npm run tsc", - "start": "electron-forge start", + "start": "cd lib && electron-forge start", "docs": "ts-node modules/build-docs.ts", "tsc": "ts-node tsc.ts", "prebuild-win64": "npm run tsc", diff --git a/tsc.ts b/tsc.ts index a43ab1c..4a9ae72 100644 --- a/tsc.ts +++ b/tsc.ts @@ -1,68 +1,83 @@ -import { exec } from 'child_process'; +import { ChildProcess, exec } from 'child_process'; import fs from 'fs'; import path from 'path'; import { removeSync, copyFileSync } from 'fs-extra'; const ignore = [ - '.git', - 'lib', - 'node_modules', - '@types', - path.join('gui', 'react', 'node_modules'), - path.join('bin', 'mkvtoolnix'), - path.join('config', 'token.yml'), - path.join('config', 'updates.json'), - path.join('config', 'cr_token.yml'), - path.join('config', 'funi_token.yml'), - '.eslint', -].map(a => path.join(__dirname, a)); + '*SEP\\.git*', + '*SEPlib*', + '*SEPnode_modules*', + '*SEP@types*', + '*SEPout*', + '*SEPbinSEPmkvtoolnix*', + '*SEPtoken.yml$', + '*SEPupdates.json$', + '*SEPcr_token.yml$', + '*SEPfuni_token.yml$', + '*SEP\\.eslint*', + '*SEP*\\.tsx?$', + 'SEP*fonts', + 'SEPreact*', +].map(a => a.replace(/\*/g, '[^]*').replace(/SEP/g, path.sep === '\\' ? '\\\\' : '/')).map(a => new RegExp(a, 'i')); export { ignore }; (async () => { + + const waitForProcess = async (proc: ChildProcess) => { + return new Promise((resolve, reject) => { + proc.stdout?.on('data', console.log); + proc.stderr?.on('data', console.error); + proc.on('close', resolve); + proc.on('error', reject); + }) + } + process.stdout.write('Removing lib dir... '); removeSync('lib'); process.stdout.write('✓\nRunning tsc... '); const tsc = exec('npx tsc'); - tsc.stdout?.on('data', console.log); - tsc.stderr?.on('data', console.log); - tsc.on('close', () => { - process.stdout.write('✓\nCopying files... '); - const files = readDir(__dirname); - const filtered = files.filter(a => { - if (a.stats.isFile()) { - return a.path.split('.').pop() !== 'ts'; - } else { - return true; - } - }); - filtered.forEach(item => { - const itemPath = path.join(__dirname, 'lib', item.path.replace(__dirname, '')); - if (item.stats.isDirectory()) { - if (!fs.existsSync(itemPath)) - fs.mkdirSync(itemPath); - } else { - copyFileSync(item.path, itemPath); - } - }); - process.stdout.write('✓\n'); + await waitForProcess(tsc); + + process.stdout.write('✓\nBuilding react... '); + const react = exec('npm run build', { + cwd: path.join(__dirname, 'gui', 'react'), }); + + await waitForProcess(react); + + copyDir(path.join(__dirname, 'gui', 'react', 'build'), path.join(__dirname, 'lib', 'gui', 'electron', 'build')); + + process.stdout.write('✓\nCopying files... '); + const files = readDir(__dirname); + files.forEach(item => { + const itemPath = path.join(__dirname, 'lib', item.path.replace(__dirname, '')); + if (item.stats.isDirectory()) { + if (!fs.existsSync(itemPath)) + fs.mkdirSync(itemPath); + } else { + copyFileSync(item.path, itemPath); + } + }); + process.stdout.write('✓\n'); })(); -const readDir = (dir: string) : { +function readDir (dir: string): { path: string, stats: fs.Stats -}[] => { +}[] { const items: { path: string, stats: fs.Stats }[] = []; const content = fs.readdirSync(dir); - for (const item of content) { + itemLoop: for (const item of content) { const itemPath = path.join(dir, item); - if (ignore.some(a => itemPath.startsWith(a))) - continue; + for (const ignoreItem of ignore) { + if (ignoreItem.test(itemPath)) + continue itemLoop; + } const stats = fs.statSync(itemPath); items.push({ path: itemPath, @@ -73,4 +88,18 @@ const readDir = (dir: string) : { } } return items; -}; \ No newline at end of file +}; + +async function copyDir(src: string, dest: string) { + await fs.promises.mkdir(dest, { recursive: true }); + let entries = await fs.promises.readdir(src, { withFileTypes: true }); + + for (let entry of entries) { + let srcPath = path.join(src, entry.name); + let destPath = path.join(dest, entry.name); + + entry.isDirectory() ? + await copyDir(srcPath, destPath) : + await fs.promises.copyFile(srcPath, destPath); + } +} \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index 2400c11..2f340f6 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -14,6 +14,7 @@ "exclude": [ "./videos", "./tsc.ts", - "lib/**/*" + "lib/**/*", + "gui/react/**/*" ] }