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,getSecurityConfig:getSecurityConfig,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"),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 r=`${e}/package.json`;let t=readFileSync(r,!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 t.devDependencies[e]}),a||delete t.dependencies["@angular/service-worker"],await writeFile(r,JSON.stringify(t,null,4))},generateSources=async(e,a,r,t,n)=>{console.time("generate app-sources"),await Promise.all([deleteResource(`${a}/src/app/pages`),deleteResource(`${a}/src/app/partials`),deleteResource(`${a}/src/app/prefabs`)]);const o=readFileSync(`${a}/angular.json`);global._WM_PACKAGE_PATH=o.includes("./node_modules/@wavemaker/app-ng-runtime/")?"./node_modules/@wavemaker/app-ng-runtime":"./libraries";const i=await getWmProjectProperties(e),g=await getSecurityConfig(e),p=await getPagesConfig(e),l=await getPrefabConfigsUsedInApp(i,e);let s={};for(const a of l.keys())s[a]=await getPrefabPartialsConfig(e,a);r&&!r.endsWith("/")&&(r+="/"),await Promise.all([generateIndexHtml(e,a,i,r,t),generateWmProjectProperties(i,a),generateAppRoutes(e,i,p,g,a),generateLazyModuleRoutes(p,l,s,a),generatePrefabConfigFile(l,a),generateCodeGenModule(i,p,l,g,a),generateTSConfig(i,a),generateAppJS(e,a),generateAppVariables(e,a,n),generateComponents(e,i,p,l,a,s,n)]);const u=await isDynamicComponentUsedInApp(a),c={aot:!u,buildOptimizer:!u};await generateAppModule(i,a,u,t),await updateAngularJSON(e,a,i,p,l,r,c,s,t),await cleanPackageJSON(a,t),t&&await generatePwaFiles(e,a,i),console.timeEnd("generate app-sources")},createAppSkeleton=async(e,a,r)=>{console.time("create-angular-app"),await createApp(e,a,r),console.timeEnd("create-angular-app")},generatePage=async(e,a,r,t)=>{console.time("generate page",r);const n=getPagesDirPath(e),o=await getWmProjectProperties(e),i=await getPagesConfig(e),g=_.find(i,{name:r}),p="PAGE"===g.type?"pages":"partials";await deleteResource(`${a}/src/app/${p}/${r}`),await generatePagePartialComponent(n,o,g.name,g.type,a,void 0,void 0,t),console.timeEnd("generate page",r)};module.exports={generateSources:generateSources,createAppSkeleton:createAppSkeleton,generatePage:generatePage};