Skip to content

Commit

Permalink
chore(cli): make @redwoodjs/cli-data-migrate package size smaller (#…
Browse files Browse the repository at this point in the history
…9085)

Follow up to #8572. Even if I
remove `@redwoodjs/babel-config`'s dependency on `@redwoodjs/structure`,
both `@redwoodjs/cli-helpers` and `@redwoodjs/telemetry` depend on it. I
have to choose my battles here, so it feels like we should just remove
cli-helpers and telemetry from this cli-data-migrate for now.
  • Loading branch information
jtoar committed Sep 2, 2023
1 parent cae5cb3 commit be56799
Show file tree
Hide file tree
Showing 14 changed files with 145 additions and 68 deletions.
4 changes: 2 additions & 2 deletions packages/babel-config/dist.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ describe('dist', () => {
"proposals": true,
"version": 3,
},
"version": "7.22.10",
"version": "7.22.11",
},
"CORE_JS_VERSION": "3.32",
"RUNTIME_CORE_JS_VERSION": "7.22.10",
"RUNTIME_CORE_JS_VERSION": "7.22.11",
"TARGETS_NODE": "18.16",
"getApiSideBabelConfigPath": [Function],
"getApiSideBabelPlugins": [Function],
Expand Down
6 changes: 4 additions & 2 deletions packages/babel-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
},
"dependencies": {
"@babel/core": "7.22.11",
"@babel/parser": "7.22.13",
"@babel/plugin-transform-class-properties": "7.22.5",
"@babel/plugin-transform-private-methods": "7.22.5",
"@babel/plugin-transform-private-property-in-object": "7.22.11",
Expand All @@ -31,13 +32,14 @@
"@babel/preset-typescript": "7.22.11",
"@babel/register": "7.22.5",
"@babel/runtime-corejs3": "7.22.11",
"@babel/traverse": "7.22.11",
"@redwoodjs/project-config": "6.1.0",
"@redwoodjs/structure": "6.1.0",
"babel-plugin-auto-import": "1.1.0",
"babel-plugin-graphql-tag": "3.3.0",
"babel-plugin-module-resolver": "5.0.0",
"core-js": "3.32.0",
"fast-glob": "3.3.1"
"fast-glob": "3.3.1",
"graphql": "16.8.0"
},
"devDependencies": {
"@types/babel-plugin-tester": "9.0.5",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import plugin from '../babel-plugin-redwood-mock-cell-data'

describe('babel plugin redwood mock cell data', () => {
const __fixtures__ = path.resolve(__dirname, '../../../../../__fixtures__')
process.env.RWJS_CWD = path.join(__fixtures__, 'example-todo-main')

pluginTester({
plugin,
Expand Down
147 changes: 132 additions & 15 deletions packages/babel-config/src/plugins/babel-plugin-redwood-mock-cell-data.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import fs from 'fs'
import path from 'path'

// TODO: Figure out why Wallaby doesn't work with a normal import.
import type { PluginObj, types } from '@babel/core'

import { getBaseDirFromFile } from '@redwoodjs/project-config'
import { getProject, URL_file } from '@redwoodjs/structure'
import { types } from '@babel/core'
import type { PluginObj } from '@babel/core'
import { parse as babelParse } from '@babel/parser'
import type { ParserPlugin } from '@babel/parser'
import traverse from '@babel/traverse'
import fg from 'fast-glob'
import { parse as graphqlParse } from 'graphql'

export default function ({ types: t }: { types: typeof types }): PluginObj {
let nodesToRemove: any[] = []
Expand Down Expand Up @@ -49,7 +52,6 @@ export default function ({ types: t }: { types: typeof types }): PluginObj {
// 4. The Cell has a operation name for the QUERY export.

const d = p.node.declaration
const filename = state.file.opts.filename

let mockFunction

Expand Down Expand Up @@ -107,40 +109,45 @@ export default function ({ types: t }: { types: typeof types }): PluginObj {
}

// Find the model of the Cell that is in the same directory.
const dir = URL_file(path.dirname(state.file.opts.filename))
const project = getProject(getBaseDirFromFile(filename))
const cell = project.cells.find((path: { uri: string }) => {
return path.uri.startsWith(dir)
const dirname = path.dirname(state.file.opts.filename)
const cellName = path.basename(dirname)

const [cellPath] = fg.sync(`${cellName}.{js,jsx,ts,tsx}`, {
cwd: dirname,
absolute: true,
ignore: ['node_modules'],
})

if (!cell || !cell?.filePath) {
if (!cellPath) {
return
}

if (!cell.queryOperationName) {
const cellMetadata = getCellMetadata(cellPath)

if (cellMetadata.hasDefaultExport || !cellMetadata.hasQueryExport) {
return
}

// mockGraphQLQuery(<operationName>, <mockFunction>)
const mockGraphQLCall = t.callExpression(
t.identifier('mockGraphQLQuery'),
[t.stringLiteral(cell.queryOperationName), mockFunction]
[t.stringLiteral(cellMetadata.operationName), mockFunction]
)

// Delete original "export const standard"
nodesToRemove = [...nodesToRemove, p]

// + import { afterQuery } from './${cellFileName}'
// + export const standard = () => afterQuery(...)
if (cell.exportedSymbols.has('afterQuery')) {
if (cellMetadata.hasAfterQueryExport) {
const importAfterQuery = t.importDeclaration(
[
t.importSpecifier(
t.identifier('afterQuery'),
t.identifier('afterQuery')
),
],
t.stringLiteral(`./${path.basename(cell.filePath)}`)
t.stringLiteral(`./${path.basename(cellPath)}`)
)

nodesToInsert = [
Expand All @@ -166,3 +173,113 @@ export default function ({ types: t }: { types: typeof types }): PluginObj {
},
}
}

export const getCellMetadata = (p: string) => {
const ast = getCellAst(p)

let hasDefaultExport = false
const namedExports: NamedExports[] = []
let operation

traverse(ast, {
ExportDefaultDeclaration() {
hasDefaultExport = true
return
},
ExportNamedDeclaration(path) {
// Re-exports from other modules
// Eg: export { a, b } from './module'
const specifiers = path.node?.specifiers

if (specifiers.length) {
for (const s of specifiers) {
const id = s.exported as types.Identifier
namedExports.push({
name: id.name,
type: 're-export',
})
}
return
}

const declaration = path.node.declaration

if (!declaration) {
return
}

if (declaration.type === 'VariableDeclaration') {
const id = declaration.declarations[0].id as types.Identifier

namedExports.push({
name: id.name as string,
type: 'variable',
})
} else if (declaration.type === 'FunctionDeclaration') {
namedExports.push({
name: declaration?.id?.name as string,
type: 'function',
})
} else if (declaration.type === 'ClassDeclaration') {
namedExports.push({
name: declaration?.id?.name,
type: 'class',
})
}
},
TaggedTemplateExpression(path) {
// @ts-expect-error wip
if (path.parent?.id?.name !== 'QUERY') {
return
}

operation = path.node.quasi.quasis[0].value.raw
},
})

const hasQueryExport = namedExports.find(({ name }) => name === 'QUERY')
const hasAfterQueryExport = namedExports.find(
({ name }) => name === 'afterQuery'
)

let operationName = ''

if (operation) {
const document = graphqlParse(operation)

for (const definition of document.definitions) {
if (definition.kind === 'OperationDefinition' && definition.name?.value) {
operationName = definition.name.value
}
}
}

return {
hasDefaultExport,
namedExports,
hasQueryExport,
hasAfterQueryExport,
operationName,
}
}

function getCellAst(filePath: string): types.Node {
const code = fs.readFileSync(filePath, 'utf-8')
const plugins = ['typescript', 'jsx'].filter(Boolean) as ParserPlugin[]

try {
return babelParse(code, {
sourceType: 'module',
plugins,
})
} catch (e: any) {
console.error(`Error parsing: ${filePath}`)
console.error(e)
throw new Error(e?.message) // we throw, so typescript doesn't complain about returning
}
}

interface NamedExports {
name: string
type: 're-export' | 'variable' | 'function' | 'class'
}
3 changes: 1 addition & 2 deletions packages/babel-config/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
},
"include": ["src"],
"references": [
{ "path": "../project-config" },
{ "path": "../structure" }
{ "path": "../project-config" }
]
}
2 changes: 0 additions & 2 deletions packages/cli-packages/dataMigrate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@
},
"dependencies": {
"@redwoodjs/babel-config": "6.1.0",
"@redwoodjs/cli-helpers": "6.1.0",
"@redwoodjs/project-config": "6.1.0",
"@redwoodjs/telemetry": "6.1.0",
"chalk": "4.1.2",
"dotenv-defaults": "5.0.2",
"execa": "5.1.1",
Expand Down
11 changes: 0 additions & 11 deletions packages/cli-packages/dataMigrate/src/__tests__/install.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { recordTelemetryAttributes } from '@redwoodjs/cli-helpers'

import * as installCommand from '../commands/install'
import { handler as dataMigrateInstallHandler } from '../commands/installHandler.js'

jest.mock('@redwoodjs/cli-helpers')
jest.mock(
'../commands/installHandler.js',
() => ({
Expand Down Expand Up @@ -36,14 +33,6 @@ describe('install', () => {
)
})

it('`handler` records telemetry attributes', async () => {
await installCommand.handler()

expect(recordTelemetryAttributes).toHaveBeenCalledWith({
command: 'data-migrate install',
})
})

it('`handler` proxies to `./installHandler.js`', async () => {
await installCommand.handler()
expect(dataMigrateInstallHandler).toHaveBeenCalled()
Expand Down
10 changes: 0 additions & 10 deletions packages/cli-packages/dataMigrate/src/__tests__/up.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { vol } from 'memfs'
import yargs from 'yargs/yargs'

import { recordTelemetryAttributes } from '@redwoodjs/cli-helpers'
import { getPaths } from '@redwoodjs/project-config'

import * as upCommand from '../commands/up'
import { handler as dataMigrateUpHandler } from '../commands/upHandler.js'

jest.mock('fs', () => require('memfs').fs)
jest.mock('@redwoodjs/cli-helpers')
jest.mock(
'../commands/upHandler.js',
() => ({
Expand Down Expand Up @@ -47,14 +45,6 @@ describe('up', () => {
expect(argv).toHaveProperty('dist-path', getPaths().api.dist)
})

it('`handler` records telemetry attributes', async () => {
await upCommand.handler({})

expect(recordTelemetryAttributes).toHaveBeenCalledWith({
command: 'data-migrate up',
})
})

it('`handler` proxies to `./upHandler.js`', async () => {
await upCommand.handler({})
expect(dataMigrateUpHandler).toHaveBeenCalled()
Expand Down
6 changes: 0 additions & 6 deletions packages/cli-packages/dataMigrate/src/commands/install.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import terminalLink from 'terminal-link'
import type { Argv } from 'yargs'

import { recordTelemetryAttributes } from '@redwoodjs/cli-helpers'

export const command = 'install'
export const description = 'Add the RW_DataMigration model to your schema'

Expand All @@ -16,10 +14,6 @@ export function builder(yargs: Argv): Argv {
}

export async function handler(): Promise<void> {
recordTelemetryAttributes({
command: 'data-migrate install',
})

const { handler: dataMigrateInstallHandler } = await import(
'./installHandler.js'
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import fs from 'fs-extra'
import { Listr } from 'listr2'

import { getPaths } from '@redwoodjs/project-config'
import { errorTelemetry } from '@redwoodjs/telemetry'

import c from '../lib/colors'

Expand Down Expand Up @@ -59,7 +58,6 @@ export async function handler() {
} catch (e) {
process.exitCode = 1
console.error(c.error((e as Error).message))
errorTelemetry(process.argv, (e as Error).message)
}
}

Expand Down
6 changes: 0 additions & 6 deletions packages/cli-packages/dataMigrate/src/commands/up.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import terminalLink from 'terminal-link'
import type { Argv } from 'yargs'

import { recordTelemetryAttributes } from '@redwoodjs/cli-helpers'
import { getPaths } from '@redwoodjs/project-config'

import { DataMigrateUpOptions } from '../types'
Expand Down Expand Up @@ -33,11 +32,6 @@ export function builder(yargs: Argv): Argv {
}

export async function handler(options: DataMigrateUpOptions): Promise<void> {
recordTelemetryAttributes({
command: 'data-migrate up',
dbFromDist: options.importDbClientFromDist,
})

const { handler: dataMigrateUpHandler } = await import('./upHandler.js')
await dataMigrateUpHandler(options)
}
3 changes: 0 additions & 3 deletions packages/cli-packages/dataMigrate/src/commands/upHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { Listr } from 'listr2'

import { registerApiSideBabelHook } from '@redwoodjs/babel-config'
import { getPaths } from '@redwoodjs/project-config'
import { errorTelemetry } from '@redwoodjs/telemetry'

import c from '../lib/colors'
import type { DataMigrateUpOptions, DataMigration } from '../types'
Expand Down Expand Up @@ -120,8 +119,6 @@ export async function handler({
console.log()
reportDataMigrations(counters)
console.log()

errorTelemetry(process.argv, (e as Error).message)
}
}

Expand Down
Loading

0 comments on commit be56799

Please sign in to comment.