diff --git a/client/index.js b/client/index.js index 07e5fc0..ac2513b 100644 --- a/client/index.js +++ b/client/index.js @@ -38,11 +38,11 @@ * * - `onRender` - register **after page is rendered** callback. * ```ts - * function onRender(callback: VoidFunction, options?: CallbackOptions): void; + * function onRender(callback: (currentUrl: URL) => void, options?: CallbackOptions): void; * ``` * - `onLeave` - register **before page is rendered** callback. * ```ts - * function onLeave(callback: VoidFunction, options?: CallbackOptions): void; + * function onLeave(callback: (currentUrl: URL, nextUrl: URL) => void, options?: CallbackOptions): void; * ``` * * The `CallbackOptions` allows configuring where, when and how a callback is executed. @@ -77,13 +77,13 @@ import Session from './session.js'; globalThis.PostDoc ??= new Session(); export function onLeave(callback, options) { - globalThis.PostDoc.onLeave(callback, options); + globalThis.PostDoc.navigator.registerOnLeaveCallback(callback, options); } export function onRender(callback, options) { - globalThis.PostDoc.onEnter(callback, options); + globalThis.PostDoc.navigator.registerOnRenderCallback(callback, options); } export async function go(url, replace) { - await globalThis.PostDoc.navigateTo(url, replace); + await globalThis.PostDoc.navigator.navigateTo(url, replace); } diff --git a/client/navigator.js b/client/navigator.js index fdd2a98..93c68df 100644 --- a/client/navigator.js +++ b/client/navigator.js @@ -3,6 +3,8 @@ import Snapshot from './snapshot.js'; export default class Navigator { #session; #pageLoaded; + #beforeLeavingSnapshotCallbacks = []; + #afterRenderingSnapshotCallbacks = []; constructor(session) { this.#session = session; @@ -14,9 +16,34 @@ export default class Navigator { return new URL(location.href); } + registerOnLeaveCallback(callback, options) { + const registeredOn = this.currentUrl; + + this.#beforeLeavingSnapshotCallbacks.push({ + test: this.#createURLMatchFunction(registeredOn, options?.forPage), + fn: callback + }); + } + + registerOnRenderCallback(callback, options) { + const registeredOn = this.currentUrl; + + const test = this.#createURLMatchFunction(registeredOn, options?.forPage); + + this.#afterRenderingSnapshotCallbacks.push({ test, fn: callback }); + + if (test(registeredOn)) { + callback(registeredOn); + } + } + async navigateTo(url, replace = false) { + url = url instanceof URL ? url : new URL(url, this.currentUrl.origin); + const nextSnapshot = await this.#prepareSnapshotFor(url); + await this.#callLeaveCallbacks(url); + await this.#session.renderer.render(nextSnapshot); history[replace ? 'replaceState' : 'pushState']( @@ -25,7 +52,7 @@ export default class Navigator { url ); - await this.#session.renderer.callRenderCallbacks(); + await this.#callRenderCallbacks(); if (url.hash.length) { this.#session.renderer.currentSnapshot.body @@ -40,6 +67,38 @@ export default class Navigator { location.reload(); } + async #callRenderCallbacks() { + const url = this.currentUrl; + + const maybePromises = this.#afterRenderingSnapshotCallbacks + .filter(({ test }) => test(url)) + .map(({ fn }) => fn(url)); + + await Promise.all(maybePromises); + } + + async #callLeaveCallbacks(nextUrl) { + const url = this.currentUrl; + + const maybePromises = this.#beforeLeavingSnapshotCallbacks + .filter(({ test }) => test(url)) + .map(({ fn }) => fn(url, nextUrl)); + + await Promise.all(maybePromises); + } + + #createURLMatchFunction(registeredOn, forPage) { + if (!forPage) { + return (url) => registeredOn.pathname === url.pathname; + } + + if (forPage instanceof RegExp) { + return (url) => forPage.test(url.href); + } + + return forPage; + } + #setup() { addEventListener('load', async () => { // Span execution until the next microtask, so the very first popstate event will be skipped. diff --git a/client/renderer.js b/client/renderer.js index e1cca1d..113cb48 100644 --- a/client/renderer.js +++ b/client/renderer.js @@ -3,8 +3,6 @@ import Snapshot from './snapshot.js'; export default class Renderer { #session; #currentSnapshot = new Snapshot(document); - #cleanupCallbacks = []; - #afterRenderCallbacks = []; constructor(session) { this.#session = session; @@ -17,62 +15,11 @@ export default class Renderer { } async render(nextSnapshot) { - await this.#performCleanups(); - await nextSnapshot.into(this.#currentSnapshot); this.#setupAnchorTraps(); } - registerCleanup(callback, options, registeredOn) { - this.#cleanupCallbacks.push({ - test: this.#createURLMatchFunction(registeredOn, options?.forPage), - fn: callback - }); - } - - registerRender(callback, options, registeredOn) { - const test = this.#createURLMatchFunction(registeredOn, options?.forPage); - - this.#afterRenderCallbacks.push({ test, fn: callback }); - - if (test(registeredOn)) { - callback(registeredOn); - } - } - - async callRenderCallbacks() { - const url = this.#session.navigator.currentUrl; - - const maybePromises = this.#afterRenderCallbacks - .filter(({ test }) => test(url)) - .map(({ fn }) => fn(url)); - - await Promise.all(maybePromises); - } - - async #performCleanups() { - const url = this.#session.navigator.currentUrl; - - const maybePromises = this.#cleanupCallbacks - .filter(({ test }) => test(url)) - .map(({ fn }) => fn(url)); - - await Promise.all(maybePromises); - } - - #createURLMatchFunction(registeredOn, forPage) { - if (!forPage) { - return (url) => registeredOn.pathname === url.pathname; - } - - if (forPage instanceof RegExp) { - return (url) => forPage.test(url.href); - } - - return forPage; - } - #setupAnchorTraps() { Array.from(this.#currentSnapshot.body.querySelectorAll('a[href]')) .filter((element) => element.host === location.host) diff --git a/client/session.js b/client/session.js index f2d355b..5647a6c 100644 --- a/client/session.js +++ b/client/session.js @@ -9,25 +9,6 @@ export default class Session { this.#setupViteConnection(); } - onLeave(callback, options) { - this.renderer.registerCleanup(callback, options, this.navigator.currentUrl); - } - - onEnter(callback, options) { - this.renderer.registerRender(callback, options, this.navigator.currentUrl); - } - - async navigateTo(url, replace) { - if ( - typeof url === 'string' && - !url.startsWith(this.navigator.currentUrl.origin) - ) { - url = new URL(url, this.navigator.currentUrl.origin); - } - - await this.navigator.navigateTo(url, replace); - } - #setupViteConnection() { if (import.meta.hot) { import.meta.hot.on('postdoc:reload-page', () => this.navigator.reload()); diff --git a/lib/commands/preview.js b/lib/commands/preview.js index 02fed6e..c4b2f6b 100644 --- a/lib/commands/preview.js +++ b/lib/commands/preview.js @@ -52,16 +52,16 @@ export const runPreview = async ({ host, open } = {}) => { Server is listening on: - ${server.resolvedUrls.local.join(' | ')} ${ -server.resolvedUrls.network.length -? '- ' + server.resolvedUrls.network.join(' | ') -: '' + server.resolvedUrls.network.length + ? '- ' + server.resolvedUrls.network.join(' | ') + : '' } `, Logger.SuccessLevel ); return server; -} +}; export default function createPreviewCommand() { return new PostDocCommand('preview') @@ -75,5 +75,5 @@ export default function createPreviewCommand() { 'The proxy option for the Vite\'s "host" CLI argument. It exposes the dev and preview server to the LAN and public addresses.' ) .action(({ host, open }) => - runAndMeasureAction(() => runPreview({host, open}))); + runAndMeasureAction(() => runPreview({ host, open }))); } diff --git a/nightwatch.conf.cjs b/nightwatch.conf.cjs index 14d4438..cc9144f 100644 --- a/nightwatch.conf.cjs +++ b/nightwatch.conf.cjs @@ -8,32 +8,32 @@ // |___/ // -const path = require("path"); +const path = require('path'); module.exports = { - src_folders: ["test/src", "test/e2e"], - globals_path: path.resolve(__dirname, "test", "lib", "globals.cjs"), + src_folders: ['test/src', 'test/e2e'], + globals_path: path.resolve(__dirname, 'test', 'lib', 'globals.cjs'), webdriver: { - start_process: true, + start_process: true }, test_settings: { default: { - launch_url: "http://localhost:5173", + launch_url: 'http://localhost:5173', desiredCapabilities: { - browserName: "chrome", + browserName: 'chrome' }, - exclude: "test/src/**", + exclude: 'test/src/**' }, unittests: { unit_tests_mode: true, - filter: "test/src/**", - exclude: "", - }, + filter: 'test/src/**', + exclude: '' + } }, test_workers: { - enabled: false, - }, + enabled: false + } }; diff --git a/test/e2e/api-page.js b/test/e2e/api-page.js index de54210..bc9afe9 100644 --- a/test/e2e/api-page.js +++ b/test/e2e/api-page.js @@ -1,59 +1,59 @@ -import { spawn, spawnSync } from "child_process"; -import { mkdir, mkdtemp, readFile, rm, writeFile } from "fs/promises"; -import { tmpdir } from "os"; -import { join, resolve } from "path"; -import { chdir } from "process"; -import Configuration from "../../lib/configuration.js"; -import Logger from "../../lib/logger.js"; -import kill from "tree-kill"; - -describe("Test api page", function () { +import { spawn, spawnSync } from 'child_process'; +import { mkdir, mkdtemp, readFile, rm, writeFile } from 'fs/promises'; +import { tmpdir } from 'os'; +import { join, resolve } from 'path'; +import { chdir } from 'process'; +import Configuration from '../../lib/configuration.js'; +import Logger from '../../lib/logger.js'; +import kill from 'tree-kill'; + +describe('Test api page', function () { const rootDirectory = process.cwd(); - const pathToPostdoc = resolve(rootDirectory, "bin/postdoc.js"); - const apiPagesFolder = 'api-pages' + const pathToPostdoc = resolve(rootDirectory, 'bin/postdoc.js'); + const apiPagesFolder = 'api-pages'; let tmpDir; let commandProcess; before(async function (browser, done) { - tmpDir = await mkdtemp(join(tmpdir(), ".foo")); + tmpDir = await mkdtemp(join(tmpdir(), '.foo')); chdir(tmpDir); - spawnSync("node", [pathToPostdoc, "init", "--name", "."]); + spawnSync('node', [pathToPostdoc, 'init', '--name', '.']); - const filename = "package.json"; - const fileContent = await readFile(filename, "utf8"); + const filename = 'package.json'; + const fileContent = await readFile(filename, 'utf8'); const finalContent = fileContent.replace( /"postdoc":\s*"(.*?)"/g, - `"postdoc": "file:${rootDirectory.replaceAll("\\", "/")}"` + `"postdoc": "file:${rootDirectory.replaceAll('\\', '/')}"` ); await writeFile(filename, finalContent); const postdocConfigFilename = 'postdoc.config.js'; - let postdocConfigContent = await readFile(postdocConfigFilename, "utf8"); + let postdocConfigContent = await readFile(postdocConfigFilename, 'utf8'); postdocConfigContent = postdocConfigContent.replace(/\bsource: null\b/, `source: "./${apiPagesFolder}"`); await writeFile(postdocConfigFilename, postdocConfigContent); - spawnSync("npm", ["install"], {shell: true}); + spawnSync('npm', ['install'], { shell: true }); await Configuration.initialise({}); await Logger.initialise(); - commandProcess = spawn("npm", ["start"], {shell: true}); + commandProcess = spawn('npm', ['start'], { shell: true }); done(); }); after(async function (browser, done) { - kill(commandProcess.pid, "SIGTERM", async function (err) { + kill(commandProcess.pid, 'SIGTERM', async function (err) { chdir(rootDirectory); await rm(tmpDir, { recursive: true }); done(); }); }); - it("Check if api-page has proper comments", async function(browser) { + it('Check if api-page has proper comments', async function(browser) { await mkdir(apiPagesFolder); const pageFilename = 'boo'; @@ -67,7 +67,7 @@ describe("Test api page", function () { function sum(a, b){ return a + b; } -​ + /** * Function to subtract two numbers * @param {number} a @@ -78,14 +78,14 @@ function diff(a, b){ } `); - await writeFile(join(apiPagesFolder, 'package.json'), ` + await writeFile(join(apiPagesFolder, 'package.json'), ` { "name": "${apiPagesFolder}", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "echo "Error: no test specified" && exit 1" }, "keywords": [], "author": "", @@ -93,13 +93,13 @@ function diff(a, b){ } `); - browser - .navigateTo(`${join(browser.baseUrl), pageFilename}.html`) - .waitForElementVisible("body"); + browser + .navigateTo(`${join(browser.baseUrl), pageFilename}.html`) + .waitForElementVisible('body'); - browser.element.findByText('sum').assert.visible(); - browser.element.findByText('Function to add two numbers').assert.visible(); - browser.element.findByText('diff').assert.visible(); - browser.element.findByText('Function to subtract two numbers').assert.visible(); - }) -}) \ No newline at end of file + browser.element.findByText('sum').assert.visible(); + browser.element.findByText('Function to add two numbers').assert.visible(); + browser.element.findByText('diff').assert.visible(); + browser.element.findByText('Function to subtract two numbers').assert.visible(); + }); +}); diff --git a/test/e2e/commands/preview.js b/test/e2e/commands/preview.js index 89e4bc2..2311800 100644 --- a/test/e2e/commands/preview.js +++ b/test/e2e/commands/preview.js @@ -1,32 +1,32 @@ -import { spawnSync } from "node:child_process"; -import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; -import { tmpdir } from "node:os"; -import { join, resolve } from "node:path"; -import { chdir } from "node:process"; -import Configuration from "../../../lib/configuration.js"; +import { spawnSync } from 'node:child_process'; +import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'; +import { tmpdir } from 'node:os'; +import { join, resolve } from 'node:path'; +import { chdir } from 'node:process'; +import Configuration from '../../../lib/configuration.js'; import { runPreview } from '../../../lib/commands/preview.js'; -import Logger from "../../../lib/logger.js"; +import Logger from '../../../lib/logger.js'; -describe("preview command", function () { +describe('preview command', function () { const rootDirectory = process.cwd(); - const pathToPostdoc = resolve(rootDirectory, "bin/postdoc.js"); + const pathToPostdoc = resolve(rootDirectory, 'bin/postdoc.js'); let tmpDir; before(async function (_, done) { - tmpDir = await mkdtemp(join(tmpdir(), ".foo")); + tmpDir = await mkdtemp(join(tmpdir(), '.foo')); chdir(tmpDir); - spawnSync("node", [pathToPostdoc, "init", "--name", "."]); + spawnSync('node', [pathToPostdoc, 'init', '--name', '.']); - const filename = "package.json"; - const fileContent = await readFile(filename, "utf8"); + const filename = 'package.json'; + const fileContent = await readFile(filename, 'utf8'); const finalContent = fileContent.replace( /"postdoc":\s*"(.*?)"/g, - `"postdoc": "file:${rootDirectory.replaceAll("\\", "/")}"` + `"postdoc": "file:${rootDirectory.replaceAll('\\', '/')}"` ); await writeFile(filename, finalContent); - spawnSync("npm", ["install"], {shell: true}); + spawnSync('npm', ['install'], { shell: true }); await Configuration.initialise({}); @@ -41,22 +41,21 @@ describe("preview command", function () { done(); }); - test("check if preview command runs static server", async function(browser) { + test('check if preview command runs static server', async function(browser) { const server = await runPreview(); await browser - .navigateTo("http://localhost:4173/") - .waitForElementVisible("body").window.close(); + .navigateTo('http://localhost:4173/') + .waitForElementVisible('body').window.close(); await new Promise((resolve, reject) => { server.httpServer.close((err) => { if (err) { reject(err); - } - else { + } else { resolve(); } }); - }) - }) -}) \ No newline at end of file + }); + }); +}); diff --git a/test/e2e/pseudo-global-variables.js b/test/e2e/pseudo-global-variables.js index fe71999..aa10c16 100644 --- a/test/e2e/pseudo-global-variables.js +++ b/test/e2e/pseudo-global-variables.js @@ -1,72 +1,72 @@ -import { spawn, spawnSync } from "node:child_process"; -import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; -import { tmpdir } from "node:os"; -import { dirname, join, resolve, basename } from "node:path"; -import { chdir } from "node:process"; -import Configuration from "../../lib/configuration.js"; -import kill from "tree-kill"; -import Logger from "../../lib/logger.js"; - -describe("Test pseudo global variables in ejs files", function () { +import { spawn, spawnSync } from 'node:child_process'; +import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'; +import { tmpdir } from 'node:os'; +import { dirname, join, resolve, basename } from 'node:path'; +import { chdir } from 'node:process'; +import Configuration from '../../lib/configuration.js'; +import kill from 'tree-kill'; +import Logger from '../../lib/logger.js'; + +describe('Test pseudo global variables in ejs files', function () { const rootDirectory = process.cwd(); - const pathToPostdoc = resolve(rootDirectory, "bin/postdoc.js"); + const pathToPostdoc = resolve(rootDirectory, 'bin/postdoc.js'); let tmpDir; let commandProcess; before(async function (browser, done) { - tmpDir = await mkdtemp(join(tmpdir(), ".foo")); + tmpDir = await mkdtemp(join(tmpdir(), '.foo')); chdir(tmpDir); - spawnSync("node", [pathToPostdoc, "init", "--name", "."]); + spawnSync('node', [pathToPostdoc, 'init', '--name', '.']); - const filename = "package.json"; - const fileContent = await readFile(filename, "utf8"); + const filename = 'package.json'; + const fileContent = await readFile(filename, 'utf8'); const finalContent = fileContent.replace( /"postdoc":\s*"(.*?)"/g, - `"postdoc": "file:${rootDirectory.replaceAll("\\", "/")}"` + `"postdoc": "file:${rootDirectory.replaceAll('\\', '/')}"` ); await writeFile(filename, finalContent); - spawnSync("npm", ["install"], {shell: true}); + spawnSync('npm', ['install'], { shell: true }); await Configuration.initialise({}); await Logger.initialise(); - commandProcess = spawn("npm", ["start"], {shell: true}); + commandProcess = spawn('npm', ['start'], { shell: true }); done(); }); after(async function (browser, done) { - kill(commandProcess.pid, "SIGTERM", async function (err) { + kill(commandProcess.pid, 'SIGTERM', async function (err) { chdir(rootDirectory); await rm(tmpDir, { recursive: true }); done(); }); }); - it("check if pseudo-global variables are available in ejs files", async function (browser) { + it('check if pseudo-global variables are available in ejs files', async function (browser) { const configuration = Configuration.get(); - const path = "globals"; + const path = 'globals'; const pathToLayoutsFolder = join(configuration.directories.layouts, path); const pathToContentFolder = join(configuration.directories.content, path); - const relativePathToFilenameInsideLayoutsFolder = join(pathToLayoutsFolder, "index.ejs"); - const relativePathToFilenameInsideContentFolder = join(pathToContentFolder, "index.md"); + const relativePathToFilenameInsideLayoutsFolder = join(pathToLayoutsFolder, 'index.ejs'); + const relativePathToFilenameInsideContentFolder = join(pathToContentFolder, 'index.md'); await mkdir(pathToContentFolder); await mkdir(pathToLayoutsFolder); await writeFile( - join("src", "js", "test-require.cjs"), + join('src', 'js', 'test-require.cjs'), ` module.exports = { text: "some text" }; ` ); await writeFile( - join("src", "js", "test-import.js"), + join('src', 'js', 'test-import.js'), ` export const text = 'some text' ` @@ -115,18 +115,18 @@ Lorem ipsum dolor sit amet, consectetur adipiscing elit. ## Part 2 Lorem ipsum dolor sit amet, consectetur adipiscing elit. - `) + `); const absolutePathToFilenameInsideLayoutsFolder = resolve(relativePathToFilenameInsideLayoutsFolder); browser .navigateTo(`${browser.baseUrl}/${path}/`) - .waitForElementVisible("body") - .assert.textEquals("#filename", absolutePathToFilenameInsideLayoutsFolder) - .assert.textEquals("#dirname", dirname(absolutePathToFilenameInsideLayoutsFolder)) - .assert.textEquals("#test-require", "some text") - .assert.textEquals("#test-import", "some text") - .assert.textEquals("#page-url", `/${path}/index.html`) - .assert.textContains("#page-content", `What is ${basename(tmpDir)}`); + .waitForElementVisible('body') + .assert.textEquals('#filename', absolutePathToFilenameInsideLayoutsFolder) + .assert.textEquals('#dirname', dirname(absolutePathToFilenameInsideLayoutsFolder)) + .assert.textEquals('#test-require', 'some text') + .assert.textEquals('#test-import', 'some text') + .assert.textEquals('#page-url', `/${path}/index.html`) + .assert.textContains('#page-content', `What is ${basename(tmpDir)}`); }); }); diff --git a/test/src/commands/build.js b/test/src/commands/build.js index 3e659f9..1517bad 100644 --- a/test/src/commands/build.js +++ b/test/src/commands/build.js @@ -1,32 +1,32 @@ -import { spawnSync } from "node:child_process"; +import { spawnSync } from 'node:child_process'; import assert from 'node:assert/strict'; -import { mkdtemp, readFile, readdir, rm, writeFile } from "node:fs/promises"; -import { tmpdir } from "node:os"; -import { join, resolve } from "node:path"; -import { chdir } from "node:process"; -import Configuration from "../../../lib/configuration.js"; -import { existsSync } from "node:fs"; - -describe("build command", function () { +import { mkdtemp, readFile, readdir, rm, writeFile } from 'node:fs/promises'; +import { tmpdir } from 'node:os'; +import { join, resolve } from 'node:path'; +import { chdir } from 'node:process'; +import Configuration from '../../../lib/configuration.js'; +import { existsSync } from 'node:fs'; + +describe('build command', function () { const rootDirectory = process.cwd(); - const pathToPostdoc = resolve(rootDirectory, "bin/postdoc.js"); + const pathToPostdoc = resolve(rootDirectory, 'bin/postdoc.js'); let tmpDir; before(async function (done) { - tmpDir = await mkdtemp(join(tmpdir(), ".foo")); + tmpDir = await mkdtemp(join(tmpdir(), '.foo')); chdir(tmpDir); - spawnSync("node", [pathToPostdoc, "init", "--name", "."]); + spawnSync('node', [pathToPostdoc, 'init', '--name', '.']); - const filename = "package.json"; - const fileContent = await readFile(filename, "utf8"); + const filename = 'package.json'; + const fileContent = await readFile(filename, 'utf8'); const finalContent = fileContent.replace( /"postdoc":\s*"(.*?)"/g, - `"postdoc": "file:${rootDirectory.replaceAll("\\", "/")}"` + `"postdoc": "file:${rootDirectory.replaceAll('\\', '/')}"` ); await writeFile(filename, finalContent); - spawnSync("npm", ["install"], {shell: true}); + spawnSync('npm', ['install'], { shell: true }); await Configuration.initialise({}); @@ -39,10 +39,10 @@ describe("build command", function () { done(); }); - test("check if build command produced folder with builded assets", async function() { + test('check if build command produced folder with builded assets', async function() { const configuration = Configuration.get(); - spawnSync("npm", ["run", "build"], {shell: true}); + spawnSync('npm', ['run', 'build'], { shell: true }); const outputFolder = configuration.directories.output; @@ -53,5 +53,5 @@ describe("build command", function () { const files = await readdir(outputFolder); assert.equal(files.length > 0, true); - }) -}) \ No newline at end of file + }); +}); diff --git a/test/src/commands/create/page.js b/test/src/commands/create/page.js index 5f324fa..4373e53 100644 --- a/test/src/commands/create/page.js +++ b/test/src/commands/create/page.js @@ -1,33 +1,33 @@ -import assert from "node:assert/strict"; -import { chdir } from "node:process"; -import { tmpdir } from "node:os"; -import { resolve } from "node:path"; -import { spawnSync } from "node:child_process"; -import { join, parse } from "node:path"; -import { mkdtemp, readFile, readdir, rm, writeFile } from "node:fs/promises"; +import assert from 'node:assert/strict'; +import { chdir } from 'node:process'; +import { tmpdir } from 'node:os'; +import { resolve } from 'node:path'; +import { spawnSync } from 'node:child_process'; +import { join, parse } from 'node:path'; +import { mkdtemp, readFile, readdir, rm, writeFile } from 'node:fs/promises'; -import Configuration from "../../../../lib/configuration.js"; +import Configuration from '../../../../lib/configuration.js'; -describe("create pages command", function () { +describe('create pages command', function () { const rootDirectory = process.cwd(); - const pathToPostdoc = resolve(rootDirectory, "bin/postdoc.js"); + const pathToPostdoc = resolve(rootDirectory, 'bin/postdoc.js'); let tmpDir; before(async function () { - tmpDir = await mkdtemp(join(tmpdir(), ".foo")); + tmpDir = await mkdtemp(join(tmpdir(), '.foo')); chdir(tmpDir); - spawnSync("node", [pathToPostdoc, "init", "--name", "."]); + spawnSync('node', [pathToPostdoc, 'init', '--name', '.']); - const filename = "package.json"; - const fileContent = await readFile(filename, "utf8"); + const filename = 'package.json'; + const fileContent = await readFile(filename, 'utf8'); const finalContent = fileContent.replace( /"postdoc":\s*"(.*?)"/g, - `"postdoc": "file:${rootDirectory.replaceAll("\\", "/")}"` + `"postdoc": "file:${rootDirectory.replaceAll('\\', '/')}"` ); await writeFile(filename, finalContent); - spawnSync("npm", ["install"], {shell: true}); + spawnSync('npm', ['install'], { shell: true }); await Configuration.initialise({}); }); @@ -37,10 +37,10 @@ describe("create pages command", function () { await rm(tmpDir, { recursive: true }); }); - it("providing a url without extension should create correct files", async function () { - const filename = "foo"; + it('providing a url without extension should create correct files', async function () { + const filename = 'foo'; - spawnSync("npx", ["postdoc", "create", "page", "-n", filename], {shell: true}); + spawnSync('npx', ['postdoc', 'create', 'page', '-n', filename], { shell: true }); const configuration = Configuration.get(); @@ -52,7 +52,7 @@ describe("create pages command", function () { ); const testPageObjectsFiles = await readdir( - join(configuration.directories.tests, "page-objects") + join(configuration.directories.tests, 'page-objects') ); assert.equal( @@ -61,7 +61,7 @@ describe("create pages command", function () { ); const testSrcFiles = await readdir( - join(configuration.directories.tests, "src") + join(configuration.directories.tests, 'src') ); assert.equal( @@ -70,11 +70,11 @@ describe("create pages command", function () { ); }); - it("providing a url with extension should create correct files", async function () { - const filenameWithExtension = "boo.md"; + it('providing a url with extension should create correct files', async function () { + const filenameWithExtension = 'boo.md'; const filenameWithoutExtension = parse(filenameWithExtension).name; - spawnSync("npx", ["postdoc", "create", "page", "-n", filenameWithExtension], {shell: true}); + spawnSync('npx', ['postdoc', 'create', 'page', '-n', filenameWithExtension], { shell: true }); const configuration = Configuration.get(); @@ -86,7 +86,7 @@ describe("create pages command", function () { ); const testPageObjectsFiles = await readdir( - join(configuration.directories.tests, "page-objects") + join(configuration.directories.tests, 'page-objects') ); assert.equal( @@ -95,7 +95,7 @@ describe("create pages command", function () { ); const testSrcFiles = await readdir( - join(configuration.directories.tests, "src") + join(configuration.directories.tests, 'src') ); assert.equal( @@ -104,12 +104,12 @@ describe("create pages command", function () { ); }); - it("providing a url without extension inside subfolder should create correct files", async function () { - const subfolder = "coo"; - const filename = "foo"; + it('providing a url without extension inside subfolder should create correct files', async function () { + const subfolder = 'coo'; + const filename = 'foo'; const url = `${subfolder}/${filename}`; - spawnSync("npx", ["postdoc", "create", "page", "-n", url], {shell: true}); + spawnSync('npx', ['postdoc', 'create', 'page', '-n', url], { shell: true }); const configuration = Configuration.get(); @@ -123,7 +123,7 @@ describe("create pages command", function () { ); const testPageObjectsFiles = await readdir( - join(configuration.directories.tests, "page-objects", subfolder) + join(configuration.directories.tests, 'page-objects', subfolder) ); assert.equal( @@ -132,7 +132,7 @@ describe("create pages command", function () { ); const testSrcFiles = await readdir( - join(configuration.directories.tests, "src", subfolder) + join(configuration.directories.tests, 'src', subfolder) ); assert.equal( @@ -141,13 +141,13 @@ describe("create pages command", function () { ); }); - it("providing a url with extension inside subfolder should create correct files", async function () { - const filenameWithExtension = "boo.md"; + it('providing a url with extension inside subfolder should create correct files', async function () { + const filenameWithExtension = 'boo.md'; const filenameWithoutExtension = parse(filenameWithExtension).name; - const subfolder = "coo"; + const subfolder = 'coo'; const url = `${subfolder}/${filenameWithExtension}`; - spawnSync("npx", ["postdoc", "create", "page", "-n", url], {shell: true}); + spawnSync('npx', ['postdoc', 'create', 'page', '-n', url], { shell: true }); const configuration = Configuration.get(); @@ -161,7 +161,7 @@ describe("create pages command", function () { ); const testPageObjectsFiles = await readdir( - join(configuration.directories.tests, "page-objects", subfolder) + join(configuration.directories.tests, 'page-objects', subfolder) ); assert.equal( @@ -170,7 +170,7 @@ describe("create pages command", function () { ); const testSrcFiles = await readdir( - join(configuration.directories.tests, "src", subfolder) + join(configuration.directories.tests, 'src', subfolder) ); assert.equal(