@@ -38,7 +38,12 @@ import type { DevsModule } from "@devicescript/vm"
3838import { readFile , writeFile } from "node:fs/promises"
3939import { printDmesg } from "./vmworker"
4040import { EXIT_CODE_COMPILATION_ERROR } from "./exitcodes"
41- import { converters , parseServiceSpecificationMarkdownToJSON } from "jacdac-ts"
41+ import {
42+ converters ,
43+ parseServiceSpecificationMarkdownToJSON ,
44+ versionTryParse ,
45+ } from "jacdac-ts"
46+ import { execSync } from "node:child_process"
4247
4348// TODO should we move this to jacdac-ts and call automatically for transports?
4449export function setupWebsocket ( ) {
@@ -198,6 +203,68 @@ function toDevsDiag(d: jdspec.Diagnostic): DevsDiagnostic {
198203 }
199204}
200205
206+ function execCmd ( cmd : string ) {
207+ try {
208+ return execSync ( cmd , { encoding : "utf-8" } ) . trim ( )
209+ } catch {
210+ return ""
211+ }
212+ }
213+
214+ function compilePackageJson (
215+ tsdir : string ,
216+ entryPoint : string ,
217+ lcfg : LocalBuildConfig ,
218+ errors : DevsDiagnostic [ ]
219+ ) {
220+ const pkgJsonPath = join ( tsdir , "package.json" )
221+ if ( existsSync ( pkgJsonPath ) ) {
222+ const pkgJSON = JSON . parse ( readFileSync ( pkgJsonPath , "utf-8" ) )
223+ lcfg . hwInfo . progName = pkgJSON . name ?? "(no name)"
224+ lcfg . hwInfo . progVersion = pkgJSON . version ?? "(no version)"
225+ const head = execCmd ( "git describe --tags --match 'v[0-9]*' --always" )
226+ let dirty = execCmd (
227+ "git status --porcelain --untracked-file=no --ignore-submodules=untracked"
228+ )
229+ if ( ! head ) dirty = "yes"
230+ const exact = ! dirty && head [ 0 ] == "v" && ! head . includes ( "-" )
231+ if ( exact ) {
232+ lcfg . hwInfo . progVersion = head
233+ } else {
234+ let v = versionTryParse ( lcfg . hwInfo . progVersion )
235+ if ( head [ 0 ] == "v" ) v = versionTryParse ( head ) || v
236+ let verStr = ""
237+ if ( v ) verStr = `v${ v . major } .${ v . minor } .${ v . patch + 1 } -`
238+ verStr += head . replace ( / .* - / , "" )
239+ if ( dirty ) {
240+ const now = new Date ( )
241+ . toISOString ( )
242+ . replace ( / T / , "." )
243+ . replace ( / : / , "." )
244+ . replace ( / : .* / , "" )
245+ . replace ( / - / g, "." )
246+ if ( verStr ) verStr += "-"
247+ verStr += now
248+ }
249+ lcfg . hwInfo . progVersion = verStr
250+ }
251+ }
252+
253+ if ( entryPoint ) {
254+ entryPoint = entryPoint . replace ( / ^ s r c [ \/ \\ ] / , "" )
255+ entryPoint = entryPoint . replace ( / ^ m a i n / , "" )
256+ entryPoint = entryPoint . replace ( / .t s $ / , "" )
257+ if ( entryPoint ) {
258+ if ( lcfg . hwInfo . progName ) lcfg . hwInfo . progName += " " + entryPoint
259+ else lcfg . hwInfo . progName += entryPoint
260+ }
261+ }
262+
263+ verboseLog (
264+ `compile: ${ lcfg . hwInfo . progName } ${ lcfg . hwInfo . progVersion ?? "" } `
265+ )
266+ }
267+
201268function compileServiceSpecs (
202269 tsdir : string ,
203270 lcfg : LocalBuildConfig ,
@@ -295,11 +362,19 @@ export class CompilationError extends Error {
295362 }
296363}
297364
298- export function buildConfigFromDir ( dir : string , options : BuildOptions = { } ) {
299- const lcfg : LocalBuildConfig = { }
365+ export function buildConfigFromDir (
366+ dir : string ,
367+ entryPoint : string = "" ,
368+ options : BuildOptions = { }
369+ ) {
370+ const lcfg : LocalBuildConfig = {
371+ hwInfo : { } ,
372+ }
300373 const errors : DevsDiagnostic [ ] = [ ]
301374
302375 if ( dir ) {
376+ verboseLog ( `build config from: ${ dir } ` )
377+ compilePackageJson ( dir , entryPoint , lcfg , errors )
303378 compileServiceSpecs ( dir , lcfg , errors )
304379 compileBoards ( dir , lcfg , errors )
305380 if ( ! options . quiet )
@@ -330,7 +405,12 @@ export async function compileFile(
330405 ensureDirSync ( options . outDir || BINDIR )
331406
332407 const folder = resolve ( "." )
333- const { errors, buildConfig } = buildConfigFromDir ( folder , options )
408+ const entryPoint = relative ( folder , fn )
409+ const { errors, buildConfig } = buildConfigFromDir (
410+ folder ,
411+ entryPoint ,
412+ options
413+ )
334414 const host = await getHost ( buildConfig , options , folder )
335415
336416 const res = compileWithHost ( fn , host )
0 commit comments