From 77e7efb576098e49a9c1c6b488ef0a6f6f16d27d Mon Sep 17 00:00:00 2001 From: Bassel Kanso Date: Fri, 13 May 2022 15:09:05 +0300 Subject: [PATCH 1/6] Make cli commands use outDir value from tscongfig file instead of static 'dist' --- packages/core/strapi/lib/commands/admin-create.js | 4 ++-- packages/core/strapi/lib/commands/admin-reset.js | 4 ++-- packages/core/strapi/lib/commands/build.js | 4 ++-- .../core/strapi/lib/commands/configurationDump.js | 7 +++---- .../core/strapi/lib/commands/configurationRestore.js | 4 ++-- packages/core/strapi/lib/commands/console.js | 7 +++---- packages/core/strapi/lib/commands/develop.js | 3 ++- packages/core/strapi/lib/commands/routes/list.js | 11 +++++++++-- packages/core/strapi/lib/commands/start.js | 3 ++- packages/core/strapi/lib/commands/watchAdmin.js | 3 ++- 10 files changed, 29 insertions(+), 21 deletions(-) diff --git a/packages/core/strapi/lib/commands/admin-create.js b/packages/core/strapi/lib/commands/admin-create.js index 451c2e766b..af03ef2980 100644 --- a/packages/core/strapi/lib/commands/admin-create.js +++ b/packages/core/strapi/lib/commands/admin-create.js @@ -1,6 +1,5 @@ 'use strict'; -const path = require('path'); const { yup } = require('@strapi/utils'); const _ = require('lodash'); const inquirer = require('inquirer'); @@ -95,6 +94,7 @@ async function createAdmin({ email, password, firstname, lastname }) { const appDir = process.cwd(); const isTSProject = await tsUtils.isUsingTypeScript(appDir); + const compiledDirectoryPath = isTSProject ? tsUtils.resolveConfigOptions(`${appDir}/tsconfig.json`).options?.outDir : null if (isTSProject) await tsUtils.compile(appDir, { @@ -102,7 +102,7 @@ async function createAdmin({ email, password, firstname, lastname }) { configOptions: { options: { incremental: true } }, }); - const distDir = isTSProject ? path.join(appDir, 'dist') : appDir; + const distDir = isTSProject ? compiledDirectoryPath : appDir; const app = await strapi({ appDir, distDir }).load(); diff --git a/packages/core/strapi/lib/commands/admin-reset.js b/packages/core/strapi/lib/commands/admin-reset.js index 4470220ce4..3ec2341280 100644 --- a/packages/core/strapi/lib/commands/admin-reset.js +++ b/packages/core/strapi/lib/commands/admin-reset.js @@ -1,6 +1,5 @@ 'use strict'; -const path = require('path'); const _ = require('lodash'); const inquirer = require('inquirer'); const tsUtils = require('@strapi/typescript-utils'); @@ -47,6 +46,7 @@ async function changePassword({ email, password }) { const appDir = process.cwd(); const isTSProject = await tsUtils.isUsingTypeScript(appDir); + const compiledDirectoryPath = isTSProject ? tsUtils.resolveConfigOptions(`${appDir}/tsconfig.json`).options?.outDir : null if (isTSProject) await tsUtils.compile(appDir, { @@ -54,7 +54,7 @@ async function changePassword({ email, password }) { configOptions: { options: { incremental: true } }, }); - const distDir = isTSProject ? path.join(appDir, 'dist') : appDir; + const distDir = isTSProject ? compiledDirectoryPath : appDir; const app = await strapi({ appDir, distDir }).load(); diff --git a/packages/core/strapi/lib/commands/build.js b/packages/core/strapi/lib/commands/build.js index efa397b224..d00b227007 100644 --- a/packages/core/strapi/lib/commands/build.js +++ b/packages/core/strapi/lib/commands/build.js @@ -1,5 +1,4 @@ 'use strict'; -const path = require('path'); const tsUtils = require('@strapi/typescript-utils'); const { buildAdmin, buildTypeScript } = require('./builders'); @@ -12,13 +11,14 @@ module.exports = async ({ optimization, forceBuild = true }) => { const srcDir = process.cwd(); const useTypeScriptServer = await tsUtils.isUsingTypeScript(srcDir); + const compiledDirectoryPath = useTypeScriptServer ? tsUtils.resolveConfigOptions(`${srcDir}/tsconfig.json`).options?.outDir : null // Typescript if (useTypeScriptServer) { await buildTypeScript({ srcDir, watch: false }); // Update the dir path for the next steps - buildDestDir = path.join(srcDir, 'dist'); + buildDestDir = compiledDirectoryPath; } await buildAdmin({ diff --git a/packages/core/strapi/lib/commands/configurationDump.js b/packages/core/strapi/lib/commands/configurationDump.js index 448794f213..b805a01e1c 100644 --- a/packages/core/strapi/lib/commands/configurationDump.js +++ b/packages/core/strapi/lib/commands/configurationDump.js @@ -1,7 +1,6 @@ 'use strict'; const fs = require('fs'); -const path = require('path'); const tsUtils = require('@strapi/typescript-utils'); const strapi = require('../index'); @@ -17,14 +16,14 @@ module.exports = async function({ file: filePath, pretty }) { const appDir = process.cwd(); const isTSProject = await tsUtils.isUsingTypeScript(appDir); - - if (isTSProject) + const compiledDirectoryPath = isTSProject ? tsUtils.resolveConfigOptions(`${appDir}/tsconfig.json`).options?.outDir : null + if (isTSProject) await tsUtils.compile(appDir, { watch: false, configOptions: { options: { incremental: true } }, }); - const distDir = isTSProject ? path.join(appDir, 'dist') : appDir; + const distDir = isTSProject ? compiledDirectoryPath : appDir; const app = await strapi({ appDir, distDir }).load(); diff --git a/packages/core/strapi/lib/commands/configurationRestore.js b/packages/core/strapi/lib/commands/configurationRestore.js index 6bea42927c..821b75f860 100644 --- a/packages/core/strapi/lib/commands/configurationRestore.js +++ b/packages/core/strapi/lib/commands/configurationRestore.js @@ -1,7 +1,6 @@ 'use strict'; const fs = require('fs'); -const path = require('path'); const _ = require('lodash'); const tsUtils = require('@strapi/typescript-utils'); @@ -18,6 +17,7 @@ module.exports = async function({ file: filePath, strategy = 'replace' }) { const appDir = process.cwd(); const isTSProject = await tsUtils.isUsingTypeScript(appDir); + const compiledDirectoryPath = isTSProject ? tsUtils.resolveConfigOptions(`${appDir}/tsconfig.json`).options?.outDir : null if (isTSProject) await tsUtils.compile(appDir, { @@ -25,7 +25,7 @@ module.exports = async function({ file: filePath, strategy = 'replace' }) { configOptions: { options: { incremental: true } }, }); - const distDir = isTSProject ? path.join(appDir, 'dist') : appDir; + const distDir = isTSProject ? compiledDirectoryPath : appDir; const app = await strapi({ appDir, distDir }).load(); diff --git a/packages/core/strapi/lib/commands/console.js b/packages/core/strapi/lib/commands/console.js index 416c124f7f..d07fe5dc6e 100644 --- a/packages/core/strapi/lib/commands/console.js +++ b/packages/core/strapi/lib/commands/console.js @@ -1,7 +1,6 @@ 'use strict'; const REPL = require('repl'); -const path = require('path'); const tsUtils = require('@strapi/typescript-utils'); const strapi = require('../index'); @@ -12,16 +11,16 @@ const strapi = require('../index'); module.exports = async () => { // Now load up the Strapi framework for real. const appDir = process.cwd(); - const isTSProject = await tsUtils.isUsingTypeScript(appDir); + const compiledDirectoryPath = isTSProject ? tsUtils.resolveConfigOptions(`${appDir}/tsconfig.json`).options?.outDir : undefined - if (isTSProject) + if (isTSProject) await tsUtils.compile(appDir, { watch: false, configOptions: { options: { incremental: true } }, }); - const distDir = isTSProject ? path.join(appDir, 'dist') : appDir; + const distDir = isTSProject ? compiledDirectoryPath : appDir; const app = await strapi({ appDir, distDir }).load(); diff --git a/packages/core/strapi/lib/commands/develop.js b/packages/core/strapi/lib/commands/develop.js index f749447c7d..8f40ce0f86 100644 --- a/packages/core/strapi/lib/commands/develop.js +++ b/packages/core/strapi/lib/commands/develop.js @@ -22,7 +22,8 @@ module.exports = async function({ build, watchAdmin, polling, browser }) { const appDir = process.cwd(); const isTSProject = await tsUtils.isUsingTypeScript(appDir); - const distDir = isTSProject ? path.join(appDir, 'dist') : appDir; + const compiledDirectoryPath = isTSProject ? tsUtils.resolveConfigOptions(`${appDir}/tsconfig.json`).options?.outDir : null + const distDir = isTSProject ? compiledDirectoryPath : appDir; try { if (cluster.isMaster || cluster.isPrimary) { diff --git a/packages/core/strapi/lib/commands/routes/list.js b/packages/core/strapi/lib/commands/routes/list.js index 0c24995c62..de0659b08b 100644 --- a/packages/core/strapi/lib/commands/routes/list.js +++ b/packages/core/strapi/lib/commands/routes/list.js @@ -1,6 +1,5 @@ 'use strict'; -const path = require('path'); const CLITable = require('cli-table3'); const chalk = require('chalk'); const { toUpper } = require('lodash/fp'); @@ -12,7 +11,15 @@ module.exports = async function() { const appDir = process.cwd(); const isTSProject = await tsUtils.isUsingTypeScript(appDir); - const distDir = isTSProject ? path.join(appDir, 'dist') : appDir; + const compiledDirectoryPath = isTSProject ? tsUtils.resolveConfigOptions(`${appDir}/tsconfig.json`).options?.outDir : null + + if (isTSProject) + await tsUtils.compile(appDir, { + watch: false, + configOptions: { options: { incremental: true } }, + }); + + const distDir = isTSProject ? compiledDirectoryPath : appDir; const app = await strapi({ appDir, distDir }).load(); diff --git a/packages/core/strapi/lib/commands/start.js b/packages/core/strapi/lib/commands/start.js index 5ca79e4315..a8b7f39890 100644 --- a/packages/core/strapi/lib/commands/start.js +++ b/packages/core/strapi/lib/commands/start.js @@ -8,7 +8,8 @@ const strapi = require('../index'); module.exports = async specifiedDir => { const appDir = process.cwd(); const isTSProject = await tsUtils.isUsingTypeScript(appDir) - const distDir = isTSProject && !specifiedDir ? 'dist' : specifiedDir; + const compiledDirectoryPath = isTSProject ? tsUtils.resolveConfigOptions(`${appDir}/tsconfig.json`).options?.outDir : null + const distDir = isTSProject && !specifiedDir ? compiledDirectoryPath : specifiedDir; strapi({ distDir }).start() }; diff --git a/packages/core/strapi/lib/commands/watchAdmin.js b/packages/core/strapi/lib/commands/watchAdmin.js index 33510783cd..0cfb0ff8b7 100644 --- a/packages/core/strapi/lib/commands/watchAdmin.js +++ b/packages/core/strapi/lib/commands/watchAdmin.js @@ -13,7 +13,8 @@ module.exports = async function({ browser }) { const currentDirectory = process.cwd(); const isTSProject = await tsUtils.isUsingTypeScript(currentDirectory); - const buildDestDir = isTSProject ? path.join(currentDirectory, 'dist') : currentDirectory; + const compiledDirectoryPath = isTSProject ? tsUtils.resolveConfigOptions(`${currentDirectory}/tsconfig.json`).options?.outDir : null + const buildDestDir = isTSProject ? compiledDirectoryPath : currentDirectory; const strapiInstance = strapi({ distDir: buildDestDir, From b34860f36e682a31b2bb4395b6f7e6a1db1e9460 Mon Sep 17 00:00:00 2001 From: Bassel Kanso Date: Fri, 13 May 2022 15:39:46 +0300 Subject: [PATCH 2/6] fix lint issue --- packages/core/strapi/lib/commands/admin-create.js | 4 +++- packages/core/strapi/lib/commands/admin-reset.js | 4 +++- packages/core/strapi/lib/commands/build.js | 4 +++- .../core/strapi/lib/commands/configurationDump.js | 6 ++++-- .../strapi/lib/commands/configurationRestore.js | 4 +++- packages/core/strapi/lib/commands/console.js | 6 ++++-- packages/core/strapi/lib/commands/develop.js | 4 +++- packages/core/strapi/lib/commands/routes/list.js | 4 +++- packages/core/strapi/lib/commands/start.js | 14 ++++++++------ packages/core/strapi/lib/commands/watchAdmin.js | 5 +++-- 10 files changed, 37 insertions(+), 18 deletions(-) diff --git a/packages/core/strapi/lib/commands/admin-create.js b/packages/core/strapi/lib/commands/admin-create.js index af03ef2980..c1b741a982 100644 --- a/packages/core/strapi/lib/commands/admin-create.js +++ b/packages/core/strapi/lib/commands/admin-create.js @@ -94,7 +94,9 @@ async function createAdmin({ email, password, firstname, lastname }) { const appDir = process.cwd(); const isTSProject = await tsUtils.isUsingTypeScript(appDir); - const compiledDirectoryPath = isTSProject ? tsUtils.resolveConfigOptions(`${appDir}/tsconfig.json`).options?.outDir : null + const compiledDirectoryPath = isTSProject + ? tsUtils.resolveConfigOptions(`${appDir}/tsconfig.json`).options.outDir + : null; if (isTSProject) await tsUtils.compile(appDir, { diff --git a/packages/core/strapi/lib/commands/admin-reset.js b/packages/core/strapi/lib/commands/admin-reset.js index 3ec2341280..013b581fed 100644 --- a/packages/core/strapi/lib/commands/admin-reset.js +++ b/packages/core/strapi/lib/commands/admin-reset.js @@ -46,7 +46,9 @@ async function changePassword({ email, password }) { const appDir = process.cwd(); const isTSProject = await tsUtils.isUsingTypeScript(appDir); - const compiledDirectoryPath = isTSProject ? tsUtils.resolveConfigOptions(`${appDir}/tsconfig.json`).options?.outDir : null + const compiledDirectoryPath = isTSProject + ? tsUtils.resolveConfigOptions(`${appDir}/tsconfig.json`).options.outDir + : null; if (isTSProject) await tsUtils.compile(appDir, { diff --git a/packages/core/strapi/lib/commands/build.js b/packages/core/strapi/lib/commands/build.js index d00b227007..08e45dc72c 100644 --- a/packages/core/strapi/lib/commands/build.js +++ b/packages/core/strapi/lib/commands/build.js @@ -11,7 +11,9 @@ module.exports = async ({ optimization, forceBuild = true }) => { const srcDir = process.cwd(); const useTypeScriptServer = await tsUtils.isUsingTypeScript(srcDir); - const compiledDirectoryPath = useTypeScriptServer ? tsUtils.resolveConfigOptions(`${srcDir}/tsconfig.json`).options?.outDir : null + const compiledDirectoryPath = useTypeScriptServer + ? tsUtils.resolveConfigOptions(`${srcDir}/tsconfig.json`).options.outDir + : null; // Typescript if (useTypeScriptServer) { diff --git a/packages/core/strapi/lib/commands/configurationDump.js b/packages/core/strapi/lib/commands/configurationDump.js index b805a01e1c..c25699e622 100644 --- a/packages/core/strapi/lib/commands/configurationDump.js +++ b/packages/core/strapi/lib/commands/configurationDump.js @@ -16,8 +16,10 @@ module.exports = async function({ file: filePath, pretty }) { const appDir = process.cwd(); const isTSProject = await tsUtils.isUsingTypeScript(appDir); - const compiledDirectoryPath = isTSProject ? tsUtils.resolveConfigOptions(`${appDir}/tsconfig.json`).options?.outDir : null - if (isTSProject) + const compiledDirectoryPath = isTSProject + ? tsUtils.resolveConfigOptions(`${appDir}/tsconfig.json`).options.outDir + : null; + if (isTSProject) await tsUtils.compile(appDir, { watch: false, configOptions: { options: { incremental: true } }, diff --git a/packages/core/strapi/lib/commands/configurationRestore.js b/packages/core/strapi/lib/commands/configurationRestore.js index 821b75f860..5f9dad0937 100644 --- a/packages/core/strapi/lib/commands/configurationRestore.js +++ b/packages/core/strapi/lib/commands/configurationRestore.js @@ -17,7 +17,9 @@ module.exports = async function({ file: filePath, strategy = 'replace' }) { const appDir = process.cwd(); const isTSProject = await tsUtils.isUsingTypeScript(appDir); - const compiledDirectoryPath = isTSProject ? tsUtils.resolveConfigOptions(`${appDir}/tsconfig.json`).options?.outDir : null + const compiledDirectoryPath = isTSProject + ? tsUtils.resolveConfigOptions(`${appDir}/tsconfig.json`).options.outDir + : null; if (isTSProject) await tsUtils.compile(appDir, { diff --git a/packages/core/strapi/lib/commands/console.js b/packages/core/strapi/lib/commands/console.js index d07fe5dc6e..5d76774352 100644 --- a/packages/core/strapi/lib/commands/console.js +++ b/packages/core/strapi/lib/commands/console.js @@ -12,9 +12,11 @@ module.exports = async () => { // Now load up the Strapi framework for real. const appDir = process.cwd(); const isTSProject = await tsUtils.isUsingTypeScript(appDir); - const compiledDirectoryPath = isTSProject ? tsUtils.resolveConfigOptions(`${appDir}/tsconfig.json`).options?.outDir : undefined + const compiledDirectoryPath = isTSProject + ? tsUtils.resolveConfigOptions(`${appDir}/tsconfig.json`).options.outDir + : undefined; - if (isTSProject) + if (isTSProject) await tsUtils.compile(appDir, { watch: false, configOptions: { options: { incremental: true } }, diff --git a/packages/core/strapi/lib/commands/develop.js b/packages/core/strapi/lib/commands/develop.js index 8f40ce0f86..6f50393c8a 100644 --- a/packages/core/strapi/lib/commands/develop.js +++ b/packages/core/strapi/lib/commands/develop.js @@ -22,7 +22,9 @@ module.exports = async function({ build, watchAdmin, polling, browser }) { const appDir = process.cwd(); const isTSProject = await tsUtils.isUsingTypeScript(appDir); - const compiledDirectoryPath = isTSProject ? tsUtils.resolveConfigOptions(`${appDir}/tsconfig.json`).options?.outDir : null + const compiledDirectoryPath = isTSProject + ? tsUtils.resolveConfigOptions(`${appDir}/tsconfig.json`).options.outDir + : null; const distDir = isTSProject ? compiledDirectoryPath : appDir; try { diff --git a/packages/core/strapi/lib/commands/routes/list.js b/packages/core/strapi/lib/commands/routes/list.js index de0659b08b..5ae320c59a 100644 --- a/packages/core/strapi/lib/commands/routes/list.js +++ b/packages/core/strapi/lib/commands/routes/list.js @@ -11,7 +11,9 @@ module.exports = async function() { const appDir = process.cwd(); const isTSProject = await tsUtils.isUsingTypeScript(appDir); - const compiledDirectoryPath = isTSProject ? tsUtils.resolveConfigOptions(`${appDir}/tsconfig.json`).options?.outDir : null + const compiledDirectoryPath = isTSProject + ? tsUtils.resolveConfigOptions(`${appDir}/tsconfig.json`).options.outDir + : null; if (isTSProject) await tsUtils.compile(appDir, { diff --git a/packages/core/strapi/lib/commands/start.js b/packages/core/strapi/lib/commands/start.js index a8b7f39890..f480f9cbee 100644 --- a/packages/core/strapi/lib/commands/start.js +++ b/packages/core/strapi/lib/commands/start.js @@ -1,15 +1,17 @@ 'use strict'; -const tsUtils = require('@strapi/typescript-utils') +const tsUtils = require('@strapi/typescript-utils'); const strapi = require('../index'); /** * `$ strapi start` */ module.exports = async specifiedDir => { - const appDir = process.cwd(); - const isTSProject = await tsUtils.isUsingTypeScript(appDir) - const compiledDirectoryPath = isTSProject ? tsUtils.resolveConfigOptions(`${appDir}/tsconfig.json`).options?.outDir : null - const distDir = isTSProject && !specifiedDir ? compiledDirectoryPath : specifiedDir; + const appDir = process.cwd(); + const isTSProject = await tsUtils.isUsingTypeScript(appDir); + const compiledDirectoryPath = isTSProject + ? tsUtils.resolveConfigOptions(`${appDir}/tsconfig.json`).options.outDir + : null; + const distDir = isTSProject && !specifiedDir ? compiledDirectoryPath : specifiedDir; - strapi({ distDir }).start() + strapi({ distDir }).start(); }; diff --git a/packages/core/strapi/lib/commands/watchAdmin.js b/packages/core/strapi/lib/commands/watchAdmin.js index 0cfb0ff8b7..647e0b7856 100644 --- a/packages/core/strapi/lib/commands/watchAdmin.js +++ b/packages/core/strapi/lib/commands/watchAdmin.js @@ -1,6 +1,5 @@ 'use strict'; -const path = require('path'); const strapiAdmin = require('@strapi/admin'); const tsUtils = require('@strapi/typescript-utils'); const { getConfigUrls, getAbsoluteServerUrl } = require('@strapi/utils'); @@ -13,7 +12,9 @@ module.exports = async function({ browser }) { const currentDirectory = process.cwd(); const isTSProject = await tsUtils.isUsingTypeScript(currentDirectory); - const compiledDirectoryPath = isTSProject ? tsUtils.resolveConfigOptions(`${currentDirectory}/tsconfig.json`).options?.outDir : null + const compiledDirectoryPath = isTSProject + ? tsUtils.resolveConfigOptions(`${currentDirectory}/tsconfig.json`).options.outDir + : null; const buildDestDir = isTSProject ? compiledDirectoryPath : currentDirectory; const strapiInstance = strapi({ From 12c64a2d11bb285537aa9926226183792debe2ac Mon Sep 17 00:00:00 2001 From: Bassel Kanso Date: Tue, 17 May 2022 16:40:09 +0300 Subject: [PATCH 3/6] refactor resolveOutDir in typescript/utils --- .../core/strapi/lib/commands/admin-create.js | 6 ++---- packages/core/strapi/lib/commands/admin-reset.js | 6 ++---- packages/core/strapi/lib/commands/build.js | 6 ++---- .../strapi/lib/commands/configurationDump.js | 6 ++---- .../strapi/lib/commands/configurationRestore.js | 6 ++---- packages/core/strapi/lib/commands/console.js | 6 ++---- packages/core/strapi/lib/commands/develop.js | 6 ++---- packages/core/strapi/lib/commands/routes/list.js | 6 ++---- packages/core/strapi/lib/commands/start.js | 11 +++++++---- packages/core/strapi/lib/commands/watchAdmin.js | 6 ++---- packages/utils/typescript/lib/utils/index.js | 2 ++ .../utils/typescript/lib/utils/resolve-outdir.js | 16 ++++++++++++++++ 12 files changed, 43 insertions(+), 40 deletions(-) create mode 100644 packages/utils/typescript/lib/utils/resolve-outdir.js diff --git a/packages/core/strapi/lib/commands/admin-create.js b/packages/core/strapi/lib/commands/admin-create.js index c1b741a982..1a893050e9 100644 --- a/packages/core/strapi/lib/commands/admin-create.js +++ b/packages/core/strapi/lib/commands/admin-create.js @@ -94,9 +94,7 @@ async function createAdmin({ email, password, firstname, lastname }) { const appDir = process.cwd(); const isTSProject = await tsUtils.isUsingTypeScript(appDir); - const compiledDirectoryPath = isTSProject - ? tsUtils.resolveConfigOptions(`${appDir}/tsconfig.json`).options.outDir - : null; + const outDir = await tsUtils.resolveOutDir(appDir); if (isTSProject) await tsUtils.compile(appDir, { @@ -104,7 +102,7 @@ async function createAdmin({ email, password, firstname, lastname }) { configOptions: { options: { incremental: true } }, }); - const distDir = isTSProject ? compiledDirectoryPath : appDir; + const distDir = isTSProject ? outDir : appDir; const app = await strapi({ appDir, distDir }).load(); diff --git a/packages/core/strapi/lib/commands/admin-reset.js b/packages/core/strapi/lib/commands/admin-reset.js index 013b581fed..3afdd2af4c 100644 --- a/packages/core/strapi/lib/commands/admin-reset.js +++ b/packages/core/strapi/lib/commands/admin-reset.js @@ -46,9 +46,7 @@ async function changePassword({ email, password }) { const appDir = process.cwd(); const isTSProject = await tsUtils.isUsingTypeScript(appDir); - const compiledDirectoryPath = isTSProject - ? tsUtils.resolveConfigOptions(`${appDir}/tsconfig.json`).options.outDir - : null; + const outDir = await tsUtils.resolveOutDir(appDir); if (isTSProject) await tsUtils.compile(appDir, { @@ -56,7 +54,7 @@ async function changePassword({ email, password }) { configOptions: { options: { incremental: true } }, }); - const distDir = isTSProject ? compiledDirectoryPath : appDir; + const distDir = isTSProject ? outDir : appDir; const app = await strapi({ appDir, distDir }).load(); diff --git a/packages/core/strapi/lib/commands/build.js b/packages/core/strapi/lib/commands/build.js index 08e45dc72c..bc1f595903 100644 --- a/packages/core/strapi/lib/commands/build.js +++ b/packages/core/strapi/lib/commands/build.js @@ -11,16 +11,14 @@ module.exports = async ({ optimization, forceBuild = true }) => { const srcDir = process.cwd(); const useTypeScriptServer = await tsUtils.isUsingTypeScript(srcDir); - const compiledDirectoryPath = useTypeScriptServer - ? tsUtils.resolveConfigOptions(`${srcDir}/tsconfig.json`).options.outDir - : null; + const outDir = await tsUtils.resolveOutDir(srcDir); // Typescript if (useTypeScriptServer) { await buildTypeScript({ srcDir, watch: false }); // Update the dir path for the next steps - buildDestDir = compiledDirectoryPath; + buildDestDir = outDir; } await buildAdmin({ diff --git a/packages/core/strapi/lib/commands/configurationDump.js b/packages/core/strapi/lib/commands/configurationDump.js index c25699e622..72258cee83 100644 --- a/packages/core/strapi/lib/commands/configurationDump.js +++ b/packages/core/strapi/lib/commands/configurationDump.js @@ -16,16 +16,14 @@ module.exports = async function({ file: filePath, pretty }) { const appDir = process.cwd(); const isTSProject = await tsUtils.isUsingTypeScript(appDir); - const compiledDirectoryPath = isTSProject - ? tsUtils.resolveConfigOptions(`${appDir}/tsconfig.json`).options.outDir - : null; + const outDir = await tsUtils.resolveOutDir(appDir); if (isTSProject) await tsUtils.compile(appDir, { watch: false, configOptions: { options: { incremental: true } }, }); - const distDir = isTSProject ? compiledDirectoryPath : appDir; + const distDir = isTSProject ? outDir : appDir; const app = await strapi({ appDir, distDir }).load(); diff --git a/packages/core/strapi/lib/commands/configurationRestore.js b/packages/core/strapi/lib/commands/configurationRestore.js index 5f9dad0937..a9f0fa1488 100644 --- a/packages/core/strapi/lib/commands/configurationRestore.js +++ b/packages/core/strapi/lib/commands/configurationRestore.js @@ -17,9 +17,7 @@ module.exports = async function({ file: filePath, strategy = 'replace' }) { const appDir = process.cwd(); const isTSProject = await tsUtils.isUsingTypeScript(appDir); - const compiledDirectoryPath = isTSProject - ? tsUtils.resolveConfigOptions(`${appDir}/tsconfig.json`).options.outDir - : null; + const outDir = await tsUtils.resolveOutDir(appDir); if (isTSProject) await tsUtils.compile(appDir, { @@ -27,7 +25,7 @@ module.exports = async function({ file: filePath, strategy = 'replace' }) { configOptions: { options: { incremental: true } }, }); - const distDir = isTSProject ? compiledDirectoryPath : appDir; + const distDir = isTSProject ? outDir : appDir; const app = await strapi({ appDir, distDir }).load(); diff --git a/packages/core/strapi/lib/commands/console.js b/packages/core/strapi/lib/commands/console.js index 5d76774352..ded57dbf86 100644 --- a/packages/core/strapi/lib/commands/console.js +++ b/packages/core/strapi/lib/commands/console.js @@ -12,9 +12,7 @@ module.exports = async () => { // Now load up the Strapi framework for real. const appDir = process.cwd(); const isTSProject = await tsUtils.isUsingTypeScript(appDir); - const compiledDirectoryPath = isTSProject - ? tsUtils.resolveConfigOptions(`${appDir}/tsconfig.json`).options.outDir - : undefined; + const outDir = await tsUtils.resolveOutDir(appDir); if (isTSProject) await tsUtils.compile(appDir, { @@ -22,7 +20,7 @@ module.exports = async () => { configOptions: { options: { incremental: true } }, }); - const distDir = isTSProject ? compiledDirectoryPath : appDir; + const distDir = isTSProject ? outDir : appDir; const app = await strapi({ appDir, distDir }).load(); diff --git a/packages/core/strapi/lib/commands/develop.js b/packages/core/strapi/lib/commands/develop.js index 6f50393c8a..184c68b8d7 100644 --- a/packages/core/strapi/lib/commands/develop.js +++ b/packages/core/strapi/lib/commands/develop.js @@ -22,10 +22,8 @@ module.exports = async function({ build, watchAdmin, polling, browser }) { const appDir = process.cwd(); const isTSProject = await tsUtils.isUsingTypeScript(appDir); - const compiledDirectoryPath = isTSProject - ? tsUtils.resolveConfigOptions(`${appDir}/tsconfig.json`).options.outDir - : null; - const distDir = isTSProject ? compiledDirectoryPath : appDir; + const outDir = await tsUtils.resolveOutDir(appDir); + const distDir = isTSProject ? outDir : appDir; try { if (cluster.isMaster || cluster.isPrimary) { diff --git a/packages/core/strapi/lib/commands/routes/list.js b/packages/core/strapi/lib/commands/routes/list.js index 5ae320c59a..78369a694c 100644 --- a/packages/core/strapi/lib/commands/routes/list.js +++ b/packages/core/strapi/lib/commands/routes/list.js @@ -11,9 +11,7 @@ module.exports = async function() { const appDir = process.cwd(); const isTSProject = await tsUtils.isUsingTypeScript(appDir); - const compiledDirectoryPath = isTSProject - ? tsUtils.resolveConfigOptions(`${appDir}/tsconfig.json`).options.outDir - : null; + const outDir = await tsUtils.resolveOutDir(appDir); if (isTSProject) await tsUtils.compile(appDir, { @@ -21,7 +19,7 @@ module.exports = async function() { configOptions: { options: { incremental: true } }, }); - const distDir = isTSProject ? compiledDirectoryPath : appDir; + const distDir = isTSProject ? outDir : appDir; const app = await strapi({ appDir, distDir }).load(); diff --git a/packages/core/strapi/lib/commands/start.js b/packages/core/strapi/lib/commands/start.js index f480f9cbee..92cf4a794e 100644 --- a/packages/core/strapi/lib/commands/start.js +++ b/packages/core/strapi/lib/commands/start.js @@ -8,10 +8,13 @@ const strapi = require('../index'); module.exports = async specifiedDir => { const appDir = process.cwd(); const isTSProject = await tsUtils.isUsingTypeScript(appDir); - const compiledDirectoryPath = isTSProject - ? tsUtils.resolveConfigOptions(`${appDir}/tsconfig.json`).options.outDir - : null; - const distDir = isTSProject && !specifiedDir ? compiledDirectoryPath : specifiedDir; + const outDir = await tsUtils.resolveOutDir(appDir); + if (isTSProject) + await tsUtils.compile(appDir, { + watch: false, + configOptions: { options: { incremental: true } }, + }); + const distDir = isTSProject && !specifiedDir ? outDir : specifiedDir; strapi({ distDir }).start(); }; diff --git a/packages/core/strapi/lib/commands/watchAdmin.js b/packages/core/strapi/lib/commands/watchAdmin.js index 647e0b7856..343e46211e 100644 --- a/packages/core/strapi/lib/commands/watchAdmin.js +++ b/packages/core/strapi/lib/commands/watchAdmin.js @@ -12,10 +12,8 @@ module.exports = async function({ browser }) { const currentDirectory = process.cwd(); const isTSProject = await tsUtils.isUsingTypeScript(currentDirectory); - const compiledDirectoryPath = isTSProject - ? tsUtils.resolveConfigOptions(`${currentDirectory}/tsconfig.json`).options.outDir - : null; - const buildDestDir = isTSProject ? compiledDirectoryPath : currentDirectory; + const outDir = await tsUtils.resolveOutDir(currentDirectory); + const buildDestDir = isTSProject ? outDir : currentDirectory; const strapiInstance = strapi({ distDir: buildDestDir, diff --git a/packages/utils/typescript/lib/utils/index.js b/packages/utils/typescript/lib/utils/index.js index c655a8b4a4..ef3dd023ca 100644 --- a/packages/utils/typescript/lib/utils/index.js +++ b/packages/utils/typescript/lib/utils/index.js @@ -6,6 +6,7 @@ const getConfigPath = require('./get-config-path'); const reportDiagnostics = require('./report-diagnostics'); const resolveConfigOptions = require('./resolve-config-options'); const formatHost = require('./format-host'); +const resolveOutDir = require('./resolve-outdir'); module.exports = { isUsingTypeScript, @@ -14,4 +15,5 @@ module.exports = { reportDiagnostics, resolveConfigOptions, formatHost, + resolveOutDir, }; diff --git a/packages/utils/typescript/lib/utils/resolve-outdir.js b/packages/utils/typescript/lib/utils/resolve-outdir.js new file mode 100644 index 0000000000..15b129e59e --- /dev/null +++ b/packages/utils/typescript/lib/utils/resolve-outdir.js @@ -0,0 +1,16 @@ +'use strict'; +const resolveConfigOptions = require('./resolve-config-options'); +const isUsingTypescript = require('./is-using-typescript'); + +const DEFAULT_TS_CONFIG_FILENAME = 'tsconfig.json'; +/** + * Checks if `dir` is a using TypeScript (whether there is a tsconfig file or not) + * @param {string} dir + * @param {string | undefined} configFilename + * @returns {string | undefined} + */ +module.exports = async (dir, configFilename = DEFAULT_TS_CONFIG_FILENAME) => { + return (await isUsingTypescript(dir)) + ? resolveConfigOptions(`${dir}/${configFilename}`).options.outDir + : undefined; +}; From 716e4fae8f543af0e37c77689eea038e5271b7fd Mon Sep 17 00:00:00 2001 From: Bassel Kanso Date: Wed, 18 May 2022 11:06:42 +0300 Subject: [PATCH 4/6] refactor resolveOutDir --- packages/utils/typescript/lib/utils/resolve-outdir.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/utils/typescript/lib/utils/resolve-outdir.js b/packages/utils/typescript/lib/utils/resolve-outdir.js index 15b129e59e..a1cf589af9 100644 --- a/packages/utils/typescript/lib/utils/resolve-outdir.js +++ b/packages/utils/typescript/lib/utils/resolve-outdir.js @@ -1,16 +1,17 @@ 'use strict'; +const path = require('path'); const resolveConfigOptions = require('./resolve-config-options'); const isUsingTypescript = require('./is-using-typescript'); const DEFAULT_TS_CONFIG_FILENAME = 'tsconfig.json'; /** - * Checks if `dir` is a using TypeScript (whether there is a tsconfig file or not) + * Gets the outDir value from config file (tsconfig) * @param {string} dir * @param {string | undefined} configFilename * @returns {string | undefined} */ module.exports = async (dir, configFilename = DEFAULT_TS_CONFIG_FILENAME) => { return (await isUsingTypescript(dir)) - ? resolveConfigOptions(`${dir}/${configFilename}`).options.outDir + ? resolveConfigOptions(path.join(dir, configFilename)).options.outDir : undefined; }; From 0ba862f990b0f3492d5e654899a5c473d0b69790 Mon Sep 17 00:00:00 2001 From: Bassel Kanso Date: Fri, 20 May 2022 15:53:42 +0300 Subject: [PATCH 5/6] throw error incase outDir folder doesn't exist --- packages/core/strapi/lib/commands/start.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/packages/core/strapi/lib/commands/start.js b/packages/core/strapi/lib/commands/start.js index 92cf4a794e..5f9896b81c 100644 --- a/packages/core/strapi/lib/commands/start.js +++ b/packages/core/strapi/lib/commands/start.js @@ -1,4 +1,5 @@ 'use strict'; +const fs = require('fs'); const tsUtils = require('@strapi/typescript-utils'); const strapi = require('../index'); @@ -9,11 +10,8 @@ module.exports = async specifiedDir => { const appDir = process.cwd(); const isTSProject = await tsUtils.isUsingTypeScript(appDir); const outDir = await tsUtils.resolveOutDir(appDir); - if (isTSProject) - await tsUtils.compile(appDir, { - watch: false, - configOptions: { options: { incremental: true } }, - }); + const buildDirExists = fs.existsSync(outDir); + if (isTSProject && !buildDirExists) throw new Error('Please build first to run this command'); const distDir = isTSProject && !specifiedDir ? outDir : specifiedDir; strapi({ distDir }).start(); From 63bc28f9d45ebd093fbe6442b85026a0e40d9809 Mon Sep 17 00:00:00 2001 From: Bassel Kanso Date: Mon, 23 May 2022 16:55:28 +0300 Subject: [PATCH 6/6] change error message --- packages/core/strapi/lib/commands/start.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/strapi/lib/commands/start.js b/packages/core/strapi/lib/commands/start.js index 5f9896b81c..6f4d2a8da2 100644 --- a/packages/core/strapi/lib/commands/start.js +++ b/packages/core/strapi/lib/commands/start.js @@ -11,7 +11,7 @@ module.exports = async specifiedDir => { const isTSProject = await tsUtils.isUsingTypeScript(appDir); const outDir = await tsUtils.resolveOutDir(appDir); const buildDirExists = fs.existsSync(outDir); - if (isTSProject && !buildDirExists) throw new Error('Please build first to run this command'); + if (isTSProject && !buildDirExists) throw new Error(`${outDir} directory not found. Please run the build command before starting your application`); const distDir = isTSProject && !specifiedDir ? outDir : specifiedDir; strapi({ distDir }).start();