-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature] 新增 remove、chmod 任务,发布 0.2.3
- Loading branch information
Showing
12 changed files
with
379 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { | ||
join, | ||
isAbsolute, | ||
} from 'path'; | ||
import { | ||
statSync, | ||
chmodSync, | ||
} from 'fs'; | ||
|
||
import { italic } from 'chalk'; | ||
|
||
import { registerTask, Task, TaskState } from '../task'; | ||
|
||
export type ChmodConfig = { | ||
source: string; | ||
mode: number; | ||
}[]; | ||
|
||
export class ChmodTask extends Task { | ||
static getMaxConcurrent() { | ||
return 1; | ||
} | ||
|
||
getTitle() { | ||
return 'Remove files'; | ||
} | ||
|
||
async execute(workspace: string, configArray: ChmodConfig): Promise<TaskState> { | ||
let hasError = false; | ||
|
||
for (const config of configArray) { | ||
// 将相对路径转成绝对路径 | ||
const source = isAbsolute(config.source) | ||
? config.source | ||
: join(workspace, config.source); | ||
|
||
this.print(italic(`Modify file permissions: ${source} ${config.mode}`)); | ||
|
||
try { | ||
const fileStat = statSync(source); | ||
// eslint-disable-next-line no-bitwise | ||
if ((fileStat.mode & config.mode) === config.mode) { | ||
continue; | ||
} | ||
chmodSync(source, config.mode); | ||
} catch (error) { | ||
const err = error as Error; | ||
this.print(err.message); | ||
hasError = true; | ||
} | ||
} | ||
|
||
return hasError ? TaskState.error : TaskState.success; | ||
} | ||
} | ||
registerTask('chmod', ChmodTask); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { | ||
join, | ||
isAbsolute, | ||
basename, | ||
extname, | ||
} from 'path'; | ||
import { | ||
existsSync, | ||
outputFile, | ||
copySync, | ||
remove, | ||
removeSync, | ||
} from 'fs-extra'; | ||
import download from 'download'; | ||
import { unzip } from 'v-unzip'; | ||
|
||
import { italic, red, gray } from 'chalk'; | ||
|
||
import { registerTask, Task, TaskState } from '../task'; | ||
import { makeDir } from '../utils'; | ||
|
||
export type DownloadConfig = { | ||
url: string; | ||
dist: string; | ||
callback?: () => void, | ||
}[]; | ||
|
||
export class DownloadTask extends Task { | ||
static getMaxConcurrent() { | ||
return 1; | ||
} | ||
|
||
getTitle() { | ||
return 'Remove files'; | ||
} | ||
|
||
async execute(workspace: string, configArray: DownloadConfig): Promise<TaskState> { | ||
let hasError = false; | ||
|
||
const tempDir = this.getCacheDir(); | ||
if (!tempDir) { | ||
this.print(red('The cache directory is not set up correctly')); | ||
return TaskState.error; | ||
} | ||
|
||
if (!existsSync(tempDir)) { | ||
await makeDir(tempDir); | ||
} | ||
|
||
for (const config of configArray) { | ||
// 将相对路径转成绝对路径 | ||
const name = basename(config.url); | ||
const tempFile = join(tempDir, name); | ||
const dist = isAbsolute(config.dist) | ||
? config.dist | ||
: join(workspace, config.dist); | ||
|
||
this.print(italic(`${config.url}`)); | ||
|
||
try { | ||
if (!existsSync(tempFile)) { | ||
const data = await download(config.url); | ||
await outputFile(tempFile, data); | ||
|
||
if (existsSync(dist)) { | ||
await remove(dist); | ||
} | ||
this.print(italic(` => download: ${tempFile}`)); | ||
} else { | ||
// this.print(gray(` => cache: ${tempFile}`)); | ||
} | ||
|
||
if (!existsSync(dist)) { | ||
this.print(gray(` => unzip: ${dist}`)); | ||
if (extname(tempFile) === '.zip') { | ||
await unzip(tempFile, dist, { | ||
/** | ||
* 递归删除 __MACOSX 文件夹 | ||
* 这个文件夹是 mac 文件系统自动创建的,并且可能会自动更改 | ||
* 文件变化有可能会引起签名失效 | ||
* @param file | ||
*/ | ||
recursive(file: string) { | ||
if (basename(file) !== '__MACOSX') { | ||
return; | ||
} | ||
removeSync(file); | ||
}, | ||
}); | ||
} else { | ||
this.print(gray(` => copy: ${dist}`)); | ||
copySync(tempFile, dist); | ||
} | ||
if (config.callback) { | ||
config.callback(); | ||
} | ||
} | ||
} catch (error) { | ||
hasError = true; | ||
const err = error as Error; | ||
this.print(red(err.message)); | ||
} | ||
} | ||
|
||
return hasError ? TaskState.error : TaskState.success; | ||
} | ||
} | ||
registerTask('download', DownloadTask); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { | ||
join, | ||
isAbsolute, | ||
relative, | ||
} from 'path'; | ||
import { | ||
existsSync, | ||
statSync, | ||
Stats, | ||
copyFileSync, | ||
} from 'fs'; | ||
import { rmSync } from 'graceful-fs'; | ||
|
||
import { italic } from 'chalk'; | ||
|
||
import { registerTask, Task, TaskState } from '../task'; | ||
|
||
export type RemoveConfig = string[]; | ||
|
||
export class RemoveTask extends Task { | ||
static getMaxConcurrent() { | ||
return 1; | ||
} | ||
|
||
getTitle() { | ||
return 'Remove files'; | ||
} | ||
|
||
async execute(workspace: string, configArray: RemoveConfig): Promise<TaskState> { | ||
let hasError = false; | ||
|
||
for (const config of configArray) { | ||
// 将相对路径转成绝对路径 | ||
const source = isAbsolute(config) | ||
? config | ||
: join(workspace, config); | ||
|
||
this.print(italic(`Remove files in ${source}`)); | ||
|
||
try { | ||
rmSync(source, { recursive: true, force: true }); | ||
} catch (error) { | ||
const err = error as Error; | ||
this.print(err.message); | ||
hasError = true; | ||
} | ||
} | ||
|
||
return hasError ? TaskState.error : TaskState.success; | ||
} | ||
} | ||
registerTask('remove', RemoveTask); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.