diff --git a/packages/core/strapi/lib/Strapi.js b/packages/core/strapi/lib/Strapi.js index 35a3f539c4..91e05c1cf1 100644 --- a/packages/core/strapi/lib/Strapi.js +++ b/packages/core/strapi/lib/Strapi.js @@ -48,15 +48,40 @@ const LIFECYCLES = { DESTROY: 'destroy', }; +/** + * Resolve the working directories based on the instance options. + * + * Behavior: + * - `appDir` is the directory where Strapi will write every file (schemas, generated APIs, controllers or services) + * - `distDir` is the directory where Strapi will read configurations, schemas and any compiled code + * + * Default values: + * - If `appDir` is `undefined`, it'll be set to `process.cwd()` + * - If `distDir` is `undefined`, it'll be set to `appDir` + */ +const resolveWorkingDirectories = opts => { + const cwd = process.cwd(); + + const appDir = opts.appDir ? path.resolve(cwd, opts.appDir) : cwd; + const distDir = opts.distDir ? path.resolve(cwd, opts.distDir) : appDir; + + return { appDir, distDir }; +}; + class Strapi { constructor(opts = {}) { destroyOnSignal(this); - this.dirs = utils.getDirs({ - appDir: process.cwd(), - distDir: opts.dir ? path.resolve(process.cwd(), opts.dir) : process.cwd(), - }); + + // Create a mapping of every useful directory (both for the app and dist directories) + this.dirs = utils.getDirs(resolveWorkingDirectories(opts)); + + // Load the app configuration from the dist directory const appConfig = loadConfiguration(this.dirs.dist.root, opts); + + // Instanciate the Strapi container this.container = createContainer(this); + + // Register every Strapi registry in the container this.container.register('config', createConfigProvider(appConfig)); this.container.register('content-types', contentTypesRegistry(this)); this.container.register('services', servicesRegistry(this)); @@ -69,10 +94,14 @@ class Strapi { this.container.register('apis', apisRegistry(this)); this.container.register('auth', createAuth(this)); + // Strapi state management variables this.isLoaded = false; this.reload = this.reload(); + + // Instanciate the Koa app & the HTTP server this.server = createServer(this); + // Strapi utils instanciation this.fs = createStrapiFs(this); this.eventHub = createEventHub(); this.startupLogger = createStartupLogger(this); diff --git a/packages/core/strapi/lib/commands/builders/admin.js b/packages/core/strapi/lib/commands/builders/admin.js index 547c698168..bb7cdefa0e 100644 --- a/packages/core/strapi/lib/commands/builders/admin.js +++ b/packages/core/strapi/lib/commands/builders/admin.js @@ -13,7 +13,7 @@ const getEnabledPlugins = require('../../core/loaders/plugins/get-enabled-plugin module.exports = async ({ buildDestDir, forceBuild = true, isTSProject, optimization, srcDir }) => { const strapiInstance = strapi({ // TODO check if this is working @convly - dir: buildDestDir, + distDir: buildDestDir, autoReload: true, serveAdminPanel: false, }); diff --git a/packages/core/strapi/lib/commands/start.js b/packages/core/strapi/lib/commands/start.js index 24daed1e94..802972ebed 100644 --- a/packages/core/strapi/lib/commands/start.js +++ b/packages/core/strapi/lib/commands/start.js @@ -5,4 +5,4 @@ const strapi = require('../index'); /** * `$ strapi start` */ -module.exports = dir => strapi({ dir }).start(); +module.exports = distDir => strapi({ distDir }).start(); diff --git a/packages/core/strapi/lib/commands/watchAdmin.js b/packages/core/strapi/lib/commands/watchAdmin.js index b698ad8fdb..d10e63889d 100644 --- a/packages/core/strapi/lib/commands/watchAdmin.js +++ b/packages/core/strapi/lib/commands/watchAdmin.js @@ -16,7 +16,7 @@ module.exports = async function({ browser }) { const buildDestDir = isTSProject ? path.join(currentDirectory, 'dist') : currentDirectory; const strapiInstance = strapi({ - dir: buildDestDir, + distDir: buildDestDir, autoReload: true, serveAdminPanel: false, }); diff --git a/test/helpers/strapi.js b/test/helpers/strapi.js index 8b41dbde0c..919f89f477 100644 --- a/test/helpers/strapi.js +++ b/test/helpers/strapi.js @@ -24,7 +24,10 @@ const createStrapiInstance = async ({ } = {}) => { // read .env file as it could have been updated dotenv.config({ path: process.env.ENV_PATH }); - const options = { dir: TEST_APP_URL }; + const options = { + appDir: TEST_APP_URL, + distDir: TEST_APP_URL, + }; const instance = strapi(options); if (bypassAuth) {