Skip to content

Commit

Permalink
Record src directory usage with version (vercel#9023)
Browse files Browse the repository at this point in the history
* Add tracking src dir usage to telemetry

* Move isSrcDir back to eventVersion

* Move spinner back

* Add test for isSrcDir telemetry

* Add test for dev mode
  • Loading branch information
ijjk authored and Timer committed Oct 23, 2019
1 parent 4328eea commit d38da87
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 4 deletions.
7 changes: 6 additions & 1 deletion packages/next/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,12 @@ export default async function build(dir: string, conf = null): Promise<void> {

let backgroundWork: (Promise<any> | undefined)[] = []
backgroundWork.push(
telemetry.record(eventVersion({ cliCommand: 'build' })),
telemetry.record(
eventVersion({
cliCommand: 'build',
isSrcDir: path.relative(dir, pagesDir!).startsWith('src'),
})
),
eventNextPlugins(path.resolve(dir)).then(events => telemetry.record(events))
)

Expand Down
2 changes: 1 addition & 1 deletion packages/next/export/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export default async function(
const distDir = join(dir, nextConfig.distDir)
if (!options.buildExport) {
const telemetry = new Telemetry({ distDir })
telemetry.record(eventVersion({ cliCommand: 'export' }))
telemetry.record(eventVersion({ cliCommand: 'export', isSrcDir: null }))
}

const subFolders = nextConfig.exportTrailingSlash
Expand Down
7 changes: 6 additions & 1 deletion packages/next/server/next-dev-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,12 @@ export default class DevServer extends Server {
this.setDevReady!()

const telemetry = new Telemetry({ distDir: this.distDir })
telemetry.record(eventVersion({ cliCommand: 'dev' }))
telemetry.record(
eventVersion({
cliCommand: 'dev',
isSrcDir: relative(this.dir, this.pagesDir!).startsWith('src'),
})
)
}

protected async close() {
Expand Down
2 changes: 2 additions & 0 deletions packages/next/telemetry/events/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ type EventCliSessionStarted = {
nextVersion: string
nodeVersion: string
cliCommand: string
isSrcDir: boolean | null
}

export function eventVersion(
Expand All @@ -21,6 +22,7 @@ export function eventVersion(
nextVersion: process.env.__NEXT_VERSION,
nodeVersion: process.version,
cliCommand: event.cliCommand,
isSrcDir: event.isSrcDir,
} as EventCliSessionStarted,
},
]
Expand Down
68 changes: 67 additions & 1 deletion test/integration/telemetry/test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
/* eslint-env jest */
/* global jasmine */
import { runNextCommand } from 'next-test-utils'
import path from 'path'
import fs from 'fs-extra'
import {
runNextCommand,
launchApp,
findPort,
killApp,
waitFor
} from 'next-test-utils'

jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2

const appDir = path.join(__dirname, '..')

describe('Telemetry CLI', () => {
it('can print telemetry status', async () => {
const { stdout } = await runNextCommand(['telemetry'], {
Expand Down Expand Up @@ -59,4 +69,60 @@ describe('Telemetry CLI', () => {
expect(stdout).toMatch(/already disabled/)
expect(stdout).toMatch(/Status: Disabled/)
})

it('detects isSrcDir dir correctly for `next build`', async () => {
const { stderr } = await runNextCommand(['build', appDir], {
stderr: true,
env: {
NEXT_TELEMETRY_DEBUG: 1
}
})

expect(stderr).toMatch(/isSrcDir.*?false/)

await fs.move(path.join(appDir, 'pages'), path.join(appDir, 'src/pages'))
const { stderr: stderr2 } = await runNextCommand(['build', appDir], {
stderr: true,
env: {
NEXT_TELEMETRY_DEBUG: 1
}
})
await fs.move(path.join(appDir, 'src/pages'), path.join(appDir, 'pages'))

expect(stderr2).toMatch(/isSrcDir.*?true/)
})

it('detects isSrcDir dir correctly for `next dev`', async () => {
let port = await findPort()
let stderr = ''

const handleStderr = msg => {
stderr += msg
}
let app = await launchApp(appDir, port, {
onStderr: handleStderr,
env: {
NEXT_TELEMETRY_DEBUG: 1
}
})
await waitFor(1000)
await killApp(app)
expect(stderr).toMatch(/isSrcDir.*?false/)

await fs.move(path.join(appDir, 'pages'), path.join(appDir, 'src/pages'))
stderr = ''

port = await findPort()
app = await launchApp(appDir, port, {
onStderr: handleStderr,
env: {
NEXT_TELEMETRY_DEBUG: 1
}
})
await waitFor(1000)
await killApp(app)
await fs.move(path.join(appDir, 'src/pages'), path.join(appDir, 'pages'))

expect(stderr).toMatch(/isSrcDir.*?true/)
})
})

0 comments on commit d38da87

Please sign in to comment.