From 0f5c65e1408b30fe461ba319ca9a01d20c293cd4 Mon Sep 17 00:00:00 2001 From: Shohei Ueda <30958501+peaceiris@users.noreply.github.com> Date: Sat, 25 Jul 2020 20:52:35 +0900 Subject: [PATCH] feat: Add exclude_assets option (#416) Related to #163 --- __tests__/get-inputs.test.ts | 4 ++++ __tests__/git-utils.test.ts | 1 + action.yml | 4 ++++ src/get-inputs.ts | 4 +++- src/git-utils.ts | 24 +++++++++++++++++++----- src/interfaces.ts | 1 + 6 files changed, 32 insertions(+), 6 deletions(-) diff --git a/__tests__/get-inputs.test.ts b/__tests__/get-inputs.test.ts index a0537074c..a28dbb8a3 100644 --- a/__tests__/get-inputs.test.ts +++ b/__tests__/get-inputs.test.ts @@ -53,6 +53,7 @@ function getInputsLog(authMethod: string, inps: Inputs): string { [INFO] TagMessage: ${inps.TagMessage} [INFO] EnableJekyll (DisableNoJekyll): ${inps.DisableNoJekyll} [INFO] CNAME: ${inps.CNAME} +[INFO] ExcludeAssets ${inps.ExcludeAssets} `; } @@ -121,6 +122,7 @@ describe('getInputs()', () => { expect(inps.TagMessage).toMatch(''); expect(inps.DisableNoJekyll).toBe(false); expect(inps.CNAME).toMatch(''); + expect(inps.ExcludeAssets).toMatch('.github'); }); test('get spec inputs', () => { @@ -142,6 +144,7 @@ describe('getInputs()', () => { process.env['INPUT_TAG_MESSAGE'] = 'Deployment v1.2.3'; process.env['INPUT_DISABLE_NOJEKYLL'] = 'true'; process.env['INPUT_CNAME'] = 'github.com'; + process.env['INPUT_EXCLUDE_ASSETS'] = '.github'; const inps: Inputs = getInputs(); @@ -163,6 +166,7 @@ describe('getInputs()', () => { expect(inps.TagMessage).toMatch('Deployment v1.2.3'); expect(inps.DisableNoJekyll).toBe(true); expect(inps.CNAME).toMatch('github.com'); + expect(inps.ExcludeAssets).toMatch('.github'); }); test('get spec inputs enable_jekyll', () => { diff --git a/__tests__/git-utils.test.ts b/__tests__/git-utils.test.ts index 6b64fb741..40cefb9f7 100644 --- a/__tests__/git-utils.test.ts +++ b/__tests__/git-utils.test.ts @@ -40,6 +40,7 @@ describe('setRepo()', () => { // process.env['INPUT_TAG_MESSAGE'] = 'Deployment v1.2.3'; // process.env['INPUT_DISABLE_NOJEKYLL'] = 'true'; // process.env['INPUT_CNAME'] = 'github.com'; + process.env['INPUT_EXCLUDE_ASSETS'] = '.github'; const inps: Inputs = getInputs(); const remoteURL = 'https://x-access-token:pat@github.com/actions/pages.git'; const date = new Date(); diff --git a/action.yml b/action.yml index 651d5f8b0..175640d02 100644 --- a/action.yml +++ b/action.yml @@ -73,3 +73,7 @@ inputs: cname: description: 'Set custom domain' required: false + exclude_assets: + description: 'Set files or directories to exclude from a publish directory.' + required: false + default: '.github' diff --git a/src/get-inputs.ts b/src/get-inputs.ts index d19692485..1f7051724 100644 --- a/src/get-inputs.ts +++ b/src/get-inputs.ts @@ -28,6 +28,7 @@ export function showInputs(inps: Inputs): void { [INFO] TagMessage: ${inps.TagMessage} [INFO] EnableJekyll (DisableNoJekyll): ${inps.DisableNoJekyll} [INFO] CNAME: ${inps.CNAME} +[INFO] ExcludeAssets ${inps.ExcludeAssets} `); } @@ -65,7 +66,8 @@ export function getInputs(): Inputs { TagName: core.getInput('tag_name'), TagMessage: core.getInput('tag_message'), DisableNoJekyll: useBuiltinJekyll, - CNAME: core.getInput('cname') + CNAME: core.getInput('cname'), + ExcludeAssets: core.getInput('exclude_assets') }; return inps; diff --git a/src/git-utils.ts b/src/git-utils.ts index a07514494..2769194ee 100644 --- a/src/git-utils.ts +++ b/src/git-utils.ts @@ -12,14 +12,28 @@ export async function createBranchForce(branch: string): Promise { return; } -export async function copyAssets(publishDir: string, destDir: string): Promise { +export async function copyAssets( + publishDir: string, + destDir: string, + excludeAssets: string +): Promise { const copyOpts = {recursive: true, force: true}; const files = fs.readdirSync(publishDir); core.debug(`${files}`); for await (const file of files) { - if (file.endsWith('.git') || file.endsWith('.github')) { + const isExcludeFile = ((): boolean => { + const excludedAssetNames: Array = excludeAssets.split(','); + for (const excludedAssetName of excludedAssetNames) { + if (file === excludedAssetName) { + return true; + } + } + return false; + })(); + if (isExcludeFile || file === '.git') { continue; } + const filePublishPath = path.join(publishDir, file); const fileDestPath = path.join(destDir, file); const destPath = path.dirname(fileDestPath); @@ -54,7 +68,7 @@ export async function setRepo(inps: Inputs, remoteURL: string, workDir: string): await createDir(destDir); process.chdir(workDir); await createBranchForce(inps.PublishBranch); - await copyAssets(publishDir, destDir); + await copyAssets(publishDir, destDir, inps.ExcludeAssets); return; } @@ -96,7 +110,7 @@ export async function setRepo(inps: Inputs, remoteURL: string, workDir: string): } } - await copyAssets(publishDir, destDir); + await copyAssets(publishDir, destDir, inps.ExcludeAssets); process.chdir(workDir); return; } else { @@ -108,7 +122,7 @@ export async function setRepo(inps: Inputs, remoteURL: string, workDir: string): await createDir(destDir); process.chdir(workDir); await createBranchForce(inps.PublishBranch); - await copyAssets(publishDir, destDir); + await copyAssets(publishDir, destDir, inps.ExcludeAssets); return; } } diff --git a/src/interfaces.ts b/src/interfaces.ts index ad1babfed..d1a615dca 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -17,6 +17,7 @@ export interface Inputs { readonly TagMessage: string; readonly DisableNoJekyll: boolean; readonly CNAME: string; + readonly ExcludeAssets: string; } export interface CmdResult {