Skip to content

Commit

Permalink
Added emulator-website to piral build types
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianRappl committed Nov 18, 2023
1 parent c2fa210 commit 98d8d0d
Showing 6 changed files with 133 additions and 29 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -3,9 +3,10 @@
## 1.4.0 (tbd)

- Fixed issue when target tarball is part of tarball content in `pilet pack` (#642)
- Moved `piral-jest-utils` and `piral-ie11polyfills-utils` into separate repository
- Moved `piral-jest-utils` and `piral-ie11polyfills-utils` into a separate repository
- Updated dependencies (#641)
- Added `piral-react` as converter for React
- Added possibility to publish emulator as a website (#644)

## 1.3.3 (November 2, 2023)

26 changes: 17 additions & 9 deletions src/tooling/piral-cli/src/apps/build-piral.ts
Original file line number Diff line number Diff line change
@@ -19,11 +19,14 @@ import {
getDestination,
validateSharedDependencies,
flattenExternals,
createEmulatorWebsite,
} from '../common';

const allName = 'all';
const releaseName = 'release';
const emulatorName = 'emulator';
const emulatorSourcesName = 'emulator-sources';
const emulatorWebsiteName = 'emulator-website';

export interface BuildPiralOptions {
/**
@@ -118,7 +121,7 @@ export const buildPiralDefaults: BuildPiralOptions = {
logLevel: LogLevels.info,
fresh: false,
minify: true,
type: 'all',
type: allName,
subdir: true,
sourceMaps: true,
watch: false,
@@ -177,10 +180,11 @@ export async function buildPiral(baseDir = process.cwd(), options: BuildPiralOpt
await removeDirectory(dest.outDir);
}

// everything except release -> build emulator
if (type !== releaseName) {
// either 'emulator-*' or 'all'
if (type === emulatorName || type === emulatorSourcesName || type === emulatorWebsiteName || type === allName) {
const emulatorPublicUrl = '/';
const targetDir = useSubdir ? join(dest.outDir, emulatorName) : dest.outDir;
const appDir = type !== emulatorWebsiteName ? join(targetDir, 'app') : targetDir;
progress('Starting emulator build ...');

// since we create this anyway let's just pretend we want to have it clean!
@@ -209,7 +213,7 @@ export async function buildPiral(baseDir = process.cwd(), options: BuildPiralOpt
entryFiles,
logLevel,
ignored,
outDir: join(targetDir, 'app'),
outDir: appDir,
outFile: dest.outFile,
_,
},
@@ -233,24 +237,28 @@ export async function buildPiral(baseDir = process.cwd(), options: BuildPiralOpt

await hooks.beforeEmulator?.({ root, externals, targetDir, outDir });

const rootDir = await createEmulatorSources(root, externals, outDir, outFile, logLevel);
const rootDir = await (type !== emulatorWebsiteName
? createEmulatorSources(root, externals, outDir, outFile, logLevel)
: createEmulatorWebsite(root, externals, outDir, outFile, logLevel));

await hooks.afterEmulator?.({ root, externals, targetDir, outDir, rootDir });

if (type !== emulatorSourcesName) {
if (type === allName || type === emulatorName) {
await hooks.beforePackage?.({ root, externals, targetDir, outDir, rootDir });
await packageEmulator(rootDir);
await hooks.afterPackage?.({ root, externals, targetDir, outDir, rootDir });
logDone(`Emulator package available in "${rootDir}".`);
} else {
} else if (type === emulatorSourcesName) {
logDone(`Emulator sources available in "${rootDir}".`);
} else if (type === emulatorWebsiteName) {
logDone(`Emulator website available in "${rootDir}".`);
}

logReset();
}

// everything except emulator and emulator-soruces -> build release
if (type !== emulatorName && type !== emulatorSourcesName) {
// either 'release' or 'all'
if (type === releaseName || type === allName) {
const targetDir = useSubdir ? join(dest.outDir, releaseName) : dest.outDir;
progress('Starting release build ...');

112 changes: 98 additions & 14 deletions src/tooling/piral-cli/src/common/emulator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { join, resolve, relative } from 'path';
import { join, resolve, relative, basename } from 'path';
import { findDependencyVersion, copyScaffoldingFiles, isValidDependency, flattenExternals } from './package';
import { createPiralStubIndexIfNotExists } from './template';
import { filesTar, filesOnceTar, packageJson, piralJson } from './constants';
@@ -7,16 +7,9 @@ import { createNpmPackage } from './npm';
import { createPiralDeclaration } from './declaration';
import { ForceOverwrite } from './enums';
import { createTarball } from './archive';
import { createDirectory, removeDirectory, matchFiles, removeAny, getFileNames } from './io';
import { updateExistingJson, readJson, writeJson, createFileIfNotExists } from './io';
import { LogLevels, SharedDependency, PiletsInfo, TemplateFileLocation } from '../types';
import {
createDirectory,
removeDirectory,
createFileIfNotExists,
updateExistingJson,
getFileNames,
removeAny,
readJson,
} from './io';

export async function createEmulatorSources(
sourceDir: string,
@@ -107,9 +100,9 @@ export async function createEmulatorSources(
version: cliVersion,
generated: true,
},
main: `./${appDir}/index.js`,
typings: `./${appDir}/index.d.ts`,
app: `./${appDir}/index.html`,
main: `./${join(appDir, 'index.js')}`,
typings: `./${join(appDir, 'index.d.ts')}`,
app: `./${join(appDir, '/index.html')}`,
peerDependencies: {},
optionalDependencies,
devDependencies: {
@@ -148,7 +141,7 @@ export async function createEmulatorSources(
// actually including this one hints that the app shell should have been included - which is forbidden
await createPiralStubIndexIfNotExists(targetDir, 'index.js', ForceOverwrite.yes, {
name: piralPkg.name,
outFile: targetFile,
outFile: basename(targetFile),
});

// generate the associated index.d.ts
@@ -166,6 +159,97 @@ export async function createEmulatorSources(
return rootDir;
}

export async function createEmulatorWebsite(
sourceDir: string,
externals: Array<SharedDependency>,
targetDir: string,
targetFile: string,
logLevel: LogLevels,
) {
const piralPkg = await readJson(sourceDir, packageJson);
const piralJsonPkg = await readJson(sourceDir, piralJson);
const pilets: PiletsInfo = {
...piralPkg.pilets,
...piralJsonPkg.pilets,
};
const allDeps = {
...piralPkg.devDependencies,
...piralPkg.dependencies,
};

const externalPackages = await Promise.all(
externals
.filter((ext) => ext.type === 'local' && isValidDependency(ext.name))
.map(async (external) => ({
name: external.name,
version: await findDependencyVersion(piralPkg, sourceDir, external),
optional: external.isAsync,
})),
);
const externalDependencies = externalPackages.reduce((deps, dep) => {
if (!dep.optional) {
deps[dep.name] = dep.version;
}

return deps;
}, {} as Record<string, string>);

const importmapEntries = externalPackages.reduce((deps, dep) => {
if (!dep.optional) {
deps[dep.name] = dep.name;
}

return deps;
}, {} as Record<string, string>);

const optionalDependencies = externalPackages.reduce((deps, dep) => {
if (dep.optional) {
deps[dep.name] = dep.name;
}

return deps;
}, {} as Record<string, string>);

const allFiles = await matchFiles(targetDir, '*');

await writeJson(
targetDir,
'emulator.json',
{
name: piralPkg.name,
description: piralPkg.description,
version: piralPkg.version,
timestamp: new Date().toISOString(),
scaffolding: {
pilets,
cli: cliVersion,
},
files: {
typings: 'index.d.ts',
main: basename(targetFile),
app: 'index.html',
assets: allFiles.map(file => relative(targetDir, file)),
},
importmap: {
imports: importmapEntries,
},
dependencies: {
optional: optionalDependencies,
included: {
...allDeps,
...externalDependencies,
},
},
},
true,
);

// generate the associated index.d.ts
await createPiralDeclaration(sourceDir, piralPkg.app ?? `./src/index.html`, targetDir, ForceOverwrite.yes, logLevel);

return targetDir;
}

export async function packageEmulator(rootDir: string) {
// finally package everything up
await createNpmPackage(rootDir);
11 changes: 8 additions & 3 deletions src/tooling/piral-cli/src/common/template.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { ForceOverwrite } from './enums';
import { createFileIfNotExists } from './io';

function fillTemplate(data: any = {}) {
export interface PiralStubFileTemplateData {
outFile: string;
name: string;
}

function fillTemplate(data: PiralStubFileTemplateData) {
return `if (process.env.NODE_ENV === 'test') {
// behavior for the test environment, we'll try to make it work
@@ -24,8 +29,8 @@ function fillTemplate(data: any = {}) {
export async function createPiralStubIndexIfNotExists(
targetDir: string,
fileName: string,
forceOverwrite?: ForceOverwrite,
data?: any,
forceOverwrite: ForceOverwrite,
data: PiralStubFileTemplateData,
) {
const content = fillTemplate(data);
await createFileIfNotExists(targetDir, fileName, content, forceOverwrite);
8 changes: 7 additions & 1 deletion src/tooling/piral-cli/src/helpers.ts
Original file line number Diff line number Diff line change
@@ -14,7 +14,13 @@ import type {
export const schemaKeys: Array<PiletSchemaVersion> = ['v0', 'v1', 'v2', 'v3', 'none'];
export const publishModeKeys: Array<PiletPublishScheme> = ['none', 'basic', 'bearer', 'digest'];
export const fromKeys: Array<PiletPublishSource> = ['local', 'remote', 'npm'];
export const piralBuildTypeKeys: Array<PiralBuildType> = ['all', 'release', 'emulator', 'emulator-sources'];
export const piralBuildTypeKeys: Array<PiralBuildType> = [
'all',
'release',
'emulator',
'emulator-sources',
'emulator-website',
];
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'];
2 changes: 1 addition & 1 deletion src/tooling/piral-cli/src/types/public.ts
Original file line number Diff line number Diff line change
@@ -231,7 +231,7 @@ export type PiletPublishScheme = 'none' | 'digest' | 'bearer' | 'basic';

export type PiletPublishSource = 'local' | 'npm' | 'remote';

export type PiralBuildType = 'all' | 'release' | 'emulator' | 'emulator-sources';
export type PiralBuildType = 'all' | 'release' | 'emulator' | 'emulator-sources' | 'emulator-website';

export type PiletBuildType = 'default' | 'standalone' | 'manifest';

0 comments on commit 98d8d0d

Please sign in to comment.