Skip to content

Commit

Permalink
For discussion of issue nuejs#198 (dont merge!)
Browse files Browse the repository at this point in the history
  • Loading branch information
goblinfactory committed Feb 15, 2024
1 parent afb3af3 commit c0aaa9a
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 33 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
node_modules
dist
.idea
.vscode
_test
15 changes: 15 additions & 0 deletions packages/nuekit/src/isLegit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/** Returns a function that will check if a file is not ignored and is legit.*/
export function makeIsLegit(ignores = []) {
const IGNORE = ['node_modules', 'package.json', 'bun.lockb', 'pnpm-lock.yaml', ...ignores]

function ignore(name='') {
return '._'.includes(name[0]) || IGNORE.includes(name)
}

function isLegit(file) {
return !ignore(file.name) && !ignore(file.dir)
}

// TODO: real symdir detection
return isLegit
}
27 changes: 8 additions & 19 deletions packages/nuekit/src/nuefs.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

import { watch, promises as fs } from 'node:fs'
import { join, parse, sep } from 'node:path'
import { makeIsLegit } from './isLegit'

/*
Super minimalistic file system watcher.
Expand All @@ -15,7 +16,10 @@ import { join, parse, sep } from 'node:path'
// avoid double events and looping (seen on Bun only)
let last = {}

export async function fswatch(dir, onfile, onremove) {
export async function fswatch(dir, ignores, onfile, onremove) {

const isLegit = makeIsLegit(ignores)

watch(dir, { recursive: true }, async function(e, path) {
try {
const file = parse(path)
Expand All @@ -30,7 +34,7 @@ export async function fswatch(dir, onfile, onremove) {
const stat = await fs.lstat(join(dir, path))

if (stat.isDirectory()) {
const paths = await fswalk(dir, path)
const paths = await fswalk(isLegit, dir, path)

// deploy everything on the directory
for (const path of paths) {
Expand All @@ -54,34 +58,19 @@ export async function fswatch(dir, onfile, onremove) {



export async function fswalk(root, _dir='', _ret=[]) {
export async function fswalk(isLegit, root, _dir='', _ret=[]) {
const files = await fs.readdir(join(root, _dir), { withFileTypes: true })

for (const f of files) {
if (isLegit(f)) {
const path = join(_dir, f.name)
if (isDir(f)) await fswalk(root, path, _ret)
if (isDir(f)) await fswalk(isLegit, root, path, _ret)
else _ret.push(path)
}
}
return _ret
}


const IGNORE = ['node_modules', 'package.json', 'bun.lockb', 'pnpm-lock.yaml']

function ignore(name='') {
return '._'.includes(name[0]) || IGNORE.includes(name)
}

function isLegit(file) {
return !ignore(file.name) && !ignore(file.dir)
}

// TODO: real symdir detection
function isDir(f) {
return f.isDirectory() || f.isSymbolicLink() && !f.name.includes('.')
}



2 changes: 1 addition & 1 deletion packages/nuekit/src/nuekit.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ export async function createKit(args) {
}

async function stats() {
const rows = await readStats(dist, site.globals)
const rows = await readStats(site.isLegit, dist, site.globals)
printTable(['Page', 'HTML', 'CSS', 'JS'], rows)
return rows
}
Expand Down
14 changes: 9 additions & 5 deletions packages/nuekit/src/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { log, getParts, getAppDir, getDirs, colors, getPosixPath } from './util.
import { parse as parseNue } from 'nuejs-core'
import { promises as fs } from 'node:fs'
import { fswalk } from './nuefs.js'
import { makeIsLegit} from './isLegit'
import { nuemark } from 'nuemark'
import yaml from 'js-yaml'

Expand Down Expand Up @@ -49,7 +50,6 @@ export async function createSite(args) {

async function readOpts() {
const data = await readData('site.yaml') || {}

// environment
try {
if (env) Object.assign(data, await readData(env))
Expand All @@ -61,7 +61,11 @@ export async function createSite(args) {
}

let site_data = await readOpts()
const self = { globals: site_data.globals || [] }
const self = {
globals: site_data.globals || [],
ignores: site_data.ignores || [],
isLegit: makeIsLegit(site_data.ignores),
}

const {
dist = `${root}/.dist/${is_prod ? 'prod' : 'dev'}`,
Expand Down Expand Up @@ -117,7 +121,7 @@ export async function createSite(args) {

for (const dir of dirs) {
try {
const paths = self.globals.includes(dir) ? await fswalk(join(root, dir)) : await fs.readdir(join(root, dir))
const paths = self.globals.includes(dir) ? await fswalk(self.isLegit, join(root, isLegit, dir)) : await fs.readdir(join(root, dir))

paths.filter(path => {
const ext = extname(path).slice(1)
Expand Down Expand Up @@ -157,7 +161,7 @@ export async function createSite(args) {
}

self.walk = async function() {
return await fswalk(root)
return await fswalk(self.isLegit, root)
}

self.getScripts = async function (dir, include=['main.js']) {
Expand All @@ -170,7 +174,7 @@ export async function createSite(args) {
const key = 'coll:' + dir
if (cache[key]) return cache[key]

const paths = await fswalk(join(root, dir))
const paths = await fswalk(self.isLegit, join(root, dir))
const mds = paths.filter(el => el.endsWith('.md')).map(el => join(dir, el))

const arr = []
Expand Down
4 changes: 2 additions & 2 deletions packages/nuekit/src/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ async function readSize(dist, path) {
return raw.length
}

export async function readStats(dist, globals) {
const paths = await fswalk(dist)
export async function readStats(isLegit, dist, globals) {
const paths = await fswalk(isLegit, dist)

async function getSize(appdir, ext) {
let total = 0
Expand Down
28 changes: 22 additions & 6 deletions packages/nuekit/test/nuekit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ expect.extend({ toMatchPath })
const root = '_test'

// setup and teardown
beforeAll(async () => {
beforeEach(async () => {
await fs.rm(root, { recursive: true, force: true })
await fs.mkdir(root, { recursive: true })
})
afterAll(async () => await fs.rm(root, { recursive: true, force: true }))
afterEach(async () => await fs.rm(root, { recursive: true, force: true }))

// helper function for creating files to the root directory
async function write(path, content='') {
Expand Down Expand Up @@ -55,11 +55,13 @@ test('defaults', async () => {
expect(site.dist).toBe('_test/.dist/dev')
expect(site.port).toBe(8080)
expect(site.globals).toEqual([])
expect(site.ignores).toEqual([])
})

const CONF = `
globals: [global]
ignores: [private-folder]
dist: .mydist
port: 1500
title: Hey
Expand All @@ -73,6 +75,7 @@ test('site.yaml', async () => {
expect(site.globals).toEqual(['global'])
expect(site.dist).toBe('.mydist')
expect(site.port).toBe(1500)
expect(site.ignores).toEqual([ "private-folder" ])

// teardown
await fs.rm(join(root, 'site.yaml'))
Expand Down Expand Up @@ -120,6 +123,7 @@ test('get data', async () => {
})

test('content collection', async () => {
await write('site.yaml', CONF)
// This test proves
// ----------------
// 1. Default sorting is on pubDate returning most recent first.
Expand All @@ -129,18 +133,30 @@ test('content collection', async () => {
await write('blog/first-b.md', createFront('Second', '2020-01-04'))
await write('blog/nested/hey1.md', createFront('Third', '2020-01-02'))
await write('blog/nested/hey2.md', createFront('Fourth', '2020-01-03'))
// 4. User defined ignore'd directories at any level are excluded
await write('blog/nested/private-folder/hey3.md', createFront('Fifth', '2020-01-03'))
// 5. System files starting with '_' or '.' are excluded.
await write('blog/.item6.md', createFront('Sixth', '2020-01-03'))
await write('blog/_item7.md', createFront('Seventh', '2020-01-03'))
// 6. User defined ignore is an exact match, not partial, so the following should not be ignored.
await write('blog/nested/not-private-folder/item8.md', createFront('Eighth', '2020-01-01'))

const site = await getSite()

const coll = await site.getContentCollection('blog')
const actual = coll.map(c => {
return { pubDate: c.pubDate, url: c.url, title: c.title, dir: c.dir, slug: c.slug }
})
// expected order is : First, Second, Fourth, Third.
// expected order is : First, Second, Fourth, Third, Eigth
// The rest are are ignored due to custom ignore in test's site.yaml -> ignore: [private-folder]
expect(actual).toEqual([
{ pubDate: undefined, url: '/blog/first-a.html', title: 'First', dir: 'blog', slug: 'first-a.html' },
{ pubDate: new Date('2020-01-04T00:00:00.000Z'), url: '/blog/first-b.html', title: 'Second', dir: 'blog', slug: 'first-b.html' },
{ pubDate: new Date('2020-01-03T00:00:00.000Z'), url: '/blog/nested/hey2.html', title: 'Fourth', dir: 'blog/nested', slug: 'hey2.html' },
{ pubDate: new Date('2020-01-02T00:00:00.000Z'), url: '/blog/nested/hey1.html', title: 'Third', dir: 'blog/nested', slug: 'hey1.html' },
{ pubDate: new Date('2020-01-04'), url: '/blog/first-b.html', title: 'Second', dir: 'blog', slug: 'first-b.html' },
{ pubDate: new Date('2020-01-03'), url: '/blog/nested/hey2.html', title: 'Fourth', dir: 'blog/nested', slug: 'hey2.html' },
{ pubDate: new Date('2020-01-02'), url: '/blog/nested/hey1.html', title: 'Third', dir: 'blog/nested', slug: 'hey1.html' },
{ pubDate: new Date('2020-01-01'), url: '/blog/nested/not-private-folder/item8.html', title: 'Eighth',
dir: 'blog/nested/not-private-folder', slug: 'item8.html'
},
])
})

Expand Down

0 comments on commit c0aaa9a

Please sign in to comment.