11#!/usr/bin/env node
2- const Wbc = require ( './wbc_lib' ) . Wbc ;
32const path = require ( 'path' ) ;
43const fs = require ( 'fs' ) ;
54const { program } = require ( 'commander' ) ;
65const packageConfig = require ( '../package.json' ) ;
7- const Qjsc = require ( '../index' ) ;
6+ const { compileJavaScriptToWbc , transformInlineScriptToWbc , legacyCompileJavaScriptToKBC } = require ( '../index' ) ;
87
98program
109 . version ( packageConfig . version )
1110 . description ( 'The WBC file generator' )
1211 . requiredOption ( '-s, --source <source>' , 'the Javascript source file path' )
1312 . requiredOption ( '-d, --dist <dist>' , 'the generated bytecode file path' )
1413 . option ( '--legacy_kbc1' , 'generate legacy kbc1 file, (compact with openkraken project)' )
14+ . option ( '--convert-html' , 'Given an HTML string as input, convert all inline JavaScript code to WBC format.' )
1515 . parse ( process . argv ) ;
1616
1717let options = program . opts ( ) ;
@@ -30,21 +30,22 @@ if (!path.isAbsolute(dist)) {
3030 dist = path . join ( process . cwd ( ) , dist ) ;
3131}
3232
33- const qjsc = new Qjsc ( ) ;
34-
3533const sourceFileName = source . split ( '/' ) . slice ( - 1 ) [ 0 ] . split ( '.' ) [ 0 ] ;
36- const sourceCode = fs . readFileSync ( source , { encoding : 'utf-8' } ) ;
37-
38- let buffer = qjsc . compile ( sourceCode ) ;
39-
40- if ( type == 'kbc1' ) {
41- let distPath = path . join ( dist , sourceFileName + '.kbc1' ) ;
42- fs . writeFileSync ( distPath , buffer ) ;
43- console . log ( 'Quickjs bytecode generated kbc1 at: \n' + distPath ) ;
44- } else if ( type == 'wbc' ) {
45- const wbc = new Wbc ( ) ;
46- let wbcBytecode = wbc . generateWbcBytecode ( buffer ) ;
47- let distPath = path . join ( dist , sourceFileName + '.wbc1' ) ;
48- fs . writeFileSync ( distPath , wbcBytecode ) ;
49- console . log ( 'Quickjs bytecode generated wbc1 at: \n' + distPath ) ;
34+ const sourceCode = fs . readFileSync ( source , { encoding : 'utf-8' } ) ;
35+
36+ if ( options . convertHtml ) {
37+ let distPath = path . join ( dist , sourceFileName + '.bhtml' ) ;
38+ const output = transformInlineScriptToWbc ( sourceCode ) ;
39+ fs . writeFileSync ( distPath , output ) ;
40+ console . log ( 'Quickjs bytecode generated at: \n' + distPath ) ;
41+ } else {
42+ if ( type == 'kbc1' ) {
43+ let distPath = path . join ( dist , sourceFileName + '.kbc1' ) ;
44+ fs . writeFileSync ( distPath , legacyCompileJavaScriptToKBC ( sourceCode ) ) ;
45+ console . log ( 'Quickjs bytecode generated kbc1 at: \n' + distPath ) ;
46+ } else if ( type == 'wbc' ) {
47+ let distPath = path . join ( dist , sourceFileName + '.wbc1' ) ;
48+ fs . writeFileSync ( distPath , compileJavaScriptToWbc ( sourceCode ) ) ;
49+ console . log ( 'Quickjs bytecode generated wbc1 at: \n' + distPath ) ;
50+ }
5051}
0 commit comments