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+
40+
1141// Initial program setup
1242program . storeOptionsAsProperties ( false ) . allowUnknownOption ( true ) ;
1343
@@ -29,7 +59,7 @@ program
2959 . command ( 'generate' )
3060 . description ( 'Generate the sitemap XML' )
3161 . action ( async ( ) => {
32- const app = await strapi ( ) . load ( ) ;
62+ const app = await getStrapiApp ( ) ;
3363
3464 try {
3565 app . plugin ( 'sitemap' ) . service ( 'core' ) . createSitemap ( ) ;
0 commit comments