Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ jobs:
- bash-ast
- docker-parser
- nginx-ast
- yamlize

steps:
- name: Checkout code
Expand Down
80 changes: 80 additions & 0 deletions packages/yamlize-cli/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# @yamlize/cli

CLI for [yamlize](https://github.com/constructive-io/dev-utils/tree/main/packages/yamlize) — generate YAML from templates with imports and variable substitution.

## Install

```sh
npm install -g @yamlize/cli
```

Or locally:

```sh
npm install @yamlize/cli
```

## Usage

```sh
yamlize --config config.yaml --inFile meta.yaml --outFile output.yaml
```

### Options

| Flag | Description |
|---|---|
| `--config` | Path to a YAML config file providing template variables (optional) |
| `--inFile` | Path to the meta YAML template file |
| `--outFile` | Path where the generated YAML file will be saved |
| `--help, -h` | Show help message |
| `--version, -v` | Show version number |

### Config File

The config file provides the context for `${{yamlize.VAR}}` template substitution:

```yaml
git:
USER_NAME: Cosmology
USER_EMAIL: developers@cosmology.zone
NODE_VERSION: '20.x'
```

### Interactive Mode

When run without arguments, the CLI will prompt for each required option interactively.

### Example

Given a template `meta.yaml`:

```yaml
name: Build
on:
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
steps:
- import-yaml: node/setup.yaml
- name: Install
run: yarn
```

And a fragment `node/setup.yaml`:

```yaml
name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: ${{yamlize.NODE_VERSION}}
```

Running:

```sh
yamlize --config config.yaml --inFile meta.yaml --outFile workflow.yaml
```

Produces the fully resolved `workflow.yaml` with imports inlined and variables substituted.
18 changes: 18 additions & 0 deletions packages/yamlize-cli/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
transform: {
'^.+\\.tsx?$': [
'ts-jest',
{
babelConfig: false,
tsconfig: 'tsconfig.json',
},
],
},
transformIgnorePatterns: [`/node_modules/*`],
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
modulePathIgnorePatterns: ['dist/*']
};
51 changes: 51 additions & 0 deletions packages/yamlize-cli/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"name": "@yamlize/cli",
"version": "0.10.0",
"description": "yamlize CLI — generate YAML from templates with imports and variable substitution",
"author": "Dan Lynch <pyramation@gmail.com>",
"homepage": "https://github.com/constructive-io/dev-utils",
"license": "MIT",
"main": "index.js",
"module": "esm/index.js",
"types": "index.d.ts",
"bin": {
"yamlize": "index.js"
},
"publishConfig": {
"access": "public",
"directory": "dist"
},
"scripts": {
"copy": "makage assets",
"clean": "makage clean",
"prepublishOnly": "npm run build",
"build": "makage build",
"lint": "eslint . --fix",
"test": "jest",
"test:watch": "jest --watch"
},
"repository": {
"type": "git",
"url": "https://github.com/constructive-io/dev-utils"
},
"keywords": [
"yaml",
"cli",
"template",
"generate"
],
"bugs": {
"url": "https://github.com/constructive-io/dev-utils/issues"
},
"dependencies": {
"inquirerer": "workspace:*",
"js-yaml": "^4.1.0",
"minimist": "1.2.8",
"yamlize": "workspace:*"
},
"devDependencies": {
"@types/js-yaml": "^4.0.9",
"@types/minimist": "^1.2.5",
"makage": "0.1.10"
}
}
53 changes: 53 additions & 0 deletions packages/yamlize-cli/src/commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { readFileSync } from 'fs';
import type { CLIOptions, Inquirerer } from 'inquirerer';
import yaml from 'js-yaml';
import type { ParsedArgs } from 'minimist';
import { yamlize } from 'yamlize';
import type { YamlizeContext } from 'yamlize';

import { help } from './usage';

export const commands = async (argv: Partial<ParsedArgs>, prompter: Inquirerer, _options: CLIOptions) => {
if (argv.version || argv.v) {
const pkg = JSON.parse(readFileSync(require.resolve('../package.json'), 'utf-8'));
console.log(pkg.version);
process.exit(0);
}

if (argv.help || argv.h) {
help();
process.exit(0);
}

argv = await prompter.prompt(argv, [
{
type: 'text',
name: 'config',
message: 'path to the config',
required: false,
},
{
type: 'text',
name: 'inFile',
message: 'Provide the path the meta yaml file',
required: true,
},
{
type: 'text',
name: 'outFile',
message: 'Provide the path the output yaml file',
required: true,
},
]);

let context: YamlizeContext = {};

if (argv.config) {
const ctxContent = readFileSync(argv.config, 'utf-8');
context = yaml.load(ctxContent) as YamlizeContext;
}

yamlize(argv.inFile, argv.outFile, context);

return argv;
};
26 changes: 26 additions & 0 deletions packages/yamlize-cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env node
import { CLI } from 'inquirerer';
import type { CLIOptions } from 'inquirerer';

import { commands } from './commands';

export const options: Partial<CLIOptions> = {
minimistOpts: {
alias: {
v: 'version',
h: 'help',
},
},
};

const app = new CLI(commands, options);

app
.run()
.then(() => {
// all done!
})
.catch((error) => {
console.error(error);
process.exit(1);
});
17 changes: 17 additions & 0 deletions packages/yamlize-cli/src/usage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export function help(): void {
console.log(`
Usage:

yamlize --config <path to config file>
--inFile <path to the meta yaml file>
--outFile <path to the output yaml file>

Options:

--help, -h Show this help message.
--version, -v Show the version number.
--config Path to the config file (optional).
--inFile Path to the meta yaml file where the configuration is defined.
--outFile Path where the generated yaml file will be saved.
`);
}
7 changes: 7 additions & 0 deletions packages/yamlize-cli/tsconfig.esm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "dist/esm",
"module": "es2022"
}
}
9 changes: 9 additions & 0 deletions packages/yamlize-cli/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "dist",
"rootDir": "src/"
},
"include": ["src/**/*.ts"],
"exclude": ["dist", "node_modules", "**/*.spec.*", "**/*.test.*"]
}
Loading
Loading