Fix directories resolve

This commit is contained in:
Convly 2022-03-15 11:02:00 +01:00
parent 91d574ffdd
commit 7d52f7cd6d
5 changed files with 40 additions and 8 deletions

View File

@ -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);

View File

@ -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,
});

View File

@ -5,4 +5,4 @@ const strapi = require('../index');
/**
* `$ strapi start`
*/
module.exports = dir => strapi({ dir }).start();
module.exports = distDir => strapi({ distDir }).start();

View File

@ -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,
});

View File

@ -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) {