Skip to content

Commit

Permalink
Added piral publish --type
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianRappl committed Feb 27, 2024
1 parent aec703e commit 53e7224
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 38 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
132 changes: 94 additions & 38 deletions src/tooling/piral-cli/src/apps/publish-piral.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { resolve } from 'path';
import { LogLevels, PublishScheme } from '../types';
import { LogLevels, PiralPublishType, PublishScheme } from '../types';
import {
setLogLevel,
progress,
Expand All @@ -20,6 +20,9 @@ import {
retrievePiletsInfo,
validateSharedDependencies,
getCertificate,
releaseName,
packageJson,
triggerBuildShell,
} from '../common';

export interface PublishPiralOptions {
Expand Down Expand Up @@ -88,6 +91,11 @@ export interface PublishPiralOptions {
beforePackage?(e: any): Promise<void>;
afterPackage?(e: any): Promise<void>;
};

/**
* Selects the target type of the build (e.g. 'release'). "all" builds all target types.
*/
type?: PiralPublishType;
}

export const publishPiralDefaults: PublishPiralOptions = {
Expand All @@ -100,6 +108,7 @@ export const publishPiralDefaults: PublishPiralOptions = {
cert: undefined,
mode: 'basic',
headers: {},
type: 'emulator',
};

export async function publishPiral(baseDir = process.cwd(), options: PublishPiralOptions = {}) {
Expand All @@ -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,
Expand All @@ -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!`);
}
}
7 changes: 7 additions & 0 deletions src/tooling/piral-cli/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
bundlerKeys,
piralBuildTypeKeys,
publishModeKeys,
piralPublishTypeKeys,
} from './helpers';
import {
ToolCommand,
Expand All @@ -26,6 +27,7 @@ import {
PiletBuildType,
PublishScheme,
SourceLanguage,
PiralPublishType,
} from './types';

function specializeCommand(commands: Array<ToolCommand<any>>, command: ToolCommand<any>, suffix: string) {
Expand Down Expand Up @@ -232,6 +234,9 @@ const allCommands: Array<ToolCommand<any>> = [
.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.');
Expand All @@ -246,8 +251,10 @@ const allCommands: Array<ToolCommand<any>> = [
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<string, string>,
hooks: args.hooks as object,
_: args,
});
},
Expand Down
2 changes: 2 additions & 0 deletions src/tooling/piral-cli/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
PiletBuildType,
PublishScheme,
SourceLanguage,
PiralPublishType,
} from './types';

export const schemaKeys: Array<PiletSchemaVersion> = ['v0', 'v1', 'v2', 'v3', 'mf', 'none'];
Expand All @@ -22,6 +23,7 @@ export const piralBuildTypeKeys: Array<PiralBuildType> = [
'emulator-sources',
'emulator-website',
];
export const piralPublishTypeKeys: Array<PiralPublishType> = ['release', 'emulator'];
export const piletBuildTypeKeys: Array<PiletBuildType> = ['default', 'standalone', 'manifest'];
export const clientTypeKeys: Array<NpmClientType> = ['npm', 'pnpm', 'pnp', 'yarn', 'lerna', 'rush', 'bun'];
export const sourceLanguageKeys: Array<SourceLanguage> = ['ts', 'js'];
Expand Down
4 changes: 4 additions & 0 deletions src/tooling/piral-cli/src/types/public.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit 53e7224

Please sign in to comment.