feat: Add destination_dir option (#403)

Related to #324 #390
This commit is contained in:
Shohei Ueda
2020-07-21 11:15:53 +09:00
committed by GitHub
parent 0cb61e91a5
commit f30118c78e
9 changed files with 93 additions and 20 deletions

View File

@@ -15,6 +15,7 @@ export function showInputs(inps: Inputs): void {
[INFO] ${authMethod}: true
[INFO] PublishBranch: ${inps.PublishBranch}
[INFO] PublishDir: ${inps.PublishDir}
[INFO] DestinationDir: ${inps.DestinationDir}
[INFO] ExternalRepository: ${inps.ExternalRepository}
[INFO] AllowEmptyCommit: ${inps.AllowEmptyCommit}
[INFO] KeepFiles: ${inps.KeepFiles}
@@ -52,6 +53,7 @@ export function getInputs(): Inputs {
PersonalToken: core.getInput('personal_token'),
PublishBranch: core.getInput('publish_branch'),
PublishDir: core.getInput('publish_dir'),
DestinationDir: core.getInput('destination_dir'),
ExternalRepository: core.getInput('external_repository'),
AllowEmptyCommit: (core.getInput('allow_empty_commit') || 'false').toUpperCase() === 'TRUE',
KeepFiles: (core.getInput('keep_files') || 'false').toUpperCase() === 'TRUE',

View File

@@ -4,7 +4,7 @@ import * as io from '@actions/io';
import path from 'path';
import fs from 'fs';
import {Inputs, CmdResult} from './interfaces';
import {createWorkDir} from './utils';
import {createDir} from './utils';
export async function createBranchForce(branch: string): Promise<void> {
await exec.exec('git', ['init']);
@@ -12,7 +12,7 @@ export async function createBranchForce(branch: string): Promise<void> {
return;
}
export async function copyAssets(publishDir: string, workDir: string): Promise<void> {
export async function copyAssets(publishDir: string, destDir: string): Promise<void> {
const copyOpts = {recursive: true, force: true};
const files = fs.readdirSync(publishDir);
core.debug(`${files}`);
@@ -21,7 +21,7 @@ export async function copyAssets(publishDir: string, workDir: string): Promise<v
continue;
}
const filePath = path.join(publishDir, file);
await io.cp(filePath, `${workDir}/`, copyOpts);
await io.cp(filePath, `${destDir}/`, copyOpts);
core.info(`[INFO] copy ${file}`);
}
@@ -33,12 +33,24 @@ export async function setRepo(inps: Inputs, remoteURL: string, workDir: string):
? inps.PublishDir
: path.join(`${process.env.GITHUB_WORKSPACE}`, inps.PublishDir);
if (path.isAbsolute(inps.DestinationDir)) {
throw new Error('destination_dir should be a relative path');
}
const destDir = ((): string => {
if (inps.DestinationDir === '') {
return workDir;
} else {
return path.join(workDir, inps.DestinationDir);
}
})();
core.info(`[INFO] ForceOrphan: ${inps.ForceOrphan}`);
if (inps.ForceOrphan) {
await createWorkDir(workDir);
await createDir(destDir);
process.chdir(workDir);
await createBranchForce(inps.PublishBranch);
await copyAssets(publishDir, workDir);
process.chdir(destDir);
await copyAssets(publishDir, destDir);
return;
}
@@ -68,7 +80,7 @@ export async function setRepo(inps: Inputs, remoteURL: string, workDir: string):
await exec.exec('git', ['rm', '-r', '--ignore-unmatch', '*']);
}
await copyAssets(publishDir, workDir);
await copyAssets(publishDir, destDir);
return;
} else {
throw new Error(`Failed to clone remote branch ${inps.PublishBranch}`);
@@ -76,10 +88,10 @@ export async function setRepo(inps: Inputs, remoteURL: string, workDir: string):
} catch (e) {
core.info(`[INFO] first deployment, create new branch ${inps.PublishBranch}`);
core.info(e.message);
await createWorkDir(workDir);
await createDir(destDir);
process.chdir(workDir);
await createBranchForce(inps.PublishBranch);
await copyAssets(publishDir, workDir);
await copyAssets(publishDir, destDir);
return;
}
}

View File

@@ -4,6 +4,7 @@ export interface Inputs {
readonly PersonalToken: string;
readonly PublishBranch: string;
readonly PublishDir: string;
readonly DestinationDir: string;
readonly ExternalRepository: string;
readonly AllowEmptyCommit: boolean;
readonly KeepFiles: boolean;

View File

@@ -23,9 +23,9 @@ export async function getWorkDirName(unixTime: string): Promise<string> {
return workDirName;
}
export async function createWorkDir(workDirName: string): Promise<void> {
await io.mkdirP(workDirName);
core.debug(`Created: ${workDirName}`);
export async function createDir(dirPath: string): Promise<void> {
await io.mkdirP(dirPath);
core.debug(`Created directory ${dirPath}`);
return;
}