Skip to content

Commit

Permalink
create eject --esm command (#566)
Browse files Browse the repository at this point in the history
  • Loading branch information
rommelandrea committed Nov 8, 2022
1 parent 1ba2fd2 commit 426e359
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 2 deletions.
6 changes: 5 additions & 1 deletion eject.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ function cli (args) {
if (opts.lang === 'ts' || opts.lang === 'typescript') {
template = 'eject-ts'
} else {
template = 'eject'
if (opts.esm) {
template = 'eject-esm'
} else {
template = 'eject'
}
}

return eject(process.cwd(), template).catch(function (err) {
Expand Down
3 changes: 3 additions & 0 deletions help/eject.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@ You can set the port to listen on with the PORT environment variable (default to

OPTS

--esm
use the ESM template

--lang=ts, --lang=typescript
use the TypeScript template
43 changes: 43 additions & 0 deletions templates/eject-esm/server.js
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)
}
})
107 changes: 107 additions & 0 deletions test/eject-esm.test.js
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)
})
})
}
}
12 changes: 11 additions & 1 deletion test/eject.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const rimraf = require('rimraf')
const walker = require('walker')
const workdir = path.join(__dirname, 'workdir')
const appTemplateDir = path.join(__dirname, '..', 'templates', 'eject')
const { eject } = require('../eject')
const { eject, cli } = require('../eject')
const expected = {}

;(function (cb) {
Expand Down Expand Up @@ -65,6 +65,16 @@ function define (t) {
}
})

test('should finish successfully with cli', async (t) => {
try {
process.chdir(workdir)
await cli([])
await verifyCopy(t, expected)
} catch (err) {
t.error(err)
}
})

function verifyCopy (t, expected) {
return new Promise((resolve, reject) => {
let count = 0
Expand Down

0 comments on commit 426e359

Please sign in to comment.