-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1ba2fd2
commit 426e359
Showing
5 changed files
with
169 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Read the .env file. | ||
import * as dotenv from 'dotenv' | ||
|
||
// Require the framework | ||
import Fastify from 'fastify' | ||
|
||
// Require library to exit fastify process, gracefully (if possible) | ||
import closeWithGrace from 'close-with-grace' | ||
|
||
// Import your application | ||
import appService from './app.js' | ||
|
||
// Dotenv config | ||
dotenv.config() | ||
|
||
// Instantiate Fastify with some config | ||
const app = Fastify({ | ||
logger: true | ||
}) | ||
|
||
// Register your application as a normal plugin. | ||
app.register(appService) | ||
|
||
// delay is the number of milliseconds for the graceful close to finish | ||
const closeListeners = closeWithGrace({ delay: process.env.FASTIFY_CLOSE_GRACE_DELAY || 500 }, async function ({ signal, err, manual }) { | ||
if (err) { | ||
app.log.error(err) | ||
} | ||
await app.close() | ||
}) | ||
|
||
app.addHook('onClose', async (instance, done) => { | ||
closeListeners.uninstall() | ||
done() | ||
}) | ||
|
||
// Start listening. | ||
app.listen({ port: process.env.PORT || 3000 }, (err) => { | ||
if (err) { | ||
app.log.error(err) | ||
process.exit(1) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
'use strict' | ||
|
||
// bailout if a test is broken | ||
// so that the folder can be inspected | ||
process.env.TAP_BAIL = true | ||
|
||
const t = require('tap') | ||
const { | ||
mkdirSync, | ||
readFileSync, | ||
readFile | ||
} = require('fs') | ||
const path = require('path') | ||
const rimraf = require('rimraf') | ||
const walker = require('walker') | ||
const workdir = path.join(__dirname, 'workdir') | ||
const appTemplateDir = path.join(__dirname, '..', 'templates', 'eject-esm') | ||
const { eject, cli } = require('../eject') | ||
const expected = {}; | ||
|
||
(function (cb) { | ||
const files = [] | ||
walker(appTemplateDir) | ||
.on('file', function (file) { | ||
files.push(file) | ||
}) | ||
.on('end', function () { | ||
let count = 0 | ||
files.forEach(function (file) { | ||
readFile(file, function (err, data) { | ||
if (err) { | ||
return cb(err) | ||
} | ||
|
||
expected[ | ||
file.replace(appTemplateDir, '').replace(/__/, '.') | ||
] = data.toString() | ||
|
||
count++ | ||
if (count === files.length) { | ||
cb(null) | ||
} | ||
}) | ||
}) | ||
}) | ||
.on('error', cb) | ||
})(function (err) { | ||
t.error(err) | ||
define(t) | ||
}) | ||
|
||
function define (t) { | ||
const { beforeEach, test } = t | ||
|
||
beforeEach(() => { | ||
rimraf.sync(workdir) | ||
mkdirSync(workdir, { recursive: true }) | ||
}) | ||
|
||
test('should finish succesfully with template', async (t) => { | ||
try { | ||
const template = 'eject-esm' | ||
await eject(workdir, template) | ||
await verifyCopy(t, expected) | ||
} catch (err) { | ||
t.error(err) | ||
} | ||
}) | ||
|
||
test('should finish successfully with cli', async (t) => { | ||
try { | ||
process.chdir(workdir) | ||
await cli(['--esm']) | ||
await verifyCopy(t, expected) | ||
} catch (err) { | ||
t.error(err) | ||
} | ||
}) | ||
|
||
function verifyCopy (t, expected) { | ||
return new Promise((resolve, reject) => { | ||
let count = 0 | ||
walker(workdir) | ||
.on('file', function (file) { | ||
count++ | ||
try { | ||
const data = readFileSync(file) | ||
file = file.replace(workdir, '') | ||
t.same( | ||
data.toString().replace(/\r\n/g, '\n'), | ||
expected[file], | ||
file + ' matching' | ||
) | ||
} catch (err) { | ||
reject(err) | ||
} | ||
}) | ||
.on('end', function () { | ||
t.equal(Object.keys(expected).length, count) | ||
resolve() | ||
}) | ||
.on('error', function (err, entry, stat) { | ||
reject(err) | ||
}) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters