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