Created
January 31, 2024 11:47
-
-
Save suhaotian/f849d2e5c54449f3c08118b51e071a33 to your computer and use it in GitHub Desktop.
git lfs auto track
This file contains hidden or 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
import fs from 'fs'; | |
import path from 'path'; | |
import { execSync } from 'child_process'; | |
const cwd = process.cwd(); | |
console.log(process.argv, cwd); | |
const files = process.argv.slice(3); | |
const conditions = process.argv[2]; | |
const cs = conditions.split(';').map((item) => { | |
const [ext, size] = item.split(':'); | |
return { exts: ext.split(','), size: +size.replace(/kb/gi, '') }; | |
}); | |
async function run() { | |
let hasTrack = false; | |
if (files && files.length > 0) { | |
for (const filepath of files) { | |
const ext = filepath.toLowerCase().split('.').pop(); | |
const extInfo = cs.find((item) => | |
item.exts.find((_ext) => _ext === ext || '.' + ext === _ext || _ext === '*') | |
); | |
if (extInfo) { | |
const fileinfo = fs.statSync(filepath); | |
const filesize = fileinfo.size / 1024; | |
if (filesize >= extInfo.size) { | |
const result = execSync(`git lfs track ${path.relative(cwd, filepath)}`).toString(); | |
if (result.indexOf('already supported') <= -1) { | |
hasTrack = true; | |
} | |
} | |
} | |
} | |
} | |
if (hasTrack) { | |
execSync(`git add .gitattributes`).toString(); | |
} | |
} | |
run(); |
This file contains hidden or 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
{ | |
"name": "git-lfs-auto-track-demo", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1", | |
"prepare": "is-ci || husky" | |
}, | |
"dependencies": { | |
"husky": "^9.0.7", | |
"lint-staged": "^15.2.0", | |
"prettier": "^3.2.4", | |
"is-ci": "^3.0.1" | |
}, | |
"prettier": { | |
"printWidth": 100, | |
"tabWidth": 2, | |
"singleQuote": true, | |
"trailingComma": "es5", | |
"bracketSameLine": true | |
}, | |
"lint-staged": { | |
"*": [ | |
"node lfs-auto-track.mjs 'jpg,jpeg,png,gif:100kb;*:1024kb'" | |
], | |
"*.{ts,tsx,mts}": [ | |
"prettier --write", | |
"eslint --fix" | |
], | |
"*.{md,css,js,mjs}": [ | |
"prettier --write" | |
] | |
}, | |
"keywords": [], | |
"author": "", | |
"license": "ISC" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment