From be6c2a8cdd3a81278dbeeecda6a7aa1e5b81e0ff Mon Sep 17 00:00:00 2001 From: Jamie King Date: Tue, 7 May 2024 10:29:57 -0700 Subject: [PATCH] feat: added ability to specify project name (#56) * feat: added ability to specify project name * test: handle npm 6 * refactor: pr feedback * test: resolve windows failures --- cmd.js | 8 +++++- readme.md | 2 +- test.js | 86 +++++++++++++++++++++++++++++++++++++++++++++++++------ 3 files changed, 85 insertions(+), 11 deletions(-) diff --git a/cmd.js b/cmd.js index beaa08c..f022a47 100755 --- a/cmd.js +++ b/cmd.js @@ -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) diff --git a/readme.md b/readme.md index 0955598..136b99f 100644 --- a/readme.md +++ b/readme.md @@ -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. diff --git a/test.js b/test.js index a974f07..05db717 100644 --- a/test.js +++ b/test.js @@ -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', @@ -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) })