Skip to content

Commit

Permalink
feat: Add exclude_assets option (#416)
Browse files Browse the repository at this point in the history
Related to #163
  • Loading branch information
peaceiris committed Jul 25, 2020
1 parent 2046290 commit 0f5c65e
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 6 deletions.
4 changes: 4 additions & 0 deletions __tests__/get-inputs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}
`;
}

Expand Down Expand Up @@ -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', () => {
Expand All @@ -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();

Expand All @@ -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', () => {
Expand Down
1 change: 1 addition & 0 deletions __tests__/git-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:[email protected]/actions/pages.git';
const date = new Date();
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
4 changes: 3 additions & 1 deletion src/get-inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}
`);
}

Expand Down Expand Up @@ -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;
Expand Down
24 changes: 19 additions & 5 deletions src/git-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,28 @@ export async function createBranchForce(branch: string): Promise<void> {
return;
}

export async function copyAssets(publishDir: string, destDir: string): Promise<void> {
export async function copyAssets(
publishDir: string,
destDir: string,
excludeAssets: string
): Promise<void> {
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<string> = 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);
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface Inputs {
readonly TagMessage: string;
readonly DisableNoJekyll: boolean;
readonly CNAME: string;
readonly ExcludeAssets: string;
}

export interface CmdResult {
Expand Down

0 comments on commit 0f5c65e

Please sign in to comment.