Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: cleanup public interface of strapi/strapi #19931

Merged
merged 1 commit into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions cli-tests/scripts/dts-export.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const {
},
engine: { createTransferEngine },
} = require('@strapi/data-transfer');
const { strapiFactory } = require('@strapi/strapi');
const { createStrapi, compileStrapi } = require('@strapi/strapi');
const path = require('path');

/**
Expand Down Expand Up @@ -77,8 +77,8 @@ const createDestinationProvider = (datasetPath) => {
};

const createStrapiInstance = async (logLevel = 'error') => {
const appContext = await strapiFactory.compile();
const app = strapiFactory(appContext);
const appContext = await compileStrapi();
const app = createStrapi(appContext);

app.log.level = logLevel;
return app.load();
Expand Down
6 changes: 3 additions & 3 deletions e2e/scripts/dts-export.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const {
},
engine: { createTransferEngine },
} = require('@strapi/data-transfer');
const { strapiFactory } = require('@strapi/strapi');
const { createStrapi, compileStrapi } = require('@strapi/strapi');
const { ALLOWED_CONTENT_TYPES } = require('../constants');

/**
Expand Down Expand Up @@ -83,8 +83,8 @@ const createDestinationProvider = (filePath) =>
});

const createStrapiInstance = async (logLevel = 'error') => {
const appContext = await strapiFactory.compile();
const app = strapiFactory(appContext);
const appContext = await compileStrapi();
const app = createStrapi(appContext);

app.log.level = logLevel;
const loadedApp = await app.load();
Expand Down
2 changes: 1 addition & 1 deletion packages/core/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"resources/"
],
"scripts": {
"build": "run pack-up build && run copy-files",
"build": "pack-up build && run copy-files",
"clean": "run -T rimraf ./dist",
"copy-files": "copyfiles -u 1 -a 'src/**/*.html' 'src/**/*.png' dist",
"lint": "run -T eslint .",
Expand Down
22 changes: 2 additions & 20 deletions packages/core/core/src/Strapi.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/* eslint-disable @typescript-eslint/no-namespace */
/* eslint-disable vars-on-top */
/* eslint-disable no-var */
import path from 'path';
import _ from 'lodash';
import { isFunction } from 'lodash/fp';
Expand All @@ -12,7 +9,6 @@ import type { Core, Modules, UID, Schema } from '@strapi/types';
import loadConfiguration from './configuration';

import * as factories from './factories';
import compile from './compile';

import * as utils from './utils';
import * as registries from './registries';
Expand Down Expand Up @@ -625,25 +621,11 @@ class Strapi extends Container implements Core.Strapi {
}
}

interface StrapiOptions {
export interface StrapiOptions {
appDir?: string;
distDir?: string;
autoReload?: boolean;
serveAdminPanel?: boolean;
}

interface Init {
(options?: StrapiOptions): Core.Strapi;
factories: typeof factories;
compile: typeof compile;
}

const initFn = (options: StrapiOptions = {}): Core.Strapi => {
const strapi = new Strapi(options);
global.strapi = strapi as LoadedStrapi;
return strapi;
};

const init: Init = Object.assign(initFn, { factories, compile });

export default init;
export default Strapi;
14 changes: 12 additions & 2 deletions packages/core/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import strapiFactory from './Strapi';
import type { Core } from '@strapi/types';

import Strapi, { type StrapiOptions } from './Strapi';

export { default as compileStrapi } from './compile';
export * as factories from './factories';

export { strapiFactory };
export const createStrapi = (options: StrapiOptions = {}): Core.Strapi => {
const strapi = new Strapi(options);

// TODO: deprecate and remove in next major
global.strapi = strapi as Core.LoadedStrapi;

return strapi;
};
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const transaction = jest.fn(async (cb) => {
await cb({ trx, rollback });
});

const strapiFactory = getStrapiFactory({
const createStrapi = getStrapiFactory({
dirs: {
static: {
public: 'static/public/assets',
Expand Down Expand Up @@ -57,7 +57,7 @@ describe('Local Strapi Destination Provider - Get Assets Stream', () => {

test('Returns a stream when assets restore is true', async () => {
const provider = createLocalStrapiDestinationProvider({
getStrapi: () => strapiFactory(),
getStrapi: () => createStrapi(),
strategy: 'restore',
restore: {
assets: true,
Expand All @@ -72,7 +72,7 @@ describe('Local Strapi Destination Provider - Get Assets Stream', () => {

test('Throw an error if attempting to create stream while restore assets is false', async () => {
const provider = createLocalStrapiDestinationProvider({
getStrapi: () => strapiFactory(),
getStrapi: () => createStrapi(),
strategy: 'restore',
restore: {
assets: false,
Expand All @@ -96,7 +96,7 @@ describe('Local Strapi Destination Provider - Get Assets Stream', () => {
};
const provider = createLocalStrapiDestinationProvider({
getStrapi: () =>
strapiFactory({
createStrapi({
dirs: {
static: {
public: assetsDirectory,
Expand Down
2 changes: 2 additions & 0 deletions packages/core/strapi/packup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ export default defineConfig({
},
{
source: './src/cli/index.ts',
import: './dist/cli/index.ts',
require: './dist/cli/index.js',
types: './dist/cli/index.d.ts',
runtime: 'node',
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@ const mock = {
};

jest.mock('@strapi/core', () => {
const strapiFactory: any = jest.fn(() => mock);
const createStrapi: any = jest.fn(() => mock);
const compileStrapi = jest.fn();

strapiFactory.compile = jest.fn();

return { strapiFactory };
return { createStrapi, compileStrapi };
});

describe('admin:create command', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@ const mock = {
};

jest.mock('@strapi/core', () => {
const strapiFactory = jest.fn(() => mock);
const createStrapi = jest.fn(() => mock);

Object.assign(strapiFactory, {
compile: jest.fn(),
});
const compileStrapi = jest.fn();

return { strapiFactory };
return { createStrapi, compileStrapi };
});

describe('admin:reset-password command', () => {
Expand Down
6 changes: 3 additions & 3 deletions packages/core/strapi/src/cli/commands/admin/create-user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createCommand } from 'commander';
import { yup } from '@strapi/utils';
import _ from 'lodash';
import inquirer from 'inquirer';
import { strapiFactory } from '@strapi/core';
import { createStrapi, compileStrapi } from '@strapi/core';

import { runAction } from '../../utils/helpers';
import type { StrapiCommand } from '../../types';
Expand Down Expand Up @@ -73,8 +73,8 @@ const promptQuestions: inquirer.QuestionCollection<Answers> = [
];

async function createAdmin({ email, password, firstname, lastname }: CmdOptions) {
const appContext = await strapiFactory.compile();
const app = await strapiFactory(appContext).load();
const appContext = await compileStrapi();
const app = await createStrapi(appContext).load();

const user = await app.admin.services.user.exists({ email });

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import _ from 'lodash';
import inquirer from 'inquirer';
import { createCommand } from 'commander';
import { strapiFactory } from '@strapi/core';
import { createStrapi, compileStrapi } from '@strapi/core';

import type { StrapiCommand } from '../../types';
import { runAction } from '../../utils/helpers';
Expand All @@ -28,8 +28,8 @@ const promptQuestions: ReadonlyArray<inquirer.DistinctQuestion<Answers>> = [
];

async function changePassword({ email, password }: CmdOptions) {
const appContext = await strapiFactory.compile();
const app = await strapiFactory(appContext).load();
const appContext = await compileStrapi();
const app = await createStrapi(appContext).load();

await app.admin.services.user.resetPasswordByEmail(email, password);

Expand Down
6 changes: 3 additions & 3 deletions packages/core/strapi/src/cli/commands/components/list.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { createCommand } from 'commander';
import CLITable from 'cli-table3';
import chalk from 'chalk';
import { strapiFactory } from '@strapi/core';
import { createStrapi, compileStrapi } from '@strapi/core';

import type { StrapiCommand } from '../../types';
import { runAction } from '../../utils/helpers';

const action = async () => {
const appContext = await strapiFactory.compile();
const app = await strapiFactory(appContext).register();
const appContext = await compileStrapi();
const app = await createStrapi(appContext).register();

const list = Object.keys(app.components);

Expand Down
6 changes: 3 additions & 3 deletions packages/core/strapi/src/cli/commands/configuration/dump.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from 'fs';
import { createCommand } from 'commander';
import { strapiFactory } from '@strapi/core';
import { createStrapi, compileStrapi } from '@strapi/core';

import type { StrapiCommand } from '../../types';
import { runAction } from '../../utils/helpers';
Expand Down Expand Up @@ -32,8 +32,8 @@ const CHUNK_SIZE = 100;
const action = async ({ file: filePath, pretty }: CmdOptions) => {
const output: Output = filePath ? fs.createWriteStream(filePath) : process.stdout;

const appContext = await strapiFactory.compile();
const app = await strapiFactory(appContext).load();
const appContext = await compileStrapi();
const app = await createStrapi(appContext).load();

const count = await app.query('strapi::core-store').count();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createCommand } from 'commander';
import fs from 'fs';
import _ from 'lodash';
import { strapiFactory } from '@strapi/core';
import { createStrapi, compileStrapi } from '@strapi/core';
import type { Database } from '@strapi/database';

import type { StrapiCommand } from '../../types';
Expand All @@ -20,8 +20,8 @@ interface CmdOptions {
const action = async ({ file: filePath, strategy = 'replace' }: CmdOptions) => {
const input = filePath ? fs.readFileSync(filePath) : await readStdin();

const appContext = await strapiFactory.compile();
const app = await strapiFactory(appContext).load();
const appContext = await compileStrapi();
const app = await createStrapi(appContext).load();

let dataToImport;
try {
Expand Down
6 changes: 3 additions & 3 deletions packages/core/strapi/src/cli/commands/console.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import REPL from 'repl';
import { createCommand } from 'commander';
import { strapiFactory } from '@strapi/core';
import { createStrapi, compileStrapi } from '@strapi/core';

import type { StrapiCommand } from '../types';
import { runAction } from '../utils/helpers';

const action = async () => {
const appContext = await strapiFactory.compile();
const app = await strapiFactory(appContext).load();
const appContext = await compileStrapi();
const app = await createStrapi(appContext).load();

app.start().then(() => {
const repl = REPL.start(app.config.info.name + ' > ' || 'strapi > '); // eslint-disable-line prefer-template
Expand Down
6 changes: 3 additions & 3 deletions packages/core/strapi/src/cli/commands/content-types/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { createCommand } from 'commander';
import CLITable from 'cli-table3';
import chalk from 'chalk';

import { strapiFactory } from '@strapi/core';
import { createStrapi, compileStrapi } from '@strapi/core';

import type { StrapiCommand } from '../../types';
import { runAction } from '../../utils/helpers';

const action = async () => {
const appContext = await strapiFactory.compile();
const app = await strapiFactory(appContext).register();
const appContext = await compileStrapi();
const app = await createStrapi(appContext).register();

const list = app.get('content-types').keys();

Expand Down
6 changes: 3 additions & 3 deletions packages/core/strapi/src/cli/commands/controllers/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { createCommand } from 'commander';
import CLITable from 'cli-table3';
import chalk from 'chalk';

import { strapiFactory } from '@strapi/core';
import { createStrapi, compileStrapi } from '@strapi/core';

import type { StrapiCommand } from '../../types';
import { runAction } from '../../utils/helpers';

const action = async () => {
const appContext = await strapiFactory.compile();
const app = await strapiFactory(appContext).register();
const appContext = await compileStrapi();
const app = await createStrapi(appContext).register();

const list = app.get('controllers').keys();

Expand Down
6 changes: 3 additions & 3 deletions packages/core/strapi/src/cli/commands/hooks/list.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { createCommand } from 'commander';
import CLITable from 'cli-table3';
import chalk from 'chalk';
import { strapiFactory } from '@strapi/core';
import { createStrapi, compileStrapi } from '@strapi/core';

import type { StrapiCommand } from '../../types';
import { runAction } from '../../utils/helpers';

const action = async () => {
const appContext = await strapiFactory.compile();
const app = await strapiFactory(appContext).register();
const appContext = await compileStrapi();
const app = await createStrapi(appContext).register();

const list = app.get('hooks').keys();

Expand Down
6 changes: 3 additions & 3 deletions packages/core/strapi/src/cli/commands/middlewares/list.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { createCommand } from 'commander';
import CLITable from 'cli-table3';
import chalk from 'chalk';
import { strapiFactory } from '@strapi/core';
import { createStrapi, compileStrapi } from '@strapi/core';

import type { StrapiCommand } from '../../types';
import { runAction } from '../../utils/helpers';

const action = async () => {
const appContext = await strapiFactory.compile();
const app = await strapiFactory(appContext).register();
const appContext = await compileStrapi();
const app = await createStrapi(appContext).register();

const list = app.get('middlewares').keys();

Expand Down
6 changes: 3 additions & 3 deletions packages/core/strapi/src/cli/commands/policies/list.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { createCommand } from 'commander';
import CLITable from 'cli-table3';
import chalk from 'chalk';
import { strapiFactory } from '@strapi/core';
import { createStrapi, compileStrapi } from '@strapi/core';

import type { StrapiCommand } from '../../types';
import { runAction } from '../../utils/helpers';

const action = async () => {
const appContext = await strapiFactory.compile();
const app = await strapiFactory(appContext).register();
const appContext = await compileStrapi();
const app = await createStrapi(appContext).register();

const list = app.get('policies').keys();

Expand Down