@@ -2,26 +2,74 @@ const { execSync } = require("child_process");
22const fs = require ( "fs" ) ;
33const os = require ( "os" ) ;
44const path = require ( "path" ) ;
5+ const { parseArgs } = require ( "node:util" ) ;
6+
7+ // arg parsing
8+ const { positionals : flows , values : args } = parseArgs ( {
9+ options : {
10+ help : {
11+ type : 'boolean' ,
12+ short : 'h' ,
13+ default : false ,
14+ } ,
15+ // Targets where the flow should be published to: only supports 'registry'
16+ target : {
17+ type : 'string' ,
18+ default : '' ,
19+ } ,
20+ } ,
21+ allowPositionals : true ,
22+ } ) ;
23+
24+ if ( args . help ) {
25+ console . log ( `
26+ Usage: node publish-images.js [options] [flows...]
27+
28+ Options:
29+ -h, --help Show this help message
30+ --target TARGET Comma-separated list of publish targets (e.g., 'registry')
31+
32+ Arguments:
33+ flows Optional flow directory names to publish (defaults to all workspaces)
34+
35+ Examples:
36+ node publish-images.js
37+ node publish-images.js --target registry
38+ node publish-images.js flow1 flow2
39+ node publish-images.js --target registry flow1 flow2
40+
41+ Environment Variables:
42+ REGISTRY Container registry URL (default: ghcr.io)
43+ OWNER Registry owner/namespace (default: thin-edge)
44+ ` ) ;
45+ process . exit ( 0 ) ;
46+ }
47+
48+ const publishToRegistry = args . target . split ( "," ) . map ( v => v . trim ( ) ) . includes ( "registry" ) ;
49+ console . log ( `publishToRegistry: ${ publishToRegistry } ` ) ;
550
651const flowFilename = "flow.toml" ;
752const registry = process . env . REGISTRY || "ghcr.io" ;
853const owner = process . env . OWNER || "thin-edge" ;
954
1055let workspaces = "--workspaces" ;
11- if ( process . argv . length > 2 ) {
12- workspaces = process . argv
13- . slice ( 2 )
56+ if ( flows . length > 0 ) {
57+ workspaces = flows
1458 . map ( ( value ) => `-w ${ path . basename ( value ) } ` )
1559 . join ( " " ) ;
1660}
1761
1862const projects = JSON . parse (
19- execSync ( `npm pkg get ${ workspaces } name version module` ) ,
63+ execSync ( `npm pkg get ${ workspaces } name version module tedge ` ) ,
2064) ;
2165
2266// create temp folder to used during packaging
2367const tmpDir = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "flow-builder" ) ) ;
2468
69+ const outputDir = "./dist" ;
70+ fs . rmSync ( outputDir , { recursive : true , force : true } ) ;
71+ fs . mkdirSync ( outputDir , { recursive : true } ) ;
72+
2573for ( const [ _ , project ] of Object . entries ( projects ) ) {
2674 const sourceProjectDir = `flows/${ project . name } ` ;
2775 const projectDir = `${ tmpDir } /${ project . name } ` ;
@@ -37,19 +85,42 @@ for (const [_, project] of Object.entries(projects)) {
3785 fs . writeFileSync (
3886 flowFile ,
3987 [
40- `# name = "${ project . name } "` ,
41- `# version = "${ project . version || "0.0.0" } "\n` ,
88+ `name = "${ project . name } "` ,
89+ `version = "${ project . version || "0.0.0" } "\n` ,
4290 contents ,
4391 ] . join ( "\n" ) ,
4492 ) ;
4593
46- const image = `${ registry } /${ owner } /${ project . name } :${ project . version } ` ;
47- console . log (
48- `\nPublishing flow. project=${ project . name } , version=${ project . version } , module=${ project . module } ` ,
49- ) ;
94+ // Which mapper the flow is designed for
95+ let mapper = "local" ;
96+ if ( project . tedge && project . tedge . mapper ) {
97+ mapper = project . tedge . mapper ;
98+ }
99+ console . info ( "project:" , project ) ;
100+
101+ // Create tar file with flow.toml and built module files, but replace '/' with %2F (url encoding)
102+ const tarFileName = mapper
103+ ? `${ mapper } %2F${ project . name } _${ project . version } .tar.gz`
104+ : `${ project . name } _${ project . version } .tar.gz` ;
105+ const tarFilePath = path . join ( outputDir , tarFileName ) ;
106+ console . log ( `Creating tar file: ${ tarFileName } ` ) ;
107+
50108 execSync (
51- `tedge-oscar flows images push ${ image } --file ${ projectDir } /${ project . module } --file ${ projectDir } /${ flowFilename } --root ${ projectDir } ` ,
109+ `tar -czf ${ tarFilePath } -C ${ projectDir } ${ flowFilename } ${ project . module } ` ,
110+ { stdio : "inherit" , env : { COPYFILE_DISABLE : 1 } } ,
52111 ) ;
112+
113+ console . log ( `Created tar file at: ${ tarFilePath } ` ) ;
114+
115+ if ( publishToRegistry ) {
116+ const image = `${ registry } /${ owner } /${ project . name } :${ project . version } ` ;
117+ console . log (
118+ `\nPublishing flow. image=${ image } , project=${ project . name } , version=${ project . version } , module=${ project . module } ` ,
119+ ) ;
120+ execSync (
121+ `tedge-oscar flows images push ${ image } --file ${ projectDir } /${ project . module } --file ${ projectDir } /${ flowFilename } --root ${ projectDir } ` ,
122+ ) ;
123+ }
53124 }
54125}
55126
0 commit comments