|
1 | | -import React from 'react'; |
2 | | -import {Text} from 'ink'; |
| 1 | +import React, {useEffect, useState} from 'react'; |
| 2 | +import {Box, Text} from 'ink'; |
3 | 3 | import {Result} from 'meow'; |
4 | 4 | import {Flags} from '../../lib/cli-flags.js'; |
| 5 | +import {checkToken, getToken} from '../../lib/token.js'; |
| 6 | +import Spinner from 'ink-spinner'; |
| 7 | +import fs from 'fs'; |
| 8 | +import {$} from 'execa'; |
| 9 | +import {cwd} from 'process'; |
5 | 10 |
|
6 | 11 | export default function Publish({cli}: {cli: Result<Flags>}) { |
| 12 | + const [isInProjectDir, setIsInProjectDir] = useState(false); |
| 13 | + const [isCheckingAuth, setIsCheckingAuth] = useState(true); |
| 14 | + const [isAuthenticated, setIsAuthenticated] = useState(false); |
| 15 | + const [isBuilding, setIsBuilding] = useState(false); |
| 16 | + const [isBuildingError, setIsBuildingError] = useState(false); |
| 17 | + const [isPublishing, setIsPublishing] = useState(false); |
| 18 | + const [isPublishingError, setIsPublishingError] = useState(false); |
| 19 | + const [isPublished, setIsPublished] = useState(false); |
| 20 | + |
| 21 | + useEffect(() => { |
| 22 | + // Check if the current dir contains pulse.config.ts |
| 23 | + const currentDir = process.cwd(); |
| 24 | + const pulseConfigPath = `${currentDir}/pulse.config.ts`; |
| 25 | + if (fs.existsSync(pulseConfigPath)) { |
| 26 | + setIsInProjectDir(true); |
| 27 | + } |
| 28 | + }, []); |
| 29 | + |
| 30 | + // Check if the user is authenticated |
| 31 | + useEffect(() => { |
| 32 | + async function checkAuth() { |
| 33 | + const token = getToken(); |
| 34 | + if (token) { |
| 35 | + const isValid = await checkToken(token); |
| 36 | + if (isValid) { |
| 37 | + setIsAuthenticated(true); |
| 38 | + } |
| 39 | + } |
| 40 | + setIsCheckingAuth(false); |
| 41 | + } |
| 42 | + |
| 43 | + if (isInProjectDir) { |
| 44 | + checkAuth(); |
| 45 | + } |
| 46 | + }, [isInProjectDir]); |
| 47 | + |
| 48 | + // Build the extension |
| 49 | + useEffect(() => { |
| 50 | + async function buildExtension() { |
| 51 | + try { |
| 52 | + setIsBuilding(true); |
| 53 | + await $`npm run build`; |
| 54 | + // Zip the dist folder |
| 55 | + await $({cwd: 'dist'})`zip -r ../node_modules/@pulse-editor/dist.zip *`; |
| 56 | + } catch (error) { |
| 57 | + setIsBuildingError(true); |
| 58 | + return; |
| 59 | + } finally { |
| 60 | + setIsBuilding(false); |
| 61 | + } |
| 62 | + |
| 63 | + await publishExtension(); |
| 64 | + } |
| 65 | + |
| 66 | + async function publishExtension() { |
| 67 | + setIsPublishing(true); |
| 68 | + |
| 69 | + // Upload the zip file to the server |
| 70 | + try { |
| 71 | + const formData = new FormData(); |
| 72 | + const buffer = fs.readFileSync('./node_modules/@pulse-editor/dist.zip'); |
| 73 | + const blob = new Blob([buffer], { |
| 74 | + type: 'application/zip', |
| 75 | + }); |
| 76 | + const file = new File([blob], 'dist.zip', { |
| 77 | + type: 'application/zip', |
| 78 | + }); |
| 79 | + formData.append('file', file, 'dist.zip'); |
| 80 | + |
| 81 | + // Send the file to the server |
| 82 | + const res = await fetch( |
| 83 | + 'https://pulse-editor.com/api/extension/publish', |
| 84 | + { |
| 85 | + method: 'POST', |
| 86 | + headers: { |
| 87 | + Authorization: `Bearer ${getToken()}`, |
| 88 | + }, |
| 89 | + body: formData, |
| 90 | + }, |
| 91 | + ); |
| 92 | + |
| 93 | + if (res.status === 200) { |
| 94 | + setIsPublished(true); |
| 95 | + } else { |
| 96 | + setIsPublishingError(true); |
| 97 | + } |
| 98 | + } catch (error) { |
| 99 | + console.error('Error uploading the file:', error); |
| 100 | + setIsPublishingError(true); |
| 101 | + return; |
| 102 | + } finally { |
| 103 | + setIsPublishing(false); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + if (isAuthenticated) { |
| 108 | + buildExtension(); |
| 109 | + } |
| 110 | + }, [isAuthenticated]); |
| 111 | + |
7 | 112 | return ( |
8 | | - <Text> |
9 | | - Publish Pulse Editor Extension in current directory to the Pulse Editor |
10 | | - Platform |
11 | | - </Text> |
| 113 | + <> |
| 114 | + {!isInProjectDir ? ( |
| 115 | + <Text color={'redBright'}> |
| 116 | + ⛔ The current directory does not contain a Pulse Editor project. |
| 117 | + </Text> |
| 118 | + ) : isCheckingAuth ? ( |
| 119 | + <Box> |
| 120 | + <Spinner type="dots" /> |
| 121 | + <Text> Checking authentication...</Text> |
| 122 | + </Box> |
| 123 | + ) : isAuthenticated ? ( |
| 124 | + <> |
| 125 | + {isBuilding && ( |
| 126 | + <Box> |
| 127 | + <Spinner type="dots" /> |
| 128 | + <Text> Building...</Text> |
| 129 | + </Box> |
| 130 | + )} |
| 131 | + {isBuildingError && ( |
| 132 | + <Text color={'redBright'}> |
| 133 | + ❌ Error building the extension. Please run `npm run build` to see |
| 134 | + the error. |
| 135 | + </Text> |
| 136 | + )} |
| 137 | + {isPublishing && ( |
| 138 | + <Box> |
| 139 | + <Spinner type="dots" /> |
| 140 | + <Text> Publishing...</Text> |
| 141 | + </Box> |
| 142 | + )} |
| 143 | + {isPublishingError && ( |
| 144 | + <Text color={'redBright'}>❌ Failed to publish extension.</Text> |
| 145 | + )} |
| 146 | + {isPublished && ( |
| 147 | + <Text color={'greenBright'}> |
| 148 | + ✅ Extension published successfully. |
| 149 | + </Text> |
| 150 | + )} |
| 151 | + </> |
| 152 | + ) : ( |
| 153 | + <Text> |
| 154 | + You are not authenticated or your access token is invalid. Publishing |
| 155 | + to Extension Marketplace is in Beta access. Please visit |
| 156 | + <Text color={'blueBright'}> https://pulse-editor.com/beta </Text>to |
| 157 | + apply for Beta access. |
| 158 | + </Text> |
| 159 | + )} |
| 160 | + </> |
12 | 161 | ); |
13 | 162 | } |
0 commit comments