@@ -3,7 +3,7 @@ import {execute, formatBytes, gzipFile, spawn} from '@junobuild/cli-tools';
33import { generateApi } from '@junobuild/did-tools' ;
44import { red , yellow } from 'kleur' ;
55import { existsSync } from 'node:fs' ;
6- import { copyFile , lstat , mkdir , readFile , rename , writeFile } from 'node:fs/promises' ;
6+ import { copyFile , lstat , mkdir , readFile , rename , rm , writeFile } from 'node:fs/promises' ;
77import { join , relative } from 'node:path' ;
88import ora , { type Ora } from 'ora' ;
99import { compare , minVersion , satisfies } from 'semver' ;
@@ -20,15 +20,16 @@ import {
2020} from '../../../constants/dev.constants' ;
2121import type { BuildArgs , BuildType } from '../../../types/build' ;
2222import {
23+ checkBindgen ,
2324 checkCandidExtractor ,
2425 checkIcWasm ,
25- checkJunoDidc ,
2626 checkWasi2ic
2727} from '../../../utils/build.rust.utils' ;
2828import { readSatelliteDid } from '../../../utils/did.utils' ;
2929import { checkRustVersion } from '../../../utils/env.utils' ;
3030import { formatTime } from '../../../utils/format.utils' ;
3131import { readPackageJson } from '../../../utils/pkg.utils' ;
32+ import { detectPackageManager } from '../../../utils/pm.utils' ;
3233import { readEmulatorConfigAndCreateDeployTargetDir } from '../../emulator/_fs.services' ;
3334import { prepareJunoPkgForSatellite , prepareJunoPkgForSputnik } from './build.metadata.services' ;
3435import { dispatchEmulatorTouchSatellite } from './touch.services' ;
@@ -55,9 +56,9 @@ export const buildRust = async ({
5556 return ;
5657 }
5758
58- const { valid : validDidc } = await checkJunoDidc ( ) ;
59+ const { valid : validBindgen } = await checkBindgen ( ) ;
5960
60- if ( ! validDidc ) {
61+ if ( ! validBindgen ) {
6162 return ;
6263 }
6364
@@ -216,40 +217,36 @@ const didc = async () => {
216217 return ;
217218 }
218219
219- const generate = async ( type : 'js' | 'ts' ) => {
220- const output = satellitedIdl ( type ) ;
220+ const pm = detectPackageManager ( ) ;
221221
222- await spawn ( {
223- command : 'junobuild-didc' ,
224- args : [ '-i' , SATELLITE_CUSTOM_DID_FILE , '-t' , type , '-o' , output ]
225- } ) ;
226-
227- const content = await readFile ( output , 'utf-8' ) ;
228-
229- // Depending on the `tsconfig`, the `factory.did.js` file might be validated.
230- // Cleaning the file prevents errors such as:
231- // TS7031: Binding element 'IDL' implicitly has an 'any' type.
232- const cleanJs = ( content : string ) : string => {
233- const cleanFactory = content . replace (
234- / e x p o r t c o n s t i d l F a c t o r y = \( { I D L } \) = > { / g,
235- `// @ts-expect-error
236- export const idlFactory = ({ IDL }) => {`
237- ) ;
238- return cleanFactory . replace (
239- / e x p o r t c o n s t i n i t = \( { I D L } \) = > { / g,
240- `// @ts-expect-error
241- export const init = ({ IDL }) => {`
242- ) ;
243- } ;
222+ const command = pm === 'npm' || isNullish ( pm ) ? 'npx' : pm ;
244223
245- const cleanedContent = type === 'js' ? cleanJs ( content ) : content ;
224+ // --actor-disabled: skip generating actor files, since we handle those ourselves
225+ // --force: overwrite files. Required; otherwise, icp-bindgen would delete files at preprocess,
226+ // which causes issues when multiple .did files are located in the same folder.
227+ await spawn ( {
228+ command,
229+ args : [
230+ 'icp-bindgen' ,
231+ '--did-file' ,
232+ SATELLITE_CUSTOM_DID_FILE ,
233+ '--out-dir' ,
234+ DEVELOPER_PROJECT_SATELLITE_DECLARATIONS_PATH ,
235+ '--actor-disabled' ,
236+ '--force'
237+ ] ,
238+ silentOut : true
239+ } ) ;
246240
247- await writeFile ( output , `${ AUTO_GENERATED } \n\n${ cleanedContent } ` ) ;
248- } ;
241+ // icp-bindgen generates the files in a `declarations` subfolder
242+ // using a different suffix for JavaScript as the one we used to use.
243+ // That's why we have to post-process the results.
244+ const generatedFolder = join ( DEVELOPER_PROJECT_SATELLITE_DECLARATIONS_PATH , 'declarations' ) ;
249245
250- const promises = ( [ 'js' , 'ts' ] as Array < 'js' | 'ts' > ) . map ( generate ) ;
246+ await rename ( join ( generatedFolder , `${ EXTENSION_DID_FILE_NAME } .d.ts` ) , satellitedIdl ( 'ts' ) ) ;
247+ await rename ( join ( generatedFolder , `${ EXTENSION_DID_FILE_NAME } .js` ) , satellitedIdl ( 'js' ) ) ;
251248
252- await Promise . all ( promises ) ;
249+ await rm ( generatedFolder , { recursive : true , force : true } ) ;
253250} ;
254251
255252const api = async ( ) => {
0 commit comments