From 53e722477eeb5c09016105acf0fdf98731031c83 Mon Sep 17 00:00:00 2001 From: Florian Rappl Date: Tue, 27 Feb 2024 14:06:20 +0100 Subject: [PATCH] Added piral publish --type --- CHANGELOG.md | 4 + .../piral-cli/src/apps/publish-piral.ts | 132 +++++++++++++----- src/tooling/piral-cli/src/commands.ts | 7 + src/tooling/piral-cli/src/helpers.ts | 2 + src/tooling/piral-cli/src/types/public.ts | 4 + 5 files changed, 111 insertions(+), 38 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3002e41eb..43f3a33bb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Piral Changelog +## 1.5.1 (tbd) + +- Added `--type` flag to `piral publish` command + ## 1.5.0 (February 25, 2024) - Fixed error when importing `once` without context (#664) diff --git a/src/tooling/piral-cli/src/apps/publish-piral.ts b/src/tooling/piral-cli/src/apps/publish-piral.ts index cbc26c4e7..ebebc21b6 100644 --- a/src/tooling/piral-cli/src/apps/publish-piral.ts +++ b/src/tooling/piral-cli/src/apps/publish-piral.ts @@ -1,5 +1,5 @@ import { resolve } from 'path'; -import { LogLevels, PublishScheme } from '../types'; +import { LogLevels, PiralPublishType, PublishScheme } from '../types'; import { setLogLevel, progress, @@ -20,6 +20,9 @@ import { retrievePiletsInfo, validateSharedDependencies, getCertificate, + releaseName, + packageJson, + triggerBuildShell, } from '../common'; export interface PublishPiralOptions { @@ -88,6 +91,11 @@ export interface PublishPiralOptions { beforePackage?(e: any): Promise; afterPackage?(e: any): Promise; }; + + /** + * Selects the target type of the build (e.g. 'release'). "all" builds all target types. + */ + type?: PiralPublishType; } export const publishPiralDefaults: PublishPiralOptions = { @@ -100,6 +108,7 @@ export const publishPiralDefaults: PublishPiralOptions = { cert: undefined, mode: 'basic', headers: {}, + type: 'emulator', }; export async function publishPiral(baseDir = process.cwd(), options: PublishPiralOptions = {}) { @@ -113,6 +122,7 @@ export async function publishPiral(baseDir = process.cwd(), options: PublishPira cert = publishPiralDefaults.cert, headers = publishPiralDefaults.headers, mode = publishPiralDefaults.mode, + type = publishPiralDefaults.type, _ = {}, hooks = {}, bundlerName, @@ -138,62 +148,108 @@ export async function publishPiral(baseDir = process.cwd(), options: PublishPira emulator = emulatorPackageName, } = await retrievePiletsInfo(entryFiles); - if (emulator !== emulatorWebsiteName) { + if (type === 'emulator' && emulator !== emulatorWebsiteName) { fail('generalError_0002', `Currently only the "${emulatorWebsiteName}" option is supported.`); } - const emulatorDir = resolve(fullBase, source, emulatorName); + const dir = type === 'release' ? releaseName : emulatorName; + const targetDir = resolve(fullBase, source, dir); if (fresh) { const piralInstances = [name]; validateSharedDependencies(externals); - await triggerBuildEmulator({ - root, - logLevel, - bundlerName, - emulatorType: emulatorWebsiteName, - hooks, - targetDir: emulatorDir, - ignored, - externals, - entryFiles, - piralInstances, - optimizeModules: true, - sourceMaps: true, - watch: false, - scripts, - contentHash: true, - outFile: 'index.html', - _, - }); + if (type === 'release') { + await triggerBuildShell({ + targetDir, + logLevel, + bundlerName, + contentHash: true, + externals, + ignored, + minify: true, + optimizeModules: false, + publicUrl: '/', + outFile: 'index.html', + root, + sourceMaps: true, + watch: false, + hooks, + entryFiles, + piralInstances, + scripts, + _, + }); + } else { + await triggerBuildEmulator({ + root, + logLevel, + bundlerName, + emulatorType: emulatorWebsiteName, + hooks, + targetDir, + ignored, + externals, + entryFiles, + piralInstances, + optimizeModules: true, + sourceMaps: true, + watch: false, + scripts, + contentHash: true, + outFile: 'index.html', + _, + }); + } logReset(); } - const { version } = await readJson(emulatorDir, emulatorJson); + if (type === 'release') { + const { version } = await readJson(root, packageJson); - if (!version) { - fail('missingEmulatorWebsite_0130', emulatorDir); - } + log('generalInfo_0000', `Using feed service "${url}".`); + const files = await matchFiles(targetDir, '**/*'); - log('generalInfo_0000', `Using feed service "${url}".`); + progress(`Publishing release artifacts to "%s" ...`, url); + const result = await publishWebsiteEmulator(version, url, apiKey, mode, targetDir, files, interactive, headers, ca); - const files = await matchFiles(emulatorDir, '**/*'); + if (!result.success) { + fail('failedUploading_0064'); + } - progress(`Publishing emulator to "%s" ...`, url); - const result = await publishWebsiteEmulator(version, url, apiKey, mode, emulatorDir, files, interactive, headers, ca); + if (result.response) { + log('httpPostResponse_0067', result); + } - if (!result.success) { - fail('failedUploading_0064'); - } + progress(`Published successfully!`); + + logDone(`Release artifacts published successfully!`); + } else { + const { version } = await readJson(targetDir, emulatorJson); - if (result.response) { - log('httpPostResponse_0067', result); - } + if (!version) { + fail('missingEmulatorWebsite_0130', targetDir); + } - progress(`Published successfully!`); + log('generalInfo_0000', `Using feed service "${url}".`); - logDone(`Emulator published successfully!`); + const files = await matchFiles(targetDir, '**/*'); + + progress(`Publishing emulator to "%s" ...`, url); + const result = await publishWebsiteEmulator(version, url, apiKey, mode, targetDir, files, interactive, headers, ca); + + if (!result.success) { + fail('failedUploading_0064'); + } + + if (result.response) { + log('httpPostResponse_0067', result); + } + + progress(`Published successfully!`); + + logDone(`Emulator published successfully!`); + } } diff --git a/src/tooling/piral-cli/src/commands.ts b/src/tooling/piral-cli/src/commands.ts index eed43a346..b240694c6 100644 --- a/src/tooling/piral-cli/src/commands.ts +++ b/src/tooling/piral-cli/src/commands.ts @@ -14,6 +14,7 @@ import { bundlerKeys, piralBuildTypeKeys, publishModeKeys, + piralPublishTypeKeys, } from './helpers'; import { ToolCommand, @@ -26,6 +27,7 @@ import { PiletBuildType, PublishScheme, SourceLanguage, + PiralPublishType, } from './types'; function specializeCommand(commands: Array>, command: ToolCommand, suffix: string) { @@ -232,6 +234,9 @@ const allCommands: Array> = [ .boolean('interactive') .describe('interactive', 'Defines if authorization tokens can be retrieved interactively.') .default('interactive', apps.publishPiralDefaults.interactive) + .choices('type', piralPublishTypeKeys) + .describe('type', 'Selects the target type of the publish.') + .default('type', apps.publishPiralDefaults.type) .string('base') .default('base', process.cwd()) .describe('base', 'Sets the base directory. By default the current directory is used.'); @@ -246,8 +251,10 @@ const allCommands: Array> = [ interactive: args.interactive as boolean, mode: args.mode as PublishScheme, bundlerName: args.bundler as string, + type: args.type as PiralPublishType, fresh: args.fresh as boolean, headers: args.headers as Record, + hooks: args.hooks as object, _: args, }); }, diff --git a/src/tooling/piral-cli/src/helpers.ts b/src/tooling/piral-cli/src/helpers.ts index 21c8310ca..a98efe6f9 100644 --- a/src/tooling/piral-cli/src/helpers.ts +++ b/src/tooling/piral-cli/src/helpers.ts @@ -9,6 +9,7 @@ import type { PiletBuildType, PublishScheme, SourceLanguage, + PiralPublishType, } from './types'; export const schemaKeys: Array = ['v0', 'v1', 'v2', 'v3', 'mf', 'none']; @@ -22,6 +23,7 @@ export const piralBuildTypeKeys: Array = [ 'emulator-sources', 'emulator-website', ]; +export const piralPublishTypeKeys: Array = ['release', 'emulator']; export const piletBuildTypeKeys: Array = ['default', 'standalone', 'manifest']; export const clientTypeKeys: Array = ['npm', 'pnpm', 'pnp', 'yarn', 'lerna', 'rush', 'bun']; export const sourceLanguageKeys: Array = ['ts', 'js']; diff --git a/src/tooling/piral-cli/src/types/public.ts b/src/tooling/piral-cli/src/types/public.ts index c974af052..93552ae78 100644 --- a/src/tooling/piral-cli/src/types/public.ts +++ b/src/tooling/piral-cli/src/types/public.ts @@ -253,6 +253,10 @@ export type PiralBuildType = | 'emulator-sources' | 'emulator-website'; +export type PiralPublishType = + | 'release' + | 'emulator'; + export type PiletBuildType = 'default' | 'standalone' | 'manifest'; export type PackageType = 'registry' | 'file' | 'git' | 'remote';