import { existsSync , promises as fs , unlinkSync } from "fs" ; import glob from "glob" ; import L from "lodash" ; import * as path from "upath" ; import { PlatformType } from "../lib" ; import { GATSBY_404 } from "../templates/gatsby" ; import { README } from "../templates/readme" ; import { WELCOME_PAGE } from "../templates/welcomePage" ; import { ensure , ensureString } from "./lang-utils" ; import { installUpgrade } from "./npm-utils" ; param searchPattern param skipPatterns export function deleteGlob ( searchPattern , skipPatterns ? ) { const filesToDelete = glob . sync ( searchPattern ) . filter ( ( file ) => ! skipPatterns || ! skipPatterns . find ( ( pattern ) => file . includes ( pattern ) ) ) ; filesToDelete . forEach ( ( f ) => unlinkSync ( f ) ) ; } export function stripExtension ( filename , removeComposedPath = false ) { const ext = removeComposedPath ? filename . substring ( filename . indexOf ( "." ) ) : path . extname ( filename ) ; if ( ! ext || filename === ext ) { return filename ; } return filename . substring ( 0 , filename . lastIndexOf ( ext ) ) ; } export async function writePlasmicLoaderJson ( projectDir , projectId , projectApiToken ) { const plasmicLoaderJson = path . join ( projectDir , "plasmic-loader.json" ) ; const content = { projects : [ { projectId , projectApiToken , } , ] , } ; await fs . writeFile ( plasmicLoaderJson , JSON . stringify ( content ) ) ; } returns export async function overwriteIndex ( projectPath , platform , scheme ) { const isNextjs = platform === "nextjs" ; const isGatsby = platform === "gatsby" ; const isCra = platform === "react" ; const config = await getPlasmicConfig ( projectPath , platform , scheme ) ; const plasmicFiles = L . map ( L . flatMap ( config . projects , ( p ) => p . components ) , ( c ) => c . importSpec . modulePath ) ; const isTypescript = config ?. code ?. lang === "ts" ; const pagesDir = ensure ( isNextjs ? path . join ( projectPath , "pages/" ) : isGatsby ? path . join ( projectPath , "src/pages/" ) : isCra ? path . join ( projectPath , "src/" ) : undefined ) ; const indexBasename = isCra ? `App` : `index` ; const extension = isTypescript ? "tsx" : "jsx" ; const indexAbsPath = path . join ( pagesDir , `${ indexBasename }.${ extension }` ) ; deleteGlob ( path . join ( pagesDir , `${ indexBasename }.*` ) , plasmicFiles . map ( ( f ) => path . basename ( f ) ) ) ; if ( platform === "gatsby" ) { deleteGlob ( path . join ( projectPath , "src/@(pages|components)/*.*" ) , [ ... plasmicFiles . map ( ( f ) => path . basename ( f ) ) , ] ) ; await fs . writeFile ( path . join ( projectPath , "src/pages/404.js" ) , GATSBY_404 ) ; } if ( ( isNextjs || isGatsby ) && plasmicFiles . find ( ( f ) => f . includes ( "/index." ) ) ) { return ; } const homeFilePossibilities = glob . sync ( path . join ( projectPath , ensureString ( config . srcDir ) , "**" , "@(index|Home|home).*" ) ) ; const content = isCra && homeFilePossibilities . length > 0 ? generateHomePage ( homeFilePossibilities [ 0 ] , indexAbsPath ) : generateWelcomePage ( config , platform ) ; await fs . writeFile ( indexAbsPath , content ) ; } param projectPath param platform param buildCommand export async function overwriteReadme ( projectPath , platform , buildCommand ) { const readmeFile = path . join ( projectPath , "README.md" ) ; const contents = README ( platform , buildCommand ) ; await fs . writeFile ( readmeFile , contents ) ; } param componentAbsPath param indexAbsPath returns function generateHomePage ( componentAbsPath , indexAbsPath ) { const componentFilename = path . basename ( componentAbsPath ) ; const componentName = stripExtension ( componentFilename ) ; const componentRelativePath = path . relative ( path . dirname ( indexAbsPath ) , componentAbsPath ) ; const appjsContents = `
import ${ componentName } from './${ stripExtension ( componentRelativePath ) }';

function App() {
  return (<${ componentName } />);
}

export default App;
  ` ; return appjsContents ; } param config param noPages returns function generateWelcomePage ( config , platform ) { let hasPages = false ; let pageComponents ; let pagesDir ; if ( platform !== "react" && config && L . isArray ( config . projects ) ) { pageComponents = L . flatMap ( config . projects , ( p ) => p . components ) . filter ( ( c ) => c . componentType === "page" ) ; pagesDir = config ?. nextjsConfig ?. pagesDir ?? config ?. gatsbyConfig ?. pagesDir ; if ( pageComponents . length > 0 && pagesDir ) { hasPages = true ; } } const getPageSection = ( ) => { const pageLinks = pageComponents . map ( ( pc ) => { const relativePath = path . relative ( pagesDir , pc . importSpec . modulePath ) ; const relativeLink = "/" + stripExtension ( relativePath ) ; if ( platform === "nextjs" ) { return `<li><Link href="${ relativeLink }"><a style={{ color: "blue" }}>${ pc . name } - ${ relativeLink }</a></Link></li>` ; } else { return `<li><a style={{ color: "blue" }} href="${ relativeLink }">${ pc . name } - ${ relativeLink }</a></li>` ; } } ) . join ( "\n" ) ; return `
          <h3>Your pages:</h3>
          <ul>
            ${ pageLinks }
          </ul>
    ` ; } ; const content = WELCOME_PAGE ( hasPages , platform , hasPages ? getPageSection ( ) : "" ) ; return content ; } async function getPlasmicConfig ( projectPath , platform , scheme ) { const isNextjs = platform === "nextjs" ; const isGatsby = platform === "gatsby" ; const isLoader = scheme === "loader" ; const isCodegen = scheme === "codegen" ; const configPath = ensure ( isCodegen ? "plasmic.json" : isNextjs && isLoader ? ".plasmic/plasmic.json" : isGatsby && isLoader ? ".cache/.plasmic/plasmic.json" : undefined ) ; const configStr = await fs . readFile ( path . join ( projectPath , configPath ) ) ; return JSON . parse ( configStr . toString ( ) ) ; } export async function ensureTsconfig ( projectPath ) { const tsconfigPath = path . join ( projectPath , "tsconfig.json" ) ; if ( ! existsSync ( tsconfigPath ) ) { await fs . writeFile ( tsconfigPath , "" ) ; const installTsResult = await installUpgrade ( "typescript @types/react" , { workingDir : projectPath , } ) ; if ( ! installTsResult ) { throw new Error ( "Failed to install Typescript" ) ; } } } export function ifTs ( ts , str ) { return ts ? str : "" ; }