@soleil-se/build-app
Version:
Script for building WebApps, RESTApps, Widgets and MCP Servers with Svelte in Sitevision.
67 lines (59 loc) • 2.16 kB
JavaScript
import { basename } from 'path';
import fse from 'fs-extra';
import chalk from 'chalk';
import prompts from 'prompts';
// eslint-disable-next-line import/no-unresolved
import got from 'got';
import FormData from 'form-data';
import { logSuccess } from '@soleil-se/build-utils/log';
import config from '@soleil-se/build-config';
import handleError from './api/handleError.js';
function getCertificate() {
if (config.webappSign) return config.webappSign.certificate || '';
return prompts({
type: 'text',
name: 'certificate',
message: 'Certificate name? (Leave empty for default)',
}).then(({ certificate }) => certificate);
}
function getAuth() {
const { auth } = config.webappSign || {};
if (auth) {
const [username, password] = Buffer.from(auth || '', 'base64').toString().split(/:(.*)/s);
return { username, password };
}
return prompts([
{ type: 'text', name: 'username', message: 'Username?', validate: (value) => (value ? true : 'Username is required.') },
{ type: 'password', name: 'password', message: 'Password?', validate: (value) => (value ? true : 'Password is required.') },
]);
}
async function signRequest(path) {
const { username, password } = await getAuth();
if (!username || !password) throw new Error('Signing aborted...');
const certificate = await getCertificate();
const form = new FormData();
const filename = basename(path);
form.append('file', fse.createReadStream(path), {
filename,
contentType: 'application/octet-stream',
});
return got({
url: 'https://developer.sitevision.se/rest-api/appsigner/signapp',
method: 'POST',
username,
password,
searchParams: { certificateName: certificate },
body: form,
responseType: 'buffer',
}).buffer();
}
export default async function signAddon({ src, manifest }) {
try {
if (!fse.existsSync(src)) throw new Error(`Could not find ${src}`);
const response = await signRequest(src);
await fse.writeFile(src, response);
logSuccess(`${manifest.type} ${chalk.white(`${manifest.name} (${manifest.id})`)} signed successfully!`);
} catch (error) {
handleError(error);
}
}