-
Notifications
You must be signed in to change notification settings - Fork 422
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
151c421
commit daf37a0
Showing
6 changed files
with
494 additions
and
99 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,118 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const readdir = require('fs-readdir-recursive'); | ||
const archiver = require('archiver'); | ||
const chalk = require('chalk'); | ||
|
||
/** | ||
* | ||
* @returns {{ | ||
* totalBytes: number; | ||
* }} | ||
*/ | ||
async function createConsistentZip( | ||
srcDir, | ||
destZip, | ||
gitUTC0Time = new Date(1980, 0, 1) | ||
) { | ||
fs.mkdirSync(path.dirname(destZip), { recursive: true }); | ||
const output = fs.createWriteStream(destZip, { flags: 'w+' }); | ||
const archive = archiver('zip', { | ||
zlib: { level: 9 } | ||
}); | ||
|
||
const pReturn = new Promise((resolve, reject) => { | ||
output.on('close', () => { | ||
console.log('[fns::close] archiver has been finalized and the output file descriptor has closed.'); | ||
}); | ||
|
||
output.on('end', () => { | ||
console.log('[fns::end] Data has been drained'); | ||
}); | ||
|
||
output.on('error', (err) => { | ||
console.error(`[fns::error] Error creating ZIP file: ${err.message}`); | ||
reject(err); | ||
}); | ||
|
||
archive.on('finish', async () => { | ||
resolve({ | ||
totalBytes: archive.pointer() | ||
}); | ||
}); | ||
|
||
archive.on('error', (err) => { | ||
console.error(`[fns::return] Error creating ZIP file: ${err.message}`); | ||
reject(err); | ||
}); | ||
}); | ||
|
||
const allItems = readdir(srcDir); | ||
/** | ||
* @type {{ | ||
* filePath: string; | ||
* zipPath: string; | ||
* time: Date; | ||
* }[]} | ||
*/ | ||
const asciiTable = []; | ||
|
||
for (const item of allItems) { | ||
const itemPath = path.join(srcDir, item); | ||
const itemZipPath = path.join('dist/', item); | ||
|
||
const stat = fs.statSync(itemPath); | ||
|
||
if (stat.isDirectory()) { | ||
await addDirectoryToZip(itemPath, itemZipPath); | ||
} else if (stat.isFile()) { | ||
const fileStream = fs.createReadStream(itemPath); | ||
asciiTable.push({ | ||
time: gitUTC0Time, | ||
zipPath: itemZipPath, | ||
// filePath: itemPath, | ||
}); | ||
// console.log(`\twill add ${chalk.green(itemZipPath)} \t\t ${chalk.yellow`(atime|mtime: ${gitUTC0Time})`}`); | ||
archive.append(fileStream, { name: itemZipPath, date: gitUTC0Time }); | ||
} | ||
} | ||
|
||
console.table(asciiTable); | ||
|
||
archive.pipe(output); | ||
|
||
await archive.finalize(); | ||
|
||
return pReturn; | ||
} | ||
|
||
const [, , srcDir, destZip, gitUTC0Time] = process.argv; | ||
|
||
console.log( | ||
`[fns] will pack ${srcDir} to ${destZip} with gitUTC0Time ${gitUTC0Time}` | ||
); | ||
|
||
function get_md5(buf) { | ||
return require('crypto').createHash('md5').update(buf, 'utf8').digest('hex'); | ||
} | ||
|
||
async function get_md5_file(filepath) { | ||
const stream = fs.createReadStream(path.resolve(__dirname, filepath)); | ||
const hash = require('crypto').createHash('md5'); | ||
stream.on('data', chunk => { | ||
hash.update(chunk, 'utf8'); | ||
}); | ||
|
||
return new Promise((resolve, reject) => { | ||
stream.on('end', () => { | ||
resolve(hash.digest('hex')); | ||
}); | ||
stream.on('error', reject); | ||
}); | ||
} | ||
|
||
createConsistentZip(srcDir, destZip, gitUTC0Time) | ||
.then(async (result) => { | ||
const md5Value = await get_md5_file(destZip); | ||
console.log(`[fns] ZIP file created at ${destZip} (md5: ${chalk.yellow(md5Value)}, size: ${chalk.yellow(result.totalBytes)} bytes)`); | ||
}); |
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 |
---|---|---|
@@ -1,41 +1,14 @@ | ||
#!/usr/bin/env bash | ||
script_dir="$( cd "$( dirname "$0" )" && pwd )" | ||
project_dir=$(dirname "$script_dir") | ||
systype=$(uname -s) | ||
|
||
pack_dist_to_zip() { | ||
local entry_dir=$project_dir; | ||
local files=$(find "$entry_dir/dist" -type f | sort) | ||
local dest_zip=$1 | ||
|
||
if [ -z $dest_zip ]; then | ||
echo "dest_zip is required" | ||
# read last_md5_value | ||
get_md5() { | ||
local TARGET_FILE=$1 | ||
if [ ! -f $TARGET_FILE ]; then | ||
echo "[get_md5] file not found: $TARGET_FILE" | ||
exit 1 | ||
fi | ||
|
||
if [ -z $build_type ]; then | ||
build_type="debug" | ||
local systype=$(uname -s) | ||
if [ $systype = "Darwin" ]; then | ||
export last_md5_value=$(md5 -q $TARGET_FILE); | ||
elif [ $systype = "Linux" ]; then | ||
export last_md5_value=$(md5sum $TARGET_FILE | awk '{ print $1 }'); | ||
fi | ||
|
||
git_committish=$(git log --format="%h" -n 1) | ||
# e.g. '2024-01-01 01:01:01 +0000' | ||
git_tz_time_linux=$(git log --format="%cd" -n 1 --date=format:'%Y-%m-%d %H:%M:%S %z') | ||
# SetFile format, e.g. '01/01/2024 01:01:01 +0000' | ||
git_tz_time_setfile=$(git log --format="%cd" -n 1 --date=format:'%m/%d/%Y %H:%M:%S %z') | ||
|
||
for file in $files; do | ||
if [ "$systype" = "Darwin" ]; then | ||
SetFile -d "$git_tz_time_setfile" "$file" | ||
SetFile -m "$git_tz_time_setfile" "$file" | ||
elif [ "$systype" = "Linux" ]; then | ||
touch -a -d "$git_tz_time_linux" "$file" | ||
touch -m -d "$git_tz_time_linux" "$file" | ||
fi | ||
|
||
local relname=${file#$entry_dir/} | ||
zip -X "$dest_zip" "$relname" | ||
done | ||
|
||
echo "git_tz_time_setfile is $git_tz_time_setfile" | ||
echo "git_tz_time_linux is $git_tz_time_linux" | ||
} |
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.