Skip to content

Commit

Permalink
[Bug] 修复配置文件夹不存在的时候写入报错的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
VisualSJ committed Nov 14, 2023
1 parent 3546c3d commit aac6cb4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
6 changes: 4 additions & 2 deletions source/task.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { join } from 'node:path';
import { join, dirname } from 'node:path';
import { writeFileSync } from 'node:fs';

import { magenta, cyan } from 'chalk';

import { formatTime } from './utils';
import { formatTime, makeDir } from './utils';

interface workflowConfig {
// 配置文件入口,需要时一个 js 文件,绝对地址
Expand Down Expand Up @@ -141,6 +141,8 @@ export async function executeTask(taskNameList: string[]) {
console.log = vendorLog;

// 每个小任务结束的时候,将配置重新写回文件
const dir = dirname(workflowOption.cacheFile);
await makeDir(dir);
writeFileSync(workflowOption.cacheFile, JSON.stringify(workflowCacheJSON, null, 2));
}

Expand Down
33 changes: 32 additions & 1 deletion source/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
Stats, stat, readdir, existsSync, mkdirSync,
Stats, stat, readdir, existsSync, mkdirSync, mkdir,
MakeDirectoryOptions,
} from 'fs';
import { join } from 'path';
import { spawn, SpawnOptionsWithoutStdio } from 'child_process';
Expand Down Expand Up @@ -100,6 +101,10 @@ export function formatTime(time: number) {
return `${(`${time}ms`).padStart(6, ' ')}`;
}

/**
* 打印
* @param str
*/
export function print(str: string | Buffer | Error) {
try {
const trimStr = (`${str}`).trim();
Expand All @@ -115,3 +120,29 @@ export function print(str: string | Buffer | Error) {
export function printEmpty() {
console.log(' ');
}

const getMode = (options: MakeDirectoryOptions) => {
const defaults = { mode: 0o777 };
if (typeof options === 'number') return options;
return ({ ...defaults, ...options }).mode;
};

/**
* 创建目录
* @param dir
* @param options
* @returns
*/
export async function makeDir(dir: string, options: MakeDirectoryOptions = {}) {
return new Promise((resolve, reject) => {
mkdir(dir, {
mode: getMode(options),
recursive: true,
}, (error) => {
if (error) {
return reject(error);
}
return resolve(undefined);
});
});
}

0 comments on commit aac6cb4

Please sign in to comment.