@tobi2409/layout 0.9.0 → 0.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -37,6 +37,12 @@ Process the HTML file:
37
37
  npx layout path/to/application.html
38
38
  ```
39
39
 
40
+ You can also pass a directory. It is searched recursively for `.html` and `.htm` files; output directories are skipped:
41
+
42
+ ```sh
43
+ npx layout path/to/pages
44
+ ```
45
+
40
46
  The output directory defaults to `generated`. Use `--output-dir` (or `-o`) to choose another name:
41
47
 
42
48
  ```sh
package/bin/layout.mjs CHANGED
@@ -1,8 +1,8 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- import { mkdir, readFile, writeFile } from 'node:fs/promises'
3
+ import { mkdir, readFile, readdir, realpath, stat, writeFile } from 'node:fs/promises'
4
4
  import { basename, dirname, extname, join } from 'node:path'
5
- import { pathToFileURL } from 'node:url'
5
+ import { fileURLToPath } from 'node:url'
6
6
  import { parse, serialize } from 'parse5'
7
7
 
8
8
  const ALIGNMENTS = new Set(['top', 'right', 'bottom', 'left', 'client'])
@@ -313,13 +313,40 @@ export async function compileFile(fileName, outputDirectoryName = 'generated') {
313
313
  return { ...result, htmlFile, cssFile, runtimeFile }
314
314
  }
315
315
 
316
+ async function findHtmlFiles(directory, outputDirectoryName) {
317
+ const files = []
318
+
319
+ for (const entry of await readdir(directory, { withFileTypes: true })) {
320
+ const entryPath = join(directory, entry.name)
321
+
322
+ if (entry.isDirectory()) {
323
+ if (entry.name !== outputDirectoryName) {
324
+ files.push(...await findHtmlFiles(entryPath, outputDirectoryName))
325
+ }
326
+ } else if (entry.isFile() && /\.html?$/i.test(entry.name)) {
327
+ files.push(entryPath)
328
+ }
329
+ }
330
+
331
+ return files
332
+ }
333
+
334
+ export async function compilePath(inputPath, outputDirectoryName = 'generated') {
335
+ const input = await stat(inputPath)
336
+ const files = input.isDirectory()
337
+ ? await findHtmlFiles(inputPath, outputDirectoryName)
338
+ : [inputPath]
339
+
340
+ return Promise.all(files.map((fileName) => compileFile(fileName, outputDirectoryName)))
341
+ }
342
+
316
343
  async function main() {
317
344
  const args = process.argv.slice(2)
318
345
  const fileName = args[0]
319
346
  let outputDirectoryName = 'generated'
320
347
 
321
348
  if (!fileName) {
322
- console.error('Usage: layout <document.html> [--output-dir <name>]')
349
+ console.error('Usage: layout <document.html|directory> [--output-dir <name>]')
323
350
  process.exitCode = 1
324
351
  return
325
352
  }
@@ -339,14 +366,19 @@ async function main() {
339
366
  }
340
367
  }
341
368
 
342
- const result = await compileFile(fileName, outputDirectoryName)
369
+ const results = await compilePath(fileName, outputDirectoryName)
343
370
 
344
- console.log(`Generated ${result.htmlFile}`)
345
- console.log(`Generated ${result.cssFile}`)
346
- console.log(`Generated ${result.runtimeFile}`)
371
+ for (const result of results) {
372
+ console.log(`Generated ${result.htmlFile}`)
373
+ console.log(`Generated ${result.cssFile}`)
374
+ console.log(`Generated ${result.runtimeFile}`)
375
+ }
347
376
  }
348
377
 
349
- if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
378
+ const isMainModule = process.argv[1]
379
+ && await realpath(process.argv[1]) === await realpath(fileURLToPath(import.meta.url))
380
+
381
+ if (isMainModule) {
350
382
  main().catch((error) => {
351
383
  console.error(error.message)
352
384
  process.exitCode = 1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tobi2409/layout",
3
- "version": "0.9.0",
3
+ "version": "0.9.1",
4
4
  "description": "Build-time HTML frame layout generator using CSS Grid",
5
5
  "license": "MIT",
6
6
  "type": "module",