Skip to content

Commit

Permalink
Merge pull request #11 from krsbx/fix-save-file
Browse files Browse the repository at this point in the history
fix: save files functions
  • Loading branch information
krsbx authored Dec 23, 2022
2 parents 3c359cb + 489e674 commit 260ff2f
Showing 18 changed files with 179 additions and 150 deletions.
6 changes: 3 additions & 3 deletions electron-builder.json5
Original file line number Diff line number Diff line change
@@ -13,13 +13,13 @@
files: ['dist', 'dist-electron'],
extraFiles: [
{
from: 'electron/resources',
to: 'resources',
from: 'electron/files',
to: 'files',
filter: ['**/*'],
},
{
from: 'node_modules/yarn',
to: 'yarn',
to: 'files/yarn',
filter: ['**/*'],
},
],
File renamed without changes.
File renamed without changes.
46 changes: 0 additions & 46 deletions electron/ipc/read-write.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,7 @@
import { ipcMain } from 'electron';
import _ from 'lodash';
import fs from 'fs-extra';
import { ExecException } from 'child_process';
import { execAsync, getFileDirPath } from '../../src/utils/common/main';
import { READ_WRITE } from '../../src/utils/constant/ipc';
import { declarationErrorHandler } from '../../src/utils/error';
import { YARN } from '../../src/utils/constant/main';

ipcMain.handle(
READ_WRITE.COMPILE_RUN,
async (event, content: string, filePath: string, configPath: string = '') => {
try {
const dirPath = getFileDirPath(filePath);

if (!fs.existsSync(dirPath)) await fs.mkdirp(dirPath);

await fs.writeFile(filePath, content);

const commands = [`${YARN} ts-node-esm`];

if (!_.isEmpty(configPath)) commands.push(`--project ${configPath}`);
commands.push(filePath);

const results = (
await execAsync(commands.join(' '), {
cwd: dirPath,
})
).stdout.split('\n');

const result = results.slice(2, -2);
result.push('\n');

return result.join('\n');
} catch (err) {
const isDeclarationError = (err as ExecException).message.includes(
'error TS7016'
);

if (isDeclarationError) {
return {
declaration: true,
message: declarationErrorHandler((err as ExecException).message),
};
}

return;
}
}
);

ipcMain.handle(
READ_WRITE.READ_FILE,
45 changes: 45 additions & 0 deletions electron/ipc/utility.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { ipcMain } from 'electron';
import _ from 'lodash';
import fs from 'fs-extra';
import { ExecException } from 'child_process';
import {
checkPackageExist,
execAsync,
getFileDirPath,
} from '../../src/utils/common/main';
import { UTILITY } from '../../src/utils/constant/ipc';
import { declarationErrorHandler } from '../../src/utils/error';
import { YARN } from '../../src/utils/constant/main';

ipcMain.handle(UTILITY.FILE_DIR_PATH, (event, filePath) => {
@@ -54,3 +57,45 @@ ipcMain.handle(UTILITY.SYNCHRONIZE_PACKAGE, async (event, cwd: string) => {
cwd,
});
});

ipcMain.handle(
UTILITY.COMPILE_RUN,
async (event, content: string, filePath: string, configPath: string = '') => {
try {
const dirPath = getFileDirPath(filePath);

if (!fs.existsSync(dirPath)) await fs.mkdirp(dirPath);

await fs.writeFile(filePath, content);

const commands = [`${YARN} ts-node-esm`];

if (!_.isEmpty(configPath)) commands.push(`--project ${configPath}`);
commands.push(filePath);

const results = (
await execAsync(commands.join(' '), {
cwd: dirPath,
})
).stdout.split('\n');

const result = results.slice(2, -2);
result.push('\n');

return result.join('\n');
} catch (err) {
const isDeclarationError = (err as ExecException).message.includes(
'error TS7016'
);

if (isDeclarationError) {
return {
declaration: true,
message: declarationErrorHandler((err as ExecException).message),
};
}

return;
}
}
);
4 changes: 2 additions & 2 deletions electron/main/setup.ts
Original file line number Diff line number Diff line change
@@ -5,10 +5,10 @@ import { getAppDataPath, getFileDirPath } from '../../src/utils/common/main';
import { INSTALLED_PATH } from '../../src/utils/constant/main';

const copyResourceFile = async (fileName: string) => {
let src = path.resolve('electron/resources', fileName);
let src = path.resolve('electron/files', fileName);

if (app.isPackaged) {
src = path.join(INSTALLED_PATH, 'resources', fileName);
src = path.join(INSTALLED_PATH, 'files', fileName);
}

const dest = path.join(getAppDataPath(), fileName);
Binary file added electron/resources/icon.icns
Binary file not shown.
Binary file added electron/resources/icon.ico
Binary file not shown.
Binary file added electron/resources/installerIcon.ico
Binary file not shown.
Binary file added electron/resources/uninstallerIcon.ico
Binary file not shown.
38 changes: 5 additions & 33 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React from 'react';
import { Box, Flex } from '@chakra-ui/react';
import { EditorProps } from '@monaco-editor/react';
import { Pane } from 'split-pane-react';
import useEditorContext from './hooks/useContext/useEditorContext';
import useFileContext from './hooks/useContext/useFileContext';
@@ -9,44 +8,17 @@ import SideBar from './components/SideBar';
import SplitPane from './components/SplitPane';
import Editor from './components/Editor';
import useCompiler from './hooks/useCompiler';
import useFileAction from './hooks/useFileAction';
import useUtility from './hooks/useUtility';
import { chakraColor } from './utils/theme';
import useAutoInstaller from './hooks/useAutoInstaller';
import useWindowAction from './hooks/useWindowAction';
import useOnEditorOnMount from './hooks/useOnEditorOnMount';

const App = () => {
const { sizes, setSizes, bgColor } = useEditorContext();
const { filePath, currentCode } = useFileContext();
const { updateCode, removeCurrentCode, quickChangeTab } = useFileContext();
const { saveFile, saveFileAs, openFile } = useFileAction();
const { closeApp } = useWindowAction();
const { getFileDirPath } = useUtility();
const { currentCode } = useFileContext();
const { updateCode } = useFileContext();
const onEditorMount = useOnEditorOnMount();
const codeResult = useCompiler(currentCode);

const onMount: EditorProps['onMount'] = (editor, monaco) => {
editor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyS, () =>
saveFile()
);
editor.addCommand(
monaco.KeyMod.CtrlCmd | monaco.KeyMod.Shift | monaco.KeyCode.KeyS,
async () => saveFileAs(await getFileDirPath(filePath))
);
editor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyO, async () =>
openFile(undefined, await getFileDirPath(filePath))
);
editor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyW, () =>
removeCurrentCode()
);
editor.addCommand(
monaco.KeyMod.CtrlCmd | monaco.KeyMod.Shift | monaco.KeyCode.KeyW,
() => closeApp()
);
editor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.Tab, () =>
quickChangeTab()
);
};

return (
<Flex
width={'100vw'}
@@ -70,7 +42,7 @@ const App = () => {
value={currentCode}
setValue={updateCode}
fontSize={22}
onMount={onMount}
onMount={onEditorMount}
style={{
borderRight: `1px solid ${chakraColor('gray', '700')}`,
height: 'calc(100vh - 40px)',
54 changes: 34 additions & 20 deletions src/components/Modal/Settings.tsx
Original file line number Diff line number Diff line change
@@ -157,8 +157,8 @@ const Settings = ({ isOpen, onClose, packageJson }: Props) => {
size={'sm'}
{...(!isProcessing && { onClick: onClose })}
position={'absolute'}
right={0}
top={0}
right={1}
top={1}
w={'40px'}
h={'40px'}
>
@@ -176,9 +176,18 @@ const Settings = ({ isOpen, onClose, packageJson }: Props) => {
value={packageName}
onChange={(e) => setPackageName(e.target.value)}
placeholder={'Package Name...'}
autoFocus
/>
<InputRightElement width={'75px'}>
<Button width={'100%'} type={'submit'}>
<Button
width={'100%'}
type={'submit'}
variant={'main'}
bgColor={'whiteAlpha.100'}
_hover={{
bgColor: 'whiteAlpha.300',
}}
>
Add
</Button>
</InputRightElement>
@@ -231,19 +240,19 @@ const Settings = ({ isOpen, onClose, packageJson }: Props) => {
</GridItem>
</Grid>
))}
{_.map(packageJson ?? {}, (version, name) => (
<Grid
templateColumns={'repeat(2, 1fr)'}
gap={1}
key={`${name}@${version}`}
>
<GridItem px={1}>
<Text color={'whiteAlpha.700'}>
{name}@{version}
</Text>
</GridItem>
<GridItem px={1} display={'flex'} justifyContent={'flex-end'}>
{!_.includes(_.values(EXCLUDED_PACKAGE), name) ? (
{_.map(packageJson ?? {}, (version, name) =>
!_.includes(_.values(EXCLUDED_PACKAGE), name) ? (
<Grid
templateColumns={'repeat(2, 1fr)'}
gap={1}
key={`${name}@${version}`}
>
<GridItem px={1}>
<Text color={'whiteAlpha.700'}>
{name}@{version}
</Text>
</GridItem>
<GridItem px={1} display={'flex'} justifyContent={'flex-end'}>
<Checkbox
borderRadius={'lg'}
onChange={() => dropPackage(name)}
@@ -254,16 +263,21 @@ const Settings = ({ isOpen, onClose, packageJson }: Props) => {
) !== -1
}
/>
) : null}
</GridItem>
</Grid>
))}
</GridItem>
</Grid>
) : null
)}
</Stack>
<Button
width={'100%'}
onClick={onUpdate}
isLoading={isProcessing}
disabled={isProcessing}
variant={'main'}
bgColor={'whiteAlpha.100'}
_hover={{
bgColor: 'whiteAlpha.300',
}}
>
Update
</Button>
2 changes: 1 addition & 1 deletion src/components/TopBar.tsx
Original file line number Diff line number Diff line change
@@ -35,7 +35,7 @@ const TopBar = () => {
visibility={{ base: 'hidden', sm: 'visible' }}
userSelect={'none'}
>
{APP_NAME}
Run TS
</Text>
<Stack
position={'absolute'}
Loading

0 comments on commit 260ff2f

Please sign in to comment.