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