@@ -5,6 +5,7 @@ import upperFirst from "lodash.upperfirst";
55import { DatasetEditorJsonFormat , GestureData } from "../model" ;
66import { getMainScript } from "./generate-main-scripts" ;
77import { getAutogeneratedTs , getDatasetJson } from "./generate-custom-scripts" ;
8+ import { Project } from "@microbit/makecode-embed/react" ;
89
910export const filenames = {
1011 mainTs : "main.ts" ,
@@ -15,14 +16,17 @@ export const filenames = {
1516 readme : "README.md" ,
1617} ;
1718
19+ const extensionName = "machine-learning" ;
20+ const extensionURL = "github:microbit-foundation/pxt-microbit-ml#v0.4.3" ;
21+
1822const pxt = {
1923 name : "Untitled" ,
2024 description : "" ,
2125 dependencies : {
2226 core : "*" ,
2327 microphone : "*" ,
2428 radio : "*" , // Needed to compile.
25- "machine-learning" : "github:microbit-foundation/pxt-microbit-ml#v0.4.3" ,
29+ [ extensionName ] : extensionURL ,
2630 } ,
2731 files : Object . values ( filenames ) ,
2832 preferredEditor : "blocksprj" ,
@@ -56,17 +60,38 @@ export const generateProject = (
5660
5761export const generateCustomFiles = (
5862 gestureState : DatasetEditorJsonFormat ,
59- model : LayersModel | undefined
63+ model : LayersModel | undefined ,
64+ project ?: Project
6065) => {
6166 const { data : gestures } = gestureState ;
6267 const useableGestures = model ? gestures : [ ] ;
63- return {
68+
69+ const customFiles = {
6470 [ filenames . autogenerated ] : model
6571 ? getAutogeneratedTs ( useableGestures , model )
6672 : "" ,
6773 // Save all gestures to dataset.json.
6874 [ filenames . datasetJson ] : getDatasetJson ( gestureState ) ,
6975 } ;
76+
77+ if ( ! project ) {
78+ return customFiles ;
79+ }
80+
81+ const currentPxtJSON = project . text ?. [ filenames . pxtJson ] ;
82+ if ( currentPxtJSON ) {
83+ try {
84+ const updatedPxt = JSON . parse ( currentPxtJSON ) as typeof pxt ;
85+ updatedPxt . dependencies [ extensionName ] = extensionURL ;
86+ return {
87+ ...customFiles ,
88+ [ filenames . pxtJson ] : JSON . stringify ( updatedPxt ) ,
89+ } ;
90+ } catch ( e ) {
91+ // If we reach this case, the project is in theory already very broken
92+ // as it will be missing a pxt file and the extension.
93+ }
94+ }
7095} ;
7196
7297export interface ActionName {
0 commit comments