255 lines
6.6 KiB
JavaScript
Raw Normal View History

'use strict';
2019-09-26 11:52:02 +02:00
/* eslint-disable no-useless-escape */
2019-04-29 15:48:16 +02:00
const path = require('path');
const _ = require('lodash');
2019-04-29 15:48:16 +02:00
const fs = require('fs-extra');
const webpack = require('webpack');
const WebpackDevServer = require('webpack-dev-server');
const chalk = require('chalk');
const getWebpackConfig = require('./webpack.config');
2019-04-29 15:48:16 +02:00
const getPkgPath = name => path.dirname(require.resolve(`${name}/package.json`));
2019-04-29 15:48:16 +02:00
function getCustomWebpackConfig(dir, config) {
const adminConfigPath = path.join(dir, 'admin', 'webpack.config.js');
let webpackConfig = getWebpackConfig(config);
if (fs.existsSync(adminConfigPath)) {
const webpackAdminConfig = require(path.resolve(adminConfigPath));
if (_.isFunction(webpackAdminConfig)) {
webpackConfig = webpackAdminConfig(webpackConfig, webpack);
if (!webpackConfig) {
console.error(
`${chalk.red('Error:')} Nothing was returned from your custom webpack configuration`
);
process.exit(1);
}
}
}
return webpackConfig;
}
async function build({ dir, env, options, optimize }) {
// Create the cache dir containing the front-end files.
await createCacheDir(dir);
const cacheDir = path.resolve(dir, '.cache');
const entry = path.resolve(cacheDir, 'admin', 'src');
const dest = path.resolve(dir, 'build');
// Roots for the @strapi/babel-plugin-switch-ee-ce
const roots = {
eeRoot: path.resolve(cacheDir, 'ee', 'admin'),
ceRoot: path.resolve(cacheDir, 'admin', 'src'),
};
const config = getCustomWebpackConfig(dir, { entry, dest, env, options, optimize, roots });
const compiler = webpack(config);
return new Promise((resolve, reject) => {
compiler.run((err, stats) => {
if (err) {
console.error(err.stack || err);
if (err.details) {
console.error(err.details);
}
return reject(err);
}
const info = stats.toJson();
if (stats.hasErrors()) {
console.error(info.errors);
}
return resolve({
stats,
warnings: info.warnings,
});
});
});
}
2019-10-15 12:05:08 +02:00
async function createPluginsJs(plugins, localPlugins, dest) {
const createPluginsArray = plugins =>
plugins.map(name => {
const shortName = _.camelCase(name.replace(/^@strapi\/plugin-/i, ''));
return { name, shortName };
});
const appPluginsArray = createPluginsArray(plugins);
const localPluginsArray = createPluginsArray(localPlugins);
2019-04-29 15:48:16 +02:00
const content = `
${appPluginsArray
.map(({ name, shortName }) => {
const req = `'../../plugins/${name}/admin/src'`;
return `import ${shortName} from ${req};`;
})
.join('\n')}
${localPluginsArray
.map(({ name, shortName }) => {
const req = `'../../../plugins/${name}/admin/src'`;
return `import ${shortName} from ${req};`;
})
.join('\n')}
const plugins = {
${[...appPluginsArray, ...localPluginsArray]
.map(({ name, shortName }) => {
return ` '${name}': ${shortName},`;
})
.join('\n')}
};
export default plugins;
`;
2019-04-29 15:48:16 +02:00
return fs.writeFile(path.resolve(dest, 'admin', 'src', 'plugins.js'), content);
2019-04-29 15:48:16 +02:00
}
async function clean({ dir }) {
const buildDir = path.join(dir, 'build');
const cacheDir = path.join(dir, '.cache');
fs.removeSync(buildDir);
fs.removeSync(cacheDir);
}
2019-04-29 15:48:16 +02:00
async function copyPlugin(name, dest) {
const pkgFilePath = getPkgPath(name);
const resolveDepPath = (...args) => path.resolve(pkgFilePath, ...args);
const resolveDest = (...args) => path.resolve(dest, 'plugins', name, ...args);
const copy = (...args) => {
return fs.copy(resolveDepPath(...args), resolveDest(...args));
2019-04-29 15:48:16 +02:00
};
// Copy the entire admin folder
await copy('admin');
await copy('package.json');
2019-04-29 15:48:16 +02:00
}
async function copyAdmin(dest) {
2021-04-29 14:20:36 +02:00
const adminPath = getPkgPath('@strapi/admin');
2019-04-29 15:48:16 +02:00
// TODO copy ee folders for plugins
await fs.copy(path.resolve(adminPath, 'ee', 'admin'), path.resolve(dest, 'ee', 'admin'));
2019-04-29 15:48:16 +02:00
await fs.ensureDir(path.resolve(dest, 'config'));
await fs.copy(path.resolve(adminPath, 'admin'), path.resolve(dest, 'admin'));
await fs.copy(
path.resolve(adminPath, 'config', 'layout.js'),
path.resolve(dest, 'config', 'layout.js')
);
// Copy package.json
await fs.copy(path.resolve(adminPath, 'package.json'), path.resolve(dest, 'package.json'));
2019-04-29 15:48:16 +02:00
}
2019-09-23 12:42:17 +02:00
async function createCacheDir(dir) {
2019-04-29 15:48:16 +02:00
const cacheDir = path.resolve(dir, '.cache');
const pkgJSON = require(path.join(dir, 'package.json'));
const pluginsToCopy = Object.keys(pkgJSON.dependencies).filter(
dep =>
2021-04-29 14:20:36 +02:00
dep.startsWith('@strapi/plugin') &&
fs.existsSync(path.resolve(getPkgPath(dep), 'admin', 'src', 'index.js'))
);
2019-10-15 18:33:27 +02:00
let localPluginsToCopy = [];
if (fs.existsSync(path.join(dir, 'plugins'))) {
localPluginsToCopy = fs
.readdirSync(path.join(dir, 'plugins'))
.filter(plugin =>
fs.existsSync(path.resolve(dir, 'plugins', plugin, 'admin', 'src', 'index.js'))
2019-10-15 18:33:27 +02:00
);
}
2019-10-15 12:05:08 +02:00
2019-04-29 15:48:16 +02:00
// create .cache dir
2019-05-15 11:05:34 +02:00
await fs.emptyDir(cacheDir);
2019-04-29 15:48:16 +02:00
// copy admin core code
2019-04-29 15:48:16 +02:00
await copyAdmin(cacheDir);
// copy plugins code
await Promise.all(pluginsToCopy.map(name => copyPlugin(name, cacheDir)));
// Copy app.js
const customAdminConfigFilePath = path.join(dir, 'admin', 'app.js');
if (fs.existsSync(customAdminConfigFilePath)) {
await fs.copy(customAdminConfigFilePath, path.resolve(cacheDir, 'admin', 'src', 'app.js'));
}
// create plugins.js with plugins requires
await createPluginsJs(pluginsToCopy, localPluginsToCopy, cacheDir);
2019-09-23 12:42:17 +02:00
}
async function watchAdmin({ dir, host, port, browser, options }) {
2019-09-26 11:52:02 +02:00
// Create the cache dir containing the front-end files.
await createCacheDir(dir);
const entry = path.join(dir, '.cache', 'admin', 'src');
const dest = path.join(dir, 'build');
const env = 'development';
const cacheDir = path.join(dir, '.cache');
// Roots for the @strapi/babel-plugin-switch-ee-ce
const roots = {
eeRoot: path.resolve(cacheDir, 'ee', 'admin'),
ceRoot: path.resolve(cacheDir, 'admin', 'src'),
};
const args = {
entry,
dest,
env,
port,
options,
roots,
};
const opts = {
clientLogLevel: 'silent',
quiet: true,
open: browser === 'true' ? true : browser,
publicPath: options.adminPath,
historyApiFallback: {
index: options.adminPath,
disableDotRule: true,
},
};
const webpackConfig = getCustomWebpackConfig(dir, args);
const server = new WebpackDevServer(webpack(webpackConfig), opts);
2019-12-12 14:32:37 +01:00
server.listen(port, host, function(err) {
if (err) {
console.log(err);
}
2019-09-24 07:10:02 +02:00
console.log(chalk.green('Starting the development server...'));
console.log();
console.log(chalk.green(`Admin development at http://${host}:${port}${opts.publicPath}`));
});
}
2019-04-29 15:48:16 +02:00
module.exports = {
clean,
2019-04-29 15:48:16 +02:00
build,
2019-09-26 11:52:02 +02:00
watchAdmin,
2019-04-29 15:48:16 +02:00
};