@@ -4,15 +4,15 @@ import * as io from '@actions/io';
44import path from 'path' ;
55import fs from 'fs' ;
66import { Inputs , CmdResult } from './interfaces' ;
7- import { createWorkDir } from './utils' ;
7+ import { createDir } from './utils' ;
88
99export async function createBranchForce ( branch : string ) : Promise < void > {
1010 await exec . exec ( 'git' , [ 'init' ] ) ;
1111 await exec . exec ( 'git' , [ 'checkout' , '--orphan' , branch ] ) ;
1212 return ;
1313}
1414
15- export async function copyAssets ( publishDir : string , workDir : string ) : Promise < void > {
15+ export async function copyAssets ( publishDir : string , destDir : string ) : Promise < void > {
1616 const copyOpts = { recursive : true , force : true } ;
1717 const files = fs . readdirSync ( publishDir ) ;
1818 core . debug ( `${ files } ` ) ;
@@ -21,7 +21,7 @@ export async function copyAssets(publishDir: string, workDir: string): Promise<v
2121 continue ;
2222 }
2323 const filePath = path . join ( publishDir , file ) ;
24- await io . cp ( filePath , `${ workDir } /` , copyOpts ) ;
24+ await io . cp ( filePath , `${ destDir } /` , copyOpts ) ;
2525 core . info ( `[INFO] copy ${ file } ` ) ;
2626 }
2727
@@ -33,12 +33,24 @@ export async function setRepo(inps: Inputs, remoteURL: string, workDir: string):
3333 ? inps . PublishDir
3434 : path . join ( `${ process . env . GITHUB_WORKSPACE } ` , inps . PublishDir ) ;
3535
36+ if ( path . isAbsolute ( inps . DestinationDir ) ) {
37+ throw new Error ( 'destination_dir should be a relative path' ) ;
38+ }
39+ const destDir = ( ( ) : string => {
40+ if ( inps . DestinationDir === '' ) {
41+ return workDir ;
42+ } else {
43+ return path . join ( workDir , inps . DestinationDir ) ;
44+ }
45+ } ) ( ) ;
46+
3647 core . info ( `[INFO] ForceOrphan: ${ inps . ForceOrphan } ` ) ;
3748 if ( inps . ForceOrphan ) {
38- await createWorkDir ( workDir ) ;
49+ await createDir ( destDir ) ;
3950 process . chdir ( workDir ) ;
4051 await createBranchForce ( inps . PublishBranch ) ;
41- await copyAssets ( publishDir , workDir ) ;
52+ process . chdir ( destDir ) ;
53+ await copyAssets ( publishDir , destDir ) ;
4254 return ;
4355 }
4456
@@ -68,18 +80,18 @@ export async function setRepo(inps: Inputs, remoteURL: string, workDir: string):
6880 await exec . exec ( 'git' , [ 'rm' , '-r' , '--ignore-unmatch' , '*' ] ) ;
6981 }
7082
71- await copyAssets ( publishDir , workDir ) ;
83+ await copyAssets ( publishDir , destDir ) ;
7284 return ;
7385 } else {
7486 throw new Error ( `Failed to clone remote branch ${ inps . PublishBranch } ` ) ;
7587 }
7688 } catch ( e ) {
7789 core . info ( `[INFO] first deployment, create new branch ${ inps . PublishBranch } ` ) ;
7890 core . info ( e . message ) ;
79- await createWorkDir ( workDir ) ;
91+ await createDir ( destDir ) ;
8092 process . chdir ( workDir ) ;
8193 await createBranchForce ( inps . PublishBranch ) ;
82- await copyAssets ( publishDir , workDir ) ;
94+ await copyAssets ( publishDir , destDir ) ;
8395 return ;
8496 }
8597}
0 commit comments