@buenos_andres/compact-builder 0.5.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +38 -0
- package/README.md +71 -0
- package/dist/Builder.d.ts +95 -0
- package/dist/Builder.js +236 -0
- package/dist/Builder.js.map +1 -0
- package/dist/Compiler.d.ts +120 -0
- package/dist/Compiler.js +267 -0
- package/dist/Compiler.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/services/CompilerService.d.ts +47 -0
- package/dist/services/CompilerService.js +100 -0
- package/dist/services/CompilerService.js.map +1 -0
- package/dist/services/EnvironmentValidator.d.ts +56 -0
- package/dist/services/EnvironmentValidator.js +82 -0
- package/dist/services/EnvironmentValidator.js.map +1 -0
- package/dist/services/FileDiscovery.d.ts +33 -0
- package/dist/services/FileDiscovery.js +76 -0
- package/dist/services/FileDiscovery.js.map +1 -0
- package/dist/services/UIService.d.ts +43 -0
- package/dist/services/UIService.js +70 -0
- package/dist/services/UIService.js.map +1 -0
- package/dist/types/errors.d.ts +75 -0
- package/dist/types/errors.js +74 -0
- package/dist/types/errors.js.map +1 -0
- package/dist/types/options.d.ts +128 -0
- package/dist/types/options.js +22 -0
- package/dist/types/options.js.map +1 -0
- package/dist/utils.d.ts +43 -0
- package/dist/utils.js +61 -0
- package/dist/utils.js.map +1 -0
- package/package.json +51 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to `@buenos_andres/compact-builder` will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## Unreleased
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
- Initial public release of `@buenos_andres/compact-builder`.
|
|
13
|
+
- Programmatic API for orchestrating the Compact toolchain:
|
|
14
|
+
- `CompactCompiler` — compiles `.compact` files to artifacts.
|
|
15
|
+
- `CompactBuilder` — runs `CompactCompiler` then assembles a publishable
|
|
16
|
+
`dist/`.
|
|
17
|
+
- Composable service classes: `EnvironmentValidator`, `FileDiscovery`,
|
|
18
|
+
`CompilerService`, and the `UIService` helper object.
|
|
19
|
+
- Option types: `CompilerOptions`, `BuilderOptions`, `BuilderOnlyOptions`,
|
|
20
|
+
`CompilerServiceOptions`, `BuildStep`, `ExecFunction`.
|
|
21
|
+
- Configurable behaviours:
|
|
22
|
+
- `hierarchical` — preserve source tree in compiler artifacts and the
|
|
23
|
+
builder's `.compact` copy step.
|
|
24
|
+
- `exclude` — glob-style patterns to skip in file discovery and dist copy
|
|
25
|
+
(`Mock*` / `*.mock.compact` excluded by default).
|
|
26
|
+
- `cleanDist` — `rm -rf dist` before building.
|
|
27
|
+
- `copyToDist` — extra files (e.g. `package.json`, `README.md`) to copy into
|
|
28
|
+
`dist/` for publishable layouts.
|
|
29
|
+
- `srcDir` / `outDir` — customize source and artifact directories.
|
|
30
|
+
- Structured error types: `CompactCliNotFoundError`, `CompilationError`,
|
|
31
|
+
`DirectoryNotFoundError`, plus the `isPromisifiedChildProcessError` type guard.
|
|
32
|
+
- Dependency-injectable `ExecFunction` for testing.
|
|
33
|
+
|
|
34
|
+
### Notes
|
|
35
|
+
|
|
36
|
+
- This package contains the library code that previously lived under
|
|
37
|
+
`@buenos_andres/compact-cli`. The CLI package now ships only the bin
|
|
38
|
+
entries (`compact-compiler`, `compact-builder`) and depends on this library.
|
package/README.md
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# @buenos_andres/compact-builder
|
|
2
|
+
|
|
3
|
+
Programmatic library for compiling and building Compact smart contracts on the
|
|
4
|
+
Midnight network. Drives the `compactc` toolchain with progress reporting,
|
|
5
|
+
structured error handling, and configurable output layouts.
|
|
6
|
+
|
|
7
|
+
This is the **library** — it ships no CLI binaries. If you want the bins
|
|
8
|
+
(`compact-compiler`, `compact-builder`) for use in `package.json` scripts,
|
|
9
|
+
install [`@buenos_andres/compact-cli`](../cli) instead, which is a thin
|
|
10
|
+
wrapper around this library.
|
|
11
|
+
|
|
12
|
+
## Install
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
yarn add --dev @buenos_andres/compact-builder
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Quick Start
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
import { CompactCompiler, CompactBuilder } from '@buenos_andres/compact-builder';
|
|
22
|
+
|
|
23
|
+
// Compile all .compact files in src/ to artifacts/
|
|
24
|
+
await new CompactCompiler({ flags: '--skip-zk' }).compile();
|
|
25
|
+
|
|
26
|
+
// Or run the full build pipeline (compile + dist assembly)
|
|
27
|
+
const builder = new CompactBuilder({
|
|
28
|
+
cleanDist: true,
|
|
29
|
+
hierarchical: true,
|
|
30
|
+
exclude: ['Mock*', '*/archive/*'],
|
|
31
|
+
copyToDist: ['package.json', '../README.md'],
|
|
32
|
+
});
|
|
33
|
+
await builder.build();
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Public API
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
// Orchestrators
|
|
40
|
+
export class CompactCompiler { /* … */ }
|
|
41
|
+
export class CompactBuilder { /* … */ }
|
|
42
|
+
|
|
43
|
+
// Service classes (use for advanced custom pipelines)
|
|
44
|
+
export class EnvironmentValidator { /* … */ }
|
|
45
|
+
export class FileDiscovery { /* … */ }
|
|
46
|
+
export class CompilerService { /* … */ }
|
|
47
|
+
export const UIService = { /* … */ };
|
|
48
|
+
|
|
49
|
+
// Option types
|
|
50
|
+
export interface CompilerOptions { /* flags, targetDir, version, hierarchical, srcDir, outDir, exclude */ }
|
|
51
|
+
export type BuilderOptions = CompilerOptions & {
|
|
52
|
+
cleanDist?: boolean;
|
|
53
|
+
copyToDist?: string[];
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
// Errors
|
|
57
|
+
export class CompactCliNotFoundError extends Error { /* … */ }
|
|
58
|
+
export class CompilationError extends Error { /* … */ }
|
|
59
|
+
export class DirectoryNotFoundError extends Error { /* … */ }
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## See also
|
|
63
|
+
|
|
64
|
+
- [`@buenos_andres/compact-cli`](https://www.npmjs.com/package/@buenos_andres/compact-cli) — bin wrapper around this library
|
|
65
|
+
- [`@buenos_andres/compact-simulator`](https://www.npmjs.com/package/@buenos_andres/compact-simulator) — TypeScript simulator for testing Compact contracts locally
|
|
66
|
+
|
|
67
|
+
See the [monorepo README](https://github.com/buenos_andres/compact-tools#readme) for the full developer guide.
|
|
68
|
+
|
|
69
|
+
## License
|
|
70
|
+
|
|
71
|
+
MIT
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { type BuilderOnlyOptions, type BuilderOptions, type BuildStep } from './types/options.ts';
|
|
3
|
+
export type { BuilderOnlyOptions, BuilderOptions };
|
|
4
|
+
/**
|
|
5
|
+
* A class to handle the build process for a project.
|
|
6
|
+
* Runs CompactCompiler as a prerequisite, then executes build steps (TypeScript compilation,
|
|
7
|
+
* artifact copying, etc.) with progress feedback and colored output for success and error states.
|
|
8
|
+
*
|
|
9
|
+
* Build steps are derived from {@link BuilderOptions} so consumers can produce a
|
|
10
|
+
* publishable distribution that matches their package conventions
|
|
11
|
+
* (preserve source tree, copy metadata, clean dist, custom excludes).
|
|
12
|
+
*
|
|
13
|
+
* @notice `cmd` scripts discard `stderr` output and fail silently because this is
|
|
14
|
+
* handled in `executeStep`.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* // Default: flatten .compact files, exclude Mock*
|
|
19
|
+
* const builder = new CompactBuilder({ flags: '--skip-zk' });
|
|
20
|
+
*
|
|
21
|
+
* // Library publish: clean dist, hierarchical tree, exclude mocks + archive, copy metadata.
|
|
22
|
+
* const builder = new CompactBuilder({
|
|
23
|
+
* cleanDist: true,
|
|
24
|
+
* hierarchical: true,
|
|
25
|
+
* exclude: ['Mock*', '*\/archive\/*'],
|
|
26
|
+
* copyToDist: ['package.json', '../README.md'],
|
|
27
|
+
* });
|
|
28
|
+
* builder.build().catch(err => console.error(err));
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export declare class CompactBuilder {
|
|
32
|
+
private readonly options;
|
|
33
|
+
private readonly steps;
|
|
34
|
+
/**
|
|
35
|
+
* Constructs a new CompactBuilder instance.
|
|
36
|
+
* @param options - Compiler + builder options (see {@link BuilderOptions}).
|
|
37
|
+
*/
|
|
38
|
+
constructor(options?: BuilderOptions);
|
|
39
|
+
/**
|
|
40
|
+
* Factory method to create a CompactBuilder from command-line arguments.
|
|
41
|
+
*
|
|
42
|
+
* @param args - Array of command-line arguments
|
|
43
|
+
* @param env - Environment variables (defaults to process.env)
|
|
44
|
+
* @returns New CompactBuilder instance configured from arguments
|
|
45
|
+
*/
|
|
46
|
+
static fromArgs(args: string[], env?: typeof process.env): CompactBuilder;
|
|
47
|
+
/**
|
|
48
|
+
* Parses command-line arguments into {@link BuilderOptions}.
|
|
49
|
+
* Builder-only flags are extracted here; remaining args are forwarded to
|
|
50
|
+
* {@link CompactCompiler.parseArgs} for compiler-side parsing.
|
|
51
|
+
*
|
|
52
|
+
* Builder-only flags (compiler flags like `--hierarchical` and `--exclude`
|
|
53
|
+
* are forwarded to {@link CompactCompiler.parseArgs}):
|
|
54
|
+
* - `--clean-dist` - rm -rf dist before building
|
|
55
|
+
* - `--copy <path>` - copy an extra file into dist/ (repeatable)
|
|
56
|
+
*
|
|
57
|
+
* @throws {Error} If `--copy` is provided without a value.
|
|
58
|
+
*/
|
|
59
|
+
static parseArgs(args: string[], env?: typeof process.env): BuilderOptions;
|
|
60
|
+
/**
|
|
61
|
+
* Executes the full build process: compiles .compact files first, then runs build steps.
|
|
62
|
+
* Displays progress with spinners and outputs results in color.
|
|
63
|
+
*
|
|
64
|
+
* @returns A promise that resolves when all steps complete successfully
|
|
65
|
+
* @throws Error if compilation or any build step fails
|
|
66
|
+
*/
|
|
67
|
+
build(): Promise<void>;
|
|
68
|
+
/**
|
|
69
|
+
* Exposes the resolved build steps. Public for testing/introspection.
|
|
70
|
+
*/
|
|
71
|
+
getSteps(): readonly BuildStep[];
|
|
72
|
+
/**
|
|
73
|
+
* Assembles the build-step pipeline from the configured options.
|
|
74
|
+
*/
|
|
75
|
+
private buildSteps;
|
|
76
|
+
/**
|
|
77
|
+
* Executes a single build step.
|
|
78
|
+
* Runs the command, shows a spinner, and prints output with indentation.
|
|
79
|
+
*
|
|
80
|
+
* @param step - The build step containing command and message
|
|
81
|
+
* @param index - Current step index (0-based) for progress display
|
|
82
|
+
* @param total - Total number of steps for progress display
|
|
83
|
+
* @returns A promise that resolves when the step completes successfully
|
|
84
|
+
* @throws Error if the step fails
|
|
85
|
+
*/
|
|
86
|
+
private executeStep;
|
|
87
|
+
/**
|
|
88
|
+
* Prints command output with indentation and specified color.
|
|
89
|
+
* Filters out empty lines and indents each line for readability.
|
|
90
|
+
*
|
|
91
|
+
* @param output - The command output string to print (stdout or stderr)
|
|
92
|
+
* @param colorFn - Chalk color function to style the output (e.g., `chalk.cyan` for success, `chalk.red` for errors)
|
|
93
|
+
*/
|
|
94
|
+
private printOutput;
|
|
95
|
+
}
|
package/dist/Builder.js
ADDED
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { exec } from 'node:child_process';
|
|
3
|
+
import { promisify } from 'node:util';
|
|
4
|
+
import chalk from 'chalk';
|
|
5
|
+
import ora, {} from 'ora';
|
|
6
|
+
import { CompactCompiler } from "./Compiler.js";
|
|
7
|
+
import { isPromisifiedChildProcessError } from "./types/errors.js";
|
|
8
|
+
import { DEFAULT_EXCLUDE_PATTERNS, } from "./types/options.js";
|
|
9
|
+
import { buildFindExcludes, shellQuote } from "./utils.js";
|
|
10
|
+
// Promisified exec for async execution
|
|
11
|
+
const execAsync = promisify(exec);
|
|
12
|
+
/**
|
|
13
|
+
* A class to handle the build process for a project.
|
|
14
|
+
* Runs CompactCompiler as a prerequisite, then executes build steps (TypeScript compilation,
|
|
15
|
+
* artifact copying, etc.) with progress feedback and colored output for success and error states.
|
|
16
|
+
*
|
|
17
|
+
* Build steps are derived from {@link BuilderOptions} so consumers can produce a
|
|
18
|
+
* publishable distribution that matches their package conventions
|
|
19
|
+
* (preserve source tree, copy metadata, clean dist, custom excludes).
|
|
20
|
+
*
|
|
21
|
+
* @notice `cmd` scripts discard `stderr` output and fail silently because this is
|
|
22
|
+
* handled in `executeStep`.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```typescript
|
|
26
|
+
* // Default: flatten .compact files, exclude Mock*
|
|
27
|
+
* const builder = new CompactBuilder({ flags: '--skip-zk' });
|
|
28
|
+
*
|
|
29
|
+
* // Library publish: clean dist, hierarchical tree, exclude mocks + archive, copy metadata.
|
|
30
|
+
* const builder = new CompactBuilder({
|
|
31
|
+
* cleanDist: true,
|
|
32
|
+
* hierarchical: true,
|
|
33
|
+
* exclude: ['Mock*', '*\/archive\/*'],
|
|
34
|
+
* copyToDist: ['package.json', '../README.md'],
|
|
35
|
+
* });
|
|
36
|
+
* builder.build().catch(err => console.error(err));
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export class CompactBuilder {
|
|
40
|
+
options;
|
|
41
|
+
steps;
|
|
42
|
+
/**
|
|
43
|
+
* Constructs a new CompactBuilder instance.
|
|
44
|
+
* @param options - Compiler + builder options (see {@link BuilderOptions}).
|
|
45
|
+
*/
|
|
46
|
+
constructor(options = {}) {
|
|
47
|
+
this.options = options;
|
|
48
|
+
this.steps = this.buildSteps();
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Factory method to create a CompactBuilder from command-line arguments.
|
|
52
|
+
*
|
|
53
|
+
* @param args - Array of command-line arguments
|
|
54
|
+
* @param env - Environment variables (defaults to process.env)
|
|
55
|
+
* @returns New CompactBuilder instance configured from arguments
|
|
56
|
+
*/
|
|
57
|
+
static fromArgs(args, env = process.env) {
|
|
58
|
+
const options = CompactBuilder.parseArgs(args, env);
|
|
59
|
+
return new CompactBuilder(options);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Parses command-line arguments into {@link BuilderOptions}.
|
|
63
|
+
* Builder-only flags are extracted here; remaining args are forwarded to
|
|
64
|
+
* {@link CompactCompiler.parseArgs} for compiler-side parsing.
|
|
65
|
+
*
|
|
66
|
+
* Builder-only flags (compiler flags like `--hierarchical` and `--exclude`
|
|
67
|
+
* are forwarded to {@link CompactCompiler.parseArgs}):
|
|
68
|
+
* - `--clean-dist` - rm -rf dist before building
|
|
69
|
+
* - `--copy <path>` - copy an extra file into dist/ (repeatable)
|
|
70
|
+
*
|
|
71
|
+
* @throws {Error} If `--copy` is provided without a value.
|
|
72
|
+
*/
|
|
73
|
+
static parseArgs(args, env = process.env) {
|
|
74
|
+
const builderOnly = {};
|
|
75
|
+
const compilerArgs = [];
|
|
76
|
+
for (let i = 0; i < args.length; i++) {
|
|
77
|
+
const arg = args[i];
|
|
78
|
+
if (arg === '--clean-dist') {
|
|
79
|
+
builderOnly.cleanDist = true;
|
|
80
|
+
}
|
|
81
|
+
else if (arg === '--copy') {
|
|
82
|
+
const value = args[i + 1];
|
|
83
|
+
if (value === undefined || value.startsWith('--')) {
|
|
84
|
+
throw new Error('--copy flag requires a path');
|
|
85
|
+
}
|
|
86
|
+
builderOnly.copyToDist ??= [];
|
|
87
|
+
builderOnly.copyToDist.push(value);
|
|
88
|
+
i++;
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
compilerArgs.push(arg);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
const compilerOptions = CompactCompiler.parseArgs(compilerArgs, env);
|
|
95
|
+
return { ...compilerOptions, ...builderOnly };
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Executes the full build process: compiles .compact files first, then runs build steps.
|
|
99
|
+
* Displays progress with spinners and outputs results in color.
|
|
100
|
+
*
|
|
101
|
+
* @returns A promise that resolves when all steps complete successfully
|
|
102
|
+
* @throws Error if compilation or any build step fails
|
|
103
|
+
*/
|
|
104
|
+
async build() {
|
|
105
|
+
// Run compact compilation as a prerequisite. CompactCompiler ignores
|
|
106
|
+
// BuilderOnlyOptions because they aren't in its resolved shape.
|
|
107
|
+
const compiler = new CompactCompiler(this.options);
|
|
108
|
+
await compiler.compile();
|
|
109
|
+
for (const [index, step] of this.steps.entries()) {
|
|
110
|
+
await this.executeStep(step, index, this.steps.length);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Exposes the resolved build steps. Public for testing/introspection.
|
|
115
|
+
*/
|
|
116
|
+
getSteps() {
|
|
117
|
+
return this.steps;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Assembles the build-step pipeline from the configured options.
|
|
121
|
+
*/
|
|
122
|
+
buildSteps() {
|
|
123
|
+
const srcDir = this.options.srcDir ?? 'src';
|
|
124
|
+
const quotedSrc = shellQuote(srcDir);
|
|
125
|
+
const excludes = buildFindExcludes(this.options.exclude ?? DEFAULT_EXCLUDE_PATTERNS);
|
|
126
|
+
const steps = [];
|
|
127
|
+
if (this.options.cleanDist) {
|
|
128
|
+
steps.push({
|
|
129
|
+
cmd: 'rm -rf dist && mkdir -p dist',
|
|
130
|
+
msg: 'Cleaning dist directory',
|
|
131
|
+
shell: '/bin/bash',
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
steps.push({
|
|
135
|
+
cmd: 'tsc --project tsconfig.build.json',
|
|
136
|
+
msg: 'Compiling TypeScript',
|
|
137
|
+
});
|
|
138
|
+
steps.push({
|
|
139
|
+
cmd: `mkdir -p dist/artifacts && cp -Rf ${quotedSrc}/artifacts/* dist/artifacts/ 2>/dev/null || true`,
|
|
140
|
+
msg: 'Copying artifacts',
|
|
141
|
+
shell: '/bin/bash',
|
|
142
|
+
});
|
|
143
|
+
if (this.options.hierarchical) {
|
|
144
|
+
steps.push({
|
|
145
|
+
// biome-ignore-start lint/suspicious/noUselessEscapeInString: shell vars must survive JS template-literal interpolation
|
|
146
|
+
cmd: `
|
|
147
|
+
SRC_DIR=${quotedSrc}
|
|
148
|
+
find "$SRC_DIR" -type f -name '*.compact' ${excludes} | while read -r file; do
|
|
149
|
+
rel_path="\${file#$SRC_DIR/}"
|
|
150
|
+
mkdir -p "dist/$(dirname "$rel_path")"
|
|
151
|
+
cp "$file" "dist/$rel_path"
|
|
152
|
+
done
|
|
153
|
+
`,
|
|
154
|
+
// biome-ignore-end lint/suspicious/noUselessEscapeInString: shell vars must survive JS template-literal interpolation
|
|
155
|
+
msg: 'Copying .compact files (preserving structure)',
|
|
156
|
+
shell: '/bin/bash',
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
else {
|
|
160
|
+
steps.push({
|
|
161
|
+
cmd: `mkdir -p dist && find ${quotedSrc} -type f -name '*.compact' ${excludes} -exec cp {} dist/ \\; 2>/dev/null || true`,
|
|
162
|
+
msg: 'Copying .compact files',
|
|
163
|
+
shell: '/bin/bash',
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
const copyTargets = this.options.copyToDist ?? [];
|
|
167
|
+
if (copyTargets.length > 0) {
|
|
168
|
+
const copyCmds = copyTargets
|
|
169
|
+
.map((path) => `cp ${shellQuote(path)} dist/ 2>/dev/null || true`)
|
|
170
|
+
.join(' && ');
|
|
171
|
+
steps.push({
|
|
172
|
+
cmd: `mkdir -p dist && ${copyCmds}`,
|
|
173
|
+
msg: 'Copying additional files to dist',
|
|
174
|
+
shell: '/bin/bash',
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
return steps;
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Executes a single build step.
|
|
181
|
+
* Runs the command, shows a spinner, and prints output with indentation.
|
|
182
|
+
*
|
|
183
|
+
* @param step - The build step containing command and message
|
|
184
|
+
* @param index - Current step index (0-based) for progress display
|
|
185
|
+
* @param total - Total number of steps for progress display
|
|
186
|
+
* @returns A promise that resolves when the step completes successfully
|
|
187
|
+
* @throws Error if the step fails
|
|
188
|
+
*/
|
|
189
|
+
async executeStep(step, index, total) {
|
|
190
|
+
const stepLabel = `[${index + 1}/${total}]`;
|
|
191
|
+
const spinner = ora(`[BUILD] ${stepLabel} ${step.msg}`).start();
|
|
192
|
+
try {
|
|
193
|
+
const { stdout, stderr } = await execAsync(step.cmd, {
|
|
194
|
+
shell: step.shell, // Only pass shell where needed
|
|
195
|
+
});
|
|
196
|
+
spinner.succeed(`[BUILD] ${stepLabel} ${step.msg}`);
|
|
197
|
+
this.printOutput(stdout, chalk.cyan);
|
|
198
|
+
this.printOutput(stderr, chalk.yellow); // Show stderr (warnings) in yellow if present
|
|
199
|
+
}
|
|
200
|
+
catch (error) {
|
|
201
|
+
spinner.fail(`[BUILD] ${stepLabel} ${step.msg}`);
|
|
202
|
+
if (isPromisifiedChildProcessError(error)) {
|
|
203
|
+
this.printOutput(error.stdout, chalk.cyan);
|
|
204
|
+
this.printOutput(error.stderr, chalk.red);
|
|
205
|
+
// biome-ignore lint/suspicious/noConsole: Needed to display build failure reason
|
|
206
|
+
console.error(chalk.red('[BUILD] ❌ Build failed:', error.message));
|
|
207
|
+
}
|
|
208
|
+
else if (error instanceof Error) {
|
|
209
|
+
// biome-ignore lint/suspicious/noConsole: Needed to display build failure reason
|
|
210
|
+
console.error(chalk.red('[BUILD] ❌ Build failed:', error.message));
|
|
211
|
+
}
|
|
212
|
+
// Library code must not call process.exit — let the caller (CLI wrapper
|
|
213
|
+
// or programmatic consumer) decide how to react. We've already surfaced
|
|
214
|
+
// the failure to the user via the spinner + printOutput above.
|
|
215
|
+
if (error instanceof Error) {
|
|
216
|
+
throw error;
|
|
217
|
+
}
|
|
218
|
+
throw new Error('[BUILD] Build failed with a non-Error exception');
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Prints command output with indentation and specified color.
|
|
223
|
+
* Filters out empty lines and indents each line for readability.
|
|
224
|
+
*
|
|
225
|
+
* @param output - The command output string to print (stdout or stderr)
|
|
226
|
+
* @param colorFn - Chalk color function to style the output (e.g., `chalk.cyan` for success, `chalk.red` for errors)
|
|
227
|
+
*/
|
|
228
|
+
printOutput(output, colorFn) {
|
|
229
|
+
const lines = output
|
|
230
|
+
.split('\n')
|
|
231
|
+
.filter((line) => line.trim() !== '')
|
|
232
|
+
.map((line) => ` ${line}`);
|
|
233
|
+
console.log(colorFn(lines.join('\n')));
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
//# sourceMappingURL=Builder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Builder.js","sourceRoot":"","sources":["../src/Builder.ts"],"names":[],"mappings":";AAEA,OAAO,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAC;AAC1C,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AACtC,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,GAAG,EAAE,EAAY,MAAM,KAAK,CAAC;AACpC,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAChD,OAAO,EAAE,8BAA8B,EAAE,MAAM,mBAAmB,CAAC;AACnE,OAAO,EAIL,wBAAwB,GACzB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,iBAAiB,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAK3D,uCAAuC;AACvC,MAAM,SAAS,GAAG,SAAS,CAAC,IAAI,CAAC,CAAC;AAElC;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAM,OAAO,cAAc;IACR,OAAO,CAAiB;IACxB,KAAK,CAAc;IAEpC;;;OAGG;IACH,YAAY,UAA0B,EAAE;QACtC,IAAI,CAA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,CAAC;YACd,CAAC;YACD,MAAM,IAAI,KAAK,CAAC,iDAAiD,CAAC,CAAC;QACrE,CAAC;IACH,CAAC;IAED;;;;;;OAMG;IACK,WAAW,CAAC,MAAc,EAAE,OAAiC;QACnE,MAAM,KAAK,GAAa,MAAM;aAC3B,KAAK,CAAC,IAAI,CAAC;aACX,MAAM,CAAC,CAAC,IAAY,EAAW,EAAE,CAAC,IAAI,CAAC,IAAI,EAAE,KAAK,EAAE,CAAC;aACrD,GAAG,CAAC,CAAC,IAAY,EAAU,EAAE,CAAC,OAAO,IAAI,EAAE,CAAC,CAAC;QAChD,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IACzC,CAAC;CACF"}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { type CompilerOptions, type CompilerServiceOptions, type ExecFunction } from './types/options.ts';
|
|
3
|
+
export { CompilerService } from './services/CompilerService.ts';
|
|
4
|
+
export { EnvironmentValidator } from './services/EnvironmentValidator.ts';
|
|
5
|
+
export { FileDiscovery } from './services/FileDiscovery.ts';
|
|
6
|
+
export { UIService } from './services/UIService.ts';
|
|
7
|
+
export type { CompilerOptions, CompilerServiceOptions, ExecFunction };
|
|
8
|
+
/** Resolved compiler options with defaults applied */
|
|
9
|
+
type ResolvedCompilerOptions = Required<Pick<CompilerOptions, 'flags' | 'hierarchical' | 'srcDir' | 'outDir' | 'exclude'>> & Pick<CompilerOptions, 'targetDir' | 'version'>;
|
|
10
|
+
/**
|
|
11
|
+
* Main compiler class that orchestrates the compilation process.
|
|
12
|
+
* Coordinates environment validation, file discovery, and compilation services
|
|
13
|
+
* to provide a complete .compact file compilation solution.
|
|
14
|
+
*
|
|
15
|
+
* Features:
|
|
16
|
+
* - Dependency injection for testability
|
|
17
|
+
* - Structured error propagation with custom error types
|
|
18
|
+
* - Progress reporting and user feedback
|
|
19
|
+
* - Support for compiler flags and toolchain versions
|
|
20
|
+
* - Environment variable integration
|
|
21
|
+
* - Configurable artifact output structure (flattened or hierarchical)
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```typescript
|
|
25
|
+
* // Basic usage with options object (flattened artifacts by default)
|
|
26
|
+
* const compiler = new CompactCompiler({
|
|
27
|
+
* flags: '--skip-zk',
|
|
28
|
+
* targetDir: 'security',
|
|
29
|
+
* version: '0.26.0',
|
|
30
|
+
* });
|
|
31
|
+
* await compiler.compile();
|
|
32
|
+
*
|
|
33
|
+
* // Factory method usage
|
|
34
|
+
* const compiler = CompactCompiler.fromArgs(['--dir', 'security', '--skip-zk']);
|
|
35
|
+
* await compiler.compile();
|
|
36
|
+
*
|
|
37
|
+
* // With hierarchical artifacts structure
|
|
38
|
+
* const compiler = CompactCompiler.fromArgs(['--hierarchical', '--skip-zk']);
|
|
39
|
+
* await compiler.compile();
|
|
40
|
+
*
|
|
41
|
+
* // With environment variables
|
|
42
|
+
* process.env.SKIP_ZK = 'true';
|
|
43
|
+
* const compiler = CompactCompiler.fromArgs(['--dir', 'token']);
|
|
44
|
+
* await compiler.compile();
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
export declare class CompactCompiler {
|
|
48
|
+
/** Environment validation service */
|
|
49
|
+
private readonly environmentValidator;
|
|
50
|
+
/** File discovery service */
|
|
51
|
+
private readonly fileDiscovery;
|
|
52
|
+
/** Compilation execution service */
|
|
53
|
+
private readonly compilerService;
|
|
54
|
+
/** Compiler options */
|
|
55
|
+
private readonly options;
|
|
56
|
+
/**
|
|
57
|
+
* Creates a new CompactCompiler instance with specified configuration.
|
|
58
|
+
*
|
|
59
|
+
* @param options - Compiler configuration options
|
|
60
|
+
* @param execFn - Optional custom exec function for dependency injection
|
|
61
|
+
*/
|
|
62
|
+
constructor(options?: CompilerOptions, execFn?: ExecFunction);
|
|
63
|
+
/**
|
|
64
|
+
* Parses command-line arguments into a CompilerOptions object.
|
|
65
|
+
*
|
|
66
|
+
* Supported argument patterns:
|
|
67
|
+
* - `--dir <directory>` - Target specific subdirectory within srcDir
|
|
68
|
+
* - `--src <directory>` - Source directory containing .compact files (default: 'src')
|
|
69
|
+
* - `--out <directory>` - Output directory for artifacts (default: 'artifacts')
|
|
70
|
+
* - `--hierarchical` - Preserve source directory structure in artifacts output
|
|
71
|
+
* - `--exclude <pattern>` - Skip `.compact` files matching the glob pattern (repeatable)
|
|
72
|
+
* - `+<version>` - Use specific toolchain version
|
|
73
|
+
* - Other arguments - Treated as compiler flags
|
|
74
|
+
* - `SKIP_ZK=true` environment variable - Adds --skip-zk flag
|
|
75
|
+
*
|
|
76
|
+
* @param args - Array of command-line arguments
|
|
77
|
+
* @param env - Environment variables (defaults to process.env)
|
|
78
|
+
* @returns Parsed CompilerOptions object
|
|
79
|
+
* @throws {Error} If --dir, --src, --out, or --exclude is provided without a value
|
|
80
|
+
*/
|
|
81
|
+
static parseArgs(args: string[], env?: typeof process.env): CompilerOptions;
|
|
82
|
+
/**
|
|
83
|
+
* Factory method to create a CompactCompiler from command-line arguments.
|
|
84
|
+
* See {@link CompactCompiler.parseArgs} for the supported argument shapes.
|
|
85
|
+
*
|
|
86
|
+
* @param args - Array of command-line arguments
|
|
87
|
+
* @param env - Environment variables (defaults to process.env)
|
|
88
|
+
* @returns New CompactCompiler instance configured from arguments
|
|
89
|
+
* @throws {Error} If --dir, --src, --out, or --exclude is provided without a value
|
|
90
|
+
*/
|
|
91
|
+
static fromArgs(args: string[], env?: typeof process.env): CompactCompiler;
|
|
92
|
+
/**
|
|
93
|
+
* Validates the compilation environment and displays version information.
|
|
94
|
+
*
|
|
95
|
+
* @throws {CompactCliNotFoundError} If Compact CLI is not available in PATH
|
|
96
|
+
* @throws {Error} If version retrieval or other validation steps fail
|
|
97
|
+
*/
|
|
98
|
+
validateEnvironment(): Promise<void>;
|
|
99
|
+
/**
|
|
100
|
+
* Main compilation method that orchestrates the entire compilation process.
|
|
101
|
+
*
|
|
102
|
+
* @throws {CompactCliNotFoundError} If Compact CLI is not available
|
|
103
|
+
* @throws {DirectoryNotFoundError} If target directory doesn't exist
|
|
104
|
+
* @throws {CompilationError} If any file compilation fails
|
|
105
|
+
*/
|
|
106
|
+
compile(): Promise<void>;
|
|
107
|
+
/**
|
|
108
|
+
* Compiles a single file with progress reporting and error handling.
|
|
109
|
+
*
|
|
110
|
+
* @param file - Relative path to the .compact file
|
|
111
|
+
* @param index - Current file index (0-based) for progress tracking
|
|
112
|
+
* @param total - Total number of files being compiled
|
|
113
|
+
* @throws {CompilationError} If compilation fails
|
|
114
|
+
*/
|
|
115
|
+
private compileFile;
|
|
116
|
+
/**
|
|
117
|
+
* For testing - returns the resolved options object
|
|
118
|
+
*/
|
|
119
|
+
get testOptions(): ResolvedCompilerOptions;
|
|
120
|
+
}
|