├── .gitignore ├── .gitpod.yml ├── .npmignore ├── LICENSE ├── README.md ├── astro.config.mjs ├── index.js ├── package.json ├── snowpack.config.js ├── src ├── components │ ├── Command.astro │ ├── Component.py │ └── Demo.astro └── pages │ └── index.astro └── yarn.lock /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | dist -------------------------------------------------------------------------------- /.gitpod.yml: -------------------------------------------------------------------------------- 1 | tasks: 2 | - init: > 3 | yarn install && 4 | yarn test 5 | command: yarn start 6 | 7 | ports: 8 | - port: 3000 9 | visibility: public 10 | onOpen: open-browser 11 | 12 | vscode: 13 | extensions: 14 | - https://marketplace.visualstudio.com/_apis/public/gallery/publishers/astro-build/vsextensions/astro-vscode/0.7.13/vspackage 15 | - esbenp.prettier-vscode 16 | 17 | github: 18 | prebuilds: 19 | # enable for the master/default branch (defaults to true) 20 | master: true 21 | # enable for all branches in this repo (defaults to false) 22 | branches: true 23 | # enable for pull requests coming from this repo (defaults to true) 24 | pullRequests: true 25 | # enable for pull requests coming from forks (defaults to false) 26 | pullRequestsFromForks: true 27 | # add a "Review in Gitpod" button as a comment to pull requests (defaults to true) 28 | addComment: true 29 | # add a "Review in Gitpod" button to pull requests (defaults to false) 30 | addBadge: false 31 | # add a label once the prebuild is ready to pull requests (defaults to false) 32 | addLabel: prebuilt-in-gitpod -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | *.vsix -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Stephen Solka 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Astro Command 2 | 3 | Astro component for static rendering of commands. This allows you build components in any language. 4 | 5 | [Demo](https://github.com/trashhalo/astro-command/blob/main/src/components/Demo.astro) 6 | 7 | ## Usage 8 | 9 | ```astro 10 | --- 11 | import { Command } from "astro-command"; 12 | --- 13 | 14 | ``` 15 | Component.py 16 | ```python 17 | #!/usr/bin/env python 18 | 19 | import sys, json 20 | 21 | data = json.load(sys.stdin) 22 | print(f'

Hello {data["message"]}

') 23 | ``` 24 | 25 | * caller: url of the caller so we can use it to look up relative paths 26 | * command: name of the command to execute. relative path from the caller 27 | * props: all props are passed to command as json via stdin 28 | * html: generated html sent out via stdout embedded 29 | -------------------------------------------------------------------------------- /astro.config.mjs: -------------------------------------------------------------------------------- 1 | export default { 2 | projectRoot: '.', 3 | devOptions: { 4 | port: 3000, // The port to run the dev server on. 5 | }, 6 | renderers: [], 7 | }; 8 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | export { default as Command } from './src/components/Command.astro'; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "astro-command", 3 | "version": "0.0.7", 4 | "main": "index.js", 5 | "license": "MIT", 6 | "keywords": ["astro-component"], 7 | "repository": "trashhalo/astro-command", 8 | "scripts": { 9 | "start": "astro dev", 10 | "test": "astro build" 11 | }, 12 | "dependencies": {}, 13 | "devDependencies": { 14 | "astro": "^0.18.13" 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /snowpack.config.js: -------------------------------------------------------------------------------- 1 | // Snowpack Configuration File 2 | // See all supported options: https://www.snowpack.dev/reference/configuration 3 | 4 | /** @type {import("snowpack").SnowpackUserConfig } */ 5 | module.exports = { 6 | mount: { 7 | /* ... */ 8 | }, 9 | plugins: [ 10 | /* ... */ 11 | ], 12 | packageOptions: { 13 | external: [] 14 | }, 15 | devOptions: { 16 | /* ... */ 17 | }, 18 | buildOptions: { 19 | /* ... */ 20 | }, 21 | }; 22 | -------------------------------------------------------------------------------- /src/components/Command.astro: -------------------------------------------------------------------------------- 1 | --- 2 | import { exec } from 'node:child_process'; 3 | import { fileURLToPath } from 'node:url'; 4 | import path from 'node:path'; 5 | 6 | const { 7 | caller, 8 | command, 9 | sendProps = true, 10 | args 11 | } = Astro.props; 12 | 13 | const input = JSON.stringify(Astro.props); 14 | const cwd = path.join(fileURLToPath(caller), '../'); 15 | 16 | let commandWithArgs = command; 17 | if(args) { 18 | commandWithArgs += ' ' + args.join(' '); 19 | } 20 | 21 | const html = await new Promise((resolve, reject) => { 22 | const sub = exec(commandWithArgs, { cwd, maxBuffer: 128*1024*1024 }, (err, stdout, stderr) => { 23 | if(err) { 24 | return reject({err, stderr}); 25 | } 26 | resolve(stdout); 27 | }); 28 | if(sendProps) { 29 | sub.on('spawn', () => { 30 | if(sub.stdin.writable) { 31 | sub.stdin.write(input); 32 | sub.stdin.end(); 33 | } 34 | }); 35 | } 36 | }); 37 | --- 38 | {html} 39 | -------------------------------------------------------------------------------- /src/components/Component.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import sys, json 4 | 5 | data = json.load(sys.stdin) 6 | print(f'

Hello {data["message"]}

') -------------------------------------------------------------------------------- /src/components/Demo.astro: -------------------------------------------------------------------------------- 1 | --- 2 | import Command from "./Command.astro"; 3 | --- 4 | 5 | -------------------------------------------------------------------------------- /src/pages/index.astro: -------------------------------------------------------------------------------- 1 | --- 2 | import Demo from "../components/Demo.astro" 3 | --- 4 | 5 | 6 | 8 | 9 | 10 | 11 | 12 | 13 | --------------------------------------------------------------------------------