1
- import { readFileSync } from "fs" ;
2
- import { basename , dirname , join , normalize , resolve } from "path" ;
3
- import { execFileSync } from "child_process" ;
4
- import { z , ZodError } from "zod" ;
1
+ import { basename , dirname , normalize , resolve } from "path" ;
2
+ import { ZodError } from "zod" ;
5
3
import { createNodeFileSystem } from "../../vfs/createNodeFileSystem" ;
6
4
import { createVirtualFileSystem } from "../../vfs/createVirtualFileSystem" ;
7
5
import { parseAndEvalExpression } from "../../optimizer/interpreter" ;
@@ -18,6 +16,7 @@ import { build } from "../../pipeline/build";
18
16
import { TactErrorCollection } from "../../error/errors" ;
19
17
import files from "../../stdlib/stdlib" ;
20
18
import { cwd } from "process" ;
19
+ import { getVersion , showCommit } from "../version" ;
21
20
22
21
export const main = async ( ) => {
23
22
const Log = CliLogger ( ) ;
@@ -46,7 +45,7 @@ const processArgs = async (Errors: CliErrors, argv: string[]) => {
46
45
47
46
await parseArgs ( Errors , Args ) ;
48
47
} else {
49
- showHelp ( ) ;
48
+ await showHelp ( ) ;
50
49
}
51
50
} ;
52
51
@@ -64,7 +63,7 @@ const ArgSchema = (Parser: ArgParser) => {
64
63
. add ( Parser . immediate ) . end ;
65
64
} ;
66
65
67
- const showHelp = ( ) => {
66
+ const showHelp = async ( ) => {
68
67
console . log ( `Usage
69
68
$ tact [...flags] (--config CONFIG | FILE)
70
69
81
80
82
81
Examples
83
82
$ tact --version
84
- ${ getVersion ( ) }
83
+ ${ await getVersion ( ) }
85
84
86
85
Learn more about Tact: https://docs.tact-lang.org
87
86
Join Telegram group: https://t.me/tactlang
@@ -92,22 +91,23 @@ type Args = ArgConsumer<GetParserResult<ReturnType<typeof ArgSchema>>>;
92
91
93
92
const parseArgs = async ( Errors : CliErrors , Args : Args ) => {
94
93
if ( Args . single ( "help" ) ) {
95
- if ( noUnknownParams ( Errors , Args ) ) {
96
- showHelp ( ) ;
94
+ if ( await noUnknownParams ( Errors , Args ) ) {
95
+ await showHelp ( ) ;
97
96
}
98
97
return ;
99
98
}
100
99
101
100
if ( Args . single ( "version" ) ) {
102
- if ( noUnknownParams ( Errors , Args ) ) {
103
- showVersion ( ) ;
101
+ if ( await noUnknownParams ( Errors , Args ) ) {
102
+ console . log ( await getVersion ( ) ) ;
103
+ showCommit ( ) ;
104
104
}
105
105
return ;
106
106
}
107
107
108
108
const expression = Args . single ( "eval" ) ;
109
109
if ( expression ) {
110
- if ( noUnknownParams ( Errors , Args ) ) {
110
+ if ( await noUnknownParams ( Errors , Args ) ) {
111
111
evaluate ( expression ) ;
112
112
}
113
113
return ;
@@ -146,8 +146,8 @@ const parseArgs = async (Errors: CliErrors, Args: Args) => {
146
146
return ;
147
147
}
148
148
149
- if ( noUnknownParams ( Errors , Args ) ) {
150
- showHelp ( ) ;
149
+ if ( await noUnknownParams ( Errors , Args ) ) {
150
+ await showHelp ( ) ;
151
151
}
152
152
} ;
153
153
@@ -228,7 +228,7 @@ const compile = async (
228
228
229
229
const stdlib = createVirtualFileSystem ( "@stdlib" , files ) ;
230
230
231
- if ( noUnknownParams ( Errors , Args ) ) {
231
+ if ( await noUnknownParams ( Errors , Args ) ) {
232
232
// TODO: all flags on the cli should take precedence over flags in the config
233
233
// Make a nice model for it instead of the current mess
234
234
// Consider making overwrites right here or something.
@@ -306,7 +306,10 @@ const setConfigOptions = (config: Config, options: ExtraOptions): void => {
306
306
}
307
307
} ;
308
308
309
- const noUnknownParams = ( Errors : CliErrors , Args : Args ) : boolean => {
309
+ const noUnknownParams = async (
310
+ Errors : CliErrors ,
311
+ Args : Args ,
312
+ ) : Promise < boolean > => {
310
313
const leftoverArgs = Args . leftover ( ) ;
311
314
312
315
if ( leftoverArgs . length === 0 ) {
@@ -316,25 +319,10 @@ const noUnknownParams = (Errors: CliErrors, Args: Args): boolean => {
316
319
for ( const argument of leftoverArgs ) {
317
320
Errors . unexpectedArgument ( argument ) ;
318
321
}
319
- showHelp ( ) ;
322
+ await showHelp ( ) ;
320
323
return false ;
321
324
} ;
322
325
323
- const showVersion = ( ) => {
324
- console . log ( getVersion ( ) ) ;
325
- // if working inside a git repository
326
- // also print the current git commit hash
327
- try {
328
- const gitCommit = execFileSync ( "git" , [ "rev-parse" , "HEAD" ] , {
329
- encoding : "utf8" ,
330
- stdio : [ "ignore" , "pipe" , "ignore" ] ,
331
- } ) . trim ( ) ;
332
- console . log ( `git commit: ${ gitCommit } ` ) ;
333
- } finally {
334
- process . exit ( 0 ) ;
335
- }
336
- } ;
337
-
338
326
const evaluate = ( expression : string ) => {
339
327
const result = parseAndEvalExpression ( expression ) ;
340
328
switch ( result . kind ) {
@@ -348,17 +336,3 @@ const evaluate = (expression: string) => {
348
336
}
349
337
}
350
338
} ;
351
-
352
- const getVersion = ( ) => {
353
- const packageSchema = z . object ( {
354
- version : z . string ( ) ,
355
- } ) ;
356
-
357
- const packageJsonPath = join ( __dirname , ".." , ".." , ".." , "package.json" ) ;
358
-
359
- const pkg = packageSchema . parse (
360
- JSON . parse ( readFileSync ( packageJsonPath , "utf-8" ) ) ,
361
- ) ;
362
-
363
- return pkg . version ;
364
- } ;
0 commit comments