0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-20 22:42:53 -05:00

Fixed admin auth asset loading (#15018)

The copyStatic method relies on the dest directory existing, which is
done created the minify method. We've reordered the calls to fix the
issue, but we're still left with the copyStatic method being dependent
on the minify method. Adding the `@private` jsdoc tag removes these
from the public interface, so that the dependency is encapsulated in
this module via the `load` method.

We've also awaited the result of the copyStatic method to avoid
dangling promises.
This commit is contained in:
Fabien 'egg' O'Carroll 2022-07-12 13:25:28 +01:00 committed by GitHub
parent 61e1ee07dc
commit 34ddbcd52e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -7,20 +7,30 @@ const config = require('../../../shared/config');
class AdminAuthAssetsService { class AdminAuthAssetsService {
constructor(options = {}) { constructor(options = {}) {
/** @private */
this.src = options.src || path.join(config.get('paths').assetSrc, 'admin-auth'); this.src = options.src || path.join(config.get('paths').assetSrc, 'admin-auth');
/** @private */
this.dest = options.dest || path.join(config.getContentPath('public'), 'admin-auth'); this.dest = options.dest || path.join(config.getContentPath('public'), 'admin-auth');
/** @private */
this.minifier = new Minifier({src: this.src, dest: this.dest}); this.minifier = new Minifier({src: this.src, dest: this.dest});
} }
/**
* @private
*/
generateGlobs() { generateGlobs() {
return { return {
'admin-auth.min.js': '*.js' 'admin-auth.min.js': '*.js'
}; };
} }
/**
* @private
* @returns {Promise<void>}
*/
async minify(globs) { async minify(globs) {
try { try {
return await this.minifier.minify(globs); await this.minifier.minify(globs);
} catch (error) { } catch (error) {
if (error.code === 'EACCES') { if (error.code === 'EACCES') {
logging.error('Ghost was not able to write admin-auth asset files due to permissions.'); logging.error('Ghost was not able to write admin-auth asset files due to permissions.');
@ -31,9 +41,13 @@ class AdminAuthAssetsService {
} }
} }
/**
* @private
* @returns {Promise<void>}
*/
async copyStatic() { async copyStatic() {
try { try {
return await fs.copyFile(path.join(this.src, 'index.html'), path.join(this.dest, 'index.html')); await fs.copyFile(path.join(this.src, 'index.html'), path.join(this.dest, 'index.html'));
} catch (error) { } catch (error) {
if (error.code === 'EACCES') { if (error.code === 'EACCES') {
logging.error('Ghost was not able to write admin-auth asset files due to permissions.'); logging.error('Ghost was not able to write admin-auth asset files due to permissions.');
@ -44,6 +58,10 @@ class AdminAuthAssetsService {
} }
} }
/**
* @private
* @returns {Promise<void>}
*/
async clearFiles() { async clearFiles() {
const rmFile = async (name) => { const rmFile = async (name) => {
await fs.unlink(path.join(this.dest, name)); await fs.unlink(path.join(this.dest, name));
@ -56,14 +74,19 @@ class AdminAuthAssetsService {
]; ];
// We don't care if removing these files fails as it's valid for them to not exist // We don't care if removing these files fails as it's valid for them to not exist
return Promise.allSettled(promises); await Promise.allSettled(promises);
} }
/**
* Minify, move into the destination directory, and clear existing asset files.
*
* @returns {Promise<void>}
*/
async load() { async load() {
await this.clearFiles();
this.copyStatic();
const globs = this.generateGlobs(); const globs = this.generateGlobs();
await this.clearFiles();
await this.minify(globs); await this.minify(globs);
await this.copyStatic();
} }
} }