-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
244 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { container, singleton, InjectionToken } from 'tsyringe'; | ||
import { ref, Ref, InjectionKey } from 'vue'; | ||
|
||
export interface Server { | ||
start(): Promise<number>; | ||
close(): Promise<void>; | ||
} | ||
|
||
export const serverToken: InjectionToken<Server> = Symbol(); | ||
|
||
export const token: InjectionKey<PreviewService> = Symbol(); | ||
|
||
@singleton() | ||
export class PreviewService { | ||
private readonly server = container.resolve(serverToken); | ||
readonly port: Ref<number | undefined> = ref(); | ||
readonly message = ref(''); | ||
async startPreviewing() { | ||
try { | ||
this.port.value = await this.server.start(); | ||
} catch (error) { | ||
this.message.value = (error as Error).message; | ||
} | ||
} | ||
|
||
reset() { | ||
this.message.value = ''; | ||
this.port.value = undefined; | ||
return this.server.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import handler from 'serve-handler'; | ||
import http from 'http'; | ||
import { getOutputDir } from '../generator/joplinPlugin/pathHelper'; | ||
|
||
export class HttpServer { | ||
private server?: http.Server; | ||
async start() { | ||
const outputDir = await getOutputDir(); | ||
this.server = http.createServer((request, response) => | ||
handler(request, response, { | ||
public: outputDir, | ||
}), | ||
); | ||
let port = 3000; | ||
|
||
return new Promise<number>((resolve, reject) => { | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
this.server!.on('error', (e: Error & { code: string }) => { | ||
if (e.code === 'EADDRINUSE') { | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
this.server!.listen(++port, () => resolve(port)); | ||
} else { | ||
reject(e.message); | ||
} | ||
}); | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
this.server!.listen(port, () => resolve(port)); | ||
}); | ||
} | ||
|
||
close() { | ||
return new Promise<void>((resolve, reject) => { | ||
if (!this.server) { | ||
return; | ||
} | ||
this.server.close((err) => { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
resolve(); | ||
} | ||
}); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { container } from 'tsyringe'; | ||
import { serverToken } from 'domain/service/PreviewService'; | ||
|
||
export interface ServerRequest { | ||
event: 'startServer' | 'closeServer'; | ||
} | ||
|
||
declare const webviewApi: { | ||
postMessage: <T>(payload: ServerRequest) => Promise<T>; | ||
}; | ||
|
||
container.registerInstance(serverToken, { | ||
start() { | ||
return webviewApi.postMessage({ event: 'startServer' }); | ||
}, | ||
close() { | ||
return webviewApi.postMessage({ event: 'closeServer' }); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<script lang="ts"> | ||
import { defineComponent, inject } from 'vue'; | ||
import { Modal, Result, Button } from 'ant-design-vue'; | ||
import { token as previewToken } from 'domain/service/PreviewService'; | ||
import { token as publishToken } from 'domain/service/PublishService'; | ||
import { useModalProps } from './composable'; | ||
export default defineComponent({ | ||
components: { Modal, Result, Button }, | ||
setup() { | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
const { publish } = inject(publishToken)!; | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
const { startPreviewing, port, message, reset } = inject(previewToken)!; | ||
return { | ||
modalProps: useModalProps(), | ||
startPreviewing, | ||
port, | ||
message, | ||
publish: () => { | ||
reset(); | ||
publish(); | ||
}, | ||
reset, | ||
}; | ||
}, | ||
}); | ||
</script> | ||
<template> | ||
<Modal :visible="Boolean(port || message)" v-bind="modalProps"> | ||
<Result | ||
class="py-3 px-4" | ||
:status="port ? 'success' : 'error'" | ||
:title="port ? 'Local Server has been started' : 'Unexpected Failure'" | ||
> | ||
<template #subTitle> | ||
<div v-if="port"> | ||
You can preview your website in <strong>http:https://localhost:{{ port }}</strong> | ||
</div> | ||
<div v-else class="text-left whitespace-pre-line"> | ||
{{ message }} | ||
</div> | ||
</template> | ||
<template #extra> | ||
<Button @click="reset">Confirm</Button> | ||
<Button v-if="port" type="primary" @click="publish">Publish</Button> | ||
</template> | ||
</Result> | ||
</Modal> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.