Skip to content

Commit

Permalink
feat: added ability to specify project name (#56)
Browse files Browse the repository at this point in the history
* feat: added ability to specify project name

* test: handle npm 6

* refactor: pr feedback

* test: resolve windows failures
  • Loading branch information
10xLaCroixDrinker committed May 7, 2024
1 parent c2fed7e commit be6c2a8
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 11 deletions.
8 changes: 7 additions & 1 deletion cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@

'use strict'

require('fastify-cli/generate').cli(['.', ...process.argv.slice(2)])
const args = [...process.argv.slice(2)]

if (!args[0] || args[0].startsWith('-')) {
args.unshift('.')
}

require('fastify-cli/generate').cli(args)
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
There is no need to install this package directly.

```sh
npm init fastify
npm init fastify [your_app_name]
```

The `npm init` command will find this package automatically and run it.
Expand Down
86 changes: 77 additions & 9 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,82 @@
'use strict'

const { test } = require('tap')
const { join, basename } = require('node:path')
const { mkdtempSync, readdirSync } = require('node:fs')
const { test, teardown, before } = require('tap')
const { join } = require('node:path')
const { mkdtempSync, readdirSync, mkdirSync, rmSync } = require('node:fs')
const { tmpdir } = require('node:os')
const { spawnSync } = require('node:child_process')

test('generates a fastify project in the current folder', async ({ equal, match }) => {
const dir = mkdtempSync(join(tmpdir(), 'create-fastify-test'))
spawnSync('node', [join(__dirname, 'cmd.js')], { cwd: dir })
match(readdirSync(dir).sort(), [
const testDir = mkdtempSync(join(tmpdir(), 'create-fastify-test-'))

before(() => {
spawnSync('npm', ['link'], { cwd: __dirname, shell: true })
})

teardown(() => {
spawnSync('npm', ['unlink', '-g'], { cwd: __dirname, shell: true })
rmSync(testDir, { recursive: true, force: true })
})

test('generates a fastify project in the current folder', (t) => {
t.plan(3)
const projectName = 'create-fastify-test-current'
const dir = join(testDir, projectName)
const opts = { cwd: dir, shell: true }
mkdirSync(dir)
spawnSync('npm', ['link', 'create-fastify'], opts)
spawnSync('npm', ['init', 'fastify'], opts)
t.match(readdirSync(dir).sort(), [
'.gitignore',
'README.md',
'app.js',
'node_modules', // added by npm link
'package.json',
'plugins',
'routes',
'test'
])
const { name, dependencies } = require(join(dir, 'package.json'))
t.ok(Object.keys(dependencies).includes('fastify'))
t.equal(name, projectName)
})

test('generates a fastify project in the current folder using --integrate', (t) => {
t.plan(3)
const projectName = 'create-fastify-test-integrate'
const dir = join(testDir, projectName)
const opts = { cwd: dir, shell: true }
mkdirSync(dir)
const { stdout: npmVersion } = spawnSync('npm', ['--version'], opts)
spawnSync('npm', ['init', '-y'], opts)
spawnSync('npm', ['link', 'create-fastify'], opts)
if (parseInt(npmVersion.toString().split('.')[0], 10) < 7) {
spawnSync('npm', ['init', 'fastify', '--integrate'], opts)
} else {
spawnSync('npm', ['init', 'fastify', '--', '--integrate'], opts)
}
t.match(readdirSync(dir).sort(), [
'.gitignore',
'README.md',
'app.js',
'node_modules', // added by npm link
'package.json',
'plugins',
'routes',
'test'
])
const { name, dependencies } = require(join(dir, 'package.json'))
t.ok(Object.keys(dependencies).includes('fastify'))
t.equal(name, projectName)
})

test('generates a fastify project in a new folder', (t) => {
t.plan(3)
const projectName = 'create-fastify-new-dir'
const dir = join(testDir, projectName)
const opts = { cwd: testDir, shell: true }
spawnSync('npm', ['link', 'create-fastify'], opts)
spawnSync('npm', ['init', 'fastify', projectName], opts)
t.match(readdirSync(dir).sort(), [
'.gitignore',
'README.md',
'app.js',
Expand All @@ -18,6 +85,7 @@ test('generates a fastify project in the current folder', async ({ equal, match
'routes',
'test'
])
const { name } = require(join(dir, 'package.json'))
equal(name.toLowerCase(), basename(dir).toLowerCase())
const { name, dependencies } = require(join(dir, 'package.json'))
t.ok(Object.keys(dependencies).includes('fastify'))
t.equal(name, projectName)
})

0 comments on commit be6c2a8

Please sign in to comment.