Skip to content

Commit

Permalink
trying agaqin
Browse files Browse the repository at this point in the history
  • Loading branch information
shannonhochkins committed Jul 5, 2024
1 parent f844702 commit b8d88d7
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 6 deletions.
2 changes: 1 addition & 1 deletion hakit/config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Hakit",
"version": "1.0.29",
"version": "1.0.30",
"slug": "hakit",
"init": false,
"ingress": true,
Expand Down
19 changes: 18 additions & 1 deletion hakit/server/default.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,19 @@ <h1>EXPERIMENTAL - DO NOT USE THIS, enable "custom_dashboard" in the configurati
<button onclick="downloadAsset()">Download zip file</button>
<button onclick="runApplication()">Run & Build Application</button>
<button onclick="checkApplicationStatus()">Check Application Status</button>
<button onclick="removeBuild()">Remove Build</button>
<button onclick="go()">GO TO APP</button>
<h2>Write to File</h2>
<input type="text" id="filename" placeholder="Filename" value="service-account.json" />
<textarea id="fileContent" rows="10" placeholder="File content"></textarea>
<button onclick="writeToFile()">Write to File</button>

<script>
let baseUrl = window.location.origin + '{{baseUrl}}'
let baseUrl = window.location.origin + '{{baseUrl}}';

function go() {
window.open(baseUrl);
}

function checkApplicationStatus() {
fetch(`${baseUrl}status`, {
Expand All @@ -60,6 +66,17 @@ <h2>Write to File</h2>
.catch(error => console.error('Error:', error));
}

function removeBuild() {
fetch(`${baseUrl}remove-build`, {
method: 'POST'
})
.then(response => response.text())
.then(data => {
alert(data);
})
.catch(error => console.error('Error:', error));
}

function downloadAsset() {
fetch(`${baseUrl}download-version`, {
method: 'POST'
Expand Down
5 changes: 3 additions & 2 deletions hakit/server/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import fs from 'fs';
import { rm } from 'fs/promises';
import path from 'path';

export * from './get-addon-info.js';

export function ensureDirectoryExists(filePath: string, empty: boolean = false) {
export async function ensureDirectoryExists(filePath: string, empty: boolean = false) {
const dirPath = path.dirname(filePath);

// if empty is true, empty the contents of the directory
if (empty && fs.existsSync(dirPath)) {
fs.rmdirSync(dirPath, { recursive: true });
await rm(dirPath, { recursive: true });
}

// Create output directory if it doesn't exist
Expand Down
2 changes: 2 additions & 0 deletions hakit/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { downloadVersion } from './routes/download-version.js';
import { runApplication } from './routes/run-application.js';
import { writeFile } from './routes/write-file.js';
import { __dirname, PORT, OPTIONS, OUTPUT_DIR } from './constants.js';
import { removeBuild } from './routes/remove-build.js';

/***************************************************************************************************************************
* Load Environment Values
Expand Down Expand Up @@ -80,6 +81,7 @@ async function loadConfig() {
const runApplicationRequest = await runApplication(app);
app.post('/run-application', runApplicationRequest);
app.post('/write-file', writeFile);
app.post('/remove-build', removeBuild);

} else {
app.get('/', async (_req, res) => {
Expand Down
2 changes: 1 addition & 1 deletion hakit/server/routes/download-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export async function downloadVersion(_req: Request, res: Response) {
if (file.id && file.name) {
const outputFilePath = join(APP_DIRECTORY, 'zip', file.name);
// this will destroy any existing files
ensureDirectoryExists(outputFilePath, true);
await ensureDirectoryExists(outputFilePath, true);
// now download it
outputFile = await downloadFile(file.id, outputFilePath);
if (!outputFile) {
Expand Down
23 changes: 23 additions & 0 deletions hakit/server/routes/remove-build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Request, Response } from 'express';
import { join } from 'path';
import { existsSync } from 'fs';
import { rm } from 'fs/promises'
import { APP_DIRECTORY } from '../constants.js';
import { translateError } from '../helpers/index.js';


export async function removeBuild(_req: Request, res: Response) {
const buildDir = join(APP_DIRECTORY, 'app', '.next');
const nextJsBuilt = existsSync(buildDir);
if (nextJsBuilt) {
// remove the directory .next
try {
await rm(buildDir, { recursive: true, force: true });
console.log('.next directory removed successfully');
return res.status(200).send('Directory removed successfully');
} catch (error) {
console.error('Error removing build directory:', translateError(error));
return res.status(500).send('Error removing build directory');
}
}
};
2 changes: 1 addition & 1 deletion hakit/server/routes/write-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export async function writeFile(req: Request, res: Response) {
}

const filePath = join(APP_DIRECTORY, filename);
ensureDirectoryExists(filePath, false);
await ensureDirectoryExists(filePath, false);
const fileContents = typeof content === 'string' ? content : JSON.stringify(content, null, 2);

await fsWriteFile(filePath, fileContents, 'utf8');
Expand Down

0 comments on commit b8d88d7

Please sign in to comment.