diff --git a/back/config/util.ts b/back/config/util.ts index e135b98a..a97f77a1 100644 --- a/back/config/util.ts +++ b/back/config/util.ts @@ -2,6 +2,7 @@ import * as fs from 'fs'; import * as path from 'path'; import got from 'got'; import iconv from 'iconv-lite'; +import { exec } from 'child_process'; export function getFileContentByName(fileName: string) { if (fs.existsSync(fileName)) { @@ -332,3 +333,15 @@ export function readDir( }); return result; } + +export function promiseExec(command: string): Promise { + return new Promise((resolve, reject) => { + exec( + command, + { maxBuffer: 200 * 1024 * 1024, encoding: 'utf8' }, + (err, stdout, stderr) => { + resolve(stdout || stderr || JSON.stringify(err)); + }, + ); + }); +} diff --git a/back/loaders/deps.ts b/back/loaders/deps.ts index a392eb1a..3da8bfe8 100644 --- a/back/loaders/deps.ts +++ b/back/loaders/deps.ts @@ -2,6 +2,7 @@ import path from 'path'; import fs from 'fs'; import chokidar from 'chokidar'; import config from '../config/index'; +import { promiseExec } from '../config/util'; function linkToNodeModule(src: string, dst?: string) { const target = path.join(config.rootPath, 'node_modules', dst || src); @@ -16,7 +17,32 @@ function linkToNodeModule(src: string, dst?: string) { }); } +async function linkCommand() { + const commandPath = await promiseExec('which node'); + const commandDir = path.dirname(commandPath); + const linkShell = [ + { + src: 'update.sh', + dest: 'ql', + }, + { + src: 'task.sh', + dest: 'task', + }, + ]; + + for (const link of linkShell) { + const source = path.join(config.rootPath, 'shell', link.src); + const target = path.join(commandDir, link.dest); + if (fs.existsSync(target)) { + fs.unlinkSync(target); + } + fs.symlink(source, target, (err) => {}); + } +} + export default async (src: string = 'deps') => { + await linkCommand(); linkToNodeModule(src); const source = path.join(config.rootPath, src); diff --git a/shell/share.sh b/shell/share.sh index 9f809a86..eedcec18 100755 --- a/shell/share.sh +++ b/shell/share.sh @@ -377,6 +377,41 @@ reload_pm2() { pm2 start $dir_static/build/public.js -n public --source-map-support --time &>/dev/null } +diff_time() { + local format="$1" + local begin_time="$2" + local end_time="$3" + + if [[ $is_macos -eq 1 ]]; then + diff_time=$(($(date -j -f "$format" "$end_time" +%s) - $(date -j -f "$format" "$begin_time" +%s))) + else + diff_time=$(($(date +%s -d "$end_time") - $(date +%s -d "$begin_time"))) + fi + echo "$diff_time" +} + +format_time() { + local format="$1" + local time="$2" + + if [[ $is_macos -eq 1 ]]; then + echo $(date -j -f "$format" "$time" "+%Y-%m-%d %H:%M:%S") + else + echo $(date -d "$time" "+%Y-%m-%d %H:%M:%S") + fi +} + +format_timestamp() { + local format="$1" + local time="$2" + + if [[ $is_macos -eq 1 ]]; then + echo $(date -j -f "$format" "$time" "+%s") + else + echo $(date -d "$time" "+%s") + fi +} + init_env detect_termux detect_macos diff --git a/shell/task.sh b/shell/task.sh index 914fd14c..4fda15a2 100755 --- a/shell/task.sh +++ b/shell/task.sh @@ -84,8 +84,8 @@ run_normal() { fi fi - local time=$(date) - log_time=$(date -d "$time" "+%Y-%m-%d-%H-%M-%S") + local time=$(date "+%Y-%m-%d-%H-%M-%S") + log_time="$time" log_dir_tmp="${file_param##*/}" if [[ $file_param =~ "/" ]]; then if [[ $file_param == /* ]]; then @@ -99,12 +99,12 @@ run_normal() { [[ $log_dir_tmp_path ]] && log_dir_tmp="${log_dir_tmp_path}_${log_dir_tmp}" log_dir="${log_dir_tmp%.*}" log_path="$log_dir/$log_time.log" - cmd="&>> $dir_log/$log_path" + cmd=">> $dir_log/$log_path 2>&1" [[ "$show_log" == "true" ]] && cmd="" make_dir "$dir_log/$log_dir" - local begin_time=$(date -d "$time" "+%Y-%m-%d %H:%M:%S") - local begin_timestamp=$(date -d "$time" "+%s") + local begin_time=$(format_time "%Y-%m-%d-%H-%M-%S" "$time") + local begin_timestamp=$(format_timestamp "%Y-%m-%d-%H-%M-%S" "$time") eval echo -e "\#\# 开始执行... $begin_time\\\n" $cmd [[ -f $task_error_log_path ]] && eval cat $task_error_log_path $cmd @@ -155,8 +155,8 @@ run_concurrent() { [[ ! -z $cookieStr ]] && export ${env_param}=${cookieStr} define_program "$file_param" - local time=$(date) - log_time=$(date -d "$time" "+%Y-%m-%d-%H-%M-%S") + local time=$(date "+%Y-%m-%d-%H-%M-%S") + log_time="$time" log_dir_tmp="${file_param##*/}" if [[ $file_param =~ "/" ]]; then if [[ $file_param == /* ]]; then @@ -170,12 +170,12 @@ run_concurrent() { [[ $log_dir_tmp_path ]] && log_dir_tmp="${log_dir_tmp_path}_${log_dir_tmp}" log_dir="${log_dir_tmp%.*}" log_path="$log_dir/$log_time.log" - cmd="&>> $dir_log/$log_path" + cmd=">> $dir_log/$log_path 2>&1" [[ "$show_log" == "true" ]] && cmd="" make_dir "$dir_log/$log_dir" - local begin_time=$(date -d "$time" "+%Y-%m-%d %H:%M:%S") - local begin_timestamp=$(date -d "$time" "+%s") + local begin_time=$(format_time "%Y-%m-%d-%H-%M-%S" "$time") + local begin_timestamp=$(format_timestamp "%Y-%m-%d-%H-%M-%S" "$time") eval echo -e "\#\# 开始执行... $begin_time\\\n" $cmd [[ -f $task_error_log_path ]] && eval cat $task_error_log_path $cmd @@ -225,8 +225,8 @@ run_designated() { fi define_program "$file_param" - local time=$(date) - log_time=$(date -d "$time" "+%Y-%m-%d-%H-%M-%S") + local time=$(date "+%Y-%m-%d-%H-%M-%S") + log_time="$time" log_dir_tmp="${file_param##*/}" if [[ $file_param =~ "/" ]]; then if [[ $file_param == /* ]]; then @@ -240,12 +240,12 @@ run_designated() { [[ $log_dir_tmp_path ]] && log_dir_tmp="${log_dir_tmp_path}_${log_dir_tmp}" log_dir="${log_dir_tmp%.*}" log_path="$log_dir/$log_time.log" - cmd="&>> $dir_log/$log_path" + cmd=">> $dir_log/$log_path 2>&1" [[ "$show_log" == "true" ]] && cmd="" make_dir "$dir_log/$log_dir" - local begin_time=$(date -d "$time" "+%Y-%m-%d %H:%M:%S") - local begin_timestamp=$(date -d "$time" "+%s") + local begin_time=$(format_time "%Y-%m-%d-%H-%M-%S" "$time") + local begin_timestamp=$(format_timestamp "%Y-%m-%d-%H-%M-%S" "$time") local envs=$(eval echo "\$${env_param}") local array=($(echo $envs | sed 's/&/ /g')) @@ -289,8 +289,8 @@ run_designated() { run_else() { local file_param="$1" define_program "$file_param" - local time=$(date) - log_time=$(date -d "$time" "+%Y-%m-%d-%H-%M-%S") + local time=$(date "+%Y-%m-%d-%H-%M-%S") + log_time="$time" log_dir_tmp="${file_param##*/}" if [[ $file_param =~ "/" ]]; then if [[ $file_param == /* ]]; then @@ -304,12 +304,12 @@ run_else() { [[ $log_dir_tmp_path ]] && log_dir_tmp="${log_dir_tmp_path}_${log_dir_tmp}" log_dir="${log_dir_tmp%.*}" log_path="$log_dir/$log_time.log" - cmd="&>> $dir_log/$log_path" + cmd=">> $dir_log/$log_path 2>&1" [[ "$show_log" == "true" ]] && cmd="" make_dir "$dir_log/$log_dir" - local begin_time=$(date -d "$time" "+%Y-%m-%d %H:%M:%S") - local begin_timestamp=$(date -d "$time" "+%s") + local begin_time=$(format_time "%Y-%m-%d-%H-%M-%S" "$time") + local begin_timestamp=$(format_timestamp "%Y-%m-%d-%H-%M-%S" "$time") eval echo -e "\#\# 开始执行... $begin_time\\\n" $cmd [[ -f $task_error_log_path ]] && eval cat $task_error_log_path $cmd diff --git a/shell/update.sh b/shell/update.sh index 52262d65..3dc3633b 100755 --- a/shell/update.sh +++ b/shell/update.sh @@ -538,7 +538,7 @@ main() { ;; esac local end_time=$(date '+%Y-%m-%d %H:%M:%S') - local diff_time=$(($(date +%s -d "$end_time") - $(date +%s -d "$begin_time"))) + local diff_time=$(diff_time "%Y-%m-%d %H:%M:%S" "$begin_time" "$end_time") if [[ $p1 != "repo" ]] && [[ $p1 != "raw" ]]; then echo -e "\n## 执行结束... $end_time 耗时 $diff_time 秒" >>$log_path cat $log_path