Skip to content

Commit

Permalink
improve types
Browse files Browse the repository at this point in the history
  • Loading branch information
mifi committed Mar 15, 2024
1 parent 96baa9a commit 27df6c2
Show file tree
Hide file tree
Showing 48 changed files with 688 additions and 672 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/dist
/vite-dist
/ts-dist
5 changes: 1 addition & 4 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ module.exports = {
browser: true,
},
rules: {
'import/no-extraneous-dependencies': ['error', {
devDependencies: true,
optionalDependencies: false,
}],
'import/no-extraneous-dependencies': 0,
},
},
{
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ node_modules
/doc
/ffmpeg
/app.log
/ts-dist
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,13 @@
"@radix-ui/react-switch": "^1.0.1",
"@tsconfig/strictest": "^2.0.2",
"@tsconfig/vite-react": "^3.0.0",
"@types/color": "^3.0.6",
"@types/css-modules": "^1.0.5",
"@types/eslint": "^8",
"@types/lodash": "^4.14.202",
"@types/node": "18",
"@types/react": "^18.2.66",
"@types/react-dom": "^18.2.22",
"@types/sortablejs": "^1.15.0",
"@typescript-eslint/eslint-plugin": "^6.12.0",
"@typescript-eslint/parser": "^6.12.0",
Expand Down Expand Up @@ -100,8 +104,8 @@
"sortablejs": "^1.13.0",
"sweetalert2": "^11.0.0",
"sweetalert2-react-content": "^5.0.7",
"tiny-invariant": "^1.3.3",
"typescript": "~5.2.0",
"typescript-plugin-css-modules": "^5.1.0",
"use-debounce": "^5.1.0",
"use-trace-update": "^1.3.0",
"vite": "^4.5.2",
Expand Down
5 changes: 4 additions & 1 deletion public/compatPlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ function createMediaSourceStream({ path, videoStreamIndex, audioStreamIndex, see
return;
}

if (!err.killed) {
// @ts-expect-error todo
if (!(err.killed)) {
// @ts-expect-error todo
console.warn(err.message);
console.warn(stderr.toString('utf8'));
}
Expand All @@ -89,6 +91,7 @@ function readOneJpegFrameWrapper({ path, seekTo, videoStreamIndex }) {
const { stdout } = await process;
return stdout;
} catch (err) {
// @ts-expect-error todo
logger.error('renderOneJpegFrame', err.shortMessage);
throw new Error('Failed to render JPEG frame');
}
Expand Down
10 changes: 8 additions & 2 deletions public/configStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const logger = require('./logger');
const { app } = electron;


/** @type {import('../types').KeyBinding[]} */
const defaultKeyBindings = [
{ keys: 'plus', action: 'addSegment' },
{ keys: 'space', action: 'togglePlayResetSpeed' },
Expand Down Expand Up @@ -80,6 +81,7 @@ const defaultKeyBindings = [
{ keys: 'alt+down', action: 'decreaseVolume' },
];

/** @type {import('../types').Config} */
const defaults = {
captureFormat: 'jpeg',
customOutDir: undefined,
Expand Down Expand Up @@ -128,13 +130,14 @@ const defaults = {
enableNativeHevc: true,
enableUpdateCheck: true,
cleanupChoices: {
trashTmpFiles: true, askForCleanup: true, closeFile: true,
trashTmpFiles: true, askForCleanup: true, closeFile: true, cleanupAfterExport: false,
},
allowMultipleInstances: false,
darkMode: true,
preferStrongColors: false,
outputFileNameMinZeroPadding: 1,
cutFromAdjustmentFrames: 0,
invertTimelineScroll: undefined,
};

// For portable app: https://github.com/mifi/lossless-cut/issues/645
Expand All @@ -146,7 +149,7 @@ async function getCustomStoragePath() {
// https://github.com/mifi/lossless-cut/issues/645#issuecomment-1001363314
// https://stackoverflow.com/questions/46307797/how-to-get-the-original-path-of-a-portable-electron-app
// https://github.com/electron-userland/electron-builder/blob/master/docs/configuration/nsis.md
const customStorageDir = process.env.PORTABLE_EXECUTABLE_DIR || dirname(app.getPath('exe'));
const customStorageDir = process.env['PORTABLE_EXECUTABLE_DIR'] || dirname(app.getPath('exe'));
const customConfigPath = join(customStorageDir, 'config.json');
if (await pathExists(customConfigPath)) return customStorageDir;
return undefined;
Expand All @@ -158,15 +161,18 @@ async function getCustomStoragePath() {

let store;

/** @type {import('../types').StoreGetConfig} */
function get(key) {
return store.get(key);
}

/** @type {import('../types').StoreSetConfig} */
function set(key, val) {
if (val === undefined) store.delete(key);
else store.set(key, val);
}

/** @type {import('../types').StoreResetConfig} */
function reset(key) {
set(key, defaults[key]);
}
Expand Down
6 changes: 3 additions & 3 deletions public/contextMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = (window) => {
const selectionMenu = Menu.buildFromTemplate([
{ role: 'copy' },
{ type: 'separator' },
{ role: 'selectall' },
{ role: 'selectAll' },
]);

const inputMenu = Menu.buildFromTemplate([
Expand All @@ -17,10 +17,10 @@ module.exports = (window) => {
{ role: 'copy' },
{ role: 'paste' },
{ type: 'separator' },
{ role: 'selectall' },
{ role: 'selectAll' },
]);

window.webContents.on('context-menu', (e, props) => {
window.webContents.on('context-menu', (_e, props) => {
const { selectionText, isEditable } = props;
if (isEditable) {
inputMenu.popup(window);
Expand Down
16 changes: 9 additions & 7 deletions public/electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ function createWindow() {
...getSizeOptions(),
darkTheme: true,
webPreferences: {
enableRemoteModule: true,
contextIsolation: false,
nodeIntegration: true,
// https://github.com/electron/electron/issues/5107
Expand Down Expand Up @@ -226,15 +225,17 @@ function initApp() {
// However when users start your app in command line, the system's single instance mechanism will be bypassed, and you have to use this method to ensure single instance.
// This can be tested with one terminal: npx electron .
// and another terminal: npx electron . path/to/file.mp4
app.on('second-instance', (event, commandLine, workingDirectory, additionalData) => {
app.on('second-instance', (_event, _commandLine, _workingDirectory, additionalData) => {
// Someone tried to run a second instance, we should focus our window.
if (mainWindow) {
if (mainWindow.isMinimized()) mainWindow.restore();
mainWindow.focus();
}

// @ts-expect-error todo
if (!Array.isArray(additionalData?.argv)) return;

// @ts-expect-error todo
const argv2 = parseCliArgs(additionalData.argv);

logger.info('second-instance', argv2);
Expand Down Expand Up @@ -268,26 +269,27 @@ function initApp() {
event.preventDefault(); // recommended in docs https://www.electronjs.org/docs/latest/api/app#event-open-file-macos
});

ipcMain.on('setAskBeforeClose', (e, val) => {
ipcMain.on('setAskBeforeClose', (_e, val) => {
askBeforeClose = val;
});

ipcMain.on('setLanguage', (e, language) => {
ipcMain.on('setLanguage', (_e, language) => {
i18n.changeLanguage(language).then(() => updateMenu()).catch((err) => logger.error('Failed to set language', err));
});

ipcMain.handle('tryTrashItem', async (e, path) => {
ipcMain.handle('tryTrashItem', async (_e, path) => {
try {
await stat(path);
} catch (err) {
// @ts-expect-error todo
if (err.code === 'ENOENT') return;
}
await shell.trashItem(path);
});

ipcMain.handle('showItemInFolder', (e, path) => shell.showItemInFolder(path));
ipcMain.handle('showItemInFolder', (_e, path) => shell.showItemInFolder(path));

ipcMain.on('apiKeyboardActionResponse', (e, { id }) => {
ipcMain.on('apiKeyboardActionResponse', (_e, { id }) => {
apiKeyboardActionRequests.get(id)?.();
});
}
Expand Down
5 changes: 5 additions & 0 deletions public/ffmpeg.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ function handleProgress(process, durationIn, onProgress, customMatcher = () => u
// eslint-disable-next-line unicorn/better-regex
if (!match) match = line.match(/(?:size|Lsize)=\s*[^\s]+\s+time=\s*([^\s]+)\s+/);
if (!match) {
// @ts-expect-error todo
customMatcher(line);
return;
}
Expand All @@ -86,11 +87,13 @@ function handleProgress(process, durationIn, onProgress, customMatcher = () => u
const progress = duration ? Math.min(progressTime / duration, 1) : 0; // sometimes progressTime will be greater than cutDuration
onProgress(progress);
} catch (err) {
// @ts-expect-error todo
console.log('Failed to parse ffmpeg progress line:', err.message);
}
});
}

// @ts-expect-error todo
function getExecaOptions({ env, ...customExecaOptions } = {}) {
const execaOptions = { ...customExecaOptions, env: { ...env } };
// https://github.com/mifi/lossless-cut/issues/1143#issuecomment-1500883489
Expand Down Expand Up @@ -251,6 +254,7 @@ async function detectSceneChanges({ filePath, minChange, onProgress, from, to })
const match = line.match(/^frame:\d+\s+pts:\d+\s+pts_time:([\d.]+)/);
if (!match) return;
const time = parseFloat(match[1]);
// @ts-expect-error todo
if (Number.isNaN(time) || time <= times.at(-1)) return;
times.push(time);
});
Expand Down Expand Up @@ -280,6 +284,7 @@ async function detectIntervals({ filePath, customArgs, onProgress, from, to, mat
if (start == null || end == null || Number.isNaN(start) || Number.isNaN(end)) return;
segments.push({ start, end });
}
// @ts-expect-error todo
handleProgress(process, to - from, onProgress, customMatcher);

await process;
Expand Down
3 changes: 2 additions & 1 deletion public/httpServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = ({ port, onKeyboardAction }) => {

const apiRouter = express.Router();

app.get('/', (req, res) => res.send(`See ${homepage}`));
app.get('/', (_req, res) => res.send(`See ${homepage}`));

app.use('/api', apiRouter);

Expand All @@ -38,6 +38,7 @@ module.exports = ({ port, onKeyboardAction }) => {
const host = '127.0.0.1';
server.listen(port, host, () => {
logger.info('HTTP API listening on', `http://${host}:${port}/`);
// @ts-expect-error tod
resolve();
});

Expand Down
1 change: 1 addition & 0 deletions public/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -442,5 +442,6 @@ module.exports = ({ app, mainWindow, newVersion, isStoreBuild }) => {
});
}

// @ts-expect-error todo
Menu.setApplicationMenu(Menu.buildFromTemplate(menu));
};
1 change: 1 addition & 0 deletions public/update-checker.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ async function checkNewVersion() {
if (semver.lt(currentVersion, newestVersion)) return newestVersion;
return undefined;
} catch (err) {
// @ts-expect-error todo
logger.error('Failed to check github version', err.message);
return undefined;
}
Expand Down
Loading

0 comments on commit 27df6c2

Please sign in to comment.