Skip to content

Commit

Permalink
feat: add builder versions to module.json
Browse files Browse the repository at this point in the history
resolves #18
  • Loading branch information
danielroe committed Apr 25, 2024
1 parent a39c183 commit f8567a3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
10 changes: 9 additions & 1 deletion src/commands/build.ts
@@ -1,14 +1,16 @@
import { existsSync, promises as fsp } from 'node:fs'
import { pathToFileURL } from 'node:url'
import { dirname, resolve } from 'pathe'
import { readTSConfig } from 'pkg-types'
import { readPackageJSON, readTSConfig } from 'pkg-types'
import type { TSConfig } from 'pkg-types'
import { defu } from 'defu'
import { consola } from 'consola'
import type { ModuleMeta, NuxtModule } from '@nuxt/schema'
import { findExports } from 'mlly'
import { defineCommand } from 'citty'

import { name, version } from '../../package.json'

export default defineCommand({
meta: {
name: 'build',
Expand Down Expand Up @@ -107,6 +109,12 @@ export default defineCommand({
}
}

// Add module builder metadata
moduleMeta.builder = {
[name]: version,
'unbuild': await readPackageJSON('unbuild').then(r => r.version).catch(() => 'unknown'),

Check failure on line 115 in src/commands/build.ts

View workflow job for this annotation

GitHub Actions / ci

Properties shouldn't be quoted as all quotes are redundant
}

// Write meta
const metaFile = resolve(ctx.options.outDir, 'module.json')
await fsp.writeFile(metaFile, JSON.stringify(moduleMeta, null, 2), 'utf8')
Expand Down
11 changes: 9 additions & 2 deletions test/build.spec.ts
Expand Up @@ -2,7 +2,9 @@ import { fileURLToPath } from 'node:url'
import { readFile, readdir } from 'node:fs/promises'
import { beforeAll, describe, it, expect } from 'vitest'
import { execaCommand } from 'execa'
import { readPackageJSON } from 'pkg-types'
import { join } from 'pathe'
import { version } from '../package.json'

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

it('should generate module metadata as separate JSON file', async () => {
const meta = await readFile(join(distDir, 'module.json'), 'utf-8')
expect(JSON.parse(meta)).toMatchInlineSnapshot(`
const unbuildPkg = await readPackageJSON('unbuild')
expect(JSON.parse(meta)).toMatchObject(
{
"builder": {

Check failure on line 83 in test/build.spec.ts

View workflow job for this annotation

GitHub Actions / ci

Properties shouldn't be quoted as all quotes are redundant

Check failure on line 83 in test/build.spec.ts

View workflow job for this annotation

GitHub Actions / ci

Strings must use singlequote
"@nuxt/module-builder": version,

Check failure on line 84 in test/build.spec.ts

View workflow job for this annotation

GitHub Actions / ci

Strings must use singlequote
"unbuild": unbuildPkg.version,

Check failure on line 85 in test/build.spec.ts

View workflow job for this annotation

GitHub Actions / ci

Strings must use singlequote
},
"configKey": "myModule",

Check failure on line 87 in test/build.spec.ts

View workflow job for this annotation

GitHub Actions / ci

Properties shouldn't be quoted as all quotes are redundant

Check failure on line 87 in test/build.spec.ts

View workflow job for this annotation

GitHub Actions / ci

Strings must use singlequote

Check failure on line 87 in test/build.spec.ts

View workflow job for this annotation

GitHub Actions / ci

Strings must use singlequote
"name": "my-module",

Check failure on line 88 in test/build.spec.ts

View workflow job for this annotation

GitHub Actions / ci

Properties shouldn't be quoted as all quotes are redundant

Check failure on line 88 in test/build.spec.ts

View workflow job for this annotation

GitHub Actions / ci

Strings must use singlequote
"version": "1.0.0",
}
`)
)
})

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

0 comments on commit f8567a3

Please sign in to comment.