import { INormalizedModules , IModule , ISandbox , ITemplate , } from "codesandbox-import-util-types" ; import denormalize from "../utils/files/denormalize" ; import parseHTML from "./html-parser" ; import { getMainFile , getTemplate } from "./templates" ; interface IDependencies { [ name ] : string ; } function getHTMLInfo ( html ) { if ( ! html ) { return { externalResources : [ ] , file : null } ; } const { externalResources } = parseHTML ( html . content ) ; return { externalResources , file : html } ; } function findMainFile ( directory , mainFile , template ) { if ( directory [ mainFile ] ) { return mainFile ; } if ( directory [ getMainFile ( template ) ] ) { return getMainFile ( template ) ; } if ( directory [ "src/index.js" ] ) { return "src/index.js" ; } if ( directory [ "index.js" ] ) { return "index.js" ; } return mainFile || getMainFile ( template ) ; } export param { Array < Module > } files param { Array < Module > } directories export default async function createSandbox ( directory ) { const packageJson = directory [ "package.json" ] ; if ( packageJson && packageJson . type === "directory" ) { throw new Error ( "package.json is a directory" ) ; } let packageJsonPackage = packageJson ? JSON . parse ( packageJson . content ) : null ; let template = getTemplate ( packageJsonPackage , directory ) ; if ( template === undefined ) { console . log ( "Got undefined template, defaulting to 'create-react-app'" ) ; template = "create-react-app" ; } else { console . log ( `Creating sandbox with template '${ template }'` ) ; } packageJsonPackage = packageJsonPackage || { main : "/index.html" } ; const mainFileUnix = findMainFile ( directory , packageJsonPackage . main , template ) ; const mainFile = process . platform === "win32" ? mainFileUnix . replace ( /\//g , "\\" ) : mainFileUnix ; const { modules , directories } = denormalize ( directory ) ; return { title : packageJsonPackage . title || packageJsonPackage . name , description : packageJsonPackage . description , tags : packageJsonPackage . keywords || [ ] , modules , directories , externalResources : [ ] , template , entry : mainFile , } ; }