Skip to content

Commit

Permalink
refactor!: rename from unstable_assembler to hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien-R44 committed Jun 2, 2024
1 parent d3e1662 commit 02d4ef1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 124 deletions.
41 changes: 8 additions & 33 deletions src/rc_file/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,45 +87,20 @@ export class RcFileParser {
}
}

/**
* Returns the assembler runner object
*/
#getAssemblerRunner(): NonNullable<RcFile['unstable_assembler']>['runner'] {
if (!this.#rcFile.unstable_assembler?.runner) {
return
}

if (!this.#rcFile.unstable_assembler.runner.name) {
throw new errors.E_MISSING_ASSEMBLER_RUNNER_NAME()
}

if (!this.#rcFile.unstable_assembler.runner.command) {
throw new errors.E_MISSING_ASSEMBLER_RUNNER_COMMAND()
}

return {
name: this.#rcFile.unstable_assembler.runner.name,
command: this.#rcFile.unstable_assembler.runner.command,
args: this.#rcFile.unstable_assembler.runner.args,
}
}

/**
* Returns the assembler object
*/
#getAssembler(): RcFile['unstable_assembler'] {
if (!this.#rcFile.unstable_assembler) {
#getAssembler(): RcFile['hooks'] {
if (!this.#rcFile.hooks) {
return
}

const runner = this.#getAssemblerRunner()
return new ObjectBuilder({})
.add('runner', runner)
.add('onBuildStarting', this.#rcFile.unstable_assembler.onBuildStarting)
.add('onBuildCompleted', this.#rcFile.unstable_assembler.onBuildCompleted)
.add('onDevServerStarted', this.#rcFile.unstable_assembler.onDevServerStarted)
.add('onSourceFileChanged', this.#rcFile.unstable_assembler.onSourceFileChanged)
.add('onHttpServerMessage', this.#rcFile.unstable_assembler.onHttpServerMessage)
.add('onBuildStarting', this.#rcFile.hooks.onBuildStarting)
.add('onBuildCompleted', this.#rcFile.hooks.onBuildCompleted)
.add('onDevServerStarted', this.#rcFile.hooks.onDevServerStarted)
.add('onSourceFileChanged', this.#rcFile.hooks.onSourceFileChanged)
.add('onHttpServerMessage', this.#rcFile.hooks.onHttpServerMessage)
.toObject()
}

Expand Down Expand Up @@ -243,7 +218,7 @@ export class RcFileParser {

return {
typescript: this.#rcFile.typescript,
...(assembler ? { unstable_assembler: assembler } : {}),
...(assembler ? { hooks: assembler } : {}),
...(assetsBundler !== undefined ? { assetsBundler } : {}),
preloads: this.#getPreloads(),
metaFiles: this.#getMetaFiles(),
Expand Down
25 changes: 7 additions & 18 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,10 @@ export type RcFile = {
* Configure a custom assets bundler to bundle and serve
* assets.
*
* This config can be used to configure assets bundler apart from
* vite and encore (since both are auto-detected)
*
* Set it to `false` to disable the assets bundler
* **Setting a custom assets bundler is deprecated and will be removed
* in future versions**. If you need a custom bundler, you should
* create a package integrating it with AdonisJS, probably using
* assembler hooks, like @adonisjs/vite does.
*/
assetsBundler?:
| {
Expand Down Expand Up @@ -221,20 +221,9 @@ export type RcFile = {
}

/**
* Assembler configuration
* Assembler hooks configuration
*/
unstable_assembler?: {
/**
* Configure a custom runner to start the dev server
* and tests. By default we are using ts-node but you
* are free to use any other runner
*/
runner?: {
name: string
command: string
args?: string[]
}

hooks?: {
/**
* When the dev server is started
*/
Expand Down Expand Up @@ -302,7 +291,7 @@ export interface RcFileInput {
timeout?: number
}
providers?: (ProviderNode | ProviderNode['file'])[]
unstable_assembler?: RcFile['unstable_assembler']
hooks?: RcFile['hooks']
}

/**
Expand Down
77 changes: 4 additions & 73 deletions tests/rc_file/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -712,76 +712,7 @@ test.group('Rc Parser | directories', () => {
})
})

test.group('Rc Parser | assembler', () => {
test('parse runner properly', ({ assert }) => {
const parser = new RcFileParser({
unstable_assembler: {
runner: {
name: 'bun',
command: 'bun',
args: ['--flag'],
},
},
})

assert.deepEqual(parser.parse(), {
raw: {
unstable_assembler: {
runner: {
name: 'bun',
command: 'bun',
args: ['--flag'],
},
},
},
typescript: true,
preloads: [],
directories,
metaFiles: [],
commands: [],
commandsAliases: {},
providers: [],
tests: {
suites: [],
timeout: 2000,
forceExit: true,
},
unstable_assembler: {
runner: {
name: 'bun',
command: 'bun',
args: ['--flag'],
},
},
})
})

test('raise error when runner properties are missing', ({ assert }) => {
assert.throws(
() =>
new RcFileParser({
unstable_assembler: {
runner: {
name: 'bun',
},
},
}).parse(),
'Invalid assembler.runner entry. Missing command property'
)

assert.throws(
() =>
new RcFileParser({
unstable_assembler: {
runner: {
command: 'bun',
},
},
}).parse(),
'Invalid assembler.runner entry. Missing name property'
)
})

test.group('Rc Parser | hooks', () => {
test('parse assembler hooks properly', ({ assert }) => {
const onBuildStarting = () => {}
const onBuildCompleted = () => {}
Expand All @@ -790,7 +721,7 @@ test.group('Rc Parser | assembler', () => {
const onHttpServerMessage = () => {}

const parser = new RcFileParser({
unstable_assembler: {
hooks: {
onBuildStarting,
onBuildCompleted,
onDevServerStarted,
Expand All @@ -801,7 +732,7 @@ test.group('Rc Parser | assembler', () => {

assert.deepEqual(parser.parse(), {
raw: {
unstable_assembler: {
hooks: {
onBuildStarting,
onBuildCompleted,
onDevServerStarted,
Expand All @@ -821,7 +752,7 @@ test.group('Rc Parser | assembler', () => {
timeout: 2000,
forceExit: true,
},
unstable_assembler: {
hooks: {
onBuildStarting,
onBuildCompleted,
onDevServerStarted,
Expand Down

0 comments on commit 02d4ef1

Please sign in to comment.