From 4557d4440ff9139000999ee5e60f3f59ef3cbdb8 Mon Sep 17 00:00:00 2001 From: Hiroki Osame Date: Sun, 3 Nov 2024 13:33:21 +0900 Subject: [PATCH 01/23] test: improve reliability --- .github/workflows/test.yml | 8 ++-- package.json | 2 +- pnpm-lock.yaml | 10 ++-- src/index.ts | 7 +++ tests/index.ts | 94 +++++++++++++++++--------------------- tests/utils/create-git.ts | 33 +++++++++++++ 6 files changed, 91 insertions(+), 63 deletions(-) create mode 100644 tests/utils/create-git.ts diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 0e876f3..ba0c295 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -6,6 +6,9 @@ on: pull_request: branches: [master, develop] +permissions: + contents: write + jobs: test: name: Test @@ -34,11 +37,6 @@ jobs: - name: Type check run: pnpm type-check - - name: Setup Git # Only used by the last test - run: | - git config --global user.name "GitHub Actions" - git config --global user.email "<>" - - name: Build run: pnpm build diff --git a/package.json b/package.json index caf4afb..eb290ee 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ "clean-pkg-json": "^1.2.0", "cleye": "^1.3.2", "execa": "^8.0.1", - "fs-fixture": "^2.5.0", + "fs-fixture": "^2.6.0", "lintroll": "^1.10.0", "manten": "^1.3.0", "npm-packlist": "^5.1.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 40ccc50..bd8f9c0 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -28,8 +28,8 @@ importers: specifier: ^8.0.1 version: 8.0.1 fs-fixture: - specifier: ^2.5.0 - version: 2.5.0 + specifier: ^2.6.0 + version: 2.6.0 lintroll: specifier: ^1.10.0 version: 1.10.0(eslint-plugin-import@2.29.1(@typescript-eslint/parser@8.12.2(eslint@9.14.0)(typescript@5.6.3))(eslint@9.14.0))(typescript@5.6.3) @@ -1213,8 +1213,8 @@ packages: resolution: {integrity: sha512-Ld2g8rrAyMYFXBhEqMz8ZAHBi4J4uS1i/CxGMDnjyFWddMXLVcDp051DZfu+t7+ab7Wv6SMqpWmyFIj5UbfFvg==} engines: {node: '>=14'} - fs-fixture@2.5.0: - resolution: {integrity: sha512-AzCSCaChZORdNcSa9w9IBnGWhDP2Bv/lOso0P9mAl5nJw2s7pjSEoDaHtzZ9VGIHQlSXR2fuTPO7ln1dM4HTNQ==} + fs-fixture@2.6.0: + resolution: {integrity: sha512-XQNHBGYgth08BSgThjQNrUuzE9XUmr7CDgGFStKaAeB9Oq+kxUHd1zS6hp1YO11B501Sjv9Jq+B2VFn+CdwmIg==} engines: {node: '>=18.0.0'} fs.realpath@1.0.0: @@ -3658,7 +3658,7 @@ snapshots: cross-spawn: 7.0.3 signal-exit: 4.1.0 - fs-fixture@2.5.0: {} + fs-fixture@2.6.0: {} fs.realpath@1.0.0: {} diff --git a/src/index.ts b/src/index.ts index b5acb63..b798562 100644 --- a/src/index.ts +++ b/src/index.ts @@ -78,6 +78,13 @@ const { stringify } = JSON; const localTemporaryBranch = `git-publish/${publishBranch}-${Date.now()}`; let success = false; + // Validate remote exists + try { + await execa('git', ['remote', 'get-url', remote]); + } catch { + throw new Error(`Git remote ${stringify(remote)} does not exist`); + } + // In the try-finally block in case it modifies the working tree // On failure, they will be reverted by the hard reset try { diff --git a/tests/index.ts b/tests/index.ts index b9ff609..7ed963d 100644 --- a/tests/index.ts +++ b/tests/index.ts @@ -1,44 +1,15 @@ -import path from 'path'; -import { execa, type Options } from 'execa'; +import path from 'node:path'; +import { execa } from 'execa'; import { describe, expect } from 'manten'; import { createFixture } from 'fs-fixture'; +import { createGit } from './utils/create-git.js'; const gitPublish = path.resolve('./dist/index.js'); -const createGit = async (cwd: string) => { - const git = ( - command: string, - args?: string[], - options?: Options, - ) => ( - execa( - 'git', - [command, ...(args || [])], - { - cwd, - ...options, - }, - ) - ); - - await git( - 'init', - [ - // In case of different default branch name - '--initial-branch=master', - ], - ); - - await git('config', ['user.name', 'name']); - await git('config', ['user.email', 'email']); - - return git; -}; - -describe('git-publish', ({ describe, test }) => { +describe('git-publish', ({ describe }) => { describe('Error cases', ({ test }) => { test('Fails if not in git repository', async () => { - const fixture = await createFixture(); + await using fixture = await createFixture(); const gitPublishProcess = await execa(gitPublish, { cwd: fixture.path, @@ -47,12 +18,10 @@ describe('git-publish', ({ describe, test }) => { expect(gitPublishProcess.exitCode).toBe(1); expect(gitPublishProcess.stderr).toBe('Error: Not in a git repository'); - - await fixture.rm(); }); test('Fails if no package.json found', async () => { - const fixture = await createFixture(); + await using fixture = await createFixture(); await createGit(fixture.path); @@ -63,12 +32,10 @@ describe('git-publish', ({ describe, test }) => { expect(gitPublishProcess.exitCode).toBe(1); expect(gitPublishProcess.stderr).toBe('Error: No package.json found in current working directory'); - - await fixture.rm(); }); test('Dirty working tree', async () => { - const fixture = await createFixture({ + await using fixture = await createFixture({ 'package.json': '{}', }); @@ -82,24 +49,47 @@ describe('git-publish', ({ describe, test }) => { expect(gitPublishProcess.exitCode).toBe(1); expect(gitPublishProcess.stderr).toBe('Error: Working tree is not clean'); - - await fixture.rm(); }); }); - test('Publishes', async ({ onTestFail }) => { - // Requires git config author to be set - // This is set on the GitHub Action because locally, an author already exists - - const gitPublishProcess = await execa(gitPublish, { - reject: false, + describe('Current project', async ({ test }) => { + await using fixture = await createFixture(process.cwd(), { + templateFilter: cpPath => !( + cpPath.endsWith(`${path.sep}node_modules`) + || path.basename(cpPath).startsWith('.') + || path.basename(cpPath) === 'dist' + ), }); - onTestFail(() => { - console.log(gitPublishProcess); + const git = await createGit(fixture.path); + await git('add', ['.']); + await git('commit', ['-am', 'Initial commit']); + + await test('Errors on missing remote', async () => { + const gitPublishProcess = await execa(gitPublish, { + cwd: fixture.path, + reject: false, + }); + + expect(gitPublishProcess.exitCode).toBe(1); + expect(gitPublishProcess.stderr).toBe('Error: Git remote "origin" does not exist'); }); - expect(gitPublishProcess.exitCode).toBe(0); - expect(gitPublishProcess.stdout).toMatch('✔'); + const { stdout: originRemote } = await execa('git', ['remote', 'get-url', 'origin']); + await git('remote', ['add', 'origin', originRemote]); + + await test('Publishes', async ({ onTestFail }) => { + const gitPublishProcess = await execa(gitPublish, { + cwd: fixture.path, + reject: false, + }); + + onTestFail(() => { + console.log(gitPublishProcess); + }); + + expect(gitPublishProcess.exitCode).toBe(0); + expect(gitPublishProcess.stdout).toMatch('✔'); + }); }); }); diff --git a/tests/utils/create-git.ts b/tests/utils/create-git.ts new file mode 100644 index 0000000..259955c --- /dev/null +++ b/tests/utils/create-git.ts @@ -0,0 +1,33 @@ +import { execa, type Options } from 'execa'; + +export const createGit = async ( + cwd: string, +) => { + const git = ( + command: string, + args?: string[], + options?: Options, + ) => ( + execa( + 'git', + [command, ...(args || [])], + { + cwd, + ...options, + }, + ) + ); + + await git( + 'init', + [ + // In case of different default branch name + '--initial-branch=master', + ], + ); + + await git('config', ['user.name', 'name']); + await git('config', ['user.email', 'email']); + + return git; +}; From 42194e02f5c27273bb69a476dbfe8888aecb2ff5 Mon Sep 17 00:00:00 2001 From: Hiroki Osame Date: Sun, 3 Nov 2024 14:12:23 +0900 Subject: [PATCH 02/23] wip --- tests/index.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/index.ts b/tests/index.ts index 7ed963d..0da9791 100644 --- a/tests/index.ts +++ b/tests/index.ts @@ -84,8 +84,12 @@ describe('git-publish', ({ describe }) => { reject: false, }); - onTestFail(() => { + onTestFail(async () => { console.log(gitPublishProcess); + const ls = await execa('ls', ['-R'], { + cwd: fixture.path, + }); + console.log(ls); }); expect(gitPublishProcess.exitCode).toBe(0); From 8f3a862889f81bc58fc53b7c4635486e4fe27e9c Mon Sep 17 00:00:00 2001 From: Hiroki Osame Date: Sun, 3 Nov 2024 17:31:17 +0900 Subject: [PATCH 03/23] wip --- src/index.ts | 6 ++++-- tests/index.ts | 55 +++++++++++++++++++++++++------------------------- 2 files changed, 31 insertions(+), 30 deletions(-) diff --git a/src/index.ts b/src/index.ts index b798562..cd7c27a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -124,10 +124,12 @@ const { stringify } = JSON; } setTitle('Running hook "prepare"'); - await execa('npm', ['run', '--if-present', 'prepare']); + const a = await execa('npm', ['run', '--if-present', 'prepare']); + console.log('a', a); setTitle('Running hook "prepack"'); - await execa('npm', ['run', '--if-present', 'prepack']); + const b = await execa('npm', ['run', '--if-present', 'prepack']); + console.log('b', b); }); if (!dry) { diff --git a/tests/index.ts b/tests/index.ts index 0da9791..2cd81b6 100644 --- a/tests/index.ts +++ b/tests/index.ts @@ -1,20 +1,31 @@ import path from 'node:path'; +import fs from 'node:fs/promises'; import { execa } from 'execa'; import { describe, expect } from 'manten'; import { createFixture } from 'fs-fixture'; import { createGit } from './utils/create-git.js'; -const gitPublish = path.resolve('./dist/index.js'); +const gitPublishPath = path.resolve('./dist/index.js'); + +// Remove node_modules/.bin from PATH added by pnpm +const PATH = process.env.PATH?.split(':').filter(p => !p.includes('node_modules/.bin')).join(':'); + +const gitPublish = ( + cwd: string, +) => execa(gitPublishPath, { + cwd, + reject: false, + env: { + PATH, + }, +}); describe('git-publish', ({ describe }) => { describe('Error cases', ({ test }) => { test('Fails if not in git repository', async () => { await using fixture = await createFixture(); - const gitPublishProcess = await execa(gitPublish, { - cwd: fixture.path, - reject: false, - }); + const gitPublishProcess = await gitPublish(fixture.path); expect(gitPublishProcess.exitCode).toBe(1); expect(gitPublishProcess.stderr).toBe('Error: Not in a git repository'); @@ -25,10 +36,7 @@ describe('git-publish', ({ describe }) => { await createGit(fixture.path); - const gitPublishProcess = await execa(gitPublish, { - cwd: fixture.path, - reject: false, - }); + const gitPublishProcess = await gitPublish(fixture.path); expect(gitPublishProcess.exitCode).toBe(1); expect(gitPublishProcess.stderr).toBe('Error: No package.json found in current working directory'); @@ -42,10 +50,7 @@ describe('git-publish', ({ describe }) => { const git = await createGit(fixture.path); await git('add', ['package.json']); - const gitPublishProcess = await execa(gitPublish, { - cwd: fixture.path, - reject: false, - }); + const gitPublishProcess = await gitPublish(fixture.path); expect(gitPublishProcess.exitCode).toBe(1); expect(gitPublishProcess.stderr).toBe('Error: Working tree is not clean'); @@ -53,23 +58,23 @@ describe('git-publish', ({ describe }) => { }); describe('Current project', async ({ test }) => { - await using fixture = await createFixture(process.cwd(), { + const fixture = await createFixture(process.cwd(), { templateFilter: cpPath => !( cpPath.endsWith(`${path.sep}node_modules`) || path.basename(cpPath).startsWith('.') || path.basename(cpPath) === 'dist' ), }); + + await fs.symlink(path.resolve('node_modules'), fixture.getPath('node_modules'), 'dir'); + console.log(fixture.path); const git = await createGit(fixture.path); await git('add', ['.']); await git('commit', ['-am', 'Initial commit']); await test('Errors on missing remote', async () => { - const gitPublishProcess = await execa(gitPublish, { - cwd: fixture.path, - reject: false, - }); + const gitPublishProcess = await gitPublish(fixture.path); expect(gitPublishProcess.exitCode).toBe(1); expect(gitPublishProcess.stderr).toBe('Error: Git remote "origin" does not exist'); @@ -78,18 +83,12 @@ describe('git-publish', ({ describe }) => { const { stdout: originRemote } = await execa('git', ['remote', 'get-url', 'origin']); await git('remote', ['add', 'origin', originRemote]); - await test('Publishes', async ({ onTestFail }) => { - const gitPublishProcess = await execa(gitPublish, { - cwd: fixture.path, - reject: false, - }); + await test('Publishes', async ({ onTestFail }) => { + const gitPublishProcess = await gitPublish(fixture.path); - onTestFail(async () => { + console.log(gitPublishProcess); + onTestFail(() => { console.log(gitPublishProcess); - const ls = await execa('ls', ['-R'], { - cwd: fixture.path, - }); - console.log(ls); }); expect(gitPublishProcess.exitCode).toBe(0); From b6f7cc0f4c5392672adb3eda6a5b37366bdeff2a Mon Sep 17 00:00:00 2001 From: Hiroki Osame Date: Sun, 3 Nov 2024 17:31:28 +0900 Subject: [PATCH 04/23] wip --- tests/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/index.ts b/tests/index.ts index 2cd81b6..2bac546 100644 --- a/tests/index.ts +++ b/tests/index.ts @@ -65,7 +65,7 @@ describe('git-publish', ({ describe }) => { || path.basename(cpPath) === 'dist' ), }); - + await fs.symlink(path.resolve('node_modules'), fixture.getPath('node_modules'), 'dir'); console.log(fixture.path); @@ -83,7 +83,7 @@ describe('git-publish', ({ describe }) => { const { stdout: originRemote } = await execa('git', ['remote', 'get-url', 'origin']); await git('remote', ['add', 'origin', originRemote]); - await test('Publishes', async ({ onTestFail }) => { + await test('Publishes', async ({ onTestFail }) => { const gitPublishProcess = await gitPublish(fixture.path); console.log(gitPublishProcess); From 83e94fcf469104fbfaf2d98faa44498c7891a639 Mon Sep 17 00:00:00 2001 From: Hiroki Osame Date: Sun, 3 Nov 2024 17:43:59 +0900 Subject: [PATCH 05/23] wip --- tests/index.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/index.ts b/tests/index.ts index 2bac546..4bcf19e 100644 --- a/tests/index.ts +++ b/tests/index.ts @@ -8,16 +8,16 @@ import { createGit } from './utils/create-git.js'; const gitPublishPath = path.resolve('./dist/index.js'); // Remove node_modules/.bin from PATH added by pnpm -const PATH = process.env.PATH?.split(':').filter(p => !p.includes('node_modules/.bin')).join(':'); +// const PATH = process.env.PATH?.split(':').filter(p => !p.includes('node_modules/.bin')).join(':'); const gitPublish = ( cwd: string, ) => execa(gitPublishPath, { cwd, reject: false, - env: { - PATH, - }, + // env: { + // PATH, + // }, }); describe('git-publish', ({ describe }) => { From b8b947645b3b30aa5bc9aa06171ab899fe24aab5 Mon Sep 17 00:00:00 2001 From: Hiroki Osame Date: Sun, 3 Nov 2024 17:47:09 +0900 Subject: [PATCH 06/23] wip --- .github/workflows/test.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ba0c295..9fa0bad 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -31,9 +31,6 @@ jobs: with: run_install: true - - name: Lint - run: pnpm lint - - name: Type check run: pnpm type-check From a4fe7c09c6de034e36c7b3e7cf0fefb3d561a948 Mon Sep 17 00:00:00 2001 From: Hiroki Osame Date: Sun, 3 Nov 2024 17:50:16 +0900 Subject: [PATCH 07/23] wip --- tests/index.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/tests/index.ts b/tests/index.ts index 4bcf19e..ffa0bd2 100644 --- a/tests/index.ts +++ b/tests/index.ts @@ -7,17 +7,11 @@ import { createGit } from './utils/create-git.js'; const gitPublishPath = path.resolve('./dist/index.js'); -// Remove node_modules/.bin from PATH added by pnpm -// const PATH = process.env.PATH?.split(':').filter(p => !p.includes('node_modules/.bin')).join(':'); - const gitPublish = ( cwd: string, ) => execa(gitPublishPath, { cwd, reject: false, - // env: { - // PATH, - // }, }); describe('git-publish', ({ describe }) => { @@ -81,6 +75,7 @@ describe('git-publish', ({ describe }) => { }); const { stdout: originRemote } = await execa('git', ['remote', 'get-url', 'origin']); + console.log({ originRemote }); await git('remote', ['add', 'origin', originRemote]); await test('Publishes', async ({ onTestFail }) => { From b5f96b045e7a33cecd671bdd311af92225d58f3b Mon Sep 17 00:00:00 2001 From: name Date: Sun, 3 Nov 2024 18:01:15 +0900 Subject: [PATCH 08/23] wip --- tests/index.ts | 32 +++++++++++++++++--------------- tests/utils/create-git.ts | 20 +++++++++++++------- 2 files changed, 30 insertions(+), 22 deletions(-) diff --git a/tests/index.ts b/tests/index.ts index ffa0bd2..8fffd84 100644 --- a/tests/index.ts +++ b/tests/index.ts @@ -12,6 +12,7 @@ const gitPublish = ( ) => execa(gitPublishPath, { cwd, reject: false, + all: true, }); describe('git-publish', ({ describe }) => { @@ -55,36 +56,37 @@ describe('git-publish', ({ describe }) => { const fixture = await createFixture(process.cwd(), { templateFilter: cpPath => !( cpPath.endsWith(`${path.sep}node_modules`) - || path.basename(cpPath).startsWith('.') + || path.basename(cpPath) === '.git' || path.basename(cpPath) === 'dist' ), }); await fs.symlink(path.resolve('node_modules'), fixture.getPath('node_modules'), 'dir'); + await fs.symlink(path.resolve('.git'), fixture.getPath('.git'), 'dir'); console.log(fixture.path); const git = await createGit(fixture.path); - await git('add', ['.']); - await git('commit', ['-am', 'Initial commit']); + // await git('add', ['.']); + // await git('commit', ['-am', 'Initial commit']); - await test('Errors on missing remote', async () => { - const gitPublishProcess = await gitPublish(fixture.path); + // await test('Errors on missing remote', async () => { + // const gitPublishProcess = await gitPublish(fixture.path); - expect(gitPublishProcess.exitCode).toBe(1); - expect(gitPublishProcess.stderr).toBe('Error: Git remote "origin" does not exist'); - }); + // expect(gitPublishProcess.exitCode).toBe(1); + // expect(gitPublishProcess.stderr).toBe('Error: Git remote "origin" does not exist'); + // }); - const { stdout: originRemote } = await execa('git', ['remote', 'get-url', 'origin']); - console.log({ originRemote }); - await git('remote', ['add', 'origin', originRemote]); + // const { stdout: originRemote } = await execa('git', ['remote', 'get-url', 'origin']); + // console.log({ originRemote }); + // await git('remote', ['add', 'origin', originRemote]); await test('Publishes', async ({ onTestFail }) => { const gitPublishProcess = await gitPublish(fixture.path); - console.log(gitPublishProcess); - onTestFail(() => { - console.log(gitPublishProcess); - }); + console.log(gitPublishProcess.all); + // onTestFail(() => { + // console.log(gitPublishProcess.all); + // }); expect(gitPublishProcess.exitCode).toBe(0); expect(gitPublishProcess.stdout).toMatch('✔'); diff --git a/tests/utils/create-git.ts b/tests/utils/create-git.ts index 259955c..1738e67 100644 --- a/tests/utils/create-git.ts +++ b/tests/utils/create-git.ts @@ -1,3 +1,5 @@ +import path from 'node:path'; +import fs from 'node:fs/promises'; import { execa, type Options } from 'execa'; export const createGit = async ( @@ -18,13 +20,17 @@ export const createGit = async ( ) ); - await git( - 'init', - [ - // In case of different default branch name - '--initial-branch=master', - ], - ); + const gitExists = await fs.access(path.join(cwd, '.git')).then(() => true, () => false); + + if (!gitExists) { + await git( + 'init', + [ + // In case of different default branch name + '--initial-branch=master', + ], + ); + } await git('config', ['user.name', 'name']); await git('config', ['user.email', 'email']); From 7d467b803c8bc08d590ecf4bd35be0ac9347f2ba Mon Sep 17 00:00:00 2001 From: name Date: Sun, 3 Nov 2024 18:06:39 +0900 Subject: [PATCH 09/23] wip --- src/index.ts | 4 ++++ tests/index.ts | 1 + 2 files changed, 5 insertions(+) diff --git a/src/index.ts b/src/index.ts index cd7c27a..ed140f5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -196,6 +196,9 @@ const { stringify } = JSON; } const publishFiles = await packlist(); + console.log({ + publishFiles, + }); if (publishFiles.length === 0) { throw new Error('No publish files found'); } @@ -210,6 +213,7 @@ const { stringify } = JSON; await execa('git', ['add', '-f', ...publishFiles]); const { stdout: trackedFiles } = await gitStatusTracked(); + console.log({ trackedFiles }); if (trackedFiles.length === 0) { console.warn('⚠️ No new changes found to commit.'); } else { diff --git a/tests/index.ts b/tests/index.ts index 8fffd84..1bb4b6e 100644 --- a/tests/index.ts +++ b/tests/index.ts @@ -84,6 +84,7 @@ describe('git-publish', ({ describe }) => { const gitPublishProcess = await gitPublish(fixture.path); console.log(gitPublishProcess.all); + console.log('====='); // onTestFail(() => { // console.log(gitPublishProcess.all); // }); From fbdfa11eb031dfb8dcd346edcba33abffc6cb7c8 Mon Sep 17 00:00:00 2001 From: name Date: Sun, 3 Nov 2024 18:08:29 +0900 Subject: [PATCH 10/23] wip --- tests/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/index.ts b/tests/index.ts index 1bb4b6e..abff9ae 100644 --- a/tests/index.ts +++ b/tests/index.ts @@ -85,6 +85,7 @@ describe('git-publish', ({ describe }) => { console.log(gitPublishProcess.all); console.log('====='); + console.log(gitPublishProcess); // onTestFail(() => { // console.log(gitPublishProcess.all); // }); From 6f126f336a8be9d21747a103c44db3e00971b03b Mon Sep 17 00:00:00 2001 From: name Date: Sun, 3 Nov 2024 18:09:31 +0900 Subject: [PATCH 11/23] wip --- tests/index.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/index.ts b/tests/index.ts index abff9ae..4d71964 100644 --- a/tests/index.ts +++ b/tests/index.ts @@ -12,7 +12,6 @@ const gitPublish = ( ) => execa(gitPublishPath, { cwd, reject: false, - all: true, }); describe('git-publish', ({ describe }) => { @@ -83,9 +82,10 @@ describe('git-publish', ({ describe }) => { await test('Publishes', async ({ onTestFail }) => { const gitPublishProcess = await gitPublish(fixture.path); - console.log(gitPublishProcess.all); + console.log('STDOUT', gitPublishProcess.stdout); + + console.log('STDERR', gitPublishProcess.stderr); console.log('====='); - console.log(gitPublishProcess); // onTestFail(() => { // console.log(gitPublishProcess.all); // }); From 5077ac0ffe9f7bf0d8d5eb28def277a84e1c53dc Mon Sep 17 00:00:00 2001 From: name Date: Sun, 3 Nov 2024 18:15:49 +0900 Subject: [PATCH 12/23] wip --- src/index.ts | 12 +++--------- tests/index.ts | 3 +++ tests/utils/create-git.ts | 2 +- 3 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src/index.ts b/src/index.ts index ed140f5..afba4eb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -79,8 +79,10 @@ const { stringify } = JSON; let success = false; // Validate remote exists + let remoteUrl; try { - await execa('git', ['remote', 'get-url', remote]); + const getRemoteUrl = await execa('git', ['remote', 'get-url', remote]); + remoteUrl = getRemoteUrl.stdout.trim(); } catch { throw new Error(`Git remote ${stringify(remote)} does not exist`); } @@ -271,14 +273,6 @@ const { stringify } = JSON; } if (success) { - let remoteUrl = remote; - - // If the "remote" flag contains an alias, resolve it to a URL - try { - const { stdout } = await execa('git', ['remote', 'get-url', remoteUrl]); - remoteUrl = stdout.trim(); - } catch {} - const parsedGitUrl = remoteUrl.match(/github\.com:(.+)\.git$/); if (parsedGitUrl) { diff --git a/tests/index.ts b/tests/index.ts index 4d71964..6d24fd9 100644 --- a/tests/index.ts +++ b/tests/index.ts @@ -12,6 +12,9 @@ const gitPublish = ( ) => execa(gitPublishPath, { cwd, reject: false, + env: { + FORCE_COLOR: '1', + }, }); describe('git-publish', ({ describe }) => { diff --git a/tests/utils/create-git.ts b/tests/utils/create-git.ts index 1738e67..6377e80 100644 --- a/tests/utils/create-git.ts +++ b/tests/utils/create-git.ts @@ -29,7 +29,7 @@ export const createGit = async ( // In case of different default branch name '--initial-branch=master', ], - ); + ); } await git('config', ['user.name', 'name']); From a8b91f4d3697a071da5decc23663e7f5c6ba13bf Mon Sep 17 00:00:00 2001 From: name Date: Sun, 3 Nov 2024 18:20:52 +0900 Subject: [PATCH 13/23] wip --- src/index.ts | 22 +++++++++++++++------- tests/index.ts | 3 --- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/index.ts b/src/index.ts index afba4eb..ade6865 100644 --- a/src/index.ts +++ b/src/index.ts @@ -236,13 +236,20 @@ const { stringify } = JSON; return; } - await execa('git', [ - 'push', - ...(fresh ? ['--force'] : []), - '--no-verify', - remote, - `${localTemporaryBranch}:${publishBranch}`, - ]); + console.log('pushing...'); + try { + await execa('git', [ + 'push', + ...(fresh ? ['--force'] : []), + '--no-verify', + remote, + `${localTemporaryBranch}:${publishBranch}`, + ]); + } + catch (error) { + console.log('ERROR', error); + throw error; + } success = true; }, ); @@ -251,6 +258,7 @@ const { stringify } = JSON; push.clear(); } } finally { + console.log('reverting...'); const revertBranch = await task(`Switching branch back to ${stringify(currentBranch)}`, async ({ setWarning }) => { if (dry) { setWarning(''); diff --git a/tests/index.ts b/tests/index.ts index 6d24fd9..4d71964 100644 --- a/tests/index.ts +++ b/tests/index.ts @@ -12,9 +12,6 @@ const gitPublish = ( ) => execa(gitPublishPath, { cwd, reject: false, - env: { - FORCE_COLOR: '1', - }, }); describe('git-publish', ({ describe }) => { From a7063401fcf8a0119bebd458ca554b02655c9461 Mon Sep 17 00:00:00 2001 From: name Date: Sun, 3 Nov 2024 18:23:26 +0900 Subject: [PATCH 14/23] wip --- src/index.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index ade6865..9f3bbcf 100644 --- a/src/index.ts +++ b/src/index.ts @@ -238,13 +238,14 @@ const { stringify } = JSON; console.log('pushing...'); try { - await execa('git', [ + const asdf = await execa('git', [ 'push', ...(fresh ? ['--force'] : []), '--no-verify', remote, `${localTemporaryBranch}:${publishBranch}`, ]); + console.log(asdf); } catch (error) { console.log('ERROR', error); From b614482d687dc04e4fce2ac8e7a52f4fbb5978cb Mon Sep 17 00:00:00 2001 From: name Date: Sun, 3 Nov 2024 18:27:09 +0900 Subject: [PATCH 15/23] wip --- tests/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/index.ts b/tests/index.ts index 4d71964..efd6566 100644 --- a/tests/index.ts +++ b/tests/index.ts @@ -80,6 +80,7 @@ describe('git-publish', ({ describe }) => { // await git('remote', ['add', 'origin', originRemote]); await test('Publishes', async ({ onTestFail }) => { + console.log(process.env); const gitPublishProcess = await gitPublish(fixture.path); console.log('STDOUT', gitPublishProcess.stdout); From df65fc3ac9d1fef2f87e215b859df9e1d696b08b Mon Sep 17 00:00:00 2001 From: name Date: Sun, 3 Nov 2024 18:30:03 +0900 Subject: [PATCH 16/23] wip --- tests/index.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/index.ts b/tests/index.ts index efd6566..0914d5f 100644 --- a/tests/index.ts +++ b/tests/index.ts @@ -12,6 +12,9 @@ const gitPublish = ( ) => execa(gitPublishPath, { cwd, reject: false, + env: { + FORCE_COLOR: 'true', + } }); describe('git-publish', ({ describe }) => { From bde42718bb28506ac6222435bf50a9f2fe0b007d Mon Sep 17 00:00:00 2001 From: name Date: Sun, 3 Nov 2024 18:36:27 +0900 Subject: [PATCH 17/23] wip --- tests/index.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/index.ts b/tests/index.ts index 0914d5f..0aaf81d 100644 --- a/tests/index.ts +++ b/tests/index.ts @@ -86,9 +86,13 @@ describe('git-publish', ({ describe }) => { console.log(process.env); const gitPublishProcess = await gitPublish(fixture.path); - console.log('STDOUT', gitPublishProcess.stdout); + console.dir({ + stdout: gitPublishProcess.stdout, + }, { colors: true, depth: null, maxArrayLength: null }); - console.log('STDERR', gitPublishProcess.stderr); + console.dir({ + stderr: gitPublishProcess.stderr, + }, { colors: true, depth: null, maxArrayLength: null }); console.log('====='); // onTestFail(() => { // console.log(gitPublishProcess.all); From 0c4f9f1b938d1d9228843e1899fbbf9f98a6c445 Mon Sep 17 00:00:00 2001 From: name Date: Sun, 3 Nov 2024 18:37:06 +0900 Subject: [PATCH 18/23] wip --- tests/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/index.ts b/tests/index.ts index 0aaf81d..a9708b3 100644 --- a/tests/index.ts +++ b/tests/index.ts @@ -14,6 +14,7 @@ const gitPublish = ( reject: false, env: { FORCE_COLOR: 'true', + CI: '1' } }); From d37d5d5f4254620414a121652948276515719ab6 Mon Sep 17 00:00:00 2001 From: name Date: Sun, 3 Nov 2024 18:37:49 +0900 Subject: [PATCH 19/23] wip --- tests/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/index.ts b/tests/index.ts index a9708b3..e3ce7f5 100644 --- a/tests/index.ts +++ b/tests/index.ts @@ -14,8 +14,8 @@ const gitPublish = ( reject: false, env: { FORCE_COLOR: 'true', - CI: '1' - } + CI: '0' + }, }); describe('git-publish', ({ describe }) => { From 7b5e087558709d828acfb90a89a0843e24be4389 Mon Sep 17 00:00:00 2001 From: name Date: Sun, 3 Nov 2024 18:38:54 +0900 Subject: [PATCH 20/23] wip --- tests/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/index.ts b/tests/index.ts index e3ce7f5..03fd5d2 100644 --- a/tests/index.ts +++ b/tests/index.ts @@ -13,9 +13,9 @@ const gitPublish = ( cwd, reject: false, env: { - FORCE_COLOR: 'true', - CI: '0' + PATH: process.env.PATH, }, + extendEnv: false, }); describe('git-publish', ({ describe }) => { From a20c7a94a2f12da9b2533baa682b9ca13eb7d5a6 Mon Sep 17 00:00:00 2001 From: name Date: Sun, 3 Nov 2024 18:43:05 +0900 Subject: [PATCH 21/23] wip --- src/index.ts | 33 +++++++++------------------------ tests/index.ts | 44 +++++++++----------------------------------- 2 files changed, 18 insertions(+), 59 deletions(-) diff --git a/src/index.ts b/src/index.ts index 9f3bbcf..76f9010 100644 --- a/src/index.ts +++ b/src/index.ts @@ -126,12 +126,10 @@ const { stringify } = JSON; } setTitle('Running hook "prepare"'); - const a = await execa('npm', ['run', '--if-present', 'prepare']); - console.log('a', a); + await execa('npm', ['run', '--if-present', 'prepare']); setTitle('Running hook "prepack"'); - const b = await execa('npm', ['run', '--if-present', 'prepack']); - console.log('b', b); + await execa('npm', ['run', '--if-present', 'prepack']); }); if (!dry) { @@ -198,9 +196,6 @@ const { stringify } = JSON; } const publishFiles = await packlist(); - console.log({ - publishFiles, - }); if (publishFiles.length === 0) { throw new Error('No publish files found'); } @@ -215,7 +210,6 @@ const { stringify } = JSON; await execa('git', ['add', '-f', ...publishFiles]); const { stdout: trackedFiles } = await gitStatusTracked(); - console.log({ trackedFiles }); if (trackedFiles.length === 0) { console.warn('⚠️ No new changes found to commit.'); } else { @@ -236,21 +230,13 @@ const { stringify } = JSON; return; } - console.log('pushing...'); - try { - const asdf = await execa('git', [ - 'push', - ...(fresh ? ['--force'] : []), - '--no-verify', - remote, - `${localTemporaryBranch}:${publishBranch}`, - ]); - console.log(asdf); - } - catch (error) { - console.log('ERROR', error); - throw error; - } + await execa('git', [ + 'push', + ...(fresh ? ['--force'] : []), + '--no-verify', + remote, + `${localTemporaryBranch}:${publishBranch}`, + ]); success = true; }, ); @@ -259,7 +245,6 @@ const { stringify } = JSON; push.clear(); } } finally { - console.log('reverting...'); const revertBranch = await task(`Switching branch back to ${stringify(currentBranch)}`, async ({ setWarning }) => { if (dry) { setWarning(''); diff --git a/tests/index.ts b/tests/index.ts index 03fd5d2..0f7f4c0 100644 --- a/tests/index.ts +++ b/tests/index.ts @@ -12,13 +12,14 @@ const gitPublish = ( ) => execa(gitPublishPath, { cwd, reject: false, + // Remove CI env var which prevents Ink from rendering env: { PATH: process.env.PATH, }, extendEnv: false, }); -describe('git-publish', ({ describe }) => { +describe('git-publish', ({ describe, test }) => { describe('Error cases', ({ test }) => { test('Fails if not in git repository', async () => { await using fixture = await createFixture(); @@ -55,7 +56,7 @@ describe('git-publish', ({ describe }) => { }); }); - describe('Current project', async ({ test }) => { + test('Publishes', async ({ onTestFail }) => { const fixture = await createFixture(process.cwd(), { templateFilter: cpPath => !( cpPath.endsWith(`${path.sep}node_modules`) @@ -67,40 +68,13 @@ describe('git-publish', ({ describe }) => { await fs.symlink(path.resolve('node_modules'), fixture.getPath('node_modules'), 'dir'); await fs.symlink(path.resolve('.git'), fixture.getPath('.git'), 'dir'); - console.log(fixture.path); - const git = await createGit(fixture.path); - // await git('add', ['.']); - // await git('commit', ['-am', 'Initial commit']); + const gitPublishProcess = await gitPublish(fixture.path); - // await test('Errors on missing remote', async () => { - // const gitPublishProcess = await gitPublish(fixture.path); - - // expect(gitPublishProcess.exitCode).toBe(1); - // expect(gitPublishProcess.stderr).toBe('Error: Git remote "origin" does not exist'); - // }); - - // const { stdout: originRemote } = await execa('git', ['remote', 'get-url', 'origin']); - // console.log({ originRemote }); - // await git('remote', ['add', 'origin', originRemote]); - - await test('Publishes', async ({ onTestFail }) => { - console.log(process.env); - const gitPublishProcess = await gitPublish(fixture.path); - - console.dir({ - stdout: gitPublishProcess.stdout, - }, { colors: true, depth: null, maxArrayLength: null }); - - console.dir({ - stderr: gitPublishProcess.stderr, - }, { colors: true, depth: null, maxArrayLength: null }); - console.log('====='); - // onTestFail(() => { - // console.log(gitPublishProcess.all); - // }); - - expect(gitPublishProcess.exitCode).toBe(0); - expect(gitPublishProcess.stdout).toMatch('✔'); + onTestFail(() => { + console.log(gitPublishProcess.all); }); + + expect(gitPublishProcess.exitCode).toBe(0); + expect(gitPublishProcess.stdout).toMatch('✔'); }); }); From 320c4b20b272b7e25cd2b4ed349a55065665a639 Mon Sep 17 00:00:00 2001 From: name Date: Sun, 3 Nov 2024 18:45:01 +0900 Subject: [PATCH 22/23] wip --- .github/workflows/test.yml | 3 +++ tests/index.ts | 16 +--------------- tests/utils/create-git.ts | 20 +++++++------------- tests/utils/git-publish.ts | 16 ++++++++++++++++ 4 files changed, 27 insertions(+), 28 deletions(-) create mode 100644 tests/utils/git-publish.ts diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 9fa0bad..ba0c295 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -31,6 +31,9 @@ jobs: with: run_install: true + - name: Lint + run: pnpm lint + - name: Type check run: pnpm type-check diff --git a/tests/index.ts b/tests/index.ts index 0f7f4c0..f157902 100644 --- a/tests/index.ts +++ b/tests/index.ts @@ -1,23 +1,9 @@ import path from 'node:path'; import fs from 'node:fs/promises'; -import { execa } from 'execa'; import { describe, expect } from 'manten'; import { createFixture } from 'fs-fixture'; import { createGit } from './utils/create-git.js'; - -const gitPublishPath = path.resolve('./dist/index.js'); - -const gitPublish = ( - cwd: string, -) => execa(gitPublishPath, { - cwd, - reject: false, - // Remove CI env var which prevents Ink from rendering - env: { - PATH: process.env.PATH, - }, - extendEnv: false, -}); +import { gitPublish } from './utils/git-publish.js'; describe('git-publish', ({ describe, test }) => { describe('Error cases', ({ test }) => { diff --git a/tests/utils/create-git.ts b/tests/utils/create-git.ts index 6377e80..259955c 100644 --- a/tests/utils/create-git.ts +++ b/tests/utils/create-git.ts @@ -1,5 +1,3 @@ -import path from 'node:path'; -import fs from 'node:fs/promises'; import { execa, type Options } from 'execa'; export const createGit = async ( @@ -20,17 +18,13 @@ export const createGit = async ( ) ); - const gitExists = await fs.access(path.join(cwd, '.git')).then(() => true, () => false); - - if (!gitExists) { - await git( - 'init', - [ - // In case of different default branch name - '--initial-branch=master', - ], - ); - } + await git( + 'init', + [ + // In case of different default branch name + '--initial-branch=master', + ], + ); await git('config', ['user.name', 'name']); await git('config', ['user.email', 'email']); diff --git a/tests/utils/git-publish.ts b/tests/utils/git-publish.ts new file mode 100644 index 0000000..ad845e8 --- /dev/null +++ b/tests/utils/git-publish.ts @@ -0,0 +1,16 @@ +import path from 'node:path'; +import { execa } from 'execa'; + +const gitPublishPath = path.resolve('./dist/index.js'); + +export const gitPublish = ( + cwd: string, +) => execa(gitPublishPath, { + cwd, + reject: false, + // Remove CI env var which prevents Ink from rendering + env: { + PATH: process.env.PATH, + }, + extendEnv: false, +}); From 8026a1d26c8151be3b6eed1879d44d520b6e2763 Mon Sep 17 00:00:00 2001 From: name Date: Sun, 3 Nov 2024 18:47:29 +0900 Subject: [PATCH 23/23] wip --- tests/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/index.ts b/tests/index.ts index f157902..da7cb0e 100644 --- a/tests/index.ts +++ b/tests/index.ts @@ -57,7 +57,7 @@ describe('git-publish', ({ describe, test }) => { const gitPublishProcess = await gitPublish(fixture.path); onTestFail(() => { - console.log(gitPublishProcess.all); + console.log(gitPublishProcess); }); expect(gitPublishProcess.exitCode).toBe(0);