Skip to content

Commit

Permalink
feat: respect nuxtignore (#386)
Browse files Browse the repository at this point in the history
* chore: fix lint

* fix(twitter): add components dir to Windi scan options (#379)

* docs: fix nuxt-img usage (#383)

* fix(storage): clean deleted contents from storage (#382)

* feat: respect nuxtignore

* fix: revert ohmyfetch to 0.1.8
  • Loading branch information
farnabaz authored Jun 7, 2021
1 parent 76658bb commit 8944c19
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 73 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
"nuxt-i18n": "^6.27.0",
"nuxt-vite": "^0.1.1",
"nuxt-windicss": "^1.0.0",
"ohmyfetch": "^0.2.0",
"ohmyfetch": "^0.1.8",
"plausible-tracker": "^0.3.1",
"prism-theme-vars": "^0.2.2",
"prismjs": "^1.23.0",
Expand Down
5 changes: 3 additions & 2 deletions src/core/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import hash from 'hasha'
import mkdirp from 'mkdirp'
import { DocusDocument, ParserOptions } from '../types'
import { generatePosition, generateSlug, generateTo, isDraft, processDocumentInfo } from './utils/document'
import { destroyStorage, initStorage } from './storage'
import { destroyStorage, initStorage, useNuxtIgnoreList } from './storage'
import { destroyDB, useDB } from './database'
import { createServerMiddleware } from './server'
import { initParser } from './parser'
Expand Down Expand Up @@ -90,7 +90,8 @@ export default <Module>async function docusModule() {
drivers: [
{
base: resolve(options.srcDir, $docus.settings.contentDir),
mountPoint: 'pages'
mountPoint: 'pages',
ignore: await useNuxtIgnoreList(nuxt)
},
{
base: resolve(options.srcDir, 'data'),
Expand Down
95 changes: 40 additions & 55 deletions src/core/storage.ts → src/core/storage/driver.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,39 @@
import { join } from 'path'
import { promises as fsPromises } from 'fs'
import defu from 'defu'
import { createStorage, defineDriver, Driver, Storage } from 'unstorage'
import { defineDriver, Driver } from 'unstorage'
import fsDriver from 'unstorage/drivers/fs'
import { promises as FS } from 'graceful-fs'
import { DocusDocument, DriverOptions, StorageOptions } from '../types'
import { useDB } from './database'
import { useHooks, logger, useParser } from './'
import { DocusDocument, DriverOptions } from '../../types'
import { useDB } from '../database'
import { useHooks } from '../hooks'
import { useParser } from '../parser'

export interface DocusDriver extends Driver {
init(): Promise<void>
}

/**
* Determine whether it is the index file or not
*
* @param path relative to full path of the file
* @returns
*/
const isIndex = path => path.endsWith('index.md')

/**
* Removes the index file name and returns directory path
*
* @param path relative to full path of the file
* @returns
*/
const removeIndex = path => path.replace(/\/index.md$/, '')

// sort keys and put index files at first
/**
* Sort keys and put index files at first
*
* @param keys array of files
* @returns
*/
function sortItemKeys(keys: string[]) {
return [...keys].sort((a, b) => {
const isA = isIndex(a)
Expand All @@ -27,6 +46,11 @@ function sortItemKeys(keys: string[]) {
}

export const docusDriver = defineDriver((options: DriverOptions) => {
// force ignore node_modules and .git and files with `_` prefix
if (options.ignore) {
options.ignore.push('**/node_modules/**', '**/.git/**', '**/_**/**')
}

const { insert, items } = useDB()
const parser = useParser()
const fs = fsDriver(options)
Expand All @@ -35,7 +59,7 @@ export const docusDriver = defineDriver((options: DriverOptions) => {
const document = await parser.parse(key, content)

if (document.extension === '.md') {
const stats = await FS.stat(join(options.base, document.path + document.extension))
const stats = await fsPromises.stat(join(options.base, document.path + document.extension))
document.createdAt = stats.birthtime
document.updatedAt = stats.mtime
}
Expand All @@ -44,7 +68,7 @@ export const docusDriver = defineDriver((options: DriverOptions) => {
document.source = key

// Unify key format
document.key = key.replace(/\//g, ':')
document.key = key

// use prefix in document path
document.path = `/${options.mountPoint}` + document.path
Expand All @@ -64,10 +88,9 @@ export const docusDriver = defineDriver((options: DriverOptions) => {
function getItemParents(key: string): Promise<DocusDocument[]> {
const parts = removeIndex(key).split('/')
const tasks = parts.reduce((parents, _part, index) => {
const path = parts.slice(0, parts.length - 1 - index)
if (!path.join('/')) return parents
const parentKey = path.join('/') + '/index.md'
if (hasItem(parentKey)) {
const path = parts.slice(0, parts.length - 1 - index).join('/')
const parentKey = path + '/index.md'
if (path && hasItem(parentKey)) {
parents.unshift(getItem(parentKey))
}
return parents
Expand All @@ -87,7 +110,7 @@ export const docusDriver = defineDriver((options: DriverOptions) => {
}

// retrive contents list
const getKeys = () => fs.getKeys().map(key => key.replace(/:/g, '/'))
const getKeys = () => fs.getKeys()

const hasItem = key => fs.hasItem(key)

Expand Down Expand Up @@ -130,15 +153,13 @@ export const docusDriver = defineDriver((options: DriverOptions) => {
}

// fetch content keys
let keys = await fs.getKeys()
let keys = await getKeys()

// sort keys to parse index files before others
keys = sortItemKeys(keys)

const tasks = keys.map(async key => {
const content = await fs.getItem(key)
await parseAndInsert(key, content)
})
const tasks = keys.map(key => fs.getItem(key).then(content => parseAndInsert(key, content)))

await Promise.all(tasks)
}

Expand All @@ -154,7 +175,7 @@ export const docusDriver = defineDriver((options: DriverOptions) => {

// remove item from database
if (event === 'remove') {
await items.removeWhere(doc => doc.source === key)
await removeItem(key)
}

// Revalidate childrent of content because parent has changed
Expand All @@ -179,39 +200,3 @@ export const docusDriver = defineDriver((options: DriverOptions) => {
watch
}
})

let _storage: Storage
let drivers: DocusDriver[]
export function initStorage(options: StorageOptions) {
drivers = []
_storage = createStorage()

if (!options?.drivers) {
logger.warn('No driver specified for storage')
} else {
drivers = options.drivers.map(options => {
const driver = docusDriver(options) as DocusDriver
_storage.mount(options.mountPoint, driver)
return driver
})
}

return {
storage: _storage,
drivers,
lazyIndex: () => Promise.all(drivers.map(d => d.init()))
}
}

export async function destroyStorage() {
await _storage.dispose()
_storage = null
drivers = null
}

export function useStorage() {
return {
storage: _storage,
drivers
}
}
12 changes: 12 additions & 0 deletions src/core/storage/ignore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { resolve } from 'path'
import { promises as fsPromises } from 'fs'

export async function useNuxtIgnoreList(nuxt: any): Promise<string[]> {
const ignore = nuxt.options.ignore || []
const ignoreFile = resolve(nuxt.options.rootDir, '.nuxtignore')
const ignoreContent = await fsPromises.readFile(ignoreFile, { encoding: 'utf-8' }).catch(() => '')
if (ignoreContent) {
ignore.push(...ignoreContent.split('\n').filter(Boolean))
}
return ignore
}
3 changes: 3 additions & 0 deletions src/core/storage/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './driver'
export * from './ignore'
export * from './storage'
43 changes: 43 additions & 0 deletions src/core/storage/storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { createStorage, Storage } from 'unstorage'
import { StorageOptions } from '../../types'
import { logger } from '..'
import { docusDriver, DocusDriver } from './driver'

let _storage: Storage
let drivers: DocusDriver[]
export function initStorage(options: StorageOptions) {
drivers = []
_storage = createStorage()

if (!options?.drivers) {
logger.warn('No driver specified for storage')
} else {
drivers = options.drivers.map(options => {
// Initialize driver
const driver = docusDriver(options) as DocusDriver

_storage.mount(options.mountPoint, driver)

return driver
})
}

return {
storage: _storage,
drivers,
lazyIndex: () => Promise.all(drivers.map(d => d.init()))
}
}

export async function destroyStorage() {
await _storage.dispose()
_storage = null
drivers = null
}

export function useStorage() {
return {
storage: _storage,
drivers
}
}
1 change: 1 addition & 0 deletions src/types/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ export interface Colors {

// Storage
export interface DriverOptions {
ignore?: string[]
mountPoint: string
base: string
}
Expand Down
16 changes: 1 addition & 15 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9393,15 +9393,6 @@ ohmyfetch@^0.1.8:
dependencies:
node-fetch "^2.6.1"

ohmyfetch@^0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/ohmyfetch/-/ohmyfetch-0.2.0.tgz#09c03a6c107ddfe6a75c83f0a9cdaa59fb31e54b"
integrity sha512-tbrMD8LauY8xivHmDJItpfth/caVDHmDljeZE+4DzA2sbSp72Vzb//NJD1LtAm3pEnK5hY48i4V/zcraeiS3FQ==
dependencies:
destr "^1.1.0"
node-fetch "^2.6.1"
ufo "^0.6.10"

on-finished@^2.3.0, on-finished@~2.3.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/on-finished/-/on-finished-2.3.0.tgz#20f1336481b083cd75337992a16971aa2d906947"
Expand Down Expand Up @@ -12912,12 +12903,7 @@ ufo@^0.6.10, ufo@^0.6.6:
resolved "https://registry.yarnpkg.com/ufo/-/ufo-0.6.11.tgz#69311ed4abc8ab671c83754b79ce0d396fea1075"
integrity sha512-Yu7TJThwlr23peOkX/+hm6LfkyBs+eDWV880468PTrjKBKjjsNWFFwIuOqDfmXngRo9TZ4+twFYueRH0OLl0Gw==

ufo@^0.7.2:
version "0.7.4"
resolved "https://registry.yarnpkg.com/ufo/-/ufo-0.7.4.tgz#06e971738bea098b95056755ba006a6b73a63f97"
integrity sha512-qFCjO4/IAaejZ6QKVBdM7FZkjhd8zQmBmE6i2bcSwBRrctPVtKXFojJa2flaqNUd7YWQoCFwd44MpOt1g94ekQ==

ufo@^0.7.5:
ufo@^0.7.2, ufo@^0.7.5:
version "0.7.5"
resolved "https://registry.yarnpkg.com/ufo/-/ufo-0.7.5.tgz#5d5b2174747c0072edd30501994c8ac4ad2e77ea"
integrity sha512-FGG+EgguC1oz5dTE1JptPWCyj6Z9mYpwvZY8PTu9Vh/Aoy+Mj9cpeQ3gg4kyEMDbMrH+lTYiw7bomG58B8X7Kg==
Expand Down

0 comments on commit 8944c19

Please sign in to comment.