|
1 | 1 | #!/usr/bin/env node
|
2 |
| -const { join } = require('path') |
| 2 | +const { resolve } = require('path') |
| 3 | +const ora = require('ora') |
3 | 4 | const { copy, remove } = require('fs-extra')
|
4 | 5 | const { exec } = require('pkg')
|
5 | 6 |
|
6 |
| -const pkg = require(join(process.cwd(), 'package.json')) |
| 7 | +const pkg = require(resolve(process.cwd(), 'package.json')) |
7 | 8 |
|
8 |
| -const finalServerPath = join(process.cwd(), '.next-pkg/server.js') |
9 |
| -const binaryFilePath = process.platform === 'win32' |
10 |
| - ? `dist/${pkg.name}.exe` |
11 |
| - : `dist/${pkg.name}` |
| 9 | +const finalServerPath = resolve(process.cwd(), '.next-pkg/server.js') |
| 10 | +const binaryFilePath = |
| 11 | + process.platform === 'win32' ? `dist/${pkg.name}.exe` : `dist/${pkg.name}` |
12 | 12 |
|
13 |
| -const deleteTmpFiles = async () => { |
14 |
| - console.log('Deleting temporary files...') |
| 13 | +const copyTmpFiles = async () => { |
| 14 | + const spinner = ora('Copying extended next-pkg server').start() |
15 | 15 | try {
|
16 |
| - await remove('.next-pkg') |
| 16 | + await copy(resolve(__dirname, '../lib/server.js'), finalServerPath) |
| 17 | + spinner.succeed() |
17 | 18 | } catch (e) {
|
18 |
| - console.log(`Error deleting temporary files: ${e}`) |
| 19 | + console.log(`Error copying temporary files: ${e}`) |
| 20 | + spinner.fail() |
| 21 | + throw e |
19 | 22 | }
|
20 | 23 | }
|
21 | 24 |
|
22 |
| -const copyTmpFiles = async () => { |
23 |
| - console.log('Copying extended next-pkg server...') |
| 25 | +const compile = async () => { |
| 26 | + const spinner = ora('Compiling server with pkg').start() |
24 | 27 | try {
|
25 |
| - await copy(join(__dirname, '../lib/server.js'), finalServerPath) |
| 28 | + const execution = exec([ |
| 29 | + finalServerPath, |
| 30 | + '--target', |
| 31 | + 'host', |
| 32 | + '--output', |
| 33 | + `${binaryFilePath}` |
| 34 | + ]) |
| 35 | + spinner.stop() |
| 36 | + process.stderr.moveCursor(0, -1) |
| 37 | + process.stderr.clearLine() |
| 38 | + spinner.start() |
| 39 | + await execution |
| 40 | + spinner.succeed() |
26 | 41 | } catch (e) {
|
27 |
| - console.log(`Error copying temporary files: ${e}`) |
| 42 | + console.log(`Error during pkg compiling process: ${e}`) |
| 43 | + spinner.fail() |
| 44 | + throw e |
28 | 45 | }
|
29 | 46 | }
|
30 | 47 |
|
31 |
| -const compile = async () => { |
32 |
| - console.log('Compiling server with pkg...') |
| 48 | +const deleteTmpFiles = async () => { |
| 49 | + const spinner = ora('Deleting temporary files').start() |
33 | 50 | try {
|
34 |
| - await exec([ finalServerPath, '--target', 'host', '--output', `${binaryFilePath}` ]) |
| 51 | + await remove('.next-pkg') |
| 52 | + spinner.succeed() |
35 | 53 | } catch (e) {
|
36 |
| - console.log(`Error during pkg compiling process: ${e}`) |
| 54 | + console.log(`Error deleting temporary files: ${e}`) |
| 55 | + spinner.fail() |
| 56 | + throw e |
37 | 57 | }
|
38 | 58 | }
|
39 | 59 |
|
40 | 60 | const cli = async () => {
|
41 | 61 | await copyTmpFiles()
|
42 | 62 | await compile()
|
43 | 63 | await deleteTmpFiles()
|
44 |
| - console.log(`Binary compiled at ${binaryFilePath}`) |
| 64 | + console.log(`📦 Binary compiled at ${binaryFilePath}`) |
45 | 65 | }
|
46 | 66 |
|
47 | 67 | cli()
|
0 commit comments