225 lines
5.9 KiB
JavaScript
Raw Normal View History

2019-04-29 15:48:16 +02:00
const path = require('path');
const fs = require('fs-extra');
const webpack = require('webpack');
const getWebpackConfig = require('./webpack.config.js');
const WebpackDevServer = require('webpack-dev-server');
2019-04-29 15:48:16 +02:00
const getPkgPath = name =>
path.dirname(require.resolve(`${name}/package.json`));
async function createPluginsJs(plugins, dest) {
2019-04-29 15:48:16 +02:00
const content = `
const injectReducer = require('./utils/injectReducer').default;
const injectSaga = require('./utils/injectSaga').default;
const useInjectReducer = require('./utils/injectReducer').useInjectReducer;
const useInjectSaga = require('./utils/injectSaga').useInjectSaga;
2019-04-29 15:48:16 +02:00
const { languages } = require('./i18n');
window.strapi = Object.assign(window.strapi || {}, {
node: MODE || 'host',
2019-05-15 12:15:08 +02:00
backendURL: BACKEND_URL === '/' ? window.location.origin : BACKEND_URL,
2019-04-29 15:48:16 +02:00
languages,
currentLanguage:
window.localStorage.getItem('strapi-admin-language') ||
window.navigator.language ||
window.navigator.userLanguage ||
'en',
injectReducer,
injectSaga,
useInjectReducer,
useInjectSaga,
2019-04-29 15:48:16 +02:00
});
module.exports = {
${plugins
.map(name => {
const shortName = name.replace(/^strapi-plugin-/i, '');
const req = `require('../../plugins/${name}/admin/src').default`;
return `'${shortName}': ${req}`;
})
.join(',\n')}
}
`;
return fs.writeFile(
path.resolve(dest, 'admin', 'src', 'plugins.js'),
content
);
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');
2019-04-29 15:48:16 +02:00
// Copy the layout.js if it exists
if (await fs.exists(path.resolve(pkgFilePath, 'config', 'layout.js'))) {
await fs.ensureDir(resolveDest('config'));
await copy('config', 'layout.js');
2019-04-29 15:48:16 +02:00
}
await copy('package.json');
2019-04-29 15:48:16 +02:00
}
async function copyAdmin(dest) {
const adminPath = getPkgPath('strapi-admin');
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')
);
}
2019-05-15 11:05:34 +02:00
async function copyCustomAdmin(src, dest) {
await fs.copy(src, path.resolve(dest, 'admin'));
}
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 =>
dep.startsWith('strapi-plugin') &&
fs.existsSync(path.resolve(getPkgPath(dep), 'admin', 'src', 'index.js'))
);
// TODO: add logic to avoid copying files if not necessary
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)));
// create plugins.js with plugins requires
await createPluginsJs(pluginsToCopy, cacheDir);
// override admin code with user customizations
2019-05-15 11:05:34 +02:00
if (fs.pathExistsSync(path.join(dir, 'admin'))) {
await copyCustomAdmin(path.join(dir, 'admin'), cacheDir);
}
2019-09-23 11:50:36 +02:00
// override plugins' admin code with user customizations
const pluginsToOverride = pluginsToCopy.reduce((acc, current) => {
const pluginName = current.replace(/^strapi-plugin-/i, '');
if (fs.pathExistsSync(path.join(dir, 'extensions', pluginName, 'admin'))) {
acc.push(pluginName);
}
return acc;
}, []);
await Promise.all(
pluginsToOverride.map(plugin =>
copyCustomAdmin(
path.join(dir, 'extensions', plugin, 'admin'),
path.join(cacheDir, 'plugins', `strapi-plugin-${plugin}`)
)
)
);
2019-09-23 12:42:17 +02:00
}
async function build({ dir, env, options }) {
const cacheDir = path.resolve(dir, '.cache');
2019-09-23 11:50:36 +02:00
2019-04-29 15:48:16 +02:00
const entry = path.resolve(cacheDir, 'admin', 'src', 'app.js');
const dest = path.resolve(dir, 'build');
2019-04-29 19:04:23 +02:00
const config = getWebpackConfig({ entry, dest, env, options });
2019-04-29 15:48:16 +02:00
const compiler = webpack(config);
return new Promise((resolve, reject) => {
compiler.run((err, stats) => {
let messages;
if (err) {
if (!err.message) {
return reject(err);
}
messages = {
errors: [err.message],
warnings: [],
};
} else {
messages = stats.toJson({ all: false, warnings: true, errors: true });
}
if (messages.errors.length) {
// Only keep the first error. Others are often indicative
// of the same problem, but confuse the reader with noise.
if (messages.errors.length > 1) {
messages.errors.length = 1;
}
return reject(new Error(messages.errors.join('\n\n')));
}
return resolve({
stats,
warnings: messages.warnings,
});
});
2019-04-30 14:47:49 +02:00
});
2019-04-29 15:48:16 +02:00
}
async function watch(dir) {
console.log('Starting the dev web server...');
const port = 8000;
const entry = path.join(dir, '.cache', 'admin', 'src', 'app.js');
const dest = path.join(dir, 'build');
const env = 'development';
const options = {
backend: 'http://localhost:1337',
publicPath: '/admin/',
};
const args = {
entry,
dest,
env,
options,
};
const opts = {
// clientLogLevel: 'none',
hot: true,
quiet: true,
publicPath: '/admin/',
historyApiFallback: {
index: '/admin/',
},
};
const server = new WebpackDevServer(webpack(getWebpackConfig(args)), opts);
server.listen(port, 'localhost', function(err) {
if (err) {
console.log(err);
}
console.log('WebpackDevServer listening at localhost:', port);
});
}
2019-04-29 15:48:16 +02:00
module.exports = {
build,
createPluginsJs,
2019-09-23 12:42:17 +02:00
createCacheDir,
watch,
copyCustomAdmin,
2019-04-29 15:48:16 +02:00
};