From fad9606a52af529957c3d46b488eb0e3067df666 Mon Sep 17 00:00:00 2001 From: David Date: Tue, 4 Oct 2022 20:24:31 +1100 Subject: [PATCH 01/12] Expand beta process --- src/publish-beta/command.ts | 31 ++++++++---------- src/publish-beta/compile.ts | 14 ++++++++ src/publish-beta/copy-files.spec.ts | 51 +++++++++++++++++++++++++++++ src/publish-beta/copy-files.ts | 22 +++++++++++++ src/publish-beta/package.ts | 5 ++- src/publish-beta/publish.ts | 14 ++++++++ 6 files changed, 117 insertions(+), 20 deletions(-) create mode 100644 src/publish-beta/compile.ts create mode 100644 src/publish-beta/copy-files.spec.ts create mode 100644 src/publish-beta/copy-files.ts create mode 100644 src/publish-beta/publish.ts diff --git a/src/publish-beta/command.ts b/src/publish-beta/command.ts index 591b940..ea49910 100644 --- a/src/publish-beta/command.ts +++ b/src/publish-beta/command.ts @@ -1,28 +1,25 @@ // publish-beta/command.ts import process from 'node:process'; +import path from 'node:path'; import { debug } from 'debug'; -import { getInput } from '@actions/core'; + +// import { getInput } from '@actions/core'; import { publishComment } from './github'; -import { isPackageAnAPI, packageJSONUpdate } from './package'; +import { packageJSONUpdate } from './package'; +import copyNonTSFiles from './copy-files'; +import compile from './compile'; +import publish from './publish'; const log = debug('action:command'); export default async function (): Promise { - const command = getInput('command').toLowerCase(); - log('Action starts:', command); - - if (command === 'publish-comment' && process.env['NEW_PACKAGE_VERSION']) { - return publishComment(process.env['NEW_PACKAGE_VERSION']); - } - - if (command === 'is-api') { - return isPackageAnAPI(process.cwd()); - } + // const command = getInput('command').toLowerCase(); + log('Action starts:command'); - if (command === 'generate-beta-version') { - return packageJSONUpdate(process.cwd()); - } - log('No valid command received: action-publish-beta [is-api | generate-beta-version | publish-comment]'); - throw new Error('no valid command'); + await compile(process.cwd()); + const packageNameAndBetaVersion = await packageJSONUpdate(process.cwd()); + await copyNonTSFiles(path.join(process.cwd(), 'src'), path.join(process.cwd(), 'dist')); + await publish(process.cwd()); + await publishComment(packageNameAndBetaVersion); } diff --git a/src/publish-beta/compile.ts b/src/publish-beta/compile.ts new file mode 100644 index 0000000..710f865 --- /dev/null +++ b/src/publish-beta/compile.ts @@ -0,0 +1,14 @@ +// publish-beta/compile.ts + +import childProcess from 'node:child_process'; +import util from 'node:util'; +import debug from 'debug'; + +const log = debug('action:compile'); + +const exec = util.promisify(childProcess.exec); +export default async function (directory: string): Promise { + log('compile starting'); + await exec('tsc --outDir dist', { cwd: directory }); + log('compile completed'); +} diff --git a/src/publish-beta/copy-files.spec.ts b/src/publish-beta/copy-files.spec.ts new file mode 100644 index 0000000..65511b4 --- /dev/null +++ b/src/publish-beta/copy-files.spec.ts @@ -0,0 +1,51 @@ +// publish-beta/copy-files.spec.ts + +import { strict as assert } from 'node:assert'; +import { mkdir, readFile, rm, writeFile } from 'node:fs/promises'; +import path from 'node:path'; +import { tmpdir } from 'node:os'; + +import { v4 as uuid } from 'uuid'; +import copyNonTSFiles from './copy-files'; + +describe('copy', () => { + beforeAll(async () => { + await mkdir(path.join(tmpdir(), 'testcopy'), { recursive: true }); + }); + + afterAll(async () => { + await rm(path.join(tmpdir(), 'testcopy'), { recursive: true }); + }); + + it('test recursive filtering', async () => { + const rootDirectory = path.join(tmpdir(), 'testcopy', uuid()); + await mkdir(rootDirectory); + + const sourceDirectory = path.join(rootDirectory, 'src'); + const destinationDirectory = path.join(rootDirectory, 'dist'); + await mkdir(destinationDirectory); + + const sourceV1 = path.join(sourceDirectory, 'api', 'v1'); + const sourceV2 = path.join(sourceDirectory, 'api', 'v2'); + + await mkdir(sourceV1, { recursive: true }); + await mkdir(sourceV2, { recursive: true }); + + await writeFile(path.join(sourceV1, 'test.ts'), 'test'); + await writeFile(path.join(sourceV2, 'test2.ts'), 'test'); + await writeFile(path.join(sourceV1, 'actiontestv1.yml'), 'actiontestv1.yml'); + await writeFile(path.join(sourceV2, 'actiontestv2.yml'), 'actiontestv2.yml'); + await writeFile(path.join(sourceDirectory, 'testfile.json'), 'testfile.json'); + await copyNonTSFiles(sourceDirectory, destinationDirectory); + + const file1 = await readFile(path.join(destinationDirectory, 'testfile.json'), 'utf8'); + assert.equal(file1, 'testfile.json'); + const file2 = await readFile(path.join(destinationDirectory, 'api/v1/actiontestv1.yml'), 'utf8'); + assert.equal(file2, 'actiontestv1.yml'); + const file3 = await readFile(path.join(destinationDirectory, 'api/v2/actiontestv2.yml'), 'utf8'); + assert.equal(file3, 'actiontestv2.yml'); + + await assert.rejects(readFile(path.join(destinationDirectory, 'api/v1/test.ts'), 'utf8')); + await assert.rejects(readFile(path.join(destinationDirectory, 'api/v2/test2.ts'), 'utf8')); + }); +}); diff --git a/src/publish-beta/copy-files.ts b/src/publish-beta/copy-files.ts new file mode 100644 index 0000000..2408bc3 --- /dev/null +++ b/src/publish-beta/copy-files.ts @@ -0,0 +1,22 @@ +// publish-beta/copy-files.ts +import path from 'node:path'; +import fs from 'node:fs/promises'; + +// copy all non .ts files from src to dist directory recursively +export default async function copyNonTSFiles(sourceDirectory: string, destinationDirectory: string): Promise { + const files = await fs.readdir(sourceDirectory, { withFileTypes: true }); + await Promise.all( + files.map(async (item) => { + const sourceItem = path.join(sourceDirectory, item.name); + const destinationItem = path.join(destinationDirectory, item.name); + if (item.isDirectory()) { + await fs.mkdir(destinationItem, { recursive: true }); + await copyNonTSFiles(sourceItem, path.join(destinationDirectory, item.name)); + return; + } + if (!item.name.endsWith('.ts')) { + await fs.copyFile(sourceItem, destinationItem); + } + }) + ); +} diff --git a/src/publish-beta/package.ts b/src/publish-beta/package.ts index 96c66ba..7d876e2 100644 --- a/src/publish-beta/package.ts +++ b/src/publish-beta/package.ts @@ -30,7 +30,7 @@ export function generatePackageBetaTag(): string { return `${prNumber}-${id}`; } -export async function packageJSONUpdate(rootProjectDirectory: string): Promise { +export async function packageJSONUpdate(rootProjectDirectory: string): Promise { const packageJSONPath = path.join(rootProjectDirectory, 'package.json'); const readPackageJson = await readFile(packageJSONPath, 'utf8'); const packageJson = JSON.parse(readPackageJson) as { version: string; name: string }; @@ -38,6 +38,5 @@ export async function packageJSONUpdate(rootProjectDirectory: string): Promise { + log('publish starting'); + await exec('npm publish --tag beta', { cwd: directory }); + log('publish completed'); +} From b618a864b4a54f546b4f5ad80c7c5a46fcdba7cc Mon Sep 17 00:00:00 2001 From: David Date: Wed, 5 Oct 2022 12:00:17 +1100 Subject: [PATCH 02/12] Add source maps --- src/publish-beta/compile.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/publish-beta/compile.ts b/src/publish-beta/compile.ts index 710f865..ba11106 100644 --- a/src/publish-beta/compile.ts +++ b/src/publish-beta/compile.ts @@ -9,6 +9,6 @@ const log = debug('action:compile'); const exec = util.promisify(childProcess.exec); export default async function (directory: string): Promise { log('compile starting'); - await exec('tsc --outDir dist', { cwd: directory }); + await exec('tsc --outDir dist --sourceMap true --declarationMap true', { cwd: directory }); log('compile completed'); } From 150aa01fe8f3cbaa17a1ea0a1e69bc36bb8aaa18 Mon Sep 17 00:00:00 2001 From: David Date: Wed, 5 Oct 2022 12:34:03 +1100 Subject: [PATCH 03/12] Throw error if now --- src/publish-beta/package.spec.ts | 13 ++++++++++++- src/publish-beta/package.ts | 9 +++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/publish-beta/package.spec.ts b/src/publish-beta/package.spec.ts index 0a8ef9c..937813b 100644 --- a/src/publish-beta/package.spec.ts +++ b/src/publish-beta/package.spec.ts @@ -11,12 +11,14 @@ import { generatePackageBetaTag, isPackageAnAPI, packageJSONUpdate } from './pac describe('package', () => { beforeAll(async () => { await mkdir(path.join(tmpdir(), 'packageUpdate'), { recursive: true }); + await mkdir(path.join(tmpdir(), 'packageUpdate2'), { recursive: true }); await mkdir(path.join(tmpdir(), 'hasAPI', 'src/api'), { recursive: true }); await mkdir(path.join(tmpdir(), 'noAPI', 'src/'), { recursive: true }); }); afterAll(async () => { await rm(path.join(tmpdir(), 'packageUpdate'), { recursive: true }); + await rm(path.join(tmpdir(), 'packageUpdate2'), { recursive: true }); await rm(path.join(tmpdir(), 'hasAPI'), { recursive: true }); await rm(path.join(tmpdir(), 'noAPI'), { recursive: true }); }); @@ -39,10 +41,19 @@ describe('package', () => { process.env['GITHUB_REF'] = '/ref/87/branch'; await writeFile( path.join(tmpdir(), 'packageUpdate/package.json'), - JSON.stringify({ name: 'testpackage', version: '1.2.10' }) + JSON.stringify({ name: 'testpackage', version: '1.2.10', files: ['/dist/'] }) ); await packageJSONUpdate(path.join(tmpdir(), 'packageUpdate')); const rawUpdatedFile = await readFile(filePath, 'utf8'); assert.ok(JSON.parse(rawUpdatedFile).version.startsWith('1.2.10-beta.87-')); }); + + it('Test with files property missing', async () => { + process.env['GITHUB_REF'] = '/ref/87/branch'; + await writeFile( + path.join(tmpdir(), 'packageUpdate2/package.json'), + JSON.stringify({ name: 'testpackage', version: '1.2.10', files: ['/dist/'] }) + ); + await assert.rejects(packageJSONUpdate(path.join(tmpdir(), 'packageUpdate'))); + }); }); diff --git a/src/publish-beta/package.ts b/src/publish-beta/package.ts index 7d876e2..16f30c0 100644 --- a/src/publish-beta/package.ts +++ b/src/publish-beta/package.ts @@ -30,9 +30,18 @@ export function generatePackageBetaTag(): string { return `${prNumber}-${id}`; } +function checkFilesPropertyExists(packageJSON: string): void { + const packageJSONObject = JSON.parse(packageJSON) as { files?: string[] }; + if (!packageJSONObject.files) { + log('Error - missing files: [] property in package.json'); + throw new Error('package.json does not have a files property'); + } +} + export async function packageJSONUpdate(rootProjectDirectory: string): Promise { const packageJSONPath = path.join(rootProjectDirectory, 'package.json'); const readPackageJson = await readFile(packageJSONPath, 'utf8'); + checkFilesPropertyExists(readPackageJson); const packageJson = JSON.parse(readPackageJson) as { version: string; name: string }; const newVersion = `${packageJson.version}-beta.${generatePackageBetaTag()}`; packageJson.version = newVersion; From b4d8b5ca80159630e6c531b8d6fc7f559529ce02 Mon Sep 17 00:00:00 2001 From: David Date: Wed, 5 Oct 2022 12:49:49 +1100 Subject: [PATCH 04/12] Better error result to console --- src/publish-beta/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/publish-beta/index.ts b/src/publish-beta/index.ts index 26f4517..78a7e94 100644 --- a/src/publish-beta/index.ts +++ b/src/publish-beta/index.ts @@ -9,9 +9,9 @@ command() process.exit(0); }) // eslint-disable-next-line unicorn/prefer-top-level-await - .catch(() => { + .catch((error) => { // eslint-disable-next-line no-console - console.log('Action Error - exit 1'); + console.log('Action Error - exit 1 - error:', error); // eslint-disable-next-line unicorn/no-process-exit process.exit(1); }); From cf6676b8a4fb2806c5c2a5873dc630c865daf6b1 Mon Sep 17 00:00:00 2001 From: David Date: Wed, 5 Oct 2022 12:53:52 +1100 Subject: [PATCH 05/12] Another attempt for better error message inside action --- src/publish-beta/package.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/publish-beta/package.ts b/src/publish-beta/package.ts index 16f30c0..3c32ab0 100644 --- a/src/publish-beta/package.ts +++ b/src/publish-beta/package.ts @@ -33,8 +33,7 @@ export function generatePackageBetaTag(): string { function checkFilesPropertyExists(packageJSON: string): void { const packageJSONObject = JSON.parse(packageJSON) as { files?: string[] }; if (!packageJSONObject.files) { - log('Error - missing files: [] property in package.json'); - throw new Error('package.json does not have a files property'); + throw new Error('package.json does not have a files: [] property'); } } From d0cc82fa33dc578acc1d479d142e474ed82191c9 Mon Sep 17 00:00:00 2001 From: David Date: Wed, 5 Oct 2022 13:11:53 +1100 Subject: [PATCH 06/12] Fix test that is sometimes tripping --- src/publish-beta/package.spec.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/publish-beta/package.spec.ts b/src/publish-beta/package.spec.ts index 937813b..509c0c5 100644 --- a/src/publish-beta/package.spec.ts +++ b/src/publish-beta/package.spec.ts @@ -52,8 +52,11 @@ describe('package', () => { process.env['GITHUB_REF'] = '/ref/87/branch'; await writeFile( path.join(tmpdir(), 'packageUpdate2/package.json'), - JSON.stringify({ name: 'testpackage', version: '1.2.10', files: ['/dist/'] }) + JSON.stringify({ name: 'testpackage', version: '1.2.10' }) + ); + await assert.rejects( + packageJSONUpdate(path.join(tmpdir(), 'packageUpdate2')), + '[Error: package.json does not have a files: [] property]' ); - await assert.rejects(packageJSONUpdate(path.join(tmpdir(), 'packageUpdate'))); }); }); From c23ebbd5679bc2042ad2e416644f9b5357ffc6f9 Mon Sep 17 00:00:00 2001 From: David Date: Wed, 5 Oct 2022 13:33:22 +1100 Subject: [PATCH 07/12] Remove unused function and comments --- src/publish-beta/command.ts | 3 --- src/publish-beta/package.spec.ts | 9 +-------- src/publish-beta/package.ts | 17 +---------------- 3 files changed, 2 insertions(+), 27 deletions(-) diff --git a/src/publish-beta/command.ts b/src/publish-beta/command.ts index ea49910..4251146 100644 --- a/src/publish-beta/command.ts +++ b/src/publish-beta/command.ts @@ -4,8 +4,6 @@ import process from 'node:process'; import path from 'node:path'; import { debug } from 'debug'; -// import { getInput } from '@actions/core'; - import { publishComment } from './github'; import { packageJSONUpdate } from './package'; import copyNonTSFiles from './copy-files'; @@ -14,7 +12,6 @@ import publish from './publish'; const log = debug('action:command'); export default async function (): Promise { - // const command = getInput('command').toLowerCase(); log('Action starts:command'); await compile(process.cwd()); diff --git a/src/publish-beta/package.spec.ts b/src/publish-beta/package.spec.ts index 509c0c5..4626028 100644 --- a/src/publish-beta/package.spec.ts +++ b/src/publish-beta/package.spec.ts @@ -6,7 +6,7 @@ import path from 'node:path'; import { tmpdir } from 'node:os'; import process from 'node:process'; -import { generatePackageBetaTag, isPackageAnAPI, packageJSONUpdate } from './package'; +import { generatePackageBetaTag, packageJSONUpdate } from './package'; describe('package', () => { beforeAll(async () => { @@ -29,13 +29,6 @@ describe('package', () => { assert.ok(packageBetaTag.startsWith('2406-')); }); - it('hasAPI', async () => { - assert.equal(await isPackageAnAPI(path.join(tmpdir(), 'hasAPI')), true); - }); - it('noAPI', async () => { - assert.equal(await isPackageAnAPI(path.join(tmpdir(), 'noAPI')), false); - }); - it('packageJSONUpdate', async () => { const filePath = path.join(tmpdir(), 'packageUpdate/package.json'); process.env['GITHUB_REF'] = '/ref/87/branch'; diff --git a/src/publish-beta/package.ts b/src/publish-beta/package.ts index 3c32ab0..1be6a87 100644 --- a/src/publish-beta/package.ts +++ b/src/publish-beta/package.ts @@ -1,7 +1,7 @@ // publish-beta/package.ts import path from 'node:path'; -import { readFile, stat, writeFile } from 'node:fs/promises'; +import { readFile, writeFile } from 'node:fs/promises'; import { debug } from 'debug'; import shortId from './short-id'; @@ -9,21 +9,6 @@ import { getPRNumber } from './github'; const log = debug('action:package'); -export async function isPackageAnAPI(rootProjectDirectory: string): Promise { - try { - await stat(path.join(rootProjectDirectory, 'src/api')); - log('isAPI:true'); - // eslint-disable-next-line no-console - console.log('::set-output name=IS_API::true'); - return true; - } catch { - log('isAPI:false'); - // eslint-disable-next-line no-console - console.log('::set-output name=IS_API::false'); - } - return false; -} - export function generatePackageBetaTag(): string { const id = shortId(); const prNumber = getPRNumber(); From 41499b263c912786141b9b0eacffc703139ade7f Mon Sep 17 00:00:00 2001 From: David Date: Fri, 7 Oct 2022 16:08:52 +1100 Subject: [PATCH 08/12] Remove command file --- src/publish-beta/command.ts | 22 ---------------------- src/publish-beta/index.ts | 23 +++++++++++++++++++++-- 2 files changed, 21 insertions(+), 24 deletions(-) delete mode 100644 src/publish-beta/command.ts diff --git a/src/publish-beta/command.ts b/src/publish-beta/command.ts deleted file mode 100644 index 4251146..0000000 --- a/src/publish-beta/command.ts +++ /dev/null @@ -1,22 +0,0 @@ -// publish-beta/command.ts - -import process from 'node:process'; -import path from 'node:path'; -import { debug } from 'debug'; - -import { publishComment } from './github'; -import { packageJSONUpdate } from './package'; -import copyNonTSFiles from './copy-files'; -import compile from './compile'; -import publish from './publish'; - -const log = debug('action:command'); -export default async function (): Promise { - log('Action starts:command'); - - await compile(process.cwd()); - const packageNameAndBetaVersion = await packageJSONUpdate(process.cwd()); - await copyNonTSFiles(path.join(process.cwd(), 'src'), path.join(process.cwd(), 'dist')); - await publish(process.cwd()); - await publishComment(packageNameAndBetaVersion); -} diff --git a/src/publish-beta/index.ts b/src/publish-beta/index.ts index 78a7e94..d6c2443 100644 --- a/src/publish-beta/index.ts +++ b/src/publish-beta/index.ts @@ -1,8 +1,27 @@ // publish-beta/index.ts + import process from 'node:process'; +import path from 'node:path'; +import { debug } from 'debug'; + +import { publishComment } from './github'; +import { packageJSONUpdate } from './package'; +import copyNonTSFiles from './copy-files'; +import compile from './compile'; +import publish from './publish'; + +const log = debug('action'); +export async function main(): Promise { + log('Action start'); + + await compile(process.cwd()); + const packageNameAndBetaVersion = await packageJSONUpdate(process.cwd()); + await copyNonTSFiles(path.join(process.cwd(), 'src'), path.join(process.cwd(), 'dist')); + await publish(process.cwd()); + await publishComment(packageNameAndBetaVersion); +} -import command from './command'; -command() +main() .then(() => { process.stdin.destroy(); // eslint-disable-next-line unicorn/no-process-exit From 619a60aeb44f680b9949f1773cd254d0a191e5e3 Mon Sep 17 00:00:00 2001 From: David Date: Mon, 10 Oct 2022 21:39:20 +1100 Subject: [PATCH 09/12] Adding src files for sourcemaps --- .../{copy-files.spec.ts => files.spec.ts} | 22 ++++++++++++++++--- src/publish-beta/{copy-files.ts => files.ts} | 19 ++++++++++++++-- src/publish-beta/index.ts | 2 +- src/publish-beta/package.spec.ts | 5 ++++- src/publish-beta/package.ts | 19 +++++++++++++++- 5 files changed, 59 insertions(+), 8 deletions(-) rename src/publish-beta/{copy-files.spec.ts => files.spec.ts} (68%) rename src/publish-beta/{copy-files.ts => files.ts} (57%) diff --git a/src/publish-beta/copy-files.spec.ts b/src/publish-beta/files.spec.ts similarity index 68% rename from src/publish-beta/copy-files.spec.ts rename to src/publish-beta/files.spec.ts index 65511b4..6570b85 100644 --- a/src/publish-beta/copy-files.spec.ts +++ b/src/publish-beta/files.spec.ts @@ -1,20 +1,22 @@ -// publish-beta/copy-files.spec.ts +// publish-beta/files.spec.ts import { strict as assert } from 'node:assert'; -import { mkdir, readFile, rm, writeFile } from 'node:fs/promises'; +import { mkdir, readdir, readFile, rm, writeFile } from 'node:fs/promises'; import path from 'node:path'; import { tmpdir } from 'node:os'; import { v4 as uuid } from 'uuid'; -import copyNonTSFiles from './copy-files'; +import copyNonTSFiles, { removeNonTSFiles } from './files'; describe('copy', () => { beforeAll(async () => { await mkdir(path.join(tmpdir(), 'testcopy'), { recursive: true }); + await mkdir(path.join(tmpdir(), 'testsrc', 'src'), { recursive: true }); }); afterAll(async () => { await rm(path.join(tmpdir(), 'testcopy'), { recursive: true }); + await rm(path.join(tmpdir(), 'testsrc'), { recursive: true }); }); it('test recursive filtering', async () => { @@ -48,4 +50,18 @@ describe('copy', () => { await assert.rejects(readFile(path.join(destinationDirectory, 'api/v1/test.ts'), 'utf8')); await assert.rejects(readFile(path.join(destinationDirectory, 'api/v2/test2.ts'), 'utf8')); }); + + it('test removal of all files except .ts (excluding .spec.ts and .test.ts)', async () => { + const sourceDirectory = path.join(tmpdir(), 'testsrc', 'src'); + await Promise.all([ + writeFile(path.join(sourceDirectory, 'test.ts'), 'test'), + writeFile(path.join(sourceDirectory, 'test.spec.ts'), 'test'), + writeFile(path.join(sourceDirectory, 'swagger.yml'), 'test'), + ]); + await removeNonTSFiles(sourceDirectory); + + const files = await readdir(sourceDirectory, { withFileTypes: true }); + assert.equal(files.length, 1); + assert.equal(files[0]?.name, 'test.ts'); + }); }); diff --git a/src/publish-beta/copy-files.ts b/src/publish-beta/files.ts similarity index 57% rename from src/publish-beta/copy-files.ts rename to src/publish-beta/files.ts index 2408bc3..07cf01e 100644 --- a/src/publish-beta/copy-files.ts +++ b/src/publish-beta/files.ts @@ -1,8 +1,7 @@ -// publish-beta/copy-files.ts +// publish-beta/files.ts import path from 'node:path'; import fs from 'node:fs/promises'; -// copy all non .ts files from src to dist directory recursively export default async function copyNonTSFiles(sourceDirectory: string, destinationDirectory: string): Promise { const files = await fs.readdir(sourceDirectory, { withFileTypes: true }); await Promise.all( @@ -20,3 +19,19 @@ export default async function copyNonTSFiles(sourceDirectory: string, destinatio }) ); } + +export async function removeNonTSFiles(sourceDirectory: string): Promise { + const files = await fs.readdir(sourceDirectory, { withFileTypes: true }); + await Promise.all( + files.map(async (item) => { + const sourceItem = path.join(sourceDirectory, item.name); + if (item.isDirectory()) { + await removeNonTSFiles(sourceItem); + return; + } + if (!item.name.endsWith('.ts') || item.name.endsWith('.spec.ts') || item.name.endsWith('.test.ts')) { + await fs.rm(sourceItem); + } + }) + ); +} diff --git a/src/publish-beta/index.ts b/src/publish-beta/index.ts index d6c2443..5d3ced3 100644 --- a/src/publish-beta/index.ts +++ b/src/publish-beta/index.ts @@ -6,7 +6,7 @@ import { debug } from 'debug'; import { publishComment } from './github'; import { packageJSONUpdate } from './package'; -import copyNonTSFiles from './copy-files'; +import copyNonTSFiles from './files'; import compile from './compile'; import publish from './publish'; diff --git a/src/publish-beta/package.spec.ts b/src/publish-beta/package.spec.ts index 4626028..b97b81e 100644 --- a/src/publish-beta/package.spec.ts +++ b/src/publish-beta/package.spec.ts @@ -29,16 +29,19 @@ describe('package', () => { assert.ok(packageBetaTag.startsWith('2406-')); }); - it('packageJSONUpdate', async () => { + it('test packageJSON Update and add /src/ to files', async () => { const filePath = path.join(tmpdir(), 'packageUpdate/package.json'); process.env['GITHUB_REF'] = '/ref/87/branch'; await writeFile( path.join(tmpdir(), 'packageUpdate/package.json'), JSON.stringify({ name: 'testpackage', version: '1.2.10', files: ['/dist/'] }) ); + await mkdir(path.join(tmpdir(), 'packageUpdate/src'), { recursive: true }); + await packageJSONUpdate(path.join(tmpdir(), 'packageUpdate')); const rawUpdatedFile = await readFile(filePath, 'utf8'); assert.ok(JSON.parse(rawUpdatedFile).version.startsWith('1.2.10-beta.87-')); + assert.deepEqual(JSON.parse(rawUpdatedFile).files.sort(), ['/dist/', '/src/'].sort()); }); it('Test with files property missing', async () => { diff --git a/src/publish-beta/package.ts b/src/publish-beta/package.ts index 1be6a87..e63831c 100644 --- a/src/publish-beta/package.ts +++ b/src/publish-beta/package.ts @@ -6,9 +6,16 @@ import { debug } from 'debug'; import shortId from './short-id'; import { getPRNumber } from './github'; +import { removeNonTSFiles } from './files'; const log = debug('action:package'); +interface PackageJSON { + name: string; + version: string; + files: string[]; +} + export function generatePackageBetaTag(): string { const id = shortId(); const prNumber = getPRNumber(); @@ -22,11 +29,21 @@ function checkFilesPropertyExists(packageJSON: string): void { } } +function addSourceToFilesProperty(input: PackageJSON) { + if (!input.files.includes('/src/')) { + input.files.push('/src/'); + } +} + export async function packageJSONUpdate(rootProjectDirectory: string): Promise { const packageJSONPath = path.join(rootProjectDirectory, 'package.json'); const readPackageJson = await readFile(packageJSONPath, 'utf8'); checkFilesPropertyExists(readPackageJson); - const packageJson = JSON.parse(readPackageJson) as { version: string; name: string }; + const packageJson = JSON.parse(readPackageJson) as PackageJSON; + addSourceToFilesProperty(packageJson); + + await removeNonTSFiles(path.join(rootProjectDirectory, 'src')); + const newVersion = `${packageJson.version}-beta.${generatePackageBetaTag()}`; packageJson.version = newVersion; await writeFile(packageJSONPath, JSON.stringify(packageJson)); From 1c8e68c45c03cfb36e1d3659eb763e557049169b Mon Sep 17 00:00:00 2001 From: David Date: Tue, 11 Oct 2022 09:09:54 +1100 Subject: [PATCH 10/12] Ensure swagger file isn't removed from src --- src/publish-beta/files.spec.ts | 5 +++-- src/publish-beta/files.ts | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/publish-beta/files.spec.ts b/src/publish-beta/files.spec.ts index 6570b85..c3adf12 100644 --- a/src/publish-beta/files.spec.ts +++ b/src/publish-beta/files.spec.ts @@ -61,7 +61,8 @@ describe('copy', () => { await removeNonTSFiles(sourceDirectory); const files = await readdir(sourceDirectory, { withFileTypes: true }); - assert.equal(files.length, 1); - assert.equal(files[0]?.name, 'test.ts'); + assert.equal(files.length, 2); + assert.ok(files.some((item) => item.name === 'test.ts')); + assert.ok(files.some((item) => item.name === 'swagger.yml')); }); }); diff --git a/src/publish-beta/files.ts b/src/publish-beta/files.ts index 07cf01e..4426822 100644 --- a/src/publish-beta/files.ts +++ b/src/publish-beta/files.ts @@ -29,7 +29,7 @@ export async function removeNonTSFiles(sourceDirectory: string): Promise { await removeNonTSFiles(sourceItem); return; } - if (!item.name.endsWith('.ts') || item.name.endsWith('.spec.ts') || item.name.endsWith('.test.ts')) { + if (item.name.endsWith('.ts') && (item.name.endsWith('.spec.ts') || item.name.endsWith('.test.ts'))) { await fs.rm(sourceItem); } }) From 362d940f372c762026d9cf7b162cc986277f1634 Mon Sep 17 00:00:00 2001 From: David Date: Tue, 11 Oct 2022 09:19:24 +1100 Subject: [PATCH 11/12] Handle src addition better --- src/publish-beta/package.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/publish-beta/package.ts b/src/publish-beta/package.ts index e63831c..3c470a8 100644 --- a/src/publish-beta/package.ts +++ b/src/publish-beta/package.ts @@ -29,10 +29,11 @@ function checkFilesPropertyExists(packageJSON: string): void { } } -function addSourceToFilesProperty(input: PackageJSON) { +function addSourceToFilesProperty(input: PackageJSON): string[] { if (!input.files.includes('/src/')) { - input.files.push('/src/'); + return [...input.files, '/src/']; } + return input.files; } export async function packageJSONUpdate(rootProjectDirectory: string): Promise { @@ -40,12 +41,13 @@ export async function packageJSONUpdate(rootProjectDirectory: string): Promise Date: Thu, 13 Oct 2022 12:22:45 +1100 Subject: [PATCH 12/12] log name switched to publish-beta --- src/publish-beta/compile.ts | 2 +- src/publish-beta/github.ts | 2 +- src/publish-beta/index.ts | 2 +- src/publish-beta/package.ts | 2 +- src/publish-beta/publish.ts | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/publish-beta/compile.ts b/src/publish-beta/compile.ts index ba11106..ee477eb 100644 --- a/src/publish-beta/compile.ts +++ b/src/publish-beta/compile.ts @@ -4,7 +4,7 @@ import childProcess from 'node:child_process'; import util from 'node:util'; import debug from 'debug'; -const log = debug('action:compile'); +const log = debug('publish-beta:compile'); const exec = util.promisify(childProcess.exec); export default async function (directory: string): Promise { diff --git a/src/publish-beta/github.ts b/src/publish-beta/github.ts index d6f58ad..8230b1d 100644 --- a/src/publish-beta/github.ts +++ b/src/publish-beta/github.ts @@ -9,7 +9,7 @@ export interface GithubConfigurationResponse { number: number; repo: string; } -const log = debug('action:github'); +const log = debug('publish-beta:github'); export function getPRNumber(): string { const prNumberSearch = process.env['GITHUB_REF']?.match(/[0-9]+/gu); diff --git a/src/publish-beta/index.ts b/src/publish-beta/index.ts index 5d3ced3..b878b1d 100644 --- a/src/publish-beta/index.ts +++ b/src/publish-beta/index.ts @@ -10,7 +10,7 @@ import copyNonTSFiles from './files'; import compile from './compile'; import publish from './publish'; -const log = debug('action'); +const log = debug('publish-beta'); export async function main(): Promise { log('Action start'); diff --git a/src/publish-beta/package.ts b/src/publish-beta/package.ts index 3c470a8..36ef17f 100644 --- a/src/publish-beta/package.ts +++ b/src/publish-beta/package.ts @@ -8,7 +8,7 @@ import shortId from './short-id'; import { getPRNumber } from './github'; import { removeNonTSFiles } from './files'; -const log = debug('action:package'); +const log = debug('publish-beta:package'); interface PackageJSON { name: string; diff --git a/src/publish-beta/publish.ts b/src/publish-beta/publish.ts index 7eed021..b076141 100644 --- a/src/publish-beta/publish.ts +++ b/src/publish-beta/publish.ts @@ -4,7 +4,7 @@ import childProcess from 'node:child_process'; import util from 'node:util'; import debug from 'debug'; -const log = debug('action:publish'); +const log = debug('publish-beta:publish'); const exec = util.promisify(childProcess.exec); export default async function (directory: string): Promise {