Init new develop

This commit is contained in:
Convly 2022-03-15 16:13:17 +01:00
parent 9a2b867061
commit 7340836b14
4 changed files with 29 additions and 30 deletions

View File

@ -17,5 +17,5 @@
}, },
// TODO remove the include when we fix the pluginsPath // TODO remove the include when we fix the pluginsPath
"include": ["src/plugins/**/*.json", "./"], "include": ["src/plugins/**/*.json", "./"],
"exclude": ["node_modules/", "dist/", "src/admin", ".cache/", ".tmp/"] "exclude": ["node_modules/", "build/", "dist/", "src/admin", ".cache/", ".tmp/"]
} }

View File

@ -18,17 +18,16 @@ const { buildTypeScript, buildAdmin } = require('./builders');
* *
*/ */
module.exports = async function({ build, watchAdmin, polling, browser }) { module.exports = async function({ build, watchAdmin, polling, browser }) {
const currentDirectory = process.cwd(); const appDir = process.cwd();
const isTSProject = await tsUtils.isTypeScriptProject(currentDirectory); const isTSProject = await tsUtils.isTypeScriptProject(appDir);
const buildDestDir = isTSProject ? path.join(currentDirectory, 'dist') : currentDirectory; const distDir = isTSProject ? path.join(appDir, 'dist') : appDir;
try { try {
if (cluster.isMaster || cluster.isPrimary) { if (cluster.isMaster || cluster.isPrimary) {
return primaryProcess({ return primaryProcess({
buildDestDir, distDir,
currentDirectory, appDir,
dir: buildDestDir,
build, build,
browser, browser,
isTSProject, isTSProject,
@ -37,7 +36,7 @@ module.exports = async function({ build, watchAdmin, polling, browser }) {
} }
if (cluster.isWorker) { if (cluster.isWorker) {
return workerProcess({ dir: buildDestDir, watchAdmin, polling }); return workerProcess({ appDir, distDir, watchAdmin, polling, isTSProject });
} }
} catch (e) { } catch (e) {
console.error(e); console.error(e);
@ -45,32 +44,25 @@ module.exports = async function({ build, watchAdmin, polling, browser }) {
} }
}; };
const primaryProcess = async ({ const primaryProcess = async ({ distDir, appDir, build, isTSProject, watchAdmin, browser }) => {
buildDestDir,
currentDirectory,
build,
isTSProject,
watchAdmin,
browser,
}) => {
if (isTSProject) { if (isTSProject) {
await buildTypeScript({ srcDir: currentDirectory, watch: true }); await buildTypeScript({ srcDir: appDir, watch: false });
} }
const config = loadConfiguration(buildDestDir); const config = loadConfiguration(distDir);
const serveAdminPanel = getOr(true, 'admin.serveAdminPanel')(config); const serveAdminPanel = getOr(true, 'admin.serveAdminPanel')(config);
const buildExists = fs.existsSync(path.join(buildDestDir, 'build')); const buildExists = fs.existsSync(path.join(distDir, 'build'));
// Don't run the build process if the admin is in watch mode // Don't run the build process if the admin is in watch mode
if (build && !watchAdmin && serveAdminPanel && !buildExists) { if (build && !watchAdmin && serveAdminPanel && !buildExists) {
try { try {
await buildAdmin({ await buildAdmin({
buildDestDir, buildDestDir: distDir,
forceBuild: false, forceBuild: false,
isTSProject, isTSProject,
optimization: false, optimization: false,
srcDir: currentDirectory, srcDir: appDir,
}); });
} catch (err) { } catch (err) {
process.exit(1); process.exit(1);
@ -106,19 +98,20 @@ const primaryProcess = async ({
cluster.fork(); cluster.fork();
}; };
const workerProcess = ({ dir, watchAdmin, polling }) => { const workerProcess = ({ appDir, distDir, watchAdmin, polling, isTSProject }) => {
const strapiInstance = strapi({ const strapiInstance = strapi({
distDir: dir, distDir,
autoReload: true, autoReload: true,
serveAdminPanel: watchAdmin ? false : true, serveAdminPanel: watchAdmin ? false : true,
}); });
const adminWatchIgnoreFiles = strapiInstance.config.get('admin.watchIgnoreFiles', []); const adminWatchIgnoreFiles = strapiInstance.config.get('admin.watchIgnoreFiles', []);
watchFileChanges({ watchFileChanges({
dir, appDir,
strapiInstance, strapiInstance,
watchIgnoreFiles: adminWatchIgnoreFiles, watchIgnoreFiles: adminWatchIgnoreFiles,
polling, polling,
isTSProject,
}); });
process.on('message', async message => { process.on('message', async message => {
@ -138,19 +131,24 @@ const workerProcess = ({ dir, watchAdmin, polling }) => {
/** /**
* Init file watching to auto restart strapi app * Init file watching to auto restart strapi app
* @param {Object} options - Options object * @param {Object} options - Options object
* @param {string} options.dir - This is the path where the app is located, the watcher will watch the files under this folder * @param {string} options.appDir - This is the path where the app is located, the watcher will watch the files under this folder
* @param {Strapi} options.strapi - Strapi instance * @param {Strapi} options.strapi - Strapi instance
* @param {array} options.watchIgnoreFiles - Array of custom file paths that should not be watched * @param {array} options.watchIgnoreFiles - Array of custom file paths that should not be watched
*/ */
function watchFileChanges({ dir, strapiInstance, watchIgnoreFiles, polling }) { function watchFileChanges({ appDir, strapiInstance, watchIgnoreFiles, polling, isTSProject }) {
const restart = () => { const restart = async () => {
if (strapiInstance.reload.isWatching && !strapiInstance.reload.isReloading) { if (strapiInstance.reload.isWatching && !strapiInstance.reload.isReloading) {
strapiInstance.reload.isReloading = true; strapiInstance.reload.isReloading = true;
if (isTSProject) {
await tsUtils.compile(appDir);
}
strapiInstance.reload(); strapiInstance.reload();
} }
}; };
const watcher = chokidar.watch(dir, { const watcher = chokidar.watch(appDir, {
ignoreInitial: true, ignoreInitial: true,
usePolling: polling, usePolling: polling,
ignored: [ ignored: [
@ -175,6 +173,7 @@ function watchFileChanges({ dir, strapiInstance, watchIgnoreFiles, polling }) {
joinBy('/', strapiInstance.dirs.public, '**'), joinBy('/', strapiInstance.dirs.public, '**'),
'**/*.db*', '**/*.db*',
'**/exports/**', '**/exports/**',
'**/dist/**',
...watchIgnoreFiles, ...watchIgnoreFiles,
], ],
}); });

View File

@ -16,5 +16,5 @@ module.exports = () => ({
noEmitOnError: true, noEmitOnError: true,
}, },
exclude: ['node_modules/', 'dist/', 'src/admin', 'src/plugins/**/admin'], exclude: ['node_modules/', 'build/', 'dist/', 'src/admin', 'src/plugins/**/admin'],
}); });

View File

@ -4,7 +4,7 @@ const compilers = require('./compilers');
const getConfigPath = require('./utils/get-config-path'); const getConfigPath = require('./utils/get-config-path');
const copyResources = require('./utils/copy-resources'); const copyResources = require('./utils/copy-resources');
module.exports = async (srcDir, { watch = false }) => { module.exports = async (srcDir, { watch = false } = {}) => {
// TODO: Use the Strapi debug logger instead or don't log at all // TODO: Use the Strapi debug logger instead or don't log at all
console.log(`Starting the compilation for TypeScript files in ${srcDir}`); console.log(`Starting the compilation for TypeScript files in ${srcDir}`);