Skip to content

Commit

Permalink
feat: add addJapaPlugin to Codemods (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien-R44 committed Nov 25, 2023
1 parent 75d3d52 commit 4b64a59
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 2 deletions.
51 changes: 49 additions & 2 deletions src/code_transformer/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@

import { join } from 'node:path'
import { fileURLToPath } from 'node:url'
import { CodeBlockWriter, Node, Project, SourceFile, SyntaxKind } from 'ts-morph'
import {
CodeBlockWriter,
FormatCodeSettings,
Node,
Project,
QuoteKind,
SourceFile,
SyntaxKind,
} from 'ts-morph'

import { RcFileTransformer } from './rc_file_transformer.js'
import type { AddMiddlewareEntry, EnvValidationDefinition } from '../types.js'
Expand All @@ -31,16 +39,19 @@ export class CodeTransformer {
/**
* Settings to use when persisting files
*/
#editorSettings = {
#editorSettings: FormatCodeSettings = {
indentSize: 2,
convertTabsToSpaces: true,
trimTrailingWhitespace: true,
// @ts-expect-error SemicolonPreference doesn't seem to be re-exported from ts-morph
semicolons: 'remove',
}

constructor(cwd: URL) {
this.#cwd = cwd
this.#project = new Project({
tsConfigFilePath: join(fileURLToPath(this.#cwd), 'tsconfig.json'),
manipulationSettings: { quoteKind: QuoteKind.Single },
})
}

Expand Down Expand Up @@ -177,6 +188,42 @@ export class CodeTransformer {
await file.save()
}

/**
* Add a new Japa plugin in the `tests/bootstrap.ts` file
*/
async addJapaPlugin(
pluginCall: string,
importDeclaration: { isNamed: boolean; module: string; identifier: string }
) {
/**
* Get the `tests/bootstrap.ts` source file
*/
const testBootstrapUrl = fileURLToPath(new URL('./tests/bootstrap.ts', this.#cwd))
const file = this.#project.getSourceFileOrThrow(testBootstrapUrl)

/**
* Add the import declaration
*/
file.addImportDeclaration({
...(importDeclaration.isNamed
? { namedImports: [importDeclaration.identifier] }
: { defaultImport: importDeclaration.identifier }),
moduleSpecifier: importDeclaration.module,
})

/**
* Insert the plugin call in the `plugins` array
*/
const pluginsArray = file
.getVariableDeclaration('plugins')
?.getInitializerIfKind(SyntaxKind.ArrayLiteralExpression)

if (pluginsArray) pluginsArray.addElement(pluginCall)

file.formatText(this.#editorSettings)
await file.save()
}

/**
* Add new env variable validation in the
* `env.ts` file
Expand Down
22 changes: 22 additions & 0 deletions tests/__snapshots__/code_transformer.spec.ts.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -354,3 +354,25 @@ export default defineConfig({
})
"`

exports[`Code transformer | addJapaPlugin > addJapaPlugin with named import 1`] = `"
import app from '@adonisjs/core/services/app'
import { assert } from '@japa/assert'
import { fooPlugin } from '@adonisjs/foo/plugin/japa'
export const plugins: Config['plugins'] = [
assert(),
fooPlugin(app)
]
"`

exports[`Code transformer | addJapaPlugin > addJapaPlugin with default import 1`] = `"
import app from '@adonisjs/core/services/app'
import { assert } from '@japa/assert'
import fooPlugin from '@adonisjs/foo/plugin/japa'
export const plugins: Config['plugins'] = [
assert(),
fooPlugin()
]
"`

52 changes: 52 additions & 0 deletions tests/code_transformer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -506,3 +506,55 @@ test.group('Code transformer | addPreloadFile', (group) => {
assert.equal(occurrences, 1)
})
})

test.group('Code transformer | addJapaPlugin', (group) => {
group.each.setup(async ({ context }) => setupFakeAdonisproject(context.fs))

test('add named import', async ({ assert, fs }) => {
await fs.create(
'tests/bootstrap.ts',
`
import app from '@adonisjs/core/services/app'
import { assert } from '@japa/assert'
export const plugins: Config['plugins'] = [
assert(),
]`
)

const transformer = new CodeTransformer(fs.baseUrl)

await transformer.addJapaPlugin('fooPlugin(app)', {
module: '@adonisjs/foo/plugin/japa',
identifier: 'fooPlugin',
isNamed: true,
})

const file = await fs.contents('tests/bootstrap.ts')
assert.snapshot(file).match()
})

test('add default import', async ({ assert, fs }) => {
await fs.create(
'tests/bootstrap.ts',
`
import app from '@adonisjs/core/services/app'
import { assert } from '@japa/assert'
export const plugins: Config['plugins'] = [
assert(),
]`
)

const transformer = new CodeTransformer(fs.baseUrl)

await transformer.addJapaPlugin('fooPlugin()', {
module: '@adonisjs/foo/plugin/japa',
identifier: 'fooPlugin',
isNamed: false,
})

const file = await fs.contents('tests/bootstrap.ts')
assert.snapshot(file).match()
})
})

0 comments on commit 4b64a59

Please sign in to comment.