Skip to content

Commit dac5dca

Browse files
committed
feat: improve CLI output options and logging behavior
- Added support for `--boc-hex` to output the compiled BoC directly to the console for easier accessibility - Improved usage help message with clearer layout and descriptions - `--boc-hex`, `--boc-base64`, and `--fift` now accept empty string ("") to output to the console - Refined log, warning, and error messages for better clarity - Introduced `--verbose` flag to enable additional output; verbose logs are now suppressed by default - Ensured output-related behavior is consistent and user-friendly across CLI usage These changes significantly enhance the developer experience, especially for scripting and debugging.
1 parent 4c9f0ae commit dac5dca

File tree

1 file changed

+71
-25
lines changed

1 file changed

+71
-25
lines changed

src/cli.ts

Lines changed: 71 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ const main = async () => {
1111
'--require-version': String,
1212
'--artifact': String,
1313
'--boc': String,
14+
'--boc-hex': String,
1415
'--boc-base64': String,
1516
'--fift': String,
1617
'--cwd': String,
18+
'--verbose': Boolean,
1719

1820
'-v': '--version',
1921
'-h': '--help',
@@ -22,37 +24,47 @@ const main = async () => {
2224

2325
if (args['--help']) {
2426
console.log(`Usage: func-js [OPTIONS] targets
27+
28+
targets: One or more FunC source files to compile
29+
2530
Options:
26-
-h, --help - print this and exit
27-
-v, --version - print func version and exit
28-
--cwd <path>, -C <path> - run func-js as if it was launched from the given path (useful in npm scripts)
29-
--require-version <version> - set the required func version, exit if it is different
30-
--artifact <path> - path where JSON artifact, containing BOC and FIFT output, will be written
31-
--boc <path> - path where compiled code will be written as binary bag of cells
32-
--boc-base64 <path> - path where compiled code will be written as bag of cells using base64 encoding
33-
--fift <path> - path where compiled fift code will be written
31+
-h, --help Show this help message and exit
32+
-v, --version Show compiler version and exit
33+
-C, --cwd <path> Use <path> as the working directory (useful in npm scripts)
34+
--require-version <version> Enforce a specific FunC compiler version
35+
--verbose Enable verbose output
36+
37+
Output options:
38+
--artifact <path> Write full JSON artifact (BoC, Fift, etc.) to the specified file
39+
--boc <path> Write BoC binary to the specified file
40+
--boc-hex <path> Write BoC as hex to the specified file. Use "" to output to the console
41+
--boc-base64 <path> Write BoC as base64 to the specified file. Use "" to output to the console
42+
--fift <path> Write Fift code to the specified file. Use "" to output to the console
3443
`);
3544
process.exit(0);
3645
}
3746

47+
const verbose = args['--verbose'] ?? false;
48+
const verboseLog = (message: string) => verbose && console.log(message);
49+
3850
const v = await compilerVersion();
3951

4052
if (args['--require-version'] !== undefined && v.funcVersion !== args['--require-version']) {
41-
console.error(`Failed to run func-js: the required func version is ${args['--require-version']}, but the func version is ${v.funcVersion}`);
53+
console.error(`Error: Required FunC version "${args['--require-version']}", but found "${v.funcVersion}"`);
4254
process.exit(1);
4355
}
4456

4557
if (args['--version']) {
46-
console.log(`func v${v.funcVersion}`);
58+
console.log(`FunC v${v.funcVersion}`);
4759
process.exit(0);
4860
}
4961

5062
if (args._.length === 0) {
51-
console.error('No targets were specified. Run with -h to see help.');
63+
console.error('Error: No target files specified. Use --help to see available options.');
5264
process.exit(1);
5365
}
5466

55-
console.log(`Compiling using func v${v.funcVersion}`);
67+
verboseLog(`Using FunC compiler v${v.funcVersion}`);
5668

5769
const basePath = args['--cwd'];
5870
const pathResolver = basePath === undefined
@@ -69,34 +81,68 @@ Options:
6981
process.exit(1);
7082
}
7183

72-
if (args['--artifact'] !== undefined) {
73-
writeFileSync(args['--artifact'], JSON.stringify({
84+
let hasOutput = false;
85+
verboseLog('Compilation successful.');
86+
87+
const jsonArtifactOutputFilePath = args['--artifact'];
88+
if (jsonArtifactOutputFilePath !== undefined) {
89+
writeFileSync(jsonArtifactOutputFilePath, JSON.stringify({
7490
artifactVersion: 1,
7591
version: v.funcVersion,
7692
sources: cr.snapshot,
7793
codeBoc: cr.codeBoc,
7894
fiftCode: cr.fiftCode,
7995
}));
96+
verboseLog(`Compilation artifact written to "${jsonArtifactOutputFilePath}"`);
97+
hasOutput = true;
8098
}
8199

82-
if (args['--boc-base64'] !== undefined) {
83-
writeFileSync(args['--boc-base64'], cr.codeBoc);
100+
const bocBinaryOutputFilePath = args['--boc'];
101+
if (bocBinaryOutputFilePath !== undefined) {
102+
writeFileSync(bocBinaryOutputFilePath, Buffer.from(cr.codeBoc, 'base64'));
103+
verboseLog(`BoC binary written to "${bocBinaryOutputFilePath}"`);
104+
hasOutput = true;
84105
}
85106

86-
if (args['--boc'] !== undefined) {
87-
writeFileSync(args['--boc'], Buffer.from(cr.codeBoc, 'base64'));
107+
const bocBase64 = cr.codeBoc;
108+
109+
const bocHexOutputFilePath = args['--boc-hex']?.trim();
110+
if (bocHexOutputFilePath != undefined) {
111+
const bocHex = Buffer.from(bocBase64, 'base64').toString('hex');
112+
if (bocHexOutputFilePath) {
113+
writeFileSync(bocHexOutputFilePath, bocHex);
114+
verboseLog(`BoC (hex) written to "${bocHexOutputFilePath}"`);
115+
} else {
116+
console.log(bocHex);
117+
}
118+
hasOutput = true;
88119
}
89120

90-
if (args['--fift'] !== undefined) {
91-
writeFileSync(args['--fift'], cr.fiftCode);
121+
const bocBase64OutputFilePath = args['--boc-base64']?.trim();
122+
if (bocBase64OutputFilePath != undefined) {
123+
if (bocBase64OutputFilePath) {
124+
writeFileSync(bocBase64OutputFilePath, bocBase64);
125+
verboseLog(`BoC (base64) written to "${bocBase64OutputFilePath}"`);
126+
} else {
127+
console.log(bocBase64);
128+
}
129+
hasOutput = true;
92130
}
93131

94-
console.log('Compiled successfully!');
132+
const fiftCodeOutputFilePath = args['--fift']?.trim();
133+
if (fiftCodeOutputFilePath != undefined) {
134+
const fiftCode = cr.fiftCode;
135+
if (fiftCodeOutputFilePath) {
136+
writeFileSync(fiftCodeOutputFilePath, fiftCode);
137+
verboseLog(`Fift code written to "${fiftCodeOutputFilePath}"`);
138+
} else {
139+
console.log(fiftCode);
140+
}
141+
hasOutput = true;
142+
}
95143

96-
if (!args['--artifact'] && !args['--boc'] && !args['--boc-base64'] && !args['--fift']) {
97-
console.warn('Warning: No output options were specified. Run with -h to see help.');
98-
} else {
99-
console.log('Written output files.');
144+
if (!hasOutput) {
145+
console.warn('Warning: No output options specified. Use --help to see available options.');
100146
}
101147
};
102148

0 commit comments

Comments
 (0)