global._WM_APP_PROPERTIES={},global._WM_PACKAGE_PATH="./libraries",global.$=require("jquery"),global._=require("lodash");const cheerio=require("cheerio"),{deleteResource:deleteResource,getIndexHtmlPath:getIndexHtmlPath,getPagesDirPath:getPagesDirPath,readFile:readFile,searchFileByName:searchFileByName,writeFile:writeFile,readFileSync:readFileSync}=require("./wm-utils"),{createAppSkeleton:createApp}=require("./gen-app-skeleton"),{generateAppModule:generateAppModule,generateCodeGenModule:generateCodeGenModule}=require("./gen-app-codegen-module"),{generatePrefabConfigFile:generatePrefabConfigFile}=require("./gen-prefabs"),{generateAppRoutes:generateAppRoutes}=require("./gen-app-routes"),{generateAppJS:generateAppJS}=require("./gen-app-js"),{generateAppVariables:generateAppVariables}=require("./gen-app-variables"),{getWmProjectProperties:getWmProjectProperties,getThemesConfigProperties:getThemesConfigProperties,getSecurityConfig:getSecurityConfig,getLayoutsConfig:getLayoutsConfig,getPagesConfig:getPagesConfig,getPrefabConfigsUsedInApp:getPrefabConfigsUsedInApp,getPrefabPartialsConfig:getPrefabPartialsConfig}=require("./project-meta"),{generateTSConfig:generateTSConfig}=require("./gen-tsconfig"),{generateComponents:generateComponents,generatePagePartialComponent:generatePagePartialComponent}=require("./gen-components"),{generateWmProjectProperties:generateWmProjectProperties}=require("./gen-wm-project-properties"),{generateIndexHtml:generateIndexHtml}=require("./gen-index-html"),{updateAngularJSON:updateAngularJSON}=require("./update-angular-json"),{generatePwaFiles:generatePwaFiles}=require("./gen-pwa-files"),{generateLazyModuleRoutes:generateLazyModuleRoutes}=require("./gen-lazy-module-routes"),{createIntermediateAppSkeleton:createIntermediateAppSkeleton,generateLayoutsAndPages:generateLayoutsAndPages}=require("./gen-layouts"),getMarkupFiles=async e=>{const a=[];return searchFileByName(e+"/src/app",/\.html$/,e=>{a.push(e)}),a},isDynamicComponentUsedInMarkup=async e=>{const a=await readFile(e,"utf8");return cheerio.load(a)("form[wmForm][metadata],form[wmForm][metadata\\.bind],div[wmTable]:not(:has(*))").length>0},isDynamicComponentUsedInApp=async e=>{const a=await getMarkupFiles(e);return(await Promise.all(a.map(e=>isDynamicComponentUsedInMarkup(e)))).find(e=>e)},cleanPackageJSON=async(e,a)=>{const t=`${e}/package.json`;let r=readFileSync(t,!0);["@ampproject/rollup-plugin-closure-compiler","rollup","rollup-plugin-alias","rollup-plugin-commonjs","rollup-plugin-includepaths","rollup-plugin-multi-entry","rollup-plugin-node-resolve"].forEach(e=>{delete r.devDependencies[e]}),a||delete r.dependencies["@angular/service-worker"],await writeFile(t,JSON.stringify(r,null,4))},generateIntermediateApp=async(e,a)=>{console.time("create-intermediate-app"),await createIntermediateAppSkeleton(e,a),await generateLayoutsAndPages(a),console.timeEnd("create-intermediate-app")},generateSources=async(e,a,t,r,n,o)=>{console.time("generate app-sources"),await Promise.all([deleteResource(`${a}/src/app/layouts`),deleteResource(`${a}/src/app/pages`),deleteResource(`${a}/src/app/partials`),deleteResource(`${a}/src/app/prefabs`)]);const i=readFileSync(`${a}/angular.json`);global._WM_PACKAGE_PATH=i.includes("./node_modules/@wavemaker/app-ng-runtime/")?"./node_modules/@wavemaker/app-ng-runtime":"./libraries";const g=await getWmProjectProperties(e),p=await getThemesConfigProperties(e),s=await getSecurityConfig(e);let l;o&&(l=await getLayoutsConfig(e));const u=await getPagesConfig(e),c=await getPrefabConfigsUsedInApp(g,e);let d={};for(const a of c.keys())d[a]=await getPrefabPartialsConfig(e,a);t&&!t.endsWith("/")&&(t+="/"),await Promise.all([generateIndexHtml(e,a,g,t,r),generateWmProjectProperties(g,a),generateAppRoutes(e,g,l,u,s,a,o),generateLazyModuleRoutes(u,c,d,a),generatePrefabConfigFile(c,a),generateCodeGenModule(g,u,c,s,a),generateTSConfig(g,a),generateAppJS(e,a),generateAppVariables(e,a,n),generateComponents(e,g,l,u,s,c,a,d,n,o)]);const m=await isDynamicComponentUsedInApp(a),P={aot:!m,buildOptimizer:!m};await generateAppModule(g,a,m,r),await updateAngularJSON(e,a,g,p,u,c,t,P,d,r),await cleanPackageJSON(a,r),r&&await generatePwaFiles(e,a,g),console.timeEnd("generate app-sources")},createAppSkeleton=async(e,a,t)=>{console.time("create-angular-app"),await createApp(e,a,t),console.timeEnd("create-angular-app")},generatePage=async(e,a,t,r)=>{console.time("generate page",t);const n=getPagesDirPath(e),o=await getWmProjectProperties(e),i=await getPagesConfig(e),g=_.find(i,{name:t}),p="PAGE"===g.type?"pages":"partials";await deleteResource(`${a}/src/app/${p}/${t}`),await generatePagePartialComponent(n,o,g.name,g.type,a,void 0,void 0,r),console.timeEnd("generate page",t)};module.exports={generateIntermediateApp:generateIntermediateApp,generateSources:generateSources,createAppSkeleton:createAppSkeleton,generatePage:generatePage};