pugkit 1.0.0-beta.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.
@@ -0,0 +1,26 @@
1
+ import { readFile } from 'node:fs/promises'
2
+ import pug from 'pug'
3
+
4
+ export function compilePug(content, options) {
5
+ const { filename, basedir } = options
6
+
7
+ const result = pug.compileClientWithDependenciesTracked(content, {
8
+ filename,
9
+ basedir,
10
+ compileDebug: false,
11
+ inlineRuntimeFunctions: true
12
+ })
13
+
14
+ // eslint-disable-next-line no-new-func
15
+ const template = new Function('locals', result.body + '; return template(locals);')
16
+
17
+ return {
18
+ template,
19
+ dependencies: result.dependencies || []
20
+ }
21
+ }
22
+
23
+ export async function compilePugFile(filePath, options) {
24
+ const content = await readFile(filePath, 'utf8')
25
+ return compilePug(content, { filename: filePath, ...options })
26
+ }
package/utils/file.mjs ADDED
@@ -0,0 +1,44 @@
1
+ import { mkdir, rm } from 'node:fs/promises'
2
+ import { existsSync } from 'node:fs'
3
+ import { dirname } from 'node:path'
4
+
5
+ /**
6
+ * ファイル操作ヘルパー
7
+ */
8
+
9
+ /**
10
+ * ディレクトリを作成(再帰的)
11
+ */
12
+ export async function ensureDir(dirPath) {
13
+ if (!existsSync(dirPath)) {
14
+ await mkdir(dirPath, { recursive: true })
15
+ }
16
+ }
17
+
18
+ /**
19
+ * ファイル書き込み前にディレクトリを確保
20
+ */
21
+ export async function ensureFileDir(filePath) {
22
+ await ensureDir(dirname(filePath))
23
+ }
24
+
25
+ /**
26
+ * ディレクトリをクリーンアップ
27
+ */
28
+ export async function cleanDir(dirPath) {
29
+ if (existsSync(dirPath)) {
30
+ await rm(dirPath, { recursive: true, force: true })
31
+ }
32
+ await mkdir(dirPath, { recursive: true })
33
+ }
34
+
35
+ /**
36
+ * ファイル拡張子を変更
37
+ */
38
+ export function changeExtension(filePath, newExt) {
39
+ const lastDot = filePath.lastIndexOf('.')
40
+ if (lastDot === -1) {
41
+ return filePath + newExt
42
+ }
43
+ return filePath.substring(0, lastDot) + newExt
44
+ }
@@ -0,0 +1,40 @@
1
+ /**
2
+ * ロガー
3
+ */
4
+
5
+ const colors = {
6
+ reset: '\x1b[0m',
7
+ dim: '\x1b[2m',
8
+ cyan: '\x1b[36m',
9
+ green: '\x1b[32m',
10
+ yellow: '\x1b[33m',
11
+ red: '\x1b[31m'
12
+ }
13
+
14
+ function formatMessage(symbol, color, label, message) {
15
+ const coloredSymbol = `${color}${symbol}${colors.reset}`
16
+ const coloredLabel = `${color}${label}${colors.reset}`
17
+ return `${coloredSymbol} ${coloredLabel} ${message}`
18
+ }
19
+
20
+ export const logger = {
21
+ info(label, message) {
22
+ console.log(formatMessage('$', colors.cyan, label, message))
23
+ },
24
+
25
+ success(label, message) {
26
+ console.log(formatMessage('+', colors.green, label, message))
27
+ },
28
+
29
+ warn(label, message) {
30
+ console.log(formatMessage('⚠', colors.yellow, label, message))
31
+ },
32
+
33
+ error(label, message) {
34
+ console.error(formatMessage('-', colors.red, label, message))
35
+ },
36
+
37
+ skip(label, message) {
38
+ console.log(formatMessage('○', colors.dim, label, message))
39
+ }
40
+ }