utils.ts
1 import * as core from '@actions/core'; 2 import * as io from '@actions/io'; 3 import path from 'path'; 4 import fs from 'fs'; 5 6 export async function getHomeDir(): Promise<string> { 7 let homedir = ''; 8 9 if (process.platform === 'win32') { 10 homedir = process.env['USERPROFILE'] || 'C:\\'; 11 } else { 12 homedir = `${process.env.HOME}`; 13 } 14 15 core.debug(`homeDir: ${homedir}`); 16 17 return homedir; 18 } 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 createDir(dirPath: string): Promise<void> { 27 await io.mkdirP(dirPath); 28 core.debug(`Created directory ${dirPath}`); 29 return; 30 } 31 32 export async function addNoJekyll(workDir: string, DisableNoJekyll: boolean): Promise<void> { 33 if (DisableNoJekyll) { 34 return; 35 } 36 const filepath = path.join(workDir, '.nojekyll'); 37 if (fs.existsSync(filepath)) { 38 return; 39 } 40 fs.closeSync(fs.openSync(filepath, 'w')); 41 core.info(`[INFO] Created ${filepath}`); 42 } 43 44 export async function addCNAME(workDir: string, content: string): Promise<void> { 45 if (content === '') { 46 return; 47 } 48 const filepath = path.join(workDir, 'CNAME'); 49 if (fs.existsSync(filepath)) { 50 core.info(`CNAME already exists, skip adding CNAME`); 51 return; 52 } 53 fs.writeFileSync(filepath, content + '\n'); 54 core.info(`[INFO] Created ${filepath}`); 55 } 56 57 export async function skipOnFork( 58 isForkRepository: boolean, 59 githubToken: string, 60 deployKey: string, 61 personalToken: string 62 ): Promise<boolean> { 63 if (isForkRepository) { 64 if (githubToken === '' && deployKey === '' && personalToken === '') { 65 return true; 66 } 67 } 68 69 return false; 70 }