Skip to content

Commit f8567a3

Browse files
committed
feat: add builder versions to module.json
resolves #18
1 parent a39c183 commit f8567a3

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/commands/build.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import { existsSync, promises as fsp } from 'node:fs'
22
import { pathToFileURL } from 'node:url'
33
import { dirname, resolve } from 'pathe'
4-
import { readTSConfig } from 'pkg-types'
4+
import { readPackageJSON, readTSConfig } from 'pkg-types'
55
import type { TSConfig } from 'pkg-types'
66
import { defu } from 'defu'
77
import { consola } from 'consola'
88
import type { ModuleMeta, NuxtModule } from '@nuxt/schema'
99
import { findExports } from 'mlly'
1010
import { defineCommand } from 'citty'
1111

12+
import { name, version } from '../../package.json'
13+
1214
export default defineCommand({
1315
meta: {
1416
name: 'build',
@@ -107,6 +109,12 @@ export default defineCommand({
107109
}
108110
}
109111

112+
// Add module builder metadata
113+
moduleMeta.builder = {
114+
[name]: version,
115+
'unbuild': await readPackageJSON('unbuild').then(r => r.version).catch(() => 'unknown'),
116+
}
117+
110118
// Write meta
111119
const metaFile = resolve(ctx.options.outDir, 'module.json')
112120
await fsp.writeFile(metaFile, JSON.stringify(moduleMeta, null, 2), 'utf8')

test/build.spec.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import { fileURLToPath } from 'node:url'
22
import { readFile, readdir } from 'node:fs/promises'
33
import { beforeAll, describe, it, expect } from 'vitest'
44
import { execaCommand } from 'execa'
5+
import { readPackageJSON } from 'pkg-types'
56
import { join } from 'pathe'
7+
import { version } from '../package.json'
68

79
describe('module builder', () => {
810
const rootDir = fileURLToPath(new URL('../example', import.meta.url))
@@ -75,13 +77,18 @@ describe('module builder', () => {
7577

7678
it('should generate module metadata as separate JSON file', async () => {
7779
const meta = await readFile(join(distDir, 'module.json'), 'utf-8')
78-
expect(JSON.parse(meta)).toMatchInlineSnapshot(`
80+
const unbuildPkg = await readPackageJSON('unbuild')
81+
expect(JSON.parse(meta)).toMatchObject(
7982
{
83+
"builder": {
84+
"@nuxt/module-builder": version,
85+
"unbuild": unbuildPkg.version,
86+
},
8087
"configKey": "myModule",
8188
"name": "my-module",
8289
"version": "1.0.0",
8390
}
84-
`)
91+
)
8592
})
8693

8794
it('should generate typed plugin', async () => {

0 commit comments

Comments
 (0)