Skip to content

Commit

Permalink
fix: nuxt 2
Browse files Browse the repository at this point in the history
  • Loading branch information
dword-design committed Jun 8, 2023
1 parent 9df6d15 commit 1ee1989
Show file tree
Hide file tree
Showing 7 changed files with 229 additions and 35 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,30 @@ It is also possible to not override the values but instead specify the field nam
```js
export default {
modules: [
'@nuxt/content',
['nuxt-content-git', {
createdAtName: 'gitCreatedAt',
updatedAtName: 'gitUpdatedAt',
}],
'@nuxt/content',
],
}
```

Then you can access them via `doc.gitCreatedAt` and `doc.gitUpdatedAt`.

## Nuxt 2

For Nuxt 2 you need to add the module _before_ `@nuxt/content`:

```js
export default {
modules: [
'nuxt-content-git',
'@nuxt/content',
},
}
```

## Deployment

The module uses the Git history to calculate the dates. That is why the history also needs to be checked out when deploying the project to live. During local development the repository is usually deeply cloned. But CI systems like GitHub Actions often only do a shallow clone for performance reasons, which will result in wrong dates.
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"fs-extra": "^11.1.1",
"nuxt": "^3.5.3",
"nuxt-dev-ready": "^2.0.1",
"ora": "^6.3.1",
"output-files": "^2.0.0",
"tree-kill-promise": "^3.0.14"
},
Expand Down
47 changes: 35 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,39 @@
import { addServerPlugin, createResolver, defineNuxtModule } from '@nuxt/kit'
import {
addServerPlugin,
createResolver,
isNuxt3 as isNuxt3Try,
} from '@nuxt/kit'
import P from 'path'

import setVariables from './set-variables.js'

const resolver = createResolver(import.meta.url)

export default defineNuxtModule({
setup: (options, nuxt) => {
nuxt.options.runtimeConfig.nuxtContentGit = {
createdAtName: 'createdAt',
updatedAtName: 'updatedAt',
...nuxt.options.runtimeConfig.nuxtContentGit,
...nuxt.options.nuxtContentGit,
...options,
}
export default function (options, nuxt) {
let isNuxt3 = true
try {
isNuxt3 = isNuxt3Try()
} catch {
isNuxt3 = false
}
nuxt = nuxt || this
options = {
createdAtName: 'createdAt',
updatedAtName: 'updatedAt',
...(isNuxt3 && nuxt.options.runtimeConfig.nuxtContentGit),
...nuxt.options.nuxtContentGit,
...options,
}
if (isNuxt3) {
nuxt.options.runtimeConfig.nuxtContentGit = options
addServerPlugin(resolver.resolve('./server-plugin.js'))
},
})
} else {
this.nuxt.hook('content:file:beforeInsert', (file, database) =>
setVariables(
file,
P.join(database.dir, `${file.path}${file.extension}`),
options,
),
)
}
}
75 changes: 72 additions & 3 deletions src/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { endent, first, last, pick, property } from '@dword-design/functions'
import {
endent,
first,
last,
pick,
property,
} from '@dword-design/functions'
import tester from '@dword-design/tester'
import testerPluginTmpDir from '@dword-design/tester-plugin-tmp-dir'
import axios from 'axios'
import packageName from 'depcheck-package-name'
import { execaCommand } from 'execa'
import { execa, execaCommand } from 'execa'
import fs from 'fs-extra'
import nuxtDevReady from 'nuxt-dev-ready'
import ora from 'ora'
import outputFiles from 'output-files'
import P from 'path'
import simpleGit from 'simple-git'
Expand Down Expand Up @@ -99,6 +106,58 @@ export default tester(
await kill(nuxt.pid)
}
},
nuxt2: async () => {
await execaCommand('git init')
await execaCommand('git config user.email "foo@bar.de"')
await execaCommand('git config user.name "foo"')
await outputFiles({
'content/home.md': '',
'nuxt.config.js': endent`
export default {
modules: [
'~/../src/index.js',
'${packageName`@nuxt/content`}',
],
}
`,
})
await execaCommand('git add .')
await execaCommand('git commit -m init')
await fs.outputFile('content/home.md', 'foo')
await execaCommand('git add .')
await execaCommand('git commit -m update')

const git = simpleGit()

const log = await git.log({
file: P.join('content', 'home.md'),
})

const createdAt = new Date(log.all |> last |> property('date'))

const updatedAt = new Date(log.latest.date)
await fs.remove('node_modules')
await fs.symlink(
P.join('..', 'node_modules', '.cache', 'nuxt2', 'node_modules'),
'node_modules',
)

const nuxt = execa(P.join('node_modules', '.bin', 'nuxt'), ['dev'])
try {
await nuxtDevReady()
expect(
axios.get('http://localhost:3000/_content/home')
|> await
|> property('data')
|> pick(['createdAt', 'updatedAt']),
).toEqual({
createdAt: createdAt.toISOString(),
updatedAt: updatedAt.toISOString(),
})
} finally {
await kill(nuxt.pid)
}
},
works: async () => {
await execaCommand('git init')
await execaCommand('git config user.email "foo@bar.de"')
Expand Down Expand Up @@ -149,7 +208,17 @@ export default tester(
[
testerPluginTmpDir(),
{
before: () => execaCommand('base prepublishOnly'),
before: async () => {
const spinner = ora('Installing Nuxt 2').start()
await fs.outputFile(
P.join('node_modules', '.cache', 'nuxt2', 'package.json'),
JSON.stringify({}),
)
await execaCommand('yarn add nuxt@^2 @nuxt/content@^1', {
cwd: P.join('node_modules', '.cache', 'nuxt2'),
})
spinner.stop()
},
},
{
beforeEach: async () => {
Expand Down
25 changes: 10 additions & 15 deletions src/server-plugin.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
import { last } from '@dword-design/functions'
import P from 'path'
import simpleGit from 'simple-git'

import { defineNitroPlugin, useRuntimeConfig } from '#imports'

export default defineNitroPlugin(nitroApp => {
const options = useRuntimeConfig().nuxtContentGit
nitroApp.hooks.hook('content:file:afterParse', async file => {
const git = simpleGit()
import setVariables from './set-variables.js'

const log = await git.log({
file: P.join('content', file._file),
})
file[options.createdAtName] =
log.all.length > 0 ? new Date(last(log.all).date) : undefined
file[options.updatedAtName] =
log.latest === null ? undefined : new Date(log.latest.date)
})
})
export default defineNitroPlugin(nitroApp =>
nitroApp.hooks.hook('content:file:afterParse', file =>
setVariables(
file,
P.join('content', file._file),
useRuntimeConfig().nuxtContentGit,
),
),
)
14 changes: 14 additions & 0 deletions src/set-variables.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { last } from '@dword-design/functions'
import simpleGit from 'simple-git'

export default async (file, path, options) => {
const git = simpleGit()

const log = await git.log({
file: path,
})
file[options.createdAtName] =
log.all.length > 0 ? new Date(last(log.all).date) : file.createdAt
file[options.updatedAtName] =
log.latest === null ? file.updatedAt : new Date(log.latest.date)
}
Loading

0 comments on commit 1ee1989

Please sign in to comment.