22
33const { Command } = require ( 'commander' ) ;
44const chalk = require ( 'chalk' ) ;
5+ const fs = require ( 'fs' ) ;
56const strapi = require ( '@strapi/strapi' ) ; // eslint-disable-line
67
78const packageJSON = require ( '../package.json' ) ;
89
910const program = new Command ( ) ;
1011
12+ const getStrapiApp = async ( ) => {
13+ try {
14+ const tsUtils = require ( '@strapi/typescript-utils' ) ; // eslint-disable-line
15+
16+ const appDir = process . cwd ( ) ;
17+ const isTSProject = await tsUtils . isUsingTypeScript ( appDir ) ;
18+ const outDir = await tsUtils . resolveOutDir ( appDir ) ;
19+ const alreadyCompiled = await fs . existsSync ( outDir ) ;
20+
21+ if ( isTSProject && ! alreadyCompiled ) {
22+ await tsUtils . compile ( appDir , {
23+ watch : false ,
24+ configOptions : { options : { incremental : true } } ,
25+ } ) ;
26+ }
27+
28+ const distDir = isTSProject ? outDir : appDir ;
29+
30+ const app = await strapi ( { appDir, distDir } ) . load ( ) ;
31+
32+ return app ;
33+ } catch ( e ) {
34+ // Fallback for pre Strapi 4.2.
35+ const app = await strapi ( ) . load ( ) ;
36+ return app ;
37+ }
38+ } ;
39+
1140// Initial program setup
1241program . storeOptionsAsProperties ( false ) . allowUnknownOption ( true ) ;
1342
@@ -29,7 +58,7 @@ program
2958 . command ( 'generate' )
3059 . description ( 'Generate the sitemap XML' )
3160 . action ( async ( ) => {
32- const app = await strapi ( ) . load ( ) ;
61+ const app = await getStrapiApp ( ) ;
3362
3463 try {
3564 app . plugin ( 'sitemap' ) . service ( 'core' ) . createSitemap ( ) ;
0 commit comments