import ejs from 'ejs' import * as fs from 'fs-extra' import { globSync } from 'glob' import * as path from 'path' import semver from 'semver' import { FACTORY_PACKAGE_VERSION, PROXY_PACKAGE_TEMPLATE_DIR, WORKING_DIR, } from '..' import type { App } from '../apps/_default' import { publishPackage } from './publishPackage' type ProxySpecificAppInfo = App & { version: string shouldPublish: boolean } type ProxySpecificContext = ProxySpecificAppInfo & { packageName: string semver: typeof semver } export const createProxyPackage = async (proxyInfo: ProxySpecificAppInfo) => { const { version, slug } = proxyInfo const packageName = `gobot-${slug}` const packageVersion = `${version}-${FACTORY_PACKAGE_VERSION}` const packageDir = path.join(WORKING_DIR, `${packageName}-${packageVersion}`) await fs.ensureDir(packageDir) const proxyPackageJson = { name: packageName, version: packageVersion, type: 'module', repository: { type: 'git', url: 'git+https://github.com/benallfree/gobot.git', }, author: 'Ben Allfree', license: 'MIT', bugs: { url: 'https://github.com/benallfree/gobot/issues', }, homepage: 'https://github.com/benallfree/gobot', pocketbase: { version: version, }, scripts: { postinstall: 'node postinstall.mjs', postuninstall: 'node postuninstall.mjs', }, dependencies: { tar: '7.4.3', 'node-fetch': '3.3.2', }, bin: { pocketbase: 'run.mjs', }, main: 'index.mjs', types: 'types.d.ts', } const context: ProxySpecificContext = { ...proxyInfo, packageName, semver, } globSync(path.join(PROXY_PACKAGE_TEMPLATE_DIR, '*')).forEach((file) => { const dst = path.join(packageDir, path.basename(file)) const template = ejs.compile(fs.readFileSync(file, 'utf8')) fs.writeFileSync(dst, template(context)) }) await fs.writeJson(path.join(packageDir, 'package.json'), proxyPackageJson, { spaces: 2, }) if (proxyInfo.shouldPublish) { await publishPackage(packageName, packageVersion, packageDir) } }