Cleanup code and make test/e2e.js run

This commit is contained in:
Alexandre Bodin 2019-04-23 11:24:23 +02:00
parent 08217f98f6
commit e78f8bbc8c
14 changed files with 98 additions and 14 deletions

View File

@ -5,6 +5,9 @@ const _ = require('lodash');
const findPackagePath = require('../load/package-path');
const loadFiles = require('../load/load-files');
/**
* Loads the apis from the different possible locations
*/
module.exports = async function({ appPath, installedPlugins }) {
const [api, admin, plugins, localPlugins] = await Promise.all([
loadLocalApis(appPath),

View File

@ -7,6 +7,9 @@ const fs = require('fs-extra');
const findPackagePath = require('../load/package-path');
const loadConfig = require('../load/load-config-files');
/**
* Load config files from the different possible locations
*/
module.exports = async ({ appPath, installedPlugins }) => {
const [config, admin, api, plugins, localPlugins] = await Promise.all([
loadAppConfig(appPath),

View File

@ -7,6 +7,9 @@ const loadFiles = require('../load/load-files');
const glob = require('../load/glob');
const filePathToPath = require('../load/filepath-to-prop-path');
/**
* Loads the extensions folder
*/
module.exports = async function({ appPath }) {
const extensionsDir = path.resolve(appPath, 'extensions');

View File

@ -7,6 +7,9 @@ const _ = require('lodash');
const glob = require('../load/glob');
const findPackagePath = require('../load/package-path');
/**
* Load hooks
*/
module.exports = async function({ installedHooks, installedPlugins, appPath }) {
let hooks = {};

View File

@ -23,6 +23,9 @@ const requiredMiddlewares = {
static: 'koa-static',
};
/**
* Load middlewares
*/
module.exports = async function(strapi) {
const { installedMiddlewares, installedPlugins, appPath } = strapi.config;

View File

@ -0,0 +1,40 @@
const filePathToPropPath = require('../filepath-to-prop-path');
const commonCases = [
['./config/test.js', ['config', 'test']],
['./config/test.json', ['config', 'test']],
['./config/test.settings.js', ['config', 'test']],
['./config/test.settings.json', ['config', 'test']],
['config/test.settings.json', ['config', 'test']],
];
describe('filePathToPropPath', () => {
test.each(commonCases)('File %s becomes %p', (input, expected) => {
expect(filePathToPropPath(input)).toEqual(expected);
});
// uses dots to create path
test('Uses dots for key separation', () => {
expect(filePathToPropPath('./config/file.key.js')).toEqual([
'config',
'file',
'key',
]);
expect(filePathToPropPath('./config/file.key.json')).toEqual([
'config',
'file',
'key',
]);
});
// removes the last prop of the path
test('Disable file name key', () => {
expect(filePathToPropPath('./config/test.js', false)).toEqual(['config']);
expect(filePathToPropPath('./config/test.key.js', false)).toEqual([
'config',
'test',
]);
});
});

View File

@ -1,9 +1,16 @@
'use strict';
const path = require('path');
const _ = require('lodash');
module.exports = (fileP, useFileNameAsKey = true) => {
/**
* Returns a path (as an array) from a file path
* @param {string} filePath - a file path
* @param {boolean} useFileNameAsKey - wethear to skip the last path key
*/
module.exports = (filePath, useFileNameAsKey = true) => {
const prop = path
.normalize(fileP)
.normalize(filePath)
.replace(/(\.settings|\.json|\.js)/g, '')
.toLowerCase()
.split('/')

View File

@ -1,5 +1,8 @@
const glob = require('glob');
/**
* Promise based glob
*/
module.exports = (...args) => {
return new Promise((resolve, reject) => {
glob(...args, (err, files) => {

View File

@ -2,11 +2,16 @@ const _ = require('lodash');
const loadFiles = require('./load-files');
const requireFileAndParse = require('./require-file-parse');
module.exports = (dir, pattern = 'config/**/*.+(js|json)') =>
/**
* @param {string} dir - directory from which to load configs
* @param {string} pattern - glob pattern to search for config files
*/
const laodConfigFiles = (dir, pattern = 'config/**/*.+(js|json)') =>
loadFiles(dir, pattern, {
requireFn: requireFileAndParse,
shouldUseFileNameAsKey,
globArgs: {
// used to load .init.json at first startup
dot: true,
},
});
@ -16,6 +21,8 @@ const shouldUseFileNameAsKey = file => {
? true
: false;
};
// files to load with filename key
const prefixedPaths = [
...['staging', 'production', 'development'].reduce((acc, env) => {
return acc.concat(
@ -35,3 +42,5 @@ const prefixedPaths = [
'queries',
'layout',
];
module.exports = laodConfigFiles;

View File

@ -3,7 +3,17 @@ const glob = require('./glob');
const _ = require('lodash');
const filePathToPath = require('./filepath-to-prop-path');
module.exports = async (
/**
* Returns an Object build from a list of files matching a glob pattern in a directory
* It builds a tree structure ressembling the folder structure in dir
* @param {string} dir - Directory to load
* @param {string} pattern - Glob pattern to search for
* @param {Object} options - Options
* @param {Function} options.requireFn - Function that will require the matches files
* @param {Function} options.shouldUseFileNameAsKey - Weather to use the filename as a key in the Object path or not
* @param {Object} options.globArgs - extra glob function arguments
*/
const loadFiles = async (
dir,
pattern,
{
@ -30,3 +40,5 @@ module.exports = async (
return root;
};
module.exports = loadFiles;

View File

@ -1,4 +1,8 @@
const path = require('path');
/**
* Returns the path to a node modules root directory (not the main file path)
* @param {string} moduleName - name of a node module
*/
module.exports = moduleName =>
path.dirname(require.resolve(`${moduleName}/package.json`));

View File

@ -43,16 +43,10 @@ yargs
'$0 <dest>',
'default command',
yargs => {
yargs
.option('run-once', {
default: false,
})
.option('quiet', {
alias: 'q',
default: false,
});
yargs.boolean('run-once').boolean('quiet');
},
argv => {
console.log(argv);
const source = path.resolve(__dirname, '..', 'packages');
const dest = path.resolve(process.cwd(), argv.dest);
watch(source, dest, argv);

View File

@ -11,7 +11,7 @@ const yargs = require('yargs');
const appName = 'testApp';
const databases = {
mongo: `--dbclient=mongo --dbhost=127.0.0.1 --dbport=27017 --dbname=strapi_test --dbusername=root --dbpassword=strapi`,
mongo: '--dbclient=mongo --dbhost=127.0.0.1 --dbport=27017 --dbname=strapi_test --dbusername=root --dbpassword=strapi',
postgres:
'--dbclient=postgres --dbhost=127.0.0.1 --dbport=5432 --dbname=strapi_test --dbusername=strapi --dbpassword=strapi',
mysql:

View File

@ -27,7 +27,7 @@ const cleanTestApp = appName => {
* @param {database} options.database - Arguments to create the testApp with the provided database params
*/
const generateTestApp = async ({ appName, database }) => {
await execa.shell(`node ${STRAPI_BIN} new ${appName} --quickstart ${database}`, {
await execa.shell(`node ${STRAPI_BIN} new ${appName} ${database}`, {
stdio: 'inherit',
});