Skip to content

Commit 1ee1989

Browse files
committed
fix: nuxt 2
1 parent 9df6d15 commit 1ee1989

File tree

7 files changed

+229
-35
lines changed

7 files changed

+229
-35
lines changed

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,30 @@ It is also possible to not override the values but instead specify the field nam
8585
```js
8686
export default {
8787
modules: [
88+
'@nuxt/content',
8889
['nuxt-content-git', {
8990
createdAtName: 'gitCreatedAt',
9091
updatedAtName: 'gitUpdatedAt',
9192
}],
92-
'@nuxt/content',
9393
],
9494
}
9595
```
9696

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

99+
## Nuxt 2
100+
101+
For Nuxt 2 you need to add the module _before_ `@nuxt/content`:
102+
103+
```js
104+
export default {
105+
modules: [
106+
'nuxt-content-git',
107+
'@nuxt/content',
108+
},
109+
}
110+
```
111+
99112
## Deployment
100113

101114
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.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"fs-extra": "^11.1.1",
5353
"nuxt": "^3.5.3",
5454
"nuxt-dev-ready": "^2.0.1",
55+
"ora": "^6.3.1",
5556
"output-files": "^2.0.0",
5657
"tree-kill-promise": "^3.0.14"
5758
},

src/index.js

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,39 @@
1-
import { addServerPlugin, createResolver, defineNuxtModule } from '@nuxt/kit'
1+
import {
2+
addServerPlugin,
3+
createResolver,
4+
isNuxt3 as isNuxt3Try,
5+
} from '@nuxt/kit'
6+
import P from 'path'
7+
8+
import setVariables from './set-variables.js'
29

310
const resolver = createResolver(import.meta.url)
411

5-
export default defineNuxtModule({
6-
setup: (options, nuxt) => {
7-
nuxt.options.runtimeConfig.nuxtContentGit = {
8-
createdAtName: 'createdAt',
9-
updatedAtName: 'updatedAt',
10-
...nuxt.options.runtimeConfig.nuxtContentGit,
11-
...nuxt.options.nuxtContentGit,
12-
...options,
13-
}
12+
export default function (options, nuxt) {
13+
let isNuxt3 = true
14+
try {
15+
isNuxt3 = isNuxt3Try()
16+
} catch {
17+
isNuxt3 = false
18+
}
19+
nuxt = nuxt || this
20+
options = {
21+
createdAtName: 'createdAt',
22+
updatedAtName: 'updatedAt',
23+
...(isNuxt3 && nuxt.options.runtimeConfig.nuxtContentGit),
24+
...nuxt.options.nuxtContentGit,
25+
...options,
26+
}
27+
if (isNuxt3) {
28+
nuxt.options.runtimeConfig.nuxtContentGit = options
1429
addServerPlugin(resolver.resolve('./server-plugin.js'))
15-
},
16-
})
30+
} else {
31+
this.nuxt.hook('content:file:beforeInsert', (file, database) =>
32+
setVariables(
33+
file,
34+
P.join(database.dir, `${file.path}${file.extension}`),
35+
options,
36+
),
37+
)
38+
}
39+
}

src/index.spec.js

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1-
import { endent, first, last, pick, property } from '@dword-design/functions'
1+
import {
2+
endent,
3+
first,
4+
last,
5+
pick,
6+
property,
7+
} from '@dword-design/functions'
28
import tester from '@dword-design/tester'
39
import testerPluginTmpDir from '@dword-design/tester-plugin-tmp-dir'
410
import axios from 'axios'
511
import packageName from 'depcheck-package-name'
6-
import { execaCommand } from 'execa'
12+
import { execa, execaCommand } from 'execa'
713
import fs from 'fs-extra'
814
import nuxtDevReady from 'nuxt-dev-ready'
15+
import ora from 'ora'
916
import outputFiles from 'output-files'
1017
import P from 'path'
1118
import simpleGit from 'simple-git'
@@ -99,6 +106,58 @@ export default tester(
99106
await kill(nuxt.pid)
100107
}
101108
},
109+
nuxt2: async () => {
110+
await execaCommand('git init')
111+
await execaCommand('git config user.email "[email protected]"')
112+
await execaCommand('git config user.name "foo"')
113+
await outputFiles({
114+
'content/home.md': '',
115+
'nuxt.config.js': endent`
116+
export default {
117+
modules: [
118+
'~/../src/index.js',
119+
'${packageName`@nuxt/content`}',
120+
],
121+
}
122+
`,
123+
})
124+
await execaCommand('git add .')
125+
await execaCommand('git commit -m init')
126+
await fs.outputFile('content/home.md', 'foo')
127+
await execaCommand('git add .')
128+
await execaCommand('git commit -m update')
129+
130+
const git = simpleGit()
131+
132+
const log = await git.log({
133+
file: P.join('content', 'home.md'),
134+
})
135+
136+
const createdAt = new Date(log.all |> last |> property('date'))
137+
138+
const updatedAt = new Date(log.latest.date)
139+
await fs.remove('node_modules')
140+
await fs.symlink(
141+
P.join('..', 'node_modules', '.cache', 'nuxt2', 'node_modules'),
142+
'node_modules',
143+
)
144+
145+
const nuxt = execa(P.join('node_modules', '.bin', 'nuxt'), ['dev'])
146+
try {
147+
await nuxtDevReady()
148+
expect(
149+
axios.get('http://localhost:3000/_content/home')
150+
|> await
151+
|> property('data')
152+
|> pick(['createdAt', 'updatedAt']),
153+
).toEqual({
154+
createdAt: createdAt.toISOString(),
155+
updatedAt: updatedAt.toISOString(),
156+
})
157+
} finally {
158+
await kill(nuxt.pid)
159+
}
160+
},
102161
works: async () => {
103162
await execaCommand('git init')
104163
await execaCommand('git config user.email "[email protected]"')
@@ -149,7 +208,17 @@ export default tester(
149208
[
150209
testerPluginTmpDir(),
151210
{
152-
before: () => execaCommand('base prepublishOnly'),
211+
before: async () => {
212+
const spinner = ora('Installing Nuxt 2').start()
213+
await fs.outputFile(
214+
P.join('node_modules', '.cache', 'nuxt2', 'package.json'),
215+
JSON.stringify({}),
216+
)
217+
await execaCommand('yarn add nuxt@^2 @nuxt/content@^1', {
218+
cwd: P.join('node_modules', '.cache', 'nuxt2'),
219+
})
220+
spinner.stop()
221+
},
153222
},
154223
{
155224
beforeEach: async () => {

src/server-plugin.js

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
1-
import { last } from '@dword-design/functions'
21
import P from 'path'
3-
import simpleGit from 'simple-git'
42

53
import { defineNitroPlugin, useRuntimeConfig } from '#imports'
64

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

12-
const log = await git.log({
13-
file: P.join('content', file._file),
14-
})
15-
file[options.createdAtName] =
16-
log.all.length > 0 ? new Date(last(log.all).date) : undefined
17-
file[options.updatedAtName] =
18-
log.latest === null ? undefined : new Date(log.latest.date)
19-
})
20-
})
7+
export default defineNitroPlugin(nitroApp =>
8+
nitroApp.hooks.hook('content:file:afterParse', file =>
9+
setVariables(
10+
file,
11+
P.join('content', file._file),
12+
useRuntimeConfig().nuxtContentGit,
13+
),
14+
),
15+
)

src/set-variables.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { last } from '@dword-design/functions'
2+
import simpleGit from 'simple-git'
3+
4+
export default async (file, path, options) => {
5+
const git = simpleGit()
6+
7+
const log = await git.log({
8+
file: path,
9+
})
10+
file[options.createdAtName] =
11+
log.all.length > 0 ? new Date(last(log.all).date) : file.createdAt
12+
file[options.updatedAtName] =
13+
log.latest === null ? file.updatedAt : new Date(log.latest.date)
14+
}

0 commit comments

Comments
 (0)