Skip to content

Commit 489ee6e

Browse files
feat: compiler demo for AdonisJS (#1055)
1 parent 764d1f0 commit 489ee6e

42 files changed

Lines changed: 5367 additions & 1050 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.changeset/light-pans-deliver.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
---

demo/adonisjs/.editorconfig

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# http://editorconfig.org
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true
10+
11+
[*.json]
12+
insert_final_newline = unset
13+
14+
[**.min.js]
15+
indent_style = unset
16+
insert_final_newline = unset
17+
18+
[MakeFile]
19+
indent_style = space
20+
21+
[*.md]
22+
trim_trailing_whitespace = false

demo/adonisjs/.env.example

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
TZ=UTC
2+
PORT=3333
3+
HOST=localhost
4+
LOG_LEVEL=info
5+
APP_KEY=
6+
NODE_ENV=development
7+
SESSION_DRIVER=cookie

demo/adonisjs/.gitignore

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Dependencies and AdonisJS build
2+
node_modules
3+
build
4+
tmp
5+
6+
# Secrets
7+
.env
8+
.env.local
9+
.env.production.local
10+
.env.development.local
11+
12+
# Frontend assets compiled code
13+
public/assets
14+
15+
# Build tools specific
16+
npm-debug.log
17+
yarn-error.log
18+
19+
# Editors specific
20+
.fleet
21+
.idea
22+
.vscode
23+
24+
# Platform specific
25+
.DS_Store

demo/adonisjs/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Lingo.dev Compiler with AdonisJS
2+
3+
## Introduction
4+
5+
This example demonstrates how to set up [Lingo.dev Compiler](https://lingo.dev/en/compiler/) with [AdonisJS](https://adonisjs.com/).
6+
7+
## Running this example
8+
9+
To run this example:
10+
11+
1. Set the `LINGODOTDEV_API_KEY` environment variable:
12+
13+
```bash
14+
export LINGODOTDEV_API_KEY="<your_api_key>"
15+
```
16+
17+
To get an API key, sign up for a free account at [lingo.dev](https://lingo.dev).
18+
19+
2. Navigate into this example's directory:
20+
21+
```bash
22+
cd demo/adonisjs
23+
```
24+
25+
3. Install the dependencies:
26+
27+
```bash
28+
pnpm install
29+
```
30+
31+
4. Run the development server:
32+
33+
```bash
34+
pnpm run dev
35+
```
36+
37+
5. Navigate to <http://localhost:3333>.
38+
39+
## Changed files
40+
41+
These are the files that were changed to get **Lingo.dev Compiler** up and running:
42+
43+
- [inertia/pages/home.tsx](./inertia/pages/home.tsx)
44+
- [vite.config.ts](./vite.config.ts)
45+
46+
You can use these files as a reference when setting up the compiler in your own project.

demo/adonisjs/ace.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
|--------------------------------------------------------------------------
3+
| JavaScript entrypoint for running ace commands
4+
|--------------------------------------------------------------------------
5+
|
6+
| DO NOT MODIFY THIS FILE AS IT WILL BE OVERRIDDEN DURING THE BUILD
7+
| PROCESS.
8+
|
9+
| See docs.adonisjs.com/guides/typescript-build-process#creating-production-build
10+
|
11+
| Since, we cannot run TypeScript source code using "node" binary, we need
12+
| a JavaScript entrypoint to run ace commands.
13+
|
14+
| This file registers the "ts-node/esm" hook with the Node.js module system
15+
| and then imports the "bin/console.ts" file.
16+
|
17+
*/
18+
19+
/**
20+
* Register hook to process TypeScript files using ts-node-maintained
21+
*/
22+
import 'ts-node-maintained/register/esm'
23+
24+
/**
25+
* Import ace console entrypoint
26+
*/
27+
await import('./bin/console.js')

demo/adonisjs/adonisrc.ts

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import { defineConfig } from '@adonisjs/core/app'
2+
3+
export default defineConfig({
4+
/*
5+
|--------------------------------------------------------------------------
6+
| Experimental flags
7+
|--------------------------------------------------------------------------
8+
|
9+
| The following features will be enabled by default in the next major release
10+
| of AdonisJS. You can opt into them today to avoid any breaking changes
11+
| during upgrade.
12+
|
13+
*/
14+
experimental: {
15+
mergeMultipartFieldsAndFiles: true,
16+
shutdownInReverseOrder: true,
17+
},
18+
19+
/*
20+
|--------------------------------------------------------------------------
21+
| Commands
22+
|--------------------------------------------------------------------------
23+
|
24+
| List of ace commands to register from packages. The application commands
25+
| will be scanned automatically from the "./commands" directory.
26+
|
27+
*/
28+
commands: [() => import('@adonisjs/core/commands')],
29+
30+
/*
31+
|--------------------------------------------------------------------------
32+
| Service providers
33+
|--------------------------------------------------------------------------
34+
|
35+
| List of service providers to import and register when booting the
36+
| application
37+
|
38+
*/
39+
providers: [
40+
() => import('@adonisjs/core/providers/app_provider'),
41+
() => import('@adonisjs/core/providers/hash_provider'),
42+
{
43+
file: () => import('@adonisjs/core/providers/repl_provider'),
44+
environment: ['repl', 'test'],
45+
},
46+
() => import('@adonisjs/core/providers/vinejs_provider'),
47+
() => import('@adonisjs/core/providers/edge_provider'),
48+
() => import('@adonisjs/session/session_provider'),
49+
() => import('@adonisjs/vite/vite_provider'),
50+
() => import('@adonisjs/shield/shield_provider'),
51+
() => import('@adonisjs/static/static_provider'),
52+
() => import('@adonisjs/cors/cors_provider'),
53+
() => import('@adonisjs/inertia/inertia_provider'),
54+
],
55+
56+
/*
57+
|--------------------------------------------------------------------------
58+
| Preloads
59+
|--------------------------------------------------------------------------
60+
|
61+
| List of modules to import before starting the application.
62+
|
63+
*/
64+
preloads: [() => import('#start/routes'), () => import('#start/kernel')],
65+
66+
/*
67+
|--------------------------------------------------------------------------
68+
| Tests
69+
|--------------------------------------------------------------------------
70+
|
71+
| List of test suites to organize tests by their type. Feel free to remove
72+
| and add additional suites.
73+
|
74+
*/
75+
tests: {
76+
suites: [
77+
{
78+
files: ['tests/unit/**/*.spec(.ts|.js)'],
79+
name: 'unit',
80+
timeout: 2000,
81+
},
82+
{
83+
files: ['tests/functional/**/*.spec(.ts|.js)'],
84+
name: 'functional',
85+
timeout: 30000,
86+
},
87+
],
88+
forceExit: false,
89+
},
90+
91+
/*
92+
|--------------------------------------------------------------------------
93+
| Metafiles
94+
|--------------------------------------------------------------------------
95+
|
96+
| A collection of files you want to copy to the build folder when creating
97+
| the production build.
98+
|
99+
*/
100+
metaFiles: [
101+
{
102+
pattern: 'resources/views/**/*.edge',
103+
reloadServer: false,
104+
},
105+
{
106+
pattern: 'public/**',
107+
reloadServer: false,
108+
},
109+
],
110+
111+
assetsBundler: false,
112+
hooks: {
113+
onBuildStarting: [() => import('@adonisjs/vite/build_hook')],
114+
},
115+
})
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import app from '@adonisjs/core/services/app'
2+
import { HttpContext, ExceptionHandler } from '@adonisjs/core/http'
3+
import type { StatusPageRange, StatusPageRenderer } from '@adonisjs/core/types/http'
4+
5+
export default class HttpExceptionHandler extends ExceptionHandler {
6+
/**
7+
* In debug mode, the exception handler will display verbose errors
8+
* with pretty printed stack traces.
9+
*/
10+
protected debug = !app.inProduction
11+
12+
/**
13+
* Status pages are used to display a custom HTML pages for certain error
14+
* codes. You might want to enable them in production only, but feel
15+
* free to enable them in development as well.
16+
*/
17+
protected renderStatusPages = app.inProduction
18+
19+
/**
20+
* Status pages is a collection of error code range and a callback
21+
* to return the HTML contents to send as a response.
22+
*/
23+
protected statusPages: Record<StatusPageRange, StatusPageRenderer> = {
24+
'404': (error, { inertia }) => inertia.render('errors/not_found', { error }),
25+
'500..599': (error, { inertia }) => inertia.render('errors/server_error', { error }),
26+
}
27+
28+
/**
29+
* The method is used for handling errors and returning
30+
* response to the client
31+
*/
32+
async handle(error: unknown, ctx: HttpContext) {
33+
return super.handle(error, ctx)
34+
}
35+
36+
/**
37+
* The method is used to report error to the logging service or
38+
* the a third party error monitoring service.
39+
*
40+
* @note You should not attempt to send a response from this method.
41+
*/
42+
async report(error: unknown, ctx: HttpContext) {
43+
return super.report(error, ctx)
44+
}
45+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Logger } from '@adonisjs/core/logger'
2+
import { HttpContext } from '@adonisjs/core/http'
3+
import { NextFn } from '@adonisjs/core/types/http'
4+
5+
/**
6+
* The container bindings middleware binds classes to their request
7+
* specific value using the container resolver.
8+
*
9+
* - We bind "HttpContext" class to the "ctx" object
10+
* - And bind "Logger" class to the "ctx.logger" object
11+
*/
12+
export default class ContainerBindingsMiddleware {
13+
handle(ctx: HttpContext, next: NextFn) {
14+
ctx.containerResolver.bindValue(HttpContext, ctx)
15+
ctx.containerResolver.bindValue(Logger, ctx.logger)
16+
17+
return next()
18+
}
19+
}

demo/adonisjs/bin/console.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
|--------------------------------------------------------------------------
3+
| Ace entry point
4+
|--------------------------------------------------------------------------
5+
|
6+
| The "console.ts" file is the entrypoint for booting the AdonisJS
7+
| command-line framework and executing commands.
8+
|
9+
| Commands do not boot the application, unless the currently running command
10+
| has "options.startApp" flag set to true.
11+
|
12+
*/
13+
14+
import 'reflect-metadata'
15+
import { Ignitor, prettyPrintError } from '@adonisjs/core'
16+
17+
/**
18+
* URL to the application root. AdonisJS need it to resolve
19+
* paths to file and directories for scaffolding commands
20+
*/
21+
const APP_ROOT = new URL('../', import.meta.url)
22+
23+
/**
24+
* The importer is used to import files in context of the
25+
* application.
26+
*/
27+
const IMPORTER = (filePath: string) => {
28+
if (filePath.startsWith('./') || filePath.startsWith('../')) {
29+
return import(new URL(filePath, APP_ROOT).href)
30+
}
31+
return import(filePath)
32+
}
33+
34+
new Ignitor(APP_ROOT, { importer: IMPORTER })
35+
.tap((app) => {
36+
app.booting(async () => {
37+
await import('#start/env')
38+
})
39+
app.listen('SIGTERM', () => app.terminate())
40+
app.listenIf(app.managedByPm2, 'SIGINT', () => app.terminate())
41+
})
42+
.ace()
43+
.handle(process.argv.splice(2))
44+
.catch((error) => {
45+
process.exitCode = 1
46+
prettyPrintError(error)
47+
})

0 commit comments

Comments
 (0)