1313 *
1414 * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
1515 ********************************************************************************/
16+ import { execSync } from 'child_process' ;
1617import * as fs from 'fs' ;
1718import * as path from 'path' ;
18- import * as sh from 'shelljs' ;
1919import { extract } from 'tar' ;
20- import * as config from './config.json' ;
21- const serverDirPath = path . resolve ( __dirname , '..' , 'server' ) ;
20+ import * as packageJson from '../../../package.json' ;
2221
23- async function run ( ) {
24- const serverFile = await downloadIfNecessary ( ) ;
25- console . log ( ) ;
22+ const SERVER_DIR_PATH = path . resolve ( __dirname , '..' , 'server' ) ;
23+ const FILE_NAME = 'wf-glsp-server-node' ;
2624
25+ async function run ( ) {
2726 const port = parsePortArg ( ) ;
2827 const host = parseHostArg ( ) ;
28+ const version = parseVersion ( ) ;
29+ process . chdir ( SERVER_DIR_PATH ) ;
30+
31+ const serverFile = await downloadIfNecessary ( version ) ;
32+ console . log ( ) ;
2933
30- sh . cd ( serverDirPath ) ;
31- sh . exec ( `node ${ serverFile } -w --port ${ port } --host ${ host } ` ) ;
34+ execSync ( `node ${ serverFile } -w --port ${ port } --host ${ host } ` , { stdio : 'inherit' } ) ;
3235}
3336
34- async function downloadIfNecessary ( ) : Promise < string > {
35- console . log ( `Check if server executable with version ${ config . version } is present.` ) ;
37+ async function downloadIfNecessary ( version : string ) : Promise < string > {
38+ console . log ( `Check if server executable with version ${ version } is present.` ) ;
3639
37- const existingServer = fs . readdirSync ( serverDirPath ) . find ( file => file . startsWith ( config . fileName ) ) ;
40+ const existingServer = fs . readdirSync ( SERVER_DIR_PATH ) . find ( file => file . startsWith ( FILE_NAME ) ) ;
3841 if ( existingServer ) {
39- const existingVersion = existingServer . replace ( config . fileName + '-' , '' ) . replace ( '.js' , '' ) ;
40- const latestVersion = sh
41- . exec ( `npm show @eclipse-glsp-examples/workflow-server-bundled@ ${ config . version } version` , { silent : true } )
42- . stdout . trim ( ) ;
42+ const existingVersion = existingServer . replace ( FILE_NAME + '-' , '' ) . replace ( '.js' , '' ) ;
43+ const latestVersion = execSync ( `npm show @eclipse-glsp-examples/workflow-server-bundled@ ${ version } version` , {
44+ encoding : 'utf-8'
45+ } ) . trim ( ) ;
4346 if ( existingVersion === latestVersion ) {
4447 console . log ( 'Server executable already present. Skip download"' ) ;
4548 return existingServer ;
@@ -48,32 +51,31 @@ async function downloadIfNecessary(): Promise<string> {
4851
4952 console . log ( 'Server executable with correct version not found. Download from npm.' ) ;
5053 if ( existingServer ) {
51- fs . rmSync ( path . resolve ( serverDirPath , existingServer ) ) ;
52- fs . rmSync ( path . resolve ( serverDirPath , existingServer . replace ( '.js' , '.js.map' ) ) ) ;
54+ fs . rmSync ( path . resolve ( SERVER_DIR_PATH , existingServer ) ) ;
55+ fs . rmSync ( path . resolve ( SERVER_DIR_PATH , existingServer . replace ( '.js' , '.js.map' ) ) ) ;
5356 }
54- sh . cd ( serverDirPath ) ;
55- const packResultJson = sh
56- . exec ( `npm pack @eclipse-glsp-examples/workflow-server-bundled@${ config . version } --json` , { silent : true } )
57- . stdout . trim ( ) ;
58- const version = JSON . parse ( packResultJson ) [ 0 ] . version ;
59- const tarBall = fs . readdirSync ( serverDirPath ) . find ( file => file . endsWith ( '.tar.gz' ) || file . endsWith ( '.tgz' ) ) ! ;
57+ const packResultJson = execSync ( `npm pack @eclipse-glsp-examples/workflow-server-bundled@${ version } --json` , {
58+ encoding : 'utf-8'
59+ } ) . trim ( ) ;
60+ const newVersion = JSON . parse ( packResultJson ) [ 0 ] . version ;
61+ const tarBall = fs . readdirSync ( SERVER_DIR_PATH ) . find ( file => file . endsWith ( '.tar.gz' ) || file . endsWith ( '.tgz' ) ) ! ;
6062 console . log ( 'Extract downloaded server tarball' ) ;
6163 await extract ( {
6264 file : tarBall ,
63- cwd : serverDirPath
65+ cwd : SERVER_DIR_PATH
6466 } ) ;
6567
66- const tempDir = path . resolve ( serverDirPath , 'package' ) ;
67- fs . copyFileSync ( path . resolve ( tempDir , 'wf-glsp-server-node.js' ) , path . resolve ( serverDirPath , `${ config . fileName } -${ version } .js` ) ) ;
68+ const tempDir = path . resolve ( SERVER_DIR_PATH , 'package' ) ;
69+ fs . copyFileSync ( path . resolve ( tempDir , 'wf-glsp-server-node.js' ) , path . resolve ( SERVER_DIR_PATH , `${ FILE_NAME } -${ newVersion } .js` ) ) ;
6870 fs . copyFileSync (
6971 path . resolve ( tempDir , 'wf-glsp-server-node.js.map' ) ,
70- path . resolve ( serverDirPath , `${ config . fileName } -${ version } .js.map` )
72+ path . resolve ( SERVER_DIR_PATH , `${ FILE_NAME } -${ newVersion } .js.map` )
7173 ) ;
7274
7375 console . log ( 'Remove temporary files' ) ;
7476 fs . rmSync ( tempDir , { force : true , recursive : true } ) ;
75- fs . rmSync ( path . resolve ( serverDirPath , tarBall ) , { force : true } ) ;
76- return `${ config . fileName } -${ version } .js` ;
77+ fs . rmSync ( path . resolve ( SERVER_DIR_PATH , tarBall ) , { force : true } ) ;
78+ return `${ FILE_NAME } -${ newVersion } .js` ;
7779}
7880
7981function parsePortArg ( ) : number {
@@ -82,7 +84,7 @@ function parsePortArg(): number {
8284 if ( portIndex >= 0 ) {
8385 port = parseInt ( process . argv [ portIndex + 1 ] ) ;
8486 }
85- if ( isNaN ( port ) ) {
87+ if ( isNaN ( port ) ) {
8688 console . error ( 'Invalid port number' ) ;
8789 process . exit ( 1 ) ;
8890 }
@@ -92,14 +94,23 @@ function parsePortArg(): number {
9294function parseHostArg ( ) : string {
9395 let host = 'localhost' ;
9496 const hostIndex = process . argv . indexOf ( '--host' ) ;
95- if ( hostIndex >= 0 ) {
97+ if ( hostIndex >= 0 ) {
9698 host = process . argv [ hostIndex + 1 ] ;
9799 }
98- if ( typeof host !== 'string' ) {
100+ if ( typeof host !== 'string' ) {
99101 console . error ( 'Invalid host' ) ;
100102 process . exit ( 1 ) ;
101103 }
102104 return host ;
103105}
104106
107+ function parseVersion ( ) : string {
108+ const versionIndex = process . argv . indexOf ( '--version' ) ;
109+ if ( versionIndex >= 0 ) {
110+ return process . argv [ versionIndex + 1 ] ;
111+ }
112+ // If no version is specified, use the version of the current package
113+ return packageJson . version . endsWith ( '-next' ) ? 'next' : packageJson . version ;
114+ }
115+
105116run ( ) ;
0 commit comments