Comparing version 1.12.0 to 1.13.0
@@ -8,2 +8,11 @@ # Change Log | ||
## [1.13.0] - 2024-07-30 | ||
### Changed | ||
- `FileUtil.copy` now uses `Context` and `CopyStepConfig` parameters. | ||
- `CopyStepConfig` now relies on `config.getOutputPath(context)` to devise output file path. This leverages `SsgConfig.toOutputPath()` and allows to copy each file to a different target. | ||
### Removed | ||
- `HtmlTagReplaceCommand` as the generic `DomReplaceCommand` with a tag selector is enough. | ||
## [1.12.0] - 2024-07-15 | ||
@@ -10,0 +19,0 @@ |
import { ReplaceCommand } from "./ReplaceCommand.js"; | ||
import { HtmlSsgContext } from "../../../HtmlSsgContext.js"; | ||
import { DomReplacer } from "./DomReplacer.js"; | ||
import { ReplacerFactory } from "./ReplacerFactory"; | ||
export declare abstract class DomReplaceCommand<T extends HTMLElement = HTMLElement, C extends HtmlSsgContext = HtmlSsgContext> implements ReplaceCommand<C> { | ||
protected selector: string; | ||
constructor(selector: string); | ||
protected replacerFactory: ReplacerFactory<DomReplacer<T>>; | ||
constructor(selector: string, replacerFactory: ReplacerFactory<DomReplacer<T>>); | ||
execute(context: C): Promise<void>; | ||
@@ -14,3 +16,3 @@ /** | ||
*/ | ||
protected abstract createReplacer(context: C): Promise<DomReplacer<T>>; | ||
protected createReplacer(context: C): Promise<DomReplacer<T>>; | ||
/** | ||
@@ -17,0 +19,0 @@ * Executed as last operation of execute() |
export class DomReplaceCommand { | ||
constructor(selector) { | ||
constructor(selector, replacerFactory) { | ||
this.selector = selector; | ||
this.replacerFactory = replacerFactory; | ||
} | ||
@@ -30,2 +31,11 @@ async execute(context) { | ||
/** | ||
* Creates a replacer in a given context. | ||
* | ||
* @param context | ||
* @protected | ||
*/ | ||
createReplacer(context) { | ||
return this.replacerFactory.create(context); | ||
} | ||
/** | ||
* Executed as last operation of execute() | ||
@@ -32,0 +42,0 @@ * @param context |
import { ReplacerFactory } from "../../ReplacerFactory.js"; | ||
import { DomReplaceCommand } from "../../DomReplaceCommand.js"; | ||
import { DomReplacer } from "../../DomReplacer.js"; | ||
import { HtmlSsgContext } from "../../../../../HtmlSsgContext.js"; | ||
/** | ||
@@ -9,3 +8,2 @@ * A replacer that looks for HTML class(es) in tags. | ||
export declare class ClassDomReplaceCommand<T extends HTMLElement = HTMLElement> extends DomReplaceCommand<T> { | ||
protected replacerFactory: ReplacerFactory<DomReplacer<T>>; | ||
/** | ||
@@ -17,3 +15,2 @@ * | ||
constructor(replacerFactory: ReplacerFactory<DomReplacer<T>>, ...classNames: string[]); | ||
protected createReplacer(context: HtmlSsgContext): Promise<DomReplacer<T>>; | ||
} |
@@ -12,8 +12,4 @@ import { DomReplaceCommand } from "../../DomReplaceCommand.js"; | ||
constructor(replacerFactory, ...classNames) { | ||
super(classNames.map(className => '.' + className).join(",")); | ||
this.replacerFactory = replacerFactory; | ||
super(classNames.map(className => "." + className).join(","), replacerFactory); | ||
} | ||
createReplacer(context) { | ||
return this.replacerFactory.create(context); | ||
} | ||
} |
export * from "./class/index.js"; | ||
export * from "./ssi/index.js"; | ||
export * from "./tag/index.js"; | ||
export * from "./StringContextHandler.js"; | ||
export * from "./StringEchoVarReplaceCommand.js"; | ||
export * from "./VarRegexReplacer.js"; |
export * from "./class/index.js"; | ||
export * from "./ssi/index.js"; | ||
export * from "./tag/index.js"; | ||
export * from "./StringContextHandler.js"; | ||
export * from "./StringEchoVarReplaceCommand.js"; | ||
export * from "./VarRegexReplacer.js"; |
import { SsgStep } from "./SsgStep.js"; | ||
import { SsgContext } from "../SsgContext.js"; | ||
import { IOptions } from "glob"; | ||
import { SsgConfig } from "../SsgConfig.js"; | ||
import { SsgContextImpl } from "../SsgContextImpl"; | ||
export interface CopyStepConfig extends SsgConfig { | ||
readonly sourcePatterns: string[]; | ||
readonly destDir: string; | ||
readonly options?: IOptions; | ||
@@ -16,7 +15,7 @@ } | ||
*/ | ||
export declare class CopyStep<C extends SsgContext = SsgContext> implements SsgStep<C, CopyStepResult> { | ||
export declare class CopyStep<C extends SsgContextImpl = SsgContextImpl> implements SsgStep<C, CopyStepResult> { | ||
protected config: CopyStepConfig; | ||
readonly name = "copy"; | ||
constructor(config: CopyStepConfig); | ||
execute(context: SsgContext): Promise<CopyStepResult>; | ||
execute(context: C): Promise<CopyStepResult>; | ||
} |
@@ -12,7 +12,4 @@ import * as process from "process"; | ||
async execute(context) { | ||
const copies = this.config.sourcePatterns; | ||
const dest = this.config.destDir; | ||
try { | ||
context.log("Copying to", dest, copies); | ||
const copiedFiles = await FileUtil.copy(dest, copies, this.config.options); | ||
const copiedFiles = await FileUtil.copy(context, this.config); | ||
const cwd = process.cwd(); | ||
@@ -23,5 +20,5 @@ const files = copiedFiles.map(file => file.startsWith(cwd) ? file.substring(cwd.length + 1) : file); | ||
catch (e) { | ||
throw Error(`Could not copy ${copies} because of ${e}`); | ||
throw Error(`Could not copy ${this.config.sourcePatterns} because of ${e}`); | ||
} | ||
} | ||
} |
@@ -1,3 +0,4 @@ | ||
import { IOptions } from "glob"; | ||
import { Dirent } from "node:fs"; | ||
import { SsgContextImpl } from "../../SsgContextImpl"; | ||
import { CopyStepConfig } from "../../step"; | ||
/** | ||
@@ -57,10 +58,9 @@ * File utility functions | ||
* | ||
* @param toDir the destination directory path. | ||
* @param sourcePatterns An array of file nmes. | ||
* @param options | ||
* @param context the destination directory path. | ||
* @param config | ||
* @return the list of output files. | ||
*/ | ||
static copy(toDir: string, sourcePatterns: string[], options?: IOptions): Promise<string[]>; | ||
static copyFiles(sourceFiles: string[], toDir: string): string[]; | ||
static copyFile(sourceFile: string, toDir: string): string; | ||
static copy<C extends SsgContextImpl>(context: C, config: CopyStepConfig): Promise<string[]>; | ||
static copyFiles<C extends SsgContextImpl>(context: C, sourceFiles: string[], config: CopyStepConfig): string[]; | ||
static copyFile<C extends SsgContextImpl>(context: C, sourceFile: string, config: CopyStepConfig): string; | ||
} |
@@ -7,2 +7,3 @@ import * as fs from "fs"; | ||
import { promise as glob } from "glob-promise"; | ||
import { FileContents } from "./FileContents"; | ||
/** | ||
@@ -133,12 +134,11 @@ * File utility functions | ||
* | ||
* @param toDir the destination directory path. | ||
* @param sourcePatterns An array of file nmes. | ||
* @param options | ||
* @param context the destination directory path. | ||
* @param config | ||
* @return the list of output files. | ||
*/ | ||
static async copy(toDir, sourcePatterns, options) { | ||
static async copy(context, config) { | ||
let result = []; | ||
for (const sourcePattern of sourcePatterns) { | ||
const sourceFiles = await glob(sourcePattern, options); | ||
const copied = this.copyFiles(sourceFiles, toDir); | ||
for (const sourcePattern of config.sourcePatterns) { | ||
const sourceFiles = await glob(sourcePattern, config.options); | ||
const copied = this.copyFiles(context, sourceFiles, config); | ||
result = result.concat(copied); | ||
@@ -148,6 +148,6 @@ } | ||
} | ||
static copyFiles(sourceFiles, toDir) { | ||
static copyFiles(context, sourceFiles, config) { | ||
const result = []; | ||
for (const sourceFile of sourceFiles) { | ||
const to = this.copyFile(sourceFile, toDir); | ||
const to = this.copyFile(context, sourceFile, config); | ||
result.push(to); | ||
@@ -157,5 +157,6 @@ } | ||
} | ||
static copyFile(sourceFile, toDir) { | ||
static copyFile(context, sourceFile, config) { | ||
context.file = new FileContents(sourceFile, "utf-8", "", new Date(), { variants: [] }); | ||
const to = path.resolve(config.getOutputPath(context)); | ||
const from = path.resolve(sourceFile); | ||
const to = path.join(toDir, sourceFile); | ||
this.ensureDirectoryOf(to); | ||
@@ -162,0 +163,0 @@ fs.copyFileSync(from, to); |
@@ -5,3 +5,3 @@ { | ||
"author": "Jérôme Beau <javarome@gmail.com> (https://javarome.com)", | ||
"version": "1.12.0", | ||
"version": "1.13.0", | ||
"description": "Static Site Generation TypeScript API", | ||
@@ -24,3 +24,3 @@ "exports": "./dist/src/index.js", | ||
"test": "rm -Rf out && tsx src/testAll.ts", | ||
"test-one": "rm -Rf out && tsx src/step/content/replace/html/class/ClassDomReplaceCommandTest.ts", | ||
"test-one": "rm -Rf out && tsx src/step/CopyStepTest.ts", | ||
"test-ci": "rm -Rf out && tsx src/testAll.ts" | ||
@@ -38,3 +38,3 @@ }, | ||
"uuid": "^10.0.0", | ||
"tsx": "4.16.0", | ||
"tsx": "4.16.2", | ||
"@javarome/testscript": "^0.10.7" | ||
@@ -41,0 +41,0 @@ }, |
@@ -11,4 +11,4 @@ [[ContentStep]] replacements are commands that are executed on the contents of a file. | ||
- [SsiEchoVarCommand](https://github.com/Javarome/ssg-api/blob/main/src/step/content/replace/html/ssi/SsiEchoVarCommand.ts) | ||
- ~~[HtmlTagReplaceCommand](https://github.com/Javarome/ssg-api/blob/main/src/step/content/replace/html/tag/HtmlTagReplaceCommand.ts) replaces HTML tags using Regexes (use DOM version instead if you have inner tags).~~ | ||
- ~~[ClassRegexReplaceCommand](https://github.com/Javarome/ssg-api/blob/main/src/step/content/replace/html/class/ClassRegexReplaceCommand.ts) replaces HTML tags bearing a specific class using Regexes (use DOM version instead if you have inner tags).~~ | ||
- [HtmlTagReplaceCommand](https://github.com/Javarome/ssg-api/blob/main/src/step/content/replace/html/tag/HtmlTagReplaceCommand.ts) replaces HTML tags using DOM selectors . | ||
- ~~[ClassDomRegexReplaceCommand](https://github.com/Javarome/ssg-api/blob/main/src/step/content/replace/html/class/ClassDomRegexReplaceCommand.ts) replaces HTML tags bearing a specific class using Regexes (use DOM version instead if you have inner tags).~~ | ||
@@ -20,3 +20,3 @@ and others in the repository (you'll find a number of [SSI](https://fr.wikipedia.org/wiki/Server_Side_Includes) commands because RR0 used to rely on them). | ||
```ts | ||
```ts | ||
const contentStepConfig1 = { // First content config is about writing a netlify.toml file which as generated from the contents of an .htaccess file | ||
@@ -23,0 +23,0 @@ roots: [".htaccess"], |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
138088
125
2135