Compare commits

...

7 Commits

Author SHA1 Message Date
semantic-release-bot 48dd3481b8 chore(release): 0.1.0-develop.1 [skip ci]
# [0.1.0-develop.1](https://git.lumeweb.com/LumeWeb/publish-webapp/compare/v0.0.1...v0.1.0-develop.1) (2023-08-15)

### Features

* initial version ([5de6422](5de6422a16))
2023-08-15 21:57:28 +00:00
Derrick Hammer 458a809c1d
ci: add semantic-release script 2023-08-15 17:56:28 -04:00
Derrick Hammer cd91d929b1
ci: setup 2023-08-15 17:42:31 -04:00
Derrick Hammer c2f7ac4317
ci: add .presetterrc.json 2023-08-15 17:34:15 -04:00
Derrick Hammer a429dc4bd8
ci: add npm-shrinkwrap.json 2023-08-15 17:33:50 -04:00
Derrick Hammer 9294a4abb0
ci: add repo and npm config to package.json 2023-08-15 17:32:48 -04:00
Derrick Hammer 5de6422a16
feat: initial version 2023-08-15 17:28:46 -04:00
7 changed files with 18723 additions and 0 deletions

13
.github/workflows/ci.yml vendored Normal file
View File

@ -0,0 +1,13 @@
name: Build/Publish
on:
push:
branches:
- master
- develop
- develop-*
jobs:
main:
uses: lumeweb/github-node-deploy-workflow/.github/workflows/main.yml@master
secrets: inherit

5
.presetterrc.json Normal file
View File

@ -0,0 +1,5 @@
{
"preset": [
"@lumeweb/node-library-preset"
]
}

6
CHANGELOG.md Normal file
View File

@ -0,0 +1,6 @@
# [0.1.0-develop.1](https://git.lumeweb.com/LumeWeb/publish-webapp/compare/v0.0.1...v0.1.0-develop.1) (2023-08-15)
### Features
* initial version ([5de6422](https://git.lumeweb.com/LumeWeb/publish-webapp/commit/5de6422a16ef9603c713951932019c8299436cb4))

18516
npm-shrinkwrap.json generated Normal file

File diff suppressed because it is too large Load Diff

38
package.json Normal file
View File

@ -0,0 +1,38 @@
{
"name": "@lumeweb/publish-webapp",
"version": "0.1.0-develop.1",
"type": "module",
"main": "lib/index.js",
"bin": "./lib/index.js",
"repository": {
"type": "git",
"url": "gitea@git.lumeweb.com:LumeWeb/publish-webapp.git"
},
"devDependencies": {
"@lumeweb/node-library-preset": "^0.2.7",
"@types/mime": "^3.0.1",
"@types/prompts": "^2.4.4",
"presetter": "*"
},
"readme": "ERROR: No README data found!",
"scripts": {
"prepare": "presetter bootstrap",
"build": "run build",
"semantic-release": "semantic-release"
},
"dependencies": {
"@lumeweb/libweb": "0.2.0-develop.31",
"array-from-async": "^3.0.0",
"chalk": "^5.3.0",
"mime": "^3.0.0",
"msgpackr": "^1.9.7",
"p-queue": "^7.3.4",
"prompts": "^2.4.2"
},
"files": [
"lib"
],
"publishConfig": {
"access": "public"
}
}

122
src/index.ts Normal file
View File

@ -0,0 +1,122 @@
import fs from "fs/promises";
import path from "path";
import * as process from "process";
import fromAsync from "array-from-async";
import * as util from "util";
import {
hexToBytes,
loginActivePortals,
maybeInitDefaultPortals,
setActivePortalMasterKey,
uploadObject,
} from "@lumeweb/libweb";
import chalk from "chalk";
import mime from "mime";
import { pack } from "msgpackr";
import PQueue from "p-queue";
import prompts from "prompts";
import type { WebAppMetadata } from "#types.js";
let key = process.env.PORTAL_PRIVATE_KEY;
let dir = process.env.DIR;
const parallelUploads = parseInt(process.env.PARALLEL_UPLOADS ?? "0", 10) || 10;
if (!key) {
key = await prompts.prompts.password({
name: "private_key",
message: "Enter your private key",
validate: (prev: string) => prev && prev.length === 64,
type: undefined,
});
}
if (!dir) {
dir = (await prompts.prompts.text({
name: "dir",
message: "Enter the directory of the webapp",
validate: (prev: string) => prev && prev.length > 0,
type: undefined,
})) as unknown as string;
}
dir = path.resolve(dir) + "/";
setActivePortalMasterKey(hexToBytes(key as string));
maybeInitDefaultPortals();
const processedFiles: Array<{ cid: string; file: string; size: number }> = [];
const queue = new PQueue({ concurrency: parallelUploads });
void (await loginActivePortals());
const files: string[] = await fromAsync(walkSync(dir));
files.forEach((item) => {
void queue.add(async () => processFile(item));
});
await queue.onIdle();
const metadata: WebAppMetadata = {
type: "web_app",
paths: {},
tryFiles: ["index.html"],
};
processedFiles
.sort((a, b) => {
if (a.file < b.file) {
return -1;
}
if (a.file > b.file) {
return 1;
}
return 0;
})
.forEach((item) => {
metadata.paths[item.file] = {
cid: item.cid,
contentType: mime.getType(item.file) ?? "application/octet-stream",
size: item.size,
};
});
const serializedMetadata = pack(metadata);
const [cid, err] = await uploadObject(serializedMetadata);
if (err) {
console.error("Failed to publish: ", err);
process.exit(1);
}
console.log(
util.format("%s: %s", chalk.green("Web App successfully published"), cid),
);
async function processFile(filePath: string) {
const fd = await fs.open(filePath);
const size = (await fd.stat()).size;
const [cid, err] = await uploadObject(fd.createReadStream(), BigInt(size));
if (err) {
console.error("Failed to publish: ", err);
process.exit(1);
}
processedFiles.push({ cid, file: filePath.replace(dir as string, ""), size });
}
async function* walkSync(dir: string): AsyncGenerator<string> {
const files = await fs.readdir(dir, { withFileTypes: true });
for (const file of files) {
if (file.isDirectory()) {
yield* walkSync(path.join(dir, file.name));
} else {
yield path.join(dir, file.name);
}
}
}

23
src/types.ts Normal file
View File

@ -0,0 +1,23 @@
export interface WebAppMetadata {
type: "web_app";
name?: string;
tryFiles?: string[];
errorPages?: {
[key: string]: string; // key should match the pattern ^\d{3}$
};
paths: {
[path: string]: PathContent; // path has maxLength 255
};
extraMetadata?: ExtraMetadata; // I'm assuming this as any since the actual structure isn't provided
}
export interface PathContent {
cid: CID; // Assuming CID is another interface or type
contentType?: string; // Should match the provided pattern
size: number;
}
// Placeholder definitions based on the $ref in the schema.
// You should replace these with the actual structures if you have them.
export type CID = any;
export type ExtraMetadata = any;