mirror of
https://gitee.com/nocobase/nocobase.git
synced 2024-12-01 19:58:15 +08:00
refactor: imporve add License (#4326)
This commit is contained in:
parent
145577942f
commit
3613d004b0
@ -56,7 +56,7 @@
|
||||
},
|
||||
"config": {
|
||||
"ghooks": {
|
||||
"pre-commit": "yarn lint-staged && node addLicense.js",
|
||||
"pre-commit": "yarn lint-staged && node ./scripts/addLicense.js",
|
||||
"commit-msg": "commitlint --edit"
|
||||
}
|
||||
},
|
||||
|
@ -1,5 +1,5 @@
|
||||
const fs = require('fs/promises');
|
||||
const { exec } = require('child_process');
|
||||
const fs = require('fs');
|
||||
|
||||
const commercialLicense = `
|
||||
/**
|
||||
@ -26,10 +26,10 @@ function getLicenseText(packageDir) {
|
||||
return packageDir.includes('/pro-plugins') ? commercialLicense : openSourceLicense;
|
||||
}
|
||||
|
||||
function addLicenseToFile(filePath) {
|
||||
async function addLicenseToFile(filePath) {
|
||||
const licenseText = getLicenseText(filePath);
|
||||
|
||||
const data = fs.readFileSync(filePath, 'utf8');
|
||||
const data = await fs.readFile(filePath, 'utf8');
|
||||
|
||||
if (data.startsWith(licenseText)) return false;
|
||||
|
||||
@ -37,44 +37,75 @@ function addLicenseToFile(filePath) {
|
||||
const newData = licenseText + '\n\n' + data;
|
||||
|
||||
// 将修改后的内容写回文件
|
||||
fs.writeFileSync(filePath, newData, 'utf8');
|
||||
await fs.writeFile(filePath, newData, 'utf8');
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// 执行 git diff 命令
|
||||
exec('git diff --cached --name-only --diff-filter=ACM', (error, stdout, stderr) => {
|
||||
function isMergeCommit() {
|
||||
return new Promise((resolve, reject) => {
|
||||
exec('git rev-parse --verify MERGE_HEAD', (error, stdout, stderr) => {
|
||||
if (error || stderr) {
|
||||
resolve(false);
|
||||
} else {
|
||||
resolve(true);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function getDiffFiles() {
|
||||
return new Promise((resolve, reject) => {
|
||||
exec('git diff --cached --name-only --diff-filter=ACM', (error, stdout, stderr) => {
|
||||
if (error) {
|
||||
console.error(`[nocobase]: git diff error ${error.message}`);
|
||||
process.exit(-1);
|
||||
reject(error);
|
||||
}
|
||||
if (stderr) {
|
||||
console.error(`[nocobase]: git diff error ${stderr}`);
|
||||
process.exit(-1);
|
||||
reject(stderr);
|
||||
}
|
||||
resolve(stdout.split('\n').filter(Boolean));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// 获取命令执行结果(文件列表)
|
||||
const files = stdout
|
||||
.split('\n')
|
||||
.filter(Boolean)
|
||||
function gitAddFiles(files) {
|
||||
return new Promise((resolve, reject) => {
|
||||
exec(`git add ${files.join(' ')}`, (error, stdout, stderr) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
}
|
||||
if (stderr) {
|
||||
reject(stderr);
|
||||
}
|
||||
resolve(stdout);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const isMerge = await isMergeCommit();
|
||||
if (isMerge) return;
|
||||
|
||||
const diffFiles = await getDiffFiles();
|
||||
const files = diffFiles
|
||||
.filter((file) => file.includes('/src/')) // 只检查 src 目录下的文件
|
||||
.filter((file) => !file.includes('/demos/')) // 忽略 demos 目录
|
||||
.filter((file) => file.endsWith('.js') || file.endsWith('.jsx') || file.endsWith('.ts') || file.endsWith('.tsx'));
|
||||
|
||||
const validFiles = files.filter((file) => addLicenseToFile(file));
|
||||
const validFiles = [];
|
||||
for (const file of files) {
|
||||
const res = await addLicenseToFile(file);
|
||||
if (res) {
|
||||
validFiles.push(file);
|
||||
}
|
||||
}
|
||||
|
||||
if (validFiles.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 执行 git add 这些文件
|
||||
exec(`git add ${validFiles.join(' ')}`, (error, stdout, stderr) => {
|
||||
if (error) {
|
||||
console.error(`[nocobase]: git add error ${error.message}`);
|
||||
process.exit(-1);
|
||||
}
|
||||
if (stderr) {
|
||||
console.error(`[nocobase]: git add error ${stderr}`);
|
||||
process.exit(-1);
|
||||
}
|
||||
});
|
||||
});
|
||||
await gitAddFiles(validFiles);
|
||||
}
|
||||
|
||||
main();
|
@ -49,7 +49,7 @@ function addLicenseToFile(filePath, licenseText) {
|
||||
async function addToPackageSource(packageDir) {
|
||||
const stream = fg.globStream('**/*.{js,jsx,ts,tsx,d.ts}', {
|
||||
cwd: path.join(packageDir, 'src'),
|
||||
ignore: ['node_modules'],
|
||||
ignore: ['**/demos'],
|
||||
absolute: true,
|
||||
onlyFiles: true,
|
||||
});
|
||||
@ -72,10 +72,10 @@ function getPackages() {
|
||||
.map((item) => path.join(path.dirname(item)));
|
||||
}
|
||||
|
||||
function run() {
|
||||
async function run() {
|
||||
const packages = getPackages();
|
||||
for (const package of packages) {
|
||||
addToPackageSource(package);
|
||||
await addToPackageSource(package);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user