Skip to content

Commit 38d2a98

Browse files
committed
feat: implement initial set of commands
1 parent 45cd50e commit 38d2a98

File tree

16 files changed

+1439
-8
lines changed

16 files changed

+1439
-8
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ Everyone is welcome to contribute. Please go through the following guides, befor
2727
2. [Code of conduct](https://adonisjs.com/code-of-conduct)
2828

2929

30+
```
31+
build ( build and over )
32+
build --production ( build for prod )
33+
build --watch ( build and watch source for changes )
34+
serve ( serve http server )
35+
serve --watch ( build, watch and serve )
36+
serve --watch-output ( build, watch and serve )
37+
manifest:generate ( generate manifest file )
38+
run:instructions ( run instructions )
39+
```
40+
3041
## Authors & License
3142
[Harminder virk](https://github.com/Harminder virk) and [contributors](https://github.com/[email protected]/adonisjs/graphs/contributors).
3243

commands/Build.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* @adonisjs/assembler
3+
*
4+
* (c) Harminder Virk <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
import { BaseCommand, flags } from '@adonisjs/ace'
11+
12+
import { Watcher } from '../src/Watcher'
13+
import { Compiler } from '../src/Compiler'
14+
import { ADONIS_ACE_CWD, ADONIS_IS_TYPESCRIPT } from '../config/env'
15+
16+
/**
17+
* Compile typescript project Javascript
18+
*/
19+
export default class Build extends BaseCommand {
20+
public static commandName = 'build'
21+
public static description = 'Compile typescript code to Javascript. Optionally watch for file changes'
22+
23+
/**
24+
* Allows watching for file changes
25+
*/
26+
@flags.boolean({ description: 'Watch for file changes and re-build the project', alias: 'w' })
27+
public watch: boolean
28+
29+
/**
30+
* Build for production
31+
*/
32+
@flags.boolean({ description: 'Build for production', alias: 'prod' })
33+
public production: boolean
34+
35+
/**
36+
* Use yarn when building for production to install dependencies
37+
*/
38+
@flags.boolean({ description: 'Use yarn for installing dependencies. Defaults to npm' })
39+
public yarn: boolean
40+
41+
/**
42+
* Invoked automatically by ace
43+
*/
44+
public async handle () {
45+
/**
46+
* Dis-allow when CWD is missing. It will always be set by `node ace`
47+
* commands and also when project is not a typescript project.
48+
*/
49+
if (!ADONIS_IS_TYPESCRIPT || !ADONIS_ACE_CWD) {
50+
this.logger.error(
51+
'Cannot build non-typescript project. Make sure to run "node ace build" from the project root',
52+
)
53+
return
54+
}
55+
56+
/**
57+
* --watch and --production flags aren't allowed together
58+
*/
59+
if (this.watch && this.production) {
60+
this.logger.info('--watch and --production flags cannot be used together. Skipping --watch')
61+
}
62+
63+
try {
64+
if (this.production) {
65+
const client = this.yarn ? 'yarn' : 'npm'
66+
await new Compiler(ADONIS_ACE_CWD, false, [], this.logger).compileForProduction(client)
67+
} else if (this.watch) {
68+
await new Watcher(ADONIS_ACE_CWD, false, [], this.logger).watch()
69+
} else {
70+
await new Compiler(ADONIS_ACE_CWD, false, [], this.logger).compile()
71+
}
72+
} catch (error) {
73+
this.logger.fatal(error)
74+
}
75+
}
76+
}

commands/Serve.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* @adonisjs/assembler
3+
*
4+
* (c) Harminder Virk <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
import { BaseCommand, flags } from '@adonisjs/ace'
11+
12+
import { Watcher } from '../src/Watcher'
13+
import { Compiler } from '../src/Compiler'
14+
import { BuildWatcher } from '../src/BuildWatcher'
15+
import { ADONIS_ACE_CWD, ADONIS_IS_TYPESCRIPT, ADONIS_BUILD_DIR } from '../config/env'
16+
17+
/**
18+
* Compile typescript project to Javascript and start
19+
* the HTTP server
20+
*/
21+
export default class Serve extends BaseCommand {
22+
public static commandName = 'serve'
23+
public static description = 'Compile typescript code to Javascript and start the HTTP server'
24+
25+
/**
26+
* Allows watching for file changes
27+
*/
28+
@flags.boolean({ description: 'Watch for file changes and re-build the project', alias: 'w' })
29+
public watch: boolean
30+
31+
/**
32+
* Allows watching for file changes
33+
*/
34+
@flags.boolean({
35+
description: 'Turn off Typescript compiler by passing --no-compile',
36+
default: true,
37+
})
38+
public compile: boolean
39+
40+
/**
41+
* Arguments to pass to the `node` binary
42+
*/
43+
@flags.array({ description: 'CLI options to pass to the node command line' })
44+
public nodeArgs: string[] = []
45+
46+
public async handle () {
47+
/**
48+
* Dis-allow when CWD is missing. It will always be set by `node ace`
49+
* commands
50+
*/
51+
if (!ADONIS_ACE_CWD) {
52+
this.logger.error(
53+
'Cannot build non-typescript project. Make sure to run "node ace serve" from the project root',
54+
)
55+
return
56+
}
57+
58+
/**
59+
* Dis-allow when running the command inside the compiled source and still
60+
* asking to re-compile the code
61+
*/
62+
if (!ADONIS_IS_TYPESCRIPT && this.compile !== false) {
63+
this.logger.error(
64+
'Cannot build non-typescript project. Make sure to run "node ace serve" from the project root',
65+
)
66+
return
67+
}
68+
69+
try {
70+
if (this.compile === false) {
71+
await new BuildWatcher(ADONIS_ACE_CWD, this.nodeArgs, this.logger).watch(ADONIS_BUILD_DIR || './')
72+
} else if (this.watch) {
73+
await new Watcher(ADONIS_ACE_CWD, true, this.nodeArgs, this.logger).watch()
74+
} else {
75+
await new Compiler(ADONIS_ACE_CWD, true, this.nodeArgs, this.logger).compile()
76+
}
77+
} catch (error) {
78+
this.logger.fatal(error)
79+
}
80+
}
81+
}

config/env.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* @adonisjs/assembler
3+
*
4+
* (c) Harminder Virk <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
/**
11+
* Env variables set by `node ace`
12+
*/
13+
export const ADONIS_ACE_CWD = process.env.ADONIS_ACE_CWD
14+
export const ADONIS_IS_TYPESCRIPT = process.env.ADONIS_IS_TYPESCRIPT
15+
export const ADONIS_BUILD_DIR = process.env.ADONIS_BUILD_DIR

config/paths.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* @adonisjs/assembler
3+
*
4+
* (c) Harminder Virk <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
export const RCFILE_NAME = '.adonisrc.json'
11+
export const TSCONFIG_FILE_NAME = 'tsconfig.json'
12+
export const DEFAULT_BUILD_DIR = 'build'
13+
export const SERVER_ENTRY_FILE = 'server.js'

index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* @adonisjs/assembler
3+
*
4+
* (c) Harminder Virk <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
import { Manifest } from '@adonisjs/ace'
11+
new Manifest(__dirname).generate([
12+
'./commands/Build',
13+
'./commands/Serve',
14+
])

package.json

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,21 @@
22
"name": "@adonisjs/assembler",
33
"version": "0.0.0",
44
"description": "Core commands to compiler and build AdonisJs project",
5+
"main": "build/index.js",
6+
"files": [
7+
"build/src",
8+
"build/ace-manifest.json",
9+
"build/index.d.ts",
10+
"build/index.js"
11+
],
512
"scripts": {
613
"mrm": "mrm --preset=@adonisjs/mrm-preset",
714
"pretest": "npm run lint",
815
"test": "node japaFile.js",
916
"lint": "tslint --project tsconfig.json",
1017
"clean": "del build",
1118
"compile": "npm run lint && npm run clean && tsc",
12-
"build": "npm run compile",
19+
"build": "npm run compile && node build/index.js",
1320
"commit": "git-cz",
1421
"release": "np",
1522
"version": "npm run build"
@@ -31,7 +38,9 @@
3138
},
3239
"homepage": "https://github.com/adonisjs/assembler#readme",
3340
"devDependencies": {
41+
"@adonisjs/ace": "^6.5.0",
3442
"@adonisjs/mrm-preset": "^2.1.0",
43+
"@poppinss/dev-utils": "^1.0.1",
3544
"@types/node": "^12.12.8",
3645
"commitizen": "^4.0.3",
3746
"cz-conventional-changelog": "^3.0.2",
@@ -54,12 +63,6 @@
5463
".ts"
5564
]
5665
},
57-
"main": "build/index.js",
58-
"files": [
59-
"build/src",
60-
"build/index.d.ts",
61-
"build/index.js"
62-
],
6366
"husky": {
6467
"hooks": {
6568
"pre-commit": "doctoc README.md --title='## Table of contents' && git add README.md",
@@ -74,5 +77,21 @@
7477
"np": {
7578
"contents": ".",
7679
"anyBranch": false
80+
},
81+
"dependencies": {
82+
"@adonisjs/application": "^1.2.1",
83+
"@adonisjs/fold": "^6.2.3",
84+
"@adonisjs/ioc-transformer": "^1.0.0",
85+
"@poppinss/chokidar-ts": "^2.0.0",
86+
"@poppinss/fancy-logs": "^1.3.0",
87+
"@poppinss/utils": "^2.1.0",
88+
"chokidar": "^3.3.0",
89+
"copyfiles": "^2.1.1",
90+
"cpy": "^7.3.0",
91+
"emittery": "^0.5.1",
92+
"execa": "^3.3.0",
93+
"fs-extra": "^8.1.0",
94+
"mem": "^6.0.0",
95+
"picomatch": "^2.1.1"
7796
}
7897
}

0 commit comments

Comments
 (0)