Skip to content

Commit

Permalink
[Feature] 新增 remove、chmod 任务,发布 0.2.3
Browse files Browse the repository at this point in the history
  • Loading branch information
VisualSJ committed Nov 23, 2023
1 parent 2649299 commit f823853
Show file tree
Hide file tree
Showing 12 changed files with 379 additions and 45 deletions.
4 changes: 4 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# 使用 .gitignore 中的规则排除不需要包含在 npm 包中的文件
.github
.eslintrc.js
.eslintignore
.prettierrc.js
tsconfig.json
/source
/test

Expand Down
12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,15 @@
"typescript": "^5.2.2"
},
"dependencies": {
"@itharbors/structures": "^1.0.1",
"chalk": "^4.1.2"
"@itharbors/structures": "^1.0.2",
"@types/download": "^8.0.5",
"@types/fs-extra": "^11.0.4",
"@types/graceful-fs": "^4.1.9",
"chalk": "^4.1.2",
"download": "^8.0.0",
"fs-extra": "^11.1.1",
"graceful-fs": "^4.2.11",
"v-unzip": "^1.1.0",
"workflow-extra": "^0.2.7"
}
}
56 changes: 56 additions & 0 deletions source/internal/chmod.ts
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);
108 changes: 108 additions & 0 deletions source/internal/download.ts
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);
12 changes: 12 additions & 0 deletions source/internal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { TscConfig, TscTask } from './tsc';
import { RepoConfig, RepoTask } from './repo';
import { LessConfig, LessTask } from './less';
import { FileConfig, FileTask } from './file';
import { RemoveConfig, RemoveTask } from './remove';
import { ChmodConfig, ChmodTask } from './chmod';
import { DownloadConfig, DownloadTask } from './download';
import { NPMConfig, NPMTask } from './npm';

/**
* 任务配置
Expand All @@ -11,6 +15,10 @@ export type ConfigType = {
repo: RepoConfig,
less: LessConfig,
file: FileConfig,
remove: RemoveConfig,
chmod: ChmodConfig,
download: DownloadConfig,
npm: NPMConfig,
};

/**
Expand All @@ -21,4 +29,8 @@ export const Task = {
repo: RepoTask,
less: LessTask,
file: FileTask,
remove: RemoveTask,
chmod: ChmodTask,
download: DownloadTask,
npm: NPMTask,
};
2 changes: 2 additions & 0 deletions source/internal/less.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ export class LessTask extends Task {
try {
await bash('npx', ['lessc', config.source, config.dist], {
cwd: workspace,
}, (chunk) => {
this.print(chunk.toString());
});

// 有变化的时候,更新缓存
Expand Down
18 changes: 11 additions & 7 deletions source/internal/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
WriteStream,
} from 'fs';

import { italic } from 'chalk';
import { gray } from 'chalk';

import { registerTask, Task, TaskState } from '../task';
import { bash } from '../utils';
Expand All @@ -25,7 +25,7 @@ export type NPMConfig = {
logFile?: string,
}[];

export class FileTask extends Task {
export class NPMTask extends Task {
static getMaxConcurrent() {
return 1;
}
Expand All @@ -43,13 +43,15 @@ export class FileTask extends Task {
? config.path
: join(workspace, config.path);

this.print(italic(`npm ${config.params.join(' ')}`));
if (config.message) {
this.print(italic(config.message));
this.print(`npm ${config.params.join(' ')} - ${config.message}`);
} else {
this.print(`npm ${config.params.join(' ')}`);
}
this.print(`Execution Path: ${config.path}`);

this.print(gray(`Execution Path: ${config.path}`));
if (config.logFile) {
this.print(`Log file: ${config.logFile}`);
this.print(gray(`Log file: ${config.logFile}`));
}

// 执行命令
Expand All @@ -60,6 +62,8 @@ export class FileTask extends Task {
}
await bash('npm', config.params, {
cwd: source,
// @ts-ignore
stdio: writeStream ? undefined : 'inherit',
}, (data) => {
if (writeStream) {
writeStream.write(data.toString());
Expand All @@ -80,4 +84,4 @@ export class FileTask extends Task {
return hasError ? TaskState.error : TaskState.success;
}
}
registerTask('file', FileTask);
registerTask('npm', NPMTask);
52 changes: 52 additions & 0 deletions source/internal/remove.ts
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);
10 changes: 9 additions & 1 deletion source/internal/repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
existsSync,
mkdirSync,
renameSync,
} from 'fs';
} from 'fs-extra';

import { yellow } from 'chalk';

Expand Down Expand Up @@ -55,6 +55,8 @@ export const RepoTaskMethods = {
await makeDir(dir);
await bash('git', ['clone', remote, dir], {
cwd: dirname(path),
// @ts-ignore
stdio: 'inherit',
});
},

Expand All @@ -78,6 +80,10 @@ export const RepoTaskMethods = {
},
};
export class RepoTask extends Task {
static getMaxConcurrent() {
return 1;
}

getTitle() {
return 'Synchronize the Git repository';
}
Expand Down Expand Up @@ -289,8 +295,10 @@ export class RepoTask extends Task {
return TaskState.success;
}

task.outputLog();
for (const repoConfig of repoConfigArray) {
await checkoutRepo(repoConfig);
task.outputLog();
}

return err ? TaskState.error : TaskState.success;
Expand Down
Loading

0 comments on commit f823853

Please sign in to comment.