-
Notifications
You must be signed in to change notification settings - Fork 142
Structurally refactor Site class into Facade and separate Managers #2775
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
e7ce1d0
Refactor structurally Site class into Facade and Managers
gerteck fea1102
Rename dev to isDevMode for better naming
gerteck ab5db8c
Add SiteAssetsManager class in own file
gerteck 9a22f73
Shift Site testcase to `/Site`
gerteck 3f72158
Add SiteAssetsManager testcases
gerteck 98a5c45
Shift SitePagesManager to separate file
gerteck eab0d2d
Add SitePagesManager testcases
gerteck afb2630
Shift SiteDeployManager to separate file
gerteck 5e317e9
Add SiteDeployManager testcases
gerteck 5872fa8
Shift SiteGenerationManager to separate file
gerteck 06c7970
Add SiteGenerationManager testcases
gerteck 60cf466
Fix slight lints and nits
gerteck a7b2c45
Refactor Site facade to strictly expose only public interfaces
gerteck cb0bdec
Clean up testcase comments
gerteck d1e6040
Add basic Site main interface functional tests
gerteck a1c8dda
Fix bug: Restore interfaces used in cli package from Site object
gerteck fcd8cd9
Fix site config codeTheme style validation
gerteck 1b32557
Update logger info message with space
gerteck 757a862
Add `core` package Site functional tests
gerteck f61f672
Refactor code smells
gerteck abcd454
Fix malformed newlined test output
gerteck fd1a7e6
Update testcase and console logging
gerteck 8d1ca19
Revert package.json changes
gerteck File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,218 @@ | ||
| import fs from 'fs-extra'; | ||
| import ignore, { Ignore } from 'ignore'; | ||
| import path from 'path'; | ||
| import walkSync from 'walk-sync'; | ||
| import Bluebird from 'bluebird'; | ||
|
|
||
| import { SiteConfig, SiteConfigStyle } from './SiteConfig'; | ||
| import { delay } from '../utils/delay'; | ||
| import * as logger from '../utils/logger'; | ||
| import { TEMPLATE_SITE_ASSET_FOLDER_NAME, _ } from './constants'; | ||
|
|
||
| function getBootswatchThemePath(theme: string) { | ||
| return require.resolve(`bootswatch/dist/${theme}/bootstrap.min.css`); | ||
| } | ||
|
|
||
| const SUPPORTED_THEMES_PATHS: Record<string, string> = { | ||
| 'bootswatch-cerulean': getBootswatchThemePath('cerulean'), | ||
| 'bootswatch-cosmo': getBootswatchThemePath('cosmo'), | ||
| 'bootswatch-flatly': getBootswatchThemePath('flatly'), | ||
| 'bootswatch-journal': getBootswatchThemePath('journal'), | ||
| 'bootswatch-litera': getBootswatchThemePath('litera'), | ||
| 'bootswatch-lumen': getBootswatchThemePath('lumen'), | ||
| 'bootswatch-lux': getBootswatchThemePath('lux'), | ||
| 'bootswatch-materia': getBootswatchThemePath('materia'), | ||
| 'bootswatch-minty': getBootswatchThemePath('minty'), | ||
| 'bootswatch-pulse': getBootswatchThemePath('pulse'), | ||
| 'bootswatch-sandstone': getBootswatchThemePath('sandstone'), | ||
| 'bootswatch-simplex': getBootswatchThemePath('simplex'), | ||
| 'bootswatch-sketchy': getBootswatchThemePath('sketchy'), | ||
| 'bootswatch-spacelab': getBootswatchThemePath('spacelab'), | ||
| 'bootswatch-united': getBootswatchThemePath('united'), | ||
| 'bootswatch-yeti': getBootswatchThemePath('yeti'), | ||
| 'bootswatch-zephyr': getBootswatchThemePath('zephyr'), | ||
| }; | ||
|
|
||
| /** | ||
| * Manages site assets such as CSS, JS, fonts, and images. | ||
| * Handles copying, building, and removing assets, as well as handling style reloads. | ||
| */ | ||
| export class SiteAssetsManager { | ||
| rootPath: string; | ||
| outputPath: string; | ||
| siteAssetsDestPath: string; | ||
| siteConfig!: SiteConfig; | ||
|
|
||
| constructor(rootPath: string, outputPath: string) { | ||
| this.rootPath = rootPath; | ||
| this.outputPath = outputPath; | ||
| this.siteAssetsDestPath = path.join(outputPath, TEMPLATE_SITE_ASSET_FOLDER_NAME); | ||
| } | ||
|
|
||
| listAssets(fileIgnore: Ignore) { | ||
| const files = walkSync(this.rootPath, { directories: false }); | ||
| return fileIgnore.filter(files); | ||
| } | ||
|
|
||
| async _buildMultipleAssets(filePaths: string | string[]) { | ||
| const filePathArray = Array.isArray(filePaths) ? filePaths : [filePaths]; | ||
| const uniquePaths = _.uniq(filePathArray); | ||
| const fileIgnore = ignore().add(this.siteConfig.ignore); | ||
| const fileRelativePaths = uniquePaths.map(filePath => path.relative(this.rootPath, filePath)); | ||
| const copyAssets = fileIgnore.filter(fileRelativePaths) | ||
| .map(asset => fs.copy(path.join(this.rootPath, asset), path.join(this.outputPath, asset))); | ||
| await Promise.all(copyAssets); | ||
| logger.info('Assets built'); | ||
| } | ||
|
|
||
| async _removeMultipleAssets(filePaths: string | string[]) { | ||
| const filePathArray = Array.isArray(filePaths) ? filePaths : [filePaths]; | ||
| const uniquePaths = _.uniq(filePathArray); | ||
| const fileRelativePaths = uniquePaths.map(filePath => path.relative(this.rootPath, filePath)); | ||
| const filesToRemove = fileRelativePaths.map( | ||
| fileRelativePath => path.join(this.outputPath, fileRelativePath)); | ||
| const removeFiles = filesToRemove.map(asset => fs.remove(asset)); | ||
| if (removeFiles.length !== 0) { | ||
| await Promise.all(removeFiles); | ||
| logger.debug('Assets removed'); | ||
| } | ||
| } | ||
|
|
||
| async buildAssets() { | ||
| logger.info('Building assets...'); | ||
| const outputFolder = path.relative(this.rootPath, this.outputPath); | ||
| const fileIgnore = ignore().add([...this.siteConfig.ignore, outputFolder]); | ||
|
|
||
| // Scan and copy assets (excluding ignore files). | ||
| const listOfAssets = this.listAssets(fileIgnore); | ||
| const assetsToCopy = listOfAssets.map(asset => | ||
| fs.copy(path.join(this.rootPath, asset), path.join(this.outputPath, asset))); | ||
| await Promise.all(assetsToCopy); | ||
| logger.info('Assets built'); | ||
| } | ||
|
|
||
| /** | ||
| * Handles the reloading of ignore attributes | ||
| */ | ||
| async handleIgnoreReload(oldIgnore: string[]) { | ||
| const assetsToRemove = _.difference(this.siteConfig.ignore, oldIgnore); | ||
|
|
||
| if (!_.isEqual(oldIgnore, this.siteConfig.ignore)) { | ||
| await this._removeMultipleAssets(assetsToRemove); | ||
| await this.buildAssets(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Handles the reloading of the style attribute if it has been modified | ||
| */ | ||
| async handleStyleReload(oldStyle: SiteConfigStyle) { | ||
| if (!_.isEqual(oldStyle.bootstrapTheme, this.siteConfig.style.bootstrapTheme)) { | ||
| await this.copyBootstrapTheme(true); | ||
| logger.info('Updated bootstrap theme'); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Copies Font Awesome assets to the assets folder | ||
| */ | ||
| async copyFontAwesomeAsset() { | ||
| const faRootSrcPath = path.dirname(require.resolve('@fortawesome/fontawesome-free/package.json')); | ||
| const faCssSrcPath = path.join(faRootSrcPath, 'css', 'all.min.css'); | ||
| const faCssDestPath = path.join(this.siteAssetsDestPath, 'fontawesome', 'css', 'all.min.css'); | ||
| const faFontsSrcPath = path.join(faRootSrcPath, 'webfonts'); | ||
| const faFontsDestPath = path.join(this.siteAssetsDestPath, 'fontawesome', 'webfonts'); | ||
|
|
||
| await fs.copy(faCssSrcPath, faCssDestPath); | ||
| await fs.copy(faFontsSrcPath, faFontsDestPath); | ||
| } | ||
|
|
||
| /** | ||
| * Copies Octicon assets to the assets folder | ||
| */ | ||
| copyOcticonsAsset() { | ||
| const octiconsCssSrcPath = require.resolve('@primer/octicons/build/build.css'); | ||
| const octiconsCssDestPath = path.join(this.siteAssetsDestPath, 'css', 'octicons.css'); | ||
|
|
||
| return fs.copy(octiconsCssSrcPath, octiconsCssDestPath); | ||
| } | ||
|
|
||
| /** | ||
| * Copies Google Material Icons assets to the assets folder | ||
| */ | ||
| copyMaterialIconsAsset() { | ||
| const materialIconsRootSrcPath = path.dirname(require.resolve('material-icons/package.json')); | ||
| const materialIconsCssAndFontsSrcPath = path.join(materialIconsRootSrcPath, 'iconfont'); | ||
| const materialIconsCssAndFontsDestPath = path.join(this.siteAssetsDestPath, 'material-icons'); | ||
|
|
||
| return fs.copy(materialIconsCssAndFontsSrcPath, materialIconsCssAndFontsDestPath); | ||
| } | ||
|
|
||
| /** | ||
| * Copies core-web bundles and external assets to the assets output folder | ||
| */ | ||
| copyCoreWebAsset() { | ||
| const coreWebRootPath = path.dirname(require.resolve('@markbind/core-web/package.json')); | ||
| const coreWebAssetPath = path.join(coreWebRootPath, 'asset'); | ||
| fs.copySync(coreWebAssetPath, this.siteAssetsDestPath); | ||
|
|
||
| const dirsToCopy = ['fonts']; | ||
| const filesToCopy = [ | ||
| 'js/markbind.min.js', | ||
| 'css/markbind.min.css', | ||
| ]; | ||
|
|
||
| const copyAllFiles = filesToCopy.map((file) => { | ||
| const srcPath = path.join(coreWebRootPath, 'dist', file); | ||
| const destPath = path.join(this.siteAssetsDestPath, file); | ||
| return fs.copy(srcPath, destPath); | ||
| }); | ||
|
|
||
| const copyFontsDir = dirsToCopy.map((dir) => { | ||
| const srcPath = path.join(coreWebRootPath, 'dist', dir); | ||
| const destPath = path.join(this.siteAssetsDestPath, 'css', dir); | ||
| return fs.copy(srcPath, destPath); | ||
| }); | ||
|
|
||
| return Promise.all([...copyAllFiles, ...copyFontsDir]); | ||
| } | ||
|
|
||
| copyBootstrapIconsAsset() { | ||
| const bootstrapIconsCssSrcPath = require.resolve('bootstrap-icons/font/bootstrap-icons.css'); | ||
| const bootstrapIconsFontsSrcPath = path.dirname(bootstrapIconsCssSrcPath); | ||
| const bootstrapIconsFontsDestPath = path.join(this.siteAssetsDestPath, 'bootstrap-icons', 'font'); | ||
| return fs.copy(bootstrapIconsFontsSrcPath, bootstrapIconsFontsDestPath); | ||
| } | ||
|
|
||
| /** | ||
| * Copies bootstrapTheme to the assets folder if a valid bootstrapTheme is specified | ||
| * @param isRebuild only true if it is a rebuild | ||
| */ | ||
| copyBootstrapTheme(isRebuild: boolean) { | ||
| const { bootstrapTheme } = this.siteConfig.style; | ||
|
|
||
| if ((!isRebuild && !bootstrapTheme) | ||
| || (bootstrapTheme && !_.has(SUPPORTED_THEMES_PATHS, bootstrapTheme))) { | ||
| return Promise.resolve(); | ||
| } | ||
|
|
||
| const themeSrcPath = !bootstrapTheme | ||
| ? require.resolve('@markbind/core-web/asset/css/bootstrap.min.css') | ||
| : SUPPORTED_THEMES_PATHS[bootstrapTheme]; | ||
| const themeDestPath = path.join(this.siteAssetsDestPath, 'css', 'bootstrap.min.css'); | ||
|
|
||
| return fs.copy(themeSrcPath, themeDestPath); | ||
| } | ||
|
|
||
| /** | ||
| * Build/copy assets that are specified in filePaths | ||
| * @param filePaths a single path or an array of paths corresponding to the assets to build | ||
| */ | ||
| buildAsset = delay(this._buildMultipleAssets.bind(this) as () => Bluebird<unknown>, 1000); | ||
|
|
||
| /** | ||
| * Remove assets that are specified in filePaths | ||
| * @param filePaths a single path or an array of paths corresponding to the assets to remove | ||
| */ | ||
| removeAsset = delay(this._removeMultipleAssets.bind(this) as () => Bluebird<unknown>, 1000); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would this be a good opportunity to refactor
require.resolveto use ESM alternatives? this applies to other instances ofrequire.resolve()tooUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeap, that would be ideal to tackle in the next few PRs