Skip to content

Commit a37f91a

Browse files
authored
Merge pull request #187 from pluginpal/feature/171-generate-cli-error
feat: fix generate cli commando with getStrapiApp
2 parents b2fc270 + 9e55b37 commit a37f91a

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

admin/src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export default {
6161
const importedTrads = await Promise.all(
6262
locales.map(async (locale) => {
6363
try {
64-
// eslint-disable-next-line import/no-dynamic-require
64+
// eslint-disable-next-line import/no-dynamic-require, global-require
6565
const data = require(`./translations/${locale}.json`);
6666
return {
6767
data: prefixPluginTranslations(data, pluginId),

server/cli.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,42 @@
22

33
const { Command } = require('commander');
44
const chalk = require('chalk');
5+
const fs = require('fs');
56
const strapi = require('@strapi/strapi'); // eslint-disable-line
67

78
const packageJSON = require('../package.json');
89

910
const 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
1242
program.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

Comments
 (0)