From 51d3b648af6db00f69c8fc12dc004efc982de2c8 Mon Sep 17 00:00:00 2001 From: Edoardo Cavazza Date: Fri, 15 Dec 2023 09:09:09 +0100 Subject: [PATCH] Support `stdin` manifest. --- .changeset/smart-humans-perform.md | 5 +++++ src/cli.ts | 28 ++++++++++++++++++++++++---- src/utils.ts | 17 ++++++++++++++++- 3 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 .changeset/smart-humans-perform.md diff --git a/.changeset/smart-humans-perform.md b/.changeset/smart-humans-perform.md new file mode 100644 index 0000000..0a5eb00 --- /dev/null +++ b/.changeset/smart-humans-perform.md @@ -0,0 +1,5 @@ +--- +'@chialab/plasma': minor +--- + +Support `stdin` manifest. diff --git a/src/cli.ts b/src/cli.ts index cf80adc..ce12884 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -8,7 +8,7 @@ import type { Package } from 'custom-elements-manifest'; import { Listr } from 'listr2'; import prompts from 'prompts'; import { candidates, SUPPORTED, transform, UNSUPPORTED, type Frameworks } from './index'; -import { findJson } from './utils'; +import { findJson, validateManifest } from './utils'; const colorFramework = (framework: string) => { switch (framework) { @@ -29,6 +29,7 @@ const colorFramework = (framework: string) => { const packageJsonFile = fileURLToPath(new URL('../package.json', import.meta.url)); const json = JSON.parse(await readFile(packageJsonFile, 'utf-8')); +let stdin = ''; program .name('plasma') @@ -51,13 +52,20 @@ program yes?: boolean; } ) => { - sourceDir = sourceDir ? resolve(sourceDir) : process.cwd(); + let manifest: Package; + if (stdin) { + manifest = JSON.parse(stdin); + } else { + sourceDir = sourceDir ? resolve(sourceDir) : process.cwd(); - const manifest = await findJson(sourceDir, 'custom-elements.json'); + manifest = await findJson(sourceDir, 'custom-elements.json'); + } if (!manifest) { throw new Error('No custom elements manifest found'); } + validateManifest(manifest); + let entrypoint = options.entrypoint; if (!entrypoint) { try { @@ -181,4 +189,16 @@ program } ); -program.parse(); +if (process.stdin.isTTY) { + program.parse(process.argv); +} else { + process.stdin.on('readable', () => { + let chunk: string; + while ((chunk = process.stdin.read()) !== null) { + stdin += chunk.toString(); + } + }); + process.stdin.on('end', () => { + program.parse(process.argv); + }); +} diff --git a/src/utils.ts b/src/utils.ts index ad4efdd..c94b9cc 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,6 +1,6 @@ import { access, readFile } from 'node:fs/promises'; import { dirname, extname, join } from 'node:path'; -import type { ClassField, CustomElementDeclaration } from 'custom-elements-manifest'; +import type { ClassField, CustomElementDeclaration, Package } from 'custom-elements-manifest'; export function capitalize(str: string) { return str.charAt(0).toUpperCase() + str.slice(1); @@ -57,3 +57,18 @@ export async function findJson(from: string, name: string) { return findJson(dir, name); } } + +export function validateManifest(manifest: Package) { + if (!manifest.schemaVersion) { + throw new Error('Malformed custom elements manifest: missing schemaVersion field'); + } + if (typeof manifest.schemaVersion !== 'string') { + throw new Error('Malformed custom elements manifest: schemaVersion is not a string'); + } + if (manifest.modules == null) { + throw new Error('Malformed custom elements manifest: missing modules field'); + } + if (!Array.isArray(manifest.modules)) { + throw new Error('Malformed custom elements manifest: modules is not an array'); + } +}