2021-11-23 12:47:46 +00:00
|
|
|
const debug = require('@tryghost/debug')('card-assets');
|
2021-10-13 08:45:56 +01:00
|
|
|
const Minifier = require('@tryghost/minifier');
|
|
|
|
const _ = require('lodash');
|
|
|
|
const path = require('path');
|
|
|
|
const fs = require('fs').promises;
|
2021-11-15 11:16:23 +00:00
|
|
|
const logging = require('@tryghost/logging');
|
2021-11-19 09:43:48 +00:00
|
|
|
const config = require('../../../shared/config');
|
2021-10-13 08:45:56 +01:00
|
|
|
|
|
|
|
class CardAssetService {
|
|
|
|
constructor(options = {}) {
|
2021-11-19 09:43:48 +00:00
|
|
|
this.src = options.src || path.join(config.get('paths').assetSrc, 'cards');
|
|
|
|
this.dest = options.dest || config.getContentPath('public');
|
2021-10-13 08:45:56 +01:00
|
|
|
this.minifier = new Minifier({src: this.src, dest: this.dest});
|
|
|
|
|
2021-10-19 17:33:20 +01:00
|
|
|
if ('config' in options) {
|
|
|
|
this.config = options.config;
|
|
|
|
}
|
|
|
|
|
2021-10-13 08:45:56 +01:00
|
|
|
this.files = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
generateGlobs() {
|
|
|
|
// CASE: The theme has asked for all card assets to be included by default
|
|
|
|
if (this.config === true) {
|
|
|
|
return {
|
|
|
|
'cards.min.css': 'css/*.css',
|
|
|
|
'cards.min.js': 'js/*.js'
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// CASE: the theme has declared an include directive, we should include exactly these assets
|
|
|
|
// Include rules take precedence over exclude rules.
|
|
|
|
if (_.has(this.config, 'include')) {
|
|
|
|
return {
|
|
|
|
'cards.min.css': `css/(${this.config.include.join('|')}).css`,
|
|
|
|
'cards.min.js': `js/(${this.config.include.join('|')}).js`
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// CASE: the theme has declared an exclude directive, we should include exactly these assets
|
|
|
|
if (_.has(this.config, 'exclude')) {
|
|
|
|
return {
|
|
|
|
'cards.min.css': `css/!(${this.config.exclude.join('|')}).css`,
|
|
|
|
'cards.min.js': `js/!(${this.config.exclude.join('|')}).js`
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// CASE: theme has asked that no assets be included
|
|
|
|
// CASE: we didn't understand config, don't do anything
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
async minify(globs) {
|
2021-11-15 11:16:23 +00:00
|
|
|
try {
|
|
|
|
return await this.minifier.minify(globs);
|
2021-11-23 16:55:54 +00:00
|
|
|
} catch (error) {
|
|
|
|
if (error.code === 'EACCES') {
|
|
|
|
logging.error('Ghost was not able to write card asset files due to permissions.');
|
|
|
|
return;
|
2021-11-15 11:16:23 +00:00
|
|
|
}
|
2021-11-23 16:55:54 +00:00
|
|
|
|
|
|
|
throw error;
|
2021-11-15 11:16:23 +00:00
|
|
|
}
|
2021-10-13 08:45:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async clearFiles() {
|
|
|
|
this.files = [];
|
|
|
|
|
2021-11-23 16:55:54 +00:00
|
|
|
const rmFile = async (name) => {
|
|
|
|
await fs.unlink(path.join(this.dest, name));
|
|
|
|
};
|
2021-10-13 08:45:56 +01:00
|
|
|
|
2021-11-23 16:55:54 +00:00
|
|
|
let promises = [
|
|
|
|
// @deprecated switch this to use fs.rm when we drop support for Node v12
|
|
|
|
rmFile('cards.min.css'),
|
|
|
|
rmFile('cards.min.js')
|
|
|
|
];
|
|
|
|
|
|
|
|
// We don't care if removing these files fails as it's valid for them to not exist
|
|
|
|
return Promise.allSettled(promises);
|
2021-10-13 08:45:56 +01:00
|
|
|
}
|
|
|
|
|
2021-11-04 12:00:33 +00:00
|
|
|
hasFile(type) {
|
|
|
|
return this.files.indexOf(`cards.min.${type}`) > -1;
|
|
|
|
}
|
|
|
|
|
2021-10-13 08:45:56 +01:00
|
|
|
/**
|
|
|
|
* A theme can declare which cards it supports, and we'll do the rest
|
|
|
|
*
|
2021-11-19 09:43:48 +00:00
|
|
|
* @param {Array|boolean} cardAssetConfig
|
2021-10-13 08:45:56 +01:00
|
|
|
* @returns
|
|
|
|
*/
|
2021-11-19 09:43:48 +00:00
|
|
|
async load(cardAssetConfig) {
|
|
|
|
if (cardAssetConfig) {
|
|
|
|
this.config = cardAssetConfig;
|
2021-10-13 08:45:56 +01:00
|
|
|
}
|
|
|
|
|
2021-11-23 12:47:46 +00:00
|
|
|
debug('loading with config', cardAssetConfig);
|
|
|
|
|
2021-10-13 08:45:56 +01:00
|
|
|
await this.clearFiles();
|
|
|
|
|
|
|
|
const globs = this.generateGlobs();
|
|
|
|
|
2021-11-23 12:47:46 +00:00
|
|
|
debug('globs', globs);
|
|
|
|
|
2021-11-15 11:16:23 +00:00
|
|
|
this.files = await this.minify(globs) || [];
|
2021-10-13 08:45:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = CardAssetService;
|