diff --git a/__tests__/fixtures/publish_dir_1/assets/lib.css b/__tests__/fixtures/publish_dir_1/assets/lib.css new file mode 100644 index 0000000..f077569 --- /dev/null +++ b/__tests__/fixtures/publish_dir_1/assets/lib.css @@ -0,0 +1 @@ +/* CSS */ diff --git a/__tests__/fixtures/publish_dir_1/assets/lib.js b/__tests__/fixtures/publish_dir_1/assets/lib.js new file mode 100644 index 0000000..2da32ee --- /dev/null +++ b/__tests__/fixtures/publish_dir_1/assets/lib.js @@ -0,0 +1 @@ +// JavaScript diff --git a/__tests__/fixtures/publish_dir_1/index.html b/__tests__/fixtures/publish_dir_1/index.html new file mode 100644 index 0000000..aed6353 --- /dev/null +++ b/__tests__/fixtures/publish_dir_1/index.html @@ -0,0 +1,11 @@ + + + + + + Document + + + + + diff --git a/__tests__/fixtures/publish_dir_1/main.css b/__tests__/fixtures/publish_dir_1/main.css new file mode 100644 index 0000000..f077569 --- /dev/null +++ b/__tests__/fixtures/publish_dir_1/main.css @@ -0,0 +1 @@ +/* CSS */ diff --git a/__tests__/fixtures/publish_dir_1/main.js b/__tests__/fixtures/publish_dir_1/main.js new file mode 100644 index 0000000..2da32ee --- /dev/null +++ b/__tests__/fixtures/publish_dir_1/main.js @@ -0,0 +1 @@ +// JavaScript diff --git a/__tests__/fixtures/publish_dir_root/.github/CODEOWNERS b/__tests__/fixtures/publish_dir_root/.github/CODEOWNERS new file mode 100644 index 0000000..524a38c --- /dev/null +++ b/__tests__/fixtures/publish_dir_root/.github/CODEOWNERS @@ -0,0 +1 @@ +# https://help.github.com/en/github/creating-cloning-and-archiving-repositories/about-code-owners diff --git a/__tests__/fixtures/publish_dir_root/.github/ISSUE_TEMPLATE/template.md b/__tests__/fixtures/publish_dir_root/.github/ISSUE_TEMPLATE/template.md new file mode 100644 index 0000000..5b4b3db --- /dev/null +++ b/__tests__/fixtures/publish_dir_root/.github/ISSUE_TEMPLATE/template.md @@ -0,0 +1 @@ + diff --git a/__tests__/fixtures/publish_dir_root/.github/dependabot.yml b/__tests__/fixtures/publish_dir_root/.github/dependabot.yml new file mode 100644 index 0000000..64ed514 --- /dev/null +++ b/__tests__/fixtures/publish_dir_root/.github/dependabot.yml @@ -0,0 +1,7 @@ +# dependabot config +version: 2 +updates: + - package-ecosystem: npm + directory: "/" + schedule: + interval: daily diff --git a/__tests__/fixtures/publish_dir_root/.github/workflows/test.yml b/__tests__/fixtures/publish_dir_root/.github/workflows/test.yml new file mode 100644 index 0000000..dd01e9a --- /dev/null +++ b/__tests__/fixtures/publish_dir_root/.github/workflows/test.yml @@ -0,0 +1 @@ +name: 'Test' diff --git a/__tests__/fixtures/publish_dir_root/assets/lib.css b/__tests__/fixtures/publish_dir_root/assets/lib.css new file mode 100644 index 0000000..f077569 --- /dev/null +++ b/__tests__/fixtures/publish_dir_root/assets/lib.css @@ -0,0 +1 @@ +/* CSS */ diff --git a/__tests__/fixtures/publish_dir_root/assets/lib.js b/__tests__/fixtures/publish_dir_root/assets/lib.js new file mode 100644 index 0000000..2da32ee --- /dev/null +++ b/__tests__/fixtures/publish_dir_root/assets/lib.js @@ -0,0 +1 @@ +// JavaScript diff --git a/__tests__/fixtures/publish_dir_root/index.html b/__tests__/fixtures/publish_dir_root/index.html new file mode 100644 index 0000000..aed6353 --- /dev/null +++ b/__tests__/fixtures/publish_dir_root/index.html @@ -0,0 +1,11 @@ + + + + + + Document + + + + + diff --git a/__tests__/fixtures/publish_dir_root/main.css b/__tests__/fixtures/publish_dir_root/main.css new file mode 100644 index 0000000..f077569 --- /dev/null +++ b/__tests__/fixtures/publish_dir_root/main.css @@ -0,0 +1 @@ +/* CSS */ diff --git a/__tests__/fixtures/publish_dir_root/main.js b/__tests__/fixtures/publish_dir_root/main.js new file mode 100644 index 0000000..2da32ee --- /dev/null +++ b/__tests__/fixtures/publish_dir_root/main.js @@ -0,0 +1 @@ +// JavaScript diff --git a/__tests__/git-utils.test.ts b/__tests__/git-utils.test.ts index 40cefb9..79f549d 100644 --- a/__tests__/git-utils.test.ts +++ b/__tests__/git-utils.test.ts @@ -1,4 +1,5 @@ import { + copyAssets, setRepo, getUserName, getUserEmail, @@ -10,6 +11,17 @@ import {Inputs} from '../src/interfaces'; import {getWorkDirName, createDir} from '../src/utils'; import {CmdResult} from '../src/interfaces'; import * as exec from '@actions/exec'; +import {cp, rm} from 'shelljs'; +import path from 'path'; +import fs from 'fs'; + +const testRoot = path.resolve(__dirname); + +async function createTestDir(name: string): Promise { + const date = new Date(); + const unixTime = date.getTime(); + return await getWorkDirName(`${unixTime}_${name}`); +} beforeEach(() => { jest.resetModules(); @@ -22,6 +34,96 @@ afterEach(() => { delete process.env['GITHUB_REPOSITORY']; }); +describe('copyAssets', () => { + let gitTempDir = ''; + (async (): Promise => { + const date = new Date(); + const unixTime = date.getTime(); + gitTempDir = await getWorkDirName(`${unixTime}_git`); + })(); + + beforeAll(async () => { + await createDir(gitTempDir); + process.chdir(gitTempDir); + await exec.exec('git', ['init']); + }); + + test('copy assets from publish_dir to root, delete .github', async () => { + const publishDir = await createTestDir('src'); + const destDir = await createTestDir('dst'); + cp('-Rf', path.resolve(testRoot, 'fixtures/publish_dir_1'), publishDir); + cp('-Rf', gitTempDir, destDir); + + await copyAssets(publishDir, destDir, '.github'); + expect(fs.existsSync(path.resolve(destDir, '.github'))).toBeFalsy(); + expect(fs.existsSync(path.resolve(destDir, 'index.html'))).toBeTruthy(); + expect(fs.existsSync(path.resolve(destDir, 'assets/lib.css'))).toBeTruthy(); + rm('-rf', publishDir, destDir); + }); + + test('copy assets from publish_dir to root, delete .github,main.js', async () => { + const publishDir = await createTestDir('src'); + const destDir = await createTestDir('dst'); + cp('-Rf', path.resolve(testRoot, 'fixtures/publish_dir_1'), publishDir); + cp('-Rf', gitTempDir, destDir); + + await copyAssets(publishDir, destDir, '.github,main.js'); + expect(fs.existsSync(path.resolve(destDir, '.github'))).toBeFalsy(); + expect(fs.existsSync(path.resolve(destDir, 'index.html'))).toBeTruthy(); + expect(fs.existsSync(path.resolve(destDir, 'main.js'))).toBeFalsy(); + expect(fs.existsSync(path.resolve(destDir, 'assets/lib.css'))).toBeTruthy(); + expect(fs.existsSync(path.resolve(destDir, 'assets/lib.js'))).toBeTruthy(); + rm('-rf', publishDir, destDir); + }); + + test('copy assets from publish_dir to root, delete nothing', async () => { + const publishDir = await createTestDir('src'); + const destDir = await createTestDir('dst'); + cp('-Rf', path.resolve(testRoot, 'fixtures/publish_dir_root'), publishDir); + cp('-Rf', gitTempDir, destDir); + + await copyAssets(publishDir, destDir, ''); + expect(fs.existsSync(path.resolve(destDir, '.github'))).toBeTruthy(); + expect(fs.existsSync(path.resolve(destDir, 'index.html'))).toBeTruthy(); + expect(fs.existsSync(path.resolve(destDir, 'main.js'))).toBeTruthy(); + expect(fs.existsSync(path.resolve(destDir, 'assets/lib.css'))).toBeTruthy(); + expect(fs.existsSync(path.resolve(destDir, 'assets/lib.js'))).toBeTruthy(); + rm('-rf', publishDir, destDir); + }); + + test('copy assets from root to root, delete .github', async () => { + const publishDir = await createTestDir('src'); + const destDir = await createTestDir('dst'); + cp('-Rf', path.resolve(testRoot, 'fixtures/publish_dir_root'), publishDir); + cp('-Rf', gitTempDir, destDir); + cp('-Rf', gitTempDir, publishDir); + + await copyAssets(publishDir, destDir, '.github'); + expect(fs.existsSync(path.resolve(destDir, '.github'))).toBeFalsy(); + expect(fs.existsSync(path.resolve(destDir, 'index.html'))).toBeTruthy(); + expect(fs.existsSync(path.resolve(destDir, 'assets/lib.css'))).toBeTruthy(); + rm('-rf', publishDir, destDir); + }); + + test('copy assets from root to root, delete nothing', async () => { + const publishDir = await createTestDir('src'); + const destDir = await createTestDir('dst'); + cp('-Rf', path.resolve(testRoot, 'fixtures/publish_dir_root'), publishDir); + cp('-Rf', gitTempDir, destDir); + cp('-Rf', gitTempDir, publishDir); + + await copyAssets(publishDir, destDir, ''); + expect(fs.existsSync(path.resolve(destDir, '.github'))).toBeTruthy(); + expect(fs.existsSync(path.resolve(destDir, 'index.html'))).toBeTruthy(); + expect(fs.existsSync(path.resolve(destDir, 'assets/lib.css'))).toBeTruthy(); + rm('-rf', publishDir, destDir); + }); + + test.todo('copy assets from root to subdir, delete .github'); + test.todo('copy assets from root to subdir, delete .github,main.js'); + test.todo('copy assets from root to subdir, delete nothing'); +}); + describe('setRepo()', () => { test('throw error destination_dir should be a relative path', async () => { process.env['INPUT_GITHUB_TOKEN'] = 'test_github_token'; diff --git a/src/git-utils.ts b/src/git-utils.ts index 6aa564a..4e5d33b 100644 --- a/src/git-utils.ts +++ b/src/git-utils.ts @@ -1,12 +1,11 @@ import * as core from '@actions/core'; import * as exec from '@actions/exec'; -import * as io from '@actions/io'; import * as glob from '@actions/glob'; import path from 'path'; import fs from 'fs'; import {Inputs, CmdResult} from './interfaces'; import {createDir} from './utils'; -import {cp} from 'shelljs'; +import {cp, rm} from 'shelljs'; export async function createBranchForce(branch: string): Promise { await exec.exec('git', ['init']); @@ -15,6 +14,7 @@ export async function createBranchForce(branch: string): Promise { } export async function deleteExcludedAssets(destDir: string, excludeAssets: string): Promise { + if (excludeAssets === '') return; core.info(`[INFO] delete excluded assets`); const excludedAssetNames: Array = excludeAssets.split(','); const excludedAssetPaths = ((): Array => { @@ -25,10 +25,11 @@ export async function deleteExcludedAssets(destDir: string, excludeAssets: strin return paths; })(); const globber = await glob.create(excludedAssetPaths.join('\n')); - for await (const asset of globber.globGenerator()) { - core.info(`[INFO] delete ${asset}`); - io.rmRF(asset); + const files = await globber.glob(); + for await (const file of globber.globGenerator()) { + core.info(`[INFO] delete ${file}`); } + rm('-rf', files); return; } @@ -39,15 +40,15 @@ export async function copyAssets( ): Promise { core.info(`[INFO] prepare publishing assets`); - if (fs.existsSync(destDir) === false) { + if (!fs.existsSync(destDir)) { core.info(`[INFO] create ${destDir}`); await createDir(destDir); } const dotGitPath = path.join(publishDir, '.git'); if (fs.existsSync(dotGitPath)) { - core.info(`[INFO] delete .git`); - io.rmRF(dotGitPath); + core.info(`[INFO] delete ${dotGitPath}`); + rm('-rf', dotGitPath); } core.info(`[INFO] copy ${publishDir} to ${destDir}`);