From 205fecd861c86d8e6dc8ed0b8dcf2a1b3b6a2da8 Mon Sep 17 00:00:00 2001 From: Matthias Giger Date: Wed, 22 May 2024 21:38:10 +0200 Subject: [PATCH] feat(configuration): show deployment files in CI and tests in workflow release-npm --- .github/workflows/push.yml | 1 + configuration/vercel.ts | 2 +- helper.ts | 7 ++++-- index.ts | 6 ++---- package.json | 9 ++++++-- test/basic.test.ts | 36 +++++++++++++++++++++++++++++-- test/fixture/build/package.json | 2 +- test/fixture/package/package.json | 5 +++++ types.ts | 2 +- 9 files changed, 57 insertions(+), 13 deletions(-) diff --git a/.github/workflows/push.yml b/.github/workflows/push.yml index 4a2dc59..39f99cc 100644 --- a/.github/workflows/push.yml +++ b/.github/workflows/push.yml @@ -16,6 +16,7 @@ jobs: - run: bun install - run: bun check - run: bun types + - run: bun test - name: 📢 Release uses: tobua/release-npm-action@v3 with: diff --git a/configuration/vercel.ts b/configuration/vercel.ts index 56ea01c..0bec064 100644 --- a/configuration/vercel.ts +++ b/configuration/vercel.ts @@ -21,5 +21,5 @@ export const templates = { export const extension = (path: string) => ({ extends: path }) export function createFile(configuration: object) { - return { name: 'vercel.json', contents: JSON.stringify(configuration, null, 2) } + return { name: 'vercel.json', contents: JSON.stringify(configuration, null, 2), showInCi: true } } diff --git a/helper.ts b/helper.ts index ce267c3..182ea6c 100644 --- a/helper.ts +++ b/helper.ts @@ -3,6 +3,7 @@ import { dirname, join } from 'node:path' import { it } from 'avait' import Bun from 'bun' import glob from 'fast-glob' +import isCi from 'is-ci' import { parse } from 'parse-gitignore' import { merge } from 'ts-deepmerge' import { z } from 'zod' @@ -177,7 +178,9 @@ export function installLocalDependencies() { } } -export async function writeFile(file: File) { +export async function writeFile(file: File, ignores: string[]) { await Bun.write(root(file.name), file.contents) - return file.name + if (!(file.showInCi && isCi)) { + ignores.push(file.name) + } } diff --git a/index.ts b/index.ts index 22f0eec..82c11c8 100644 --- a/index.ts +++ b/index.ts @@ -18,12 +18,10 @@ async function configureProject() { if (!files) continue if (Array.isArray(files)) { for (const file of files.filter((item) => item?.name)) { - const name = await writeFile(file as File) - ignores.push(name) + await writeFile(file as File, ignores) } } else { - const name = await writeFile(files as File) - ignores.push(name) + await writeFile(files as File, ignores) } } diff --git a/package.json b/package.json index 061f1a9..51506fe 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "dependencies": { "avait": "^1.0.0", "fast-glob": "^3.3.2", + "is-ci": "^3.0.1", "logua": "^3.0.3", "parse-gitignore": "^2.0.0", "ts-deepmerge": "^7.0.0", @@ -20,7 +21,8 @@ }, "devDependencies": { "@biomejs/biome": "^1.7.3", - "@types/bun": "^1.1.2", + "@types/bun": "^1.1.3", + "@types/is-ci": "^3.0.4", "@types/parse-gitignore": "^1.0.2", "eslint-config-airbnb": "^19.0.4", "typescript": "^5.4.5" @@ -78,7 +80,10 @@ "typescript": { "compilerOptions": { "target": "ES2022", - "lib": ["DOM", "ES2022"] + "lib": [ + "DOM", + "ES2022" + ] }, "extends": "plugin", "files": [ diff --git a/test/basic.test.ts b/test/basic.test.ts index 35a65c3..0e9ced7 100644 --- a/test/basic.test.ts +++ b/test/basic.test.ts @@ -1,12 +1,12 @@ import { expect, test } from 'bun:test' import { execSync } from 'node:child_process' -import { existsSync } from 'node:fs' +import { existsSync, rmSync } from 'node:fs' import { join } from 'node:path' import Bun from 'bun' // To include additional files in fixtures remove the ignore entries temporarly from .gitignore in root and fixtures and add the specific files needed. -test('Adds configuration files for basic package setup.', () => { +test('Adds configuration files for basic package setup.', async () => { const fixturePath = './test/fixture/package' execSync('bun ./../../../index.ts', { @@ -18,6 +18,13 @@ test('Adds configuration files for basic package setup.', () => { expect(existsSync(join(fixturePath, '.prettierignore'))).toBe(true) expect(existsSync(join(fixturePath, 'biome.json'))).toBe(true) expect(existsSync(join(fixturePath, 'LICENSE.md'))).toBe(true) + expect(existsSync(join(fixturePath, 'vercel.json'))).toBe(true) + expect(existsSync(join(fixturePath, '.gitignore'))).toBe(true) + + const gitignoreFile = await Bun.file(join(fixturePath, '.gitignore')).text() + + expect(gitignoreFile).toContain('vercel.json') + expect(gitignoreFile).toContain('biome.json') }) test('Adds configuration files for basic file setup.', async () => { @@ -145,3 +152,28 @@ test('Will also install local dependencies if listed.', () => { expect(existsSync(join(fixturePath, 'node_modules/keep/package.json'))).toBe(true) expect(existsSync(join(fixturePath, 'node_modules/empty-dependency/package.json'))).toBe(true) }) + +test("Doesn't add deployment files to gitignore in CI.", async () => { + const fixturePath = './test/fixture/package' + + // TODO existing ignores should not be taken over. + rmSync(join(fixturePath, '.gitignore')) + + execSync('bun ./../../../index.ts', { + cwd: fixturePath, + stdio: 'inherit', + env: { + ...process.env, + // biome-ignore lint/style/useNamingConvention: Casing used by Node.js. + CI: 'true', + }, + }) + + expect(existsSync(join(fixturePath, 'vercel.json'))).toBe(true) + expect(existsSync(join(fixturePath, '.gitignore'))).toBe(true) + + const gitignoreFile = await Bun.file(join(fixturePath, '.gitignore')).text() + + expect(gitignoreFile).not.toContain('vercel.json') + expect(gitignoreFile).toContain('biome.json') +}) diff --git a/test/fixture/build/package.json b/test/fixture/build/package.json index 89bff01..7d9fdf1 100644 --- a/test/fixture/build/package.json +++ b/test/fixture/build/package.json @@ -1,6 +1,6 @@ { "name": "build", - "configuratidon": { + "configuration": { "postcss": { "plugins": { "tailwindcss": {} diff --git a/test/fixture/package/package.json b/test/fixture/package/package.json index 99de4a0..7060734 100644 --- a/test/fixture/package/package.json +++ b/test/fixture/package/package.json @@ -13,6 +13,11 @@ "compilerOptions": { "target": "ES6" } + }, + "vercel": { + "github": { + "silent": true + } } } } diff --git a/types.ts b/types.ts index a9f003f..d0ed50e 100644 --- a/types.ts +++ b/types.ts @@ -11,7 +11,7 @@ export type PackageJson = { configuration?: { [key: string]: string | object | string[] } } -export type File = { name: string; contents: string } +export type File = { name: string; contents: string; showInCi?: boolean } export type Configuration = { name: ConfigurationKeys