Compare commits

...

2 Commits

Author SHA1 Message Date
github-actions[bot]
8df9a3e9c3 Version Packages 2021-07-02 13:42:36 +02:00
Benny Powers
1b9559f2a5 feat(cli): add async before11ty hook (#183) 2021-07-02 11:58:54 +02:00
8 changed files with 92 additions and 37 deletions

View File

@@ -86,3 +86,15 @@ const config = {
export default config;
```
## Lifecycle
You can hook into the rocket lifecycle by specifying a function for `before11ty`. This function runs before 11ty calls it's write method. If it is an async function, Rocket will await it's promise.
```js
export default {
async before11ty() {
await copyDataFiles();
},
};
```

View File

@@ -0,0 +1,11 @@
# Presets >> Create your own > Options || 10
Your preset can hook into the rocket lifecycle by specifying a function for `before11ty`. This function runs before 11ty calls it's write method. If it is an async function, Rocket will await it's promise.
```js
export default {
async before11ty() {
await copyDataFiles();
},
};
```

View File

@@ -1,5 +1,11 @@
# @rocket/cli
## 0.9.5
### Patch Changes
- 1b9559f: Adds `before11ty` hook to config and presets
## 0.9.4
### Patch Changes

View File

@@ -1,6 +1,6 @@
{
"name": "@rocket/cli",
"version": "0.9.4",
"version": "0.9.5",
"publishConfig": {
"access": "public"
},

View File

@@ -31,6 +31,7 @@ export class RocketEleventy extends Eleventy {
async write() {
await this.__rocketCli.mergePresets();
for (const fn of this.__rocketCli.config.__before11tyFunctions) await fn();
await super.write();
await this.__rocketCli.update();
}
@@ -120,7 +121,7 @@ export class RocketCli {
for (const folder of ['_assets', '_data', '_includes']) {
const to = path.join(this.config._inputDirCwdRelative, `_merged${folder}`);
await fs.emptyDir(to);
for (const sourceDir of this.config._presetPathes) {
for (const sourceDir of this.config._presetPaths) {
const from = path.join(sourceDir, folder);
if (fs.existsSync(from)) {
if (folder === '_includes') {

View File

@@ -5,6 +5,7 @@
/** @typedef {import('@web/dev-server').DevServerConfig} DevServerConfig */
/** @typedef {import('../types/main').RocketCliOptions} RocketCliOptions */
/** @typedef {import('../types/main').ImagePreset} ImagePreset */
/** @typedef {import('../types/main').RocketPlugin} RocketPlugin */
import path from 'path';
@@ -37,7 +38,7 @@ function ignore({ src }) {
/**
* @param {Partial<RocketCliOptions>} inConfig
* @returns {Promise<RocketCliOptions>}
* @returns {Promise<RocketCliOptions & { __before11tyFunctions: RocketCliOptions['before11ty'][] }>}
*/
export async function normalizeConfig(inConfig) {
let config = {
@@ -61,6 +62,11 @@ export async function normalizeConfig(inConfig) {
devServer: {},
...inConfig,
/** @type{RocketCliOptions['before11ty'][]} */
__before11tyFunctions: [],
/** @type{{[key: string]: ImagePreset}} */
imagePresets: {
responsive: {
widths: [600, 900, 1640],
@@ -121,9 +127,9 @@ export async function normalizeConfig(inConfig) {
const _inputDirCwdRelative = path.join(_configDirCwdRelative, config.inputDir);
// cli core preset is always first
config._presetPathes = [path.join(__dirname, '..', 'preset')];
config._presetPaths = [path.join(__dirname, '..', 'preset')];
for (const preset of config.presets) {
config._presetPathes.push(preset.path);
config._presetPaths.push(preset.path);
if (preset.adjustImagePresets) {
config.imagePresets = preset.adjustImagePresets(config.imagePresets);
@@ -159,9 +165,13 @@ export async function normalizeConfig(inConfig) {
if (preset.setupCliPlugins) {
config.setupCliPlugins = [...config.setupCliPlugins, ...preset.setupCliPlugins];
}
if (typeof preset.before11ty === 'function') {
config.__before11tyFunctions.push(preset.before11ty);
}
}
// add "local" preset
config._presetPathes.push(path.resolve(_inputDirCwdRelative));
config._presetPaths.push(path.resolve(_inputDirCwdRelative));
/** @type {MetaPlugin[]} */
let pluginsMeta = [
@@ -186,6 +196,10 @@ export async function normalizeConfig(inConfig) {
plugins.push(pluginInst);
}
if (typeof config.before11ty === 'function') {
config.__before11tyFunctions.push(config.before11ty);
}
// TODO: check pathPrefix to NOT have a '/' at the end as it will mean it may get ignored by 11ty 🤷‍♂️
return {

View File

@@ -10,7 +10,7 @@ function cleanup(config) {
const configNoPaths = { ...config };
delete configNoPaths._inputDirCwdRelative;
delete configNoPaths.configFile;
delete configNoPaths._presetPathes;
delete configNoPaths._presetPaths;
delete configNoPaths.eleventy;
delete configNoPaths.outputDevDir;
delete configNoPaths.imagePresets.responsive.ignore;
@@ -24,11 +24,12 @@ describe('normalizeConfig', () => {
// testing pathes is always a little more complicted 😅
expect(config._inputDirCwdRelative).to.match(/empty\/docs$/);
expect(config._presetPathes[0]).to.match(/cli\/preset$/);
expect(config._presetPathes[1]).to.match(/empty\/docs$/);
expect(config._presetPaths[0]).to.match(/cli\/preset$/);
expect(config._presetPaths[1]).to.match(/empty\/docs$/);
expect(config.outputDevDir).to.match(/_site-dev$/);
expect(cleanup(config)).to.deep.equal({
__before11tyFunctions: [],
command: 'help',
createSocialMediaImages: true,
devServer: {},
@@ -70,6 +71,7 @@ describe('normalizeConfig', () => {
});
expect(cleanup(config)).to.deep.equal({
__before11tyFunctions: [],
command: 'help',
createSocialMediaImages: true,
devServer: {
@@ -110,6 +112,7 @@ describe('normalizeConfig', () => {
});
expect(cleanup(config)).to.deep.equal({
__before11tyFunctions: [],
command: 'help',
createSocialMediaImages: true,
devServer: {
@@ -155,6 +158,7 @@ describe('normalizeConfig', () => {
});
expect(cleanup(config)).to.deep.equal({
__before11tyFunctions: [],
command: 'help',
createSocialMediaImages: true,
devServer: {},

View File

@@ -4,21 +4,25 @@ import { CheckHtmlLinksCliOptions } from 'check-html-links/dist-types/types/main
export interface RocketPreset {
path: string;
adjustImagePresets?: (preset: { [key: string]: ImagePreset }) => { [key: string]: ImagePreset };
before11ty?: () => void | Promise<void>;
// TODO: improve all setup functions
setupUnifiedPlugins?: function[];
setupDevAndBuildPlugins: function[];
setupBuildPlugins: function[];
setupDevPlugins: function[];
setupCliPlugins: function[];
setupEleventyPlugins: function[];
setupEleventyComputedConfig: function[];
setupDevAndBuildPlugins?: function[];
setupBuildPlugins?: function[];
setupDevPlugins?: function[];
setupCliPlugins?: function[];
setupEleventyPlugins?: function[];
setupEleventyComputedConfig?: function[];
}
interface RocketStartConfig {
createSocialMediaImages?: boolean;
}
type ImageFormat = 'avif' | 'webp' | 'jpg' | 'png' | 'svg';
type ImageFormat = 'avif' | 'webp' | 'jpg' | 'jpeg' | 'png' | 'svg';
interface ImagePreset {
widths: number[];
@@ -27,46 +31,49 @@ interface ImagePreset {
}
export interface RocketCliOptions {
presets: Array<RocketPreset>;
presets?: Array<RocketPreset>;
pathPrefix?: string;
serviceWorkerName?: string;
inputDir: string;
outputDir: string;
inputDir?: string;
outputDir?: string;
emptyOutputDir?: boolean;
absoluteBaseUrl?: string;
watch: boolean;
watch?: boolean;
createSocialMediaImages?: boolean;
imagePresets: {
imagePresets?: {
[key: string]: ImagePreset;
};
checkLinks: Partial<CheckHtmlLinksCliOptions>;
before11ty?: () => void | Promise<void>;
checkLinks?: Partial<CheckHtmlLinksCliOptions>;
start?: RocketStartConfig;
// TODO: improve all setup functions
setupUnifiedPlugins?: function[];
setupDevAndBuildPlugins: function[];
setupBuildPlugins: function[];
setupDevPlugins: function[];
setupCliPlugins: function[];
setupEleventyPlugins: function[];
setupEleventyComputedConfig: function[];
setupDevAndBuildPlugins?: function[];
setupBuildPlugins?: function[];
setupDevPlugins?: function[];
setupCliPlugins?: function[];
setupEleventyPlugins?: function[];
setupEleventyComputedConfig?: function[];
// advanced
devServer: DevServerConfig;
eleventy: function; // TODO: improve
plugins: RocketPlugin[];
devServer?: DevServerConfig;
eleventy?: (eleventyConfig: any) => void; // TODO: improve
plugins?: RocketPlugin[];
// rarely used
command: string;
command?: string;
configFile?: string;
outputDevDir: string;
outputDevDir?: string;
private _inputDirCwdRelative: string;
private _presetPathes?: Array<string>;
private _inputDirCwdRelative?: string;
private _presetPaths?: string[];
private __before11tyFunctions?: (() => void | Promise<void>)[];
}
export interface RocketPlugin {
commands: Array<string>;
commands: string[];
}