Skip to content

Commit

Permalink
Support stdin manifest.
Browse files Browse the repository at this point in the history
  • Loading branch information
edoardocavazza committed Dec 15, 2023
1 parent 65feae4 commit 51d3b64
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/smart-humans-perform.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@chialab/plasma': minor
---

Support `stdin` manifest.
28 changes: 24 additions & 4 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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')
Expand All @@ -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<Package>(sourceDir, 'custom-elements.json');
manifest = await findJson<Package>(sourceDir, 'custom-elements.json');
}
if (!manifest) {
throw new Error('No custom elements manifest found');
}

validateManifest(manifest);

let entrypoint = options.entrypoint;
if (!entrypoint) {
try {
Expand Down Expand Up @@ -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);
});
}
17 changes: 16 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -57,3 +57,18 @@ export async function findJson<T = {}>(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');
}
}

0 comments on commit 51d3b64

Please sign in to comment.