diff --git a/__tests__/fs-helper.test.ts b/__tests__/fs-helper.test.ts index 641d9f5..b86174a 100644 --- a/__tests__/fs-helper.test.ts +++ b/__tests__/fs-helper.test.ts @@ -1,5 +1,6 @@ import * as fsHelper from '../src/fs-helper' import * as fs from 'fs' +import * as path from 'path' import * as os from 'os' import { execSync } from 'child_process' @@ -131,6 +132,33 @@ describe('isDirectory', () => { }) }) +describe('isActionRepo', () => { + let stagingDir: string + + beforeEach(() => { + stagingDir = fsHelper.createTempDir() + }) + + afterEach(() => { + fs.rmSync(stagingDir, { recursive: true }) + }) + + it('returns true if action.yml exists at the root', () => { + fs.writeFileSync(path.join(stagingDir, `action.yml`), fileContent) + expect(fsHelper.isActionRepo(stagingDir)).toEqual(true) + }) + + it('returns true if action.yaml exists at the root', () => { + fs.writeFileSync(path.join(stagingDir, `action.yaml`), fileContent) + expect(fsHelper.isActionRepo(stagingDir)).toEqual(true) + }) + + it('returns false if action.y(a)ml doesn\'t exist at the root', () => { + fs.writeFileSync(path.join(stagingDir, `action.yaaml`), fileContent) + expect(fsHelper.isActionRepo(stagingDir)).toEqual(false) + }) +}) + describe('readFileContents', () => { let dir: string diff --git a/src/fs-helper.ts b/src/fs-helper.ts index 2245132..a191aba 100644 --- a/src/fs-helper.ts +++ b/src/fs-helper.ts @@ -91,6 +91,14 @@ export function isDirectory(dirPath: string): boolean { return fs.existsSync(dirPath) && fs.lstatSync(dirPath).isDirectory() } +export function isActionRepo(stagingDir: string): boolean { + return ( + fs.existsSync(path.join(stagingDir, 'action.yml')) || + fs.existsSync(path.join(stagingDir, 'action.yaml')) + ) +} + + export function readFileContents(filePath: string): Buffer { return fs.readFileSync(filePath) } @@ -116,6 +124,7 @@ export function bundleFilesintoDirectory( return targetDir } + // Converts a file path to a filemetadata object by querying the fs for relevant metadata. async function fileMetadata(filePath: string): Promise { const stats = fs.statSync(filePath) diff --git a/src/main.ts b/src/main.ts index 7a661ec..2c5c6f0 100644 --- a/src/main.ts +++ b/src/main.ts @@ -65,6 +65,13 @@ export async function run(pathInput: string): Promise { path = fsHelper.bundleFilesintoDirectory(paths, bundleDir) } + if (!fsHelper.isActionRepo(path)) { + core.setFailed( + 'action.y(a)ml not found. Action packages can be created only for action repositories.' + ) + return + } + // Create a temporary directory to store the archives const archiveDir = fsHelper.createTempDir() tmpDirs.push(archiveDir)