Skip to content

Commit 7c4b591

Browse files
peaceirisdhimmelnicolas-van
committed
feat: Add disable_nojekyll and cname options
- Add .nojekyll file by default for only the master and gh-pages branches. When the file already exists, this action does nothing. - When we set other branches to publish_dir, this action does not add .nojekyll file. cf. #112 Co-authored-by: Daniel Himmelstein <[email protected]> Co-authored-by: Nicolas Vanhoren <[email protected]>
1 parent 0b1dd44 commit 7c4b591

File tree

9 files changed

+243
-23
lines changed

9 files changed

+243
-23
lines changed

__tests__/get-inputs.test.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ afterEach(() => {
2121
delete process.env['INPUT_COMMIT_MESSAGE'];
2222
delete process.env['INPUT_TAG_NAME'];
2323
delete process.env['INPUT_TAG_MESSAGE'];
24+
delete process.env['INPUT_DISABLE_NOJEKYLL'];
25+
delete process.env['INPUT_CNAME'];
2426
});
2527

2628
describe('getInputs()', () => {
@@ -30,15 +32,6 @@ describe('getInputs()', () => {
3032
// process.env['INPUT_PERSONAL_TOKEN'] = 'test_personal_token';
3133
process.env['INPUT_PUBLISH_BRANCH'] = 'gh-pages';
3234
process.env['INPUT_PUBLISH_DIR'] = 'public';
33-
// process.env['INPUT_EXTERNAL_REPOSITORY'] = 'user/repo';
34-
// process.env['INPUT_ALLOW_EMPTY_COMMIT'] = 'true';
35-
// process.env['INPUT_KEEP_FILES'] = 'true';
36-
// process.env['INPUT_FORCE_ORPHAN'] = 'true';
37-
// process.env['INPUT_USER_NAME'] = 'username';
38-
// process.env['INPUT_USER_EMAIL'] = '[email protected]';
39-
// process.env['INPUT_COMMIT_MESSAGE'] = 'feat: Add new feature';
40-
// process.env['INPUT_TAG_NAME'] = 'deploy-v1.2.3';
41-
// process.env['INPUT_TAG_MESSAGE'] = 'Deployment v1.2.3';
4235

4336
const inps: Inputs = getInputs();
4437

@@ -56,6 +49,8 @@ describe('getInputs()', () => {
5649
expect(inps.CommitMessage).toMatch('');
5750
expect(inps.TagName).toMatch('');
5851
expect(inps.TagMessage).toMatch('');
52+
expect(inps.DisableNoJekyll).toBe(false);
53+
expect(inps.CNAME).toMatch('');
5954
});
6055

6156
test('get spec inputs', () => {
@@ -73,6 +68,8 @@ describe('getInputs()', () => {
7368
process.env['INPUT_COMMIT_MESSAGE'] = 'feat: Add new feature';
7469
process.env['INPUT_TAG_NAME'] = 'deploy-v1.2.3';
7570
process.env['INPUT_TAG_MESSAGE'] = 'Deployment v1.2.3';
71+
process.env['INPUT_DISABLE_NOJEKYLL'] = 'true';
72+
process.env['INPUT_CNAME'] = 'github.com';
7673

7774
const inps: Inputs = getInputs();
7875

@@ -90,5 +87,7 @@ describe('getInputs()', () => {
9087
expect(inps.CommitMessage).toMatch('feat: Add new feature');
9188
expect(inps.TagName).toMatch('deploy-v1.2.3');
9289
expect(inps.TagMessage).toMatch('Deployment v1.2.3');
90+
expect(inps.DisableNoJekyll).toBe(true);
91+
expect(inps.CNAME).toMatch('github.com');
9392
});
9493
});

__tests__/utils.test.ts

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
import path from 'path';
2+
import fs from 'fs';
3+
import {
4+
getHomeDir,
5+
getWorkDirName,
6+
createWorkDir,
7+
addNoJekyll,
8+
addCNAME
9+
} from '../src/utils';
10+
11+
beforeEach(() => {
12+
jest.resetModules();
13+
});
14+
15+
// afterEach(() => {
16+
17+
// });
18+
19+
describe('getHomeDir()', () => {
20+
test('get home directory name', async () => {
21+
let test = '';
22+
if (process.platform === 'win32') {
23+
test = 'C:\\Users\\runneradmin';
24+
} else {
25+
test = `${process.env.HOME}`;
26+
}
27+
const homeDir = await getHomeDir();
28+
expect(homeDir).toMatch(test);
29+
});
30+
});
31+
32+
async function getWorkDir(): Promise<string> {
33+
const date = new Date();
34+
const unixTime = date.getTime();
35+
let workDir = '';
36+
workDir = await getWorkDirName(`${unixTime}`);
37+
await createWorkDir(workDir);
38+
return workDir;
39+
}
40+
41+
describe('addNoJekyll()', () => {
42+
test('add .nojekyll gh-pages', async () => {
43+
let workDir = '';
44+
(async (): Promise<void> => {
45+
workDir = await getWorkDir();
46+
})();
47+
const filepath = path.join(workDir, '.nojekyll');
48+
49+
await addNoJekyll(workDir, false, 'gh-pages');
50+
const test1 = fs.existsSync(filepath);
51+
expect(test1).toBe(true);
52+
53+
fs.unlinkSync(filepath);
54+
});
55+
56+
test('add .nojekyll master', async () => {
57+
let workDir = '';
58+
(async (): Promise<void> => {
59+
workDir = await getWorkDir();
60+
})();
61+
const filepath = path.join(workDir, '.nojekyll');
62+
63+
await addNoJekyll(workDir, false, 'master');
64+
const test2 = fs.existsSync(filepath);
65+
expect(test2).toBe(true);
66+
67+
fs.unlinkSync(filepath);
68+
});
69+
70+
test('not add .nojekyll disable_nojekyll gh-pages', async () => {
71+
let workDir = '';
72+
(async (): Promise<void> => {
73+
workDir = await getWorkDir();
74+
})();
75+
const filepath = path.join(workDir, '.nojekyll');
76+
77+
await addNoJekyll(workDir, true, 'gh-pages');
78+
const test3 = fs.existsSync(filepath);
79+
expect(test3).toBe(false);
80+
});
81+
82+
test('not add .nojekyll disable_nojekyll master', async () => {
83+
let workDir = '';
84+
(async (): Promise<void> => {
85+
workDir = await getWorkDir();
86+
})();
87+
const filepath = path.join(workDir, '.nojekyll');
88+
89+
await addNoJekyll(workDir, true, 'master');
90+
const test4 = fs.existsSync(filepath);
91+
expect(test4).toBe(false);
92+
});
93+
94+
test('not add .nojekyll other-branch', async () => {
95+
let workDir = '';
96+
(async (): Promise<void> => {
97+
workDir = await getWorkDir();
98+
})();
99+
const filepath = path.join(workDir, '.nojekyll');
100+
101+
await addNoJekyll(workDir, false, 'other-branch');
102+
const test5 = fs.existsSync(filepath);
103+
expect(test5).toBe(false);
104+
});
105+
106+
test('not add .nojekyll disable_nojekyll other-branch', async () => {
107+
let workDir = '';
108+
(async (): Promise<void> => {
109+
workDir = await getWorkDir();
110+
})();
111+
const filepath = path.join(workDir, '.nojekyll');
112+
113+
await addNoJekyll(workDir, true, 'other-branch');
114+
const test6 = fs.existsSync(filepath);
115+
expect(test6).toBe(false);
116+
});
117+
});
118+
119+
describe('addCNAME()', () => {
120+
test('add CNAME', async () => {
121+
let workDir = '';
122+
(async (): Promise<void> => {
123+
workDir = await getWorkDir();
124+
})();
125+
const filepath = path.join(workDir, 'CNAME');
126+
127+
await addCNAME(workDir, 'github.com');
128+
const test1 = fs.readFileSync(filepath, 'utf8');
129+
expect(test1).toMatch('github.com');
130+
131+
fs.unlinkSync(filepath);
132+
});
133+
134+
test('do nothing', async () => {
135+
let workDir = '';
136+
(async (): Promise<void> => {
137+
workDir = await getWorkDir();
138+
})();
139+
const filepath = path.join(workDir, 'CNAME');
140+
141+
await addCNAME(workDir, '');
142+
const test2 = fs.existsSync(filepath);
143+
expect(test2).toBe(false);
144+
});
145+
146+
test('CNAME already exists', async () => {
147+
let workDir = '';
148+
(async (): Promise<void> => {
149+
workDir = await getWorkDir();
150+
})();
151+
const filepath = path.join(workDir, 'CNAME');
152+
153+
await addCNAME(workDir, 'github.io');
154+
await addCNAME(workDir, 'github.com');
155+
const test3 = fs.readFileSync(filepath, 'utf8');
156+
expect(test3).toMatch('github.io');
157+
158+
fs.unlinkSync(filepath);
159+
});
160+
});

action.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,10 @@ inputs:
5555
tag_message:
5656
description: 'Set tag message'
5757
required: false
58+
disable_nojekyll:
59+
description: 'Disable adding .nojekyll file to master or gh-pages branches'
60+
required: false
61+
default: 'false'
62+
cname:
63+
description: 'Set custom domain'
64+
required: false

src/get-inputs.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ function showInputs(inps: Inputs): void {
2121
core.info(`[INFO] CommitMessage: ${inps.CommitMessage}`);
2222
core.info(`[INFO] TagName: ${inps.TagName}`);
2323
core.info(`[INFO] TagMessage: ${inps.TagMessage}`);
24+
core.info(`[INFO] DisableNoJekyll: ${inps.DisableNoJekyll}`);
25+
core.info(`[INFO] CNAME: ${inps.CNAME}`);
2426
}
2527

2628
export function getInputs(): Inputs {
@@ -41,7 +43,10 @@ export function getInputs(): Inputs {
4143
UserEmail: core.getInput('user_email'),
4244
CommitMessage: core.getInput('commit_message'),
4345
TagName: core.getInput('tag_name'),
44-
TagMessage: core.getInput('tag_message')
46+
TagMessage: core.getInput('tag_message'),
47+
DisableNoJekyll:
48+
(core.getInput('disable_nojekyll') || 'false').toUpperCase() === 'TRUE',
49+
CNAME: core.getInput('cname')
4550
};
4651

4752
showInputs(inps);

src/git-utils.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,7 @@ import * as io from '@actions/io';
55
import path from 'path';
66
import fs from 'fs';
77
import {Inputs, CmdResult} from './interfaces';
8-
import {getHomeDir} from './utils';
9-
10-
export async function createWorkDir(workDirName: string): Promise<void> {
11-
await io.mkdirP(workDirName);
12-
core.debug(`workDir was created: ${workDirName}`);
13-
return;
14-
}
8+
import {createWorkDir} from './utils';
159

1610
export async function createBranchForce(branch: string): Promise<void> {
1711
await exec.exec('git', ['init']);
@@ -41,9 +35,8 @@ export async function copyAssets(
4135
export async function setRepo(
4236
inps: Inputs,
4337
remoteURL: string,
44-
unixTime: string
38+
workDir: string
4539
): Promise<void> {
46-
const workDir = path.join(getHomeDir(), `actions_github_pages_${unixTime}`);
4740
const publishDir = path.join(
4841
`${process.env.GITHUB_WORKSPACE}`,
4942
inps.PublishDir

src/interfaces.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export interface Inputs {
1313
readonly CommitMessage: string;
1414
readonly TagName: string;
1515
readonly TagMessage: string;
16+
readonly DisableNoJekyll: boolean;
17+
readonly CNAME: string;
1618
}
1719

1820
export interface CmdResult {

src/main.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {Inputs} from './interfaces';
44
import {getInputs} from './get-inputs';
55
import {setTokens} from './set-tokens';
66
import * as git from './git-utils';
7+
import {getWorkDirName, addNoJekyll, addCNAME} from './utils';
78

89
export async function run(): Promise<void> {
910
try {
@@ -16,12 +17,16 @@ export async function run(): Promise<void> {
1617

1718
const date = new Date();
1819
const unixTime = date.getTime();
19-
await git.setRepo(inps, remoteURL, `${unixTime}`);
20+
const workDir = await getWorkDirName(`${unixTime}`);
21+
await git.setRepo(inps, remoteURL, workDir);
22+
23+
await addNoJekyll(workDir, inps.DisableNoJekyll, inps.PublishBranch);
24+
await addCNAME(workDir, inps.CNAME);
2025

2126
try {
2227
await exec.exec('git', ['remote', 'rm', 'origin']);
2328
} catch (e) {
24-
core.info(`[INFO] e`);
29+
core.info(`[INFO] ${e}`);
2530
}
2631
await exec.exec('git', ['remote', 'add', 'origin', remoteURL]);
2732
await exec.exec('git', ['add', '--all']);

src/set-tokens.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export async function setSSHKey(
2222
): Promise<string> {
2323
core.info('[INFO] setup SSH deploy key');
2424

25-
const homeDir = getHomeDir();
25+
const homeDir = await getHomeDir();
2626
const sshDir = path.join(homeDir, '.ssh');
2727
await io.mkdirP(sshDir);
2828
await exec.exec('chmod', ['700', sshDir]);

src/utils.ts

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import * as core from '@actions/core';
2+
import * as io from '@actions/io';
3+
import path from 'path';
4+
import fs from 'fs';
25

3-
export function getHomeDir(): string {
6+
export async function getHomeDir(): Promise<string> {
47
let homedir = '';
58

69
if (process.platform === 'win32') {
@@ -13,3 +16,49 @@ export function getHomeDir(): string {
1316

1417
return homedir;
1518
}
19+
20+
export async function getWorkDirName(unixTime: string): Promise<string> {
21+
const homeDir = await getHomeDir();
22+
const workDirName = path.join(homeDir, `actions_github_pages_${unixTime}`);
23+
return workDirName;
24+
}
25+
26+
export async function createWorkDir(workDirName: string): Promise<void> {
27+
await io.mkdirP(workDirName);
28+
core.debug(`Created: ${workDirName}`);
29+
return;
30+
}
31+
32+
export async function addNoJekyll(
33+
workDir: string,
34+
DisableNoJekyll: boolean,
35+
PublishBranch: string
36+
): Promise<void> {
37+
if (DisableNoJekyll) {
38+
return;
39+
}
40+
if (PublishBranch === 'master' || PublishBranch === 'gh-pages') {
41+
const filepath = path.join(workDir, '.nojekyll');
42+
if (fs.existsSync(filepath)) {
43+
return;
44+
}
45+
fs.closeSync(fs.openSync(filepath, 'w'));
46+
core.info(`[INFO] Created ${filepath}`);
47+
}
48+
}
49+
50+
export async function addCNAME(
51+
workDir: string,
52+
content: string
53+
): Promise<void> {
54+
if (content === '') {
55+
return;
56+
}
57+
const filepath = path.join(workDir, 'CNAME');
58+
if (fs.existsSync(filepath)) {
59+
core.warning(`CNAME already exists`);
60+
return;
61+
}
62+
fs.writeFileSync(filepath, content + '\n');
63+
core.info(`[INFO] Created ${filepath}`);
64+
}

0 commit comments

Comments
 (0)