mirror of
https://github.com/eigent-ai/eigent.git
synced 2026-05-31 13:19:15 +00:00
chore: update the script
This commit is contained in:
parent
dcd5d6ab69
commit
6f8faaebe9
3 changed files with 59 additions and 175 deletions
|
|
@ -1,10 +1,8 @@
|
|||
{
|
||||
"*.{ts,tsx,d.ts}": [
|
||||
"node licenses/update_license_ts.js . licenses/license_template_ts.txt",
|
||||
"eslint --fix",
|
||||
"prettier --write"
|
||||
"node licenses/update_license.js"
|
||||
],
|
||||
"*.{js,jsx}": ["eslint --fix", "prettier --write"],
|
||||
"*.{json,css,md}": ["prettier --write"],
|
||||
"*.py": ["python licenses/update_license.py . licenses/license_template.txt"]
|
||||
"*.py": [
|
||||
"node licenses/update_license.js"
|
||||
]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -50,7 +50,8 @@ function updateLicenseInFile(
|
|||
filePath,
|
||||
licenseTemplate,
|
||||
startLineStartWith,
|
||||
endLineStartWith
|
||||
endLineStartWith,
|
||||
commentMarker
|
||||
) {
|
||||
const content = fs.readFileSync(filePath, 'utf-8');
|
||||
const newLicense = licenseTemplate.trim();
|
||||
|
|
@ -60,7 +61,7 @@ function updateLicenseInFile(
|
|||
const lines = content.split('\n');
|
||||
|
||||
for (const line of lines) {
|
||||
if (line.trim().startsWith('//')) {
|
||||
if (line.trim().startsWith(commentMarker)) {
|
||||
commentLines.push(line);
|
||||
} else if (line.trim() === '') {
|
||||
// Allow empty lines in the header
|
||||
|
|
@ -100,13 +101,14 @@ function updateLicenseInFile(
|
|||
}
|
||||
|
||||
/**
|
||||
* Recursively update licenses in all TypeScript files in a directory
|
||||
* Recursively update licenses in all files in a directory
|
||||
*/
|
||||
function updateLicenseInDirectory(
|
||||
directoryPath,
|
||||
licenseTemplate,
|
||||
startLineStartWith,
|
||||
endLineStartWith,
|
||||
commentMarker,
|
||||
extensions = ['.ts', '.tsx', '.d.ts']
|
||||
) {
|
||||
let fileCount = 0;
|
||||
|
|
@ -145,7 +147,8 @@ function updateLicenseInDirectory(
|
|||
fullPath,
|
||||
licenseTemplate,
|
||||
startLineStartWith,
|
||||
endLineStartWith
|
||||
endLineStartWith,
|
||||
commentMarker
|
||||
)
|
||||
) {
|
||||
fileCount++;
|
||||
|
|
@ -158,10 +161,8 @@ function updateLicenseInDirectory(
|
|||
processDirectory(directoryPath);
|
||||
console.log(`\nLicense check complete: ${fileCount} file(s) updated`);
|
||||
|
||||
// Exit with code 1 if any files were modified (for pre-commit hook to catch)
|
||||
if (fileCount > 0) {
|
||||
process.exit(1);
|
||||
}
|
||||
// Exit with code 0 on success (files were properly updated)
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -170,43 +171,60 @@ function updateLicenseInDirectory(
|
|||
function main() {
|
||||
const args = process.argv.slice(2);
|
||||
|
||||
if (args.length < 2) {
|
||||
console.error(
|
||||
'Usage: node update_license_ts.js <directory_path> <license_template_path>'
|
||||
);
|
||||
if (args.length < 1) {
|
||||
console.error('Usage: node update_license.js <file1> [file2] [file3] ...');
|
||||
console.error('\nProcesses individual files passed by lint-staged');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const [directoryPath, licenseTemplatePath] = args;
|
||||
// Process each file passed as argument (from lint-staged)
|
||||
let filesUpdated = 0;
|
||||
|
||||
// Check if directory exists
|
||||
if (
|
||||
!fs.existsSync(directoryPath) ||
|
||||
!fs.statSync(directoryPath).isDirectory()
|
||||
) {
|
||||
console.error(`Error: ${directoryPath} is not a valid directory`);
|
||||
process.exit(1);
|
||||
for (const filePath of args) {
|
||||
// Determine template and comment marker based on file extension
|
||||
const ext = path.extname(filePath);
|
||||
let licenseTemplatePath, commentMarker;
|
||||
|
||||
if (['.ts', '.tsx', '.d.ts', '.js', '.jsx'].includes(ext)) {
|
||||
licenseTemplatePath = path.join(__dirname, 'license_template_ts.txt');
|
||||
commentMarker = '//';
|
||||
} else if (ext === '.py') {
|
||||
licenseTemplatePath = path.join(__dirname, 'license_template_py.txt');
|
||||
commentMarker = '#';
|
||||
} else {
|
||||
console.log(`⊘ Skipping ${filePath} (unsupported extension)`);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check if license template exists
|
||||
if (!fs.existsSync(licenseTemplatePath)) {
|
||||
console.error(`Error: ${licenseTemplatePath} not found`);
|
||||
continue;
|
||||
}
|
||||
|
||||
const licenseTemplate = fs.readFileSync(licenseTemplatePath, 'utf-8');
|
||||
const startLineStartWith = `${commentMarker} ========= Copyright`;
|
||||
const endLineStartWith = `${commentMarker} ========= Copyright`;
|
||||
|
||||
if (
|
||||
updateLicenseInFile(
|
||||
filePath,
|
||||
licenseTemplate,
|
||||
startLineStartWith,
|
||||
endLineStartWith,
|
||||
commentMarker
|
||||
)
|
||||
) {
|
||||
filesUpdated++;
|
||||
}
|
||||
}
|
||||
|
||||
// Check if license template exists
|
||||
if (
|
||||
!fs.existsSync(licenseTemplatePath) ||
|
||||
!fs.statSync(licenseTemplatePath).isFile()
|
||||
) {
|
||||
console.error(`Error: ${licenseTemplatePath} not found`);
|
||||
process.exit(1);
|
||||
if (filesUpdated > 0) {
|
||||
console.log(`\n✔ License check complete: ${filesUpdated} file(s) updated`);
|
||||
}
|
||||
|
||||
const licenseTemplate = fs.readFileSync(licenseTemplatePath, 'utf-8');
|
||||
const startLineStartWith = '// ========= Copyright';
|
||||
const endLineStartWith = '// ========= Copyright';
|
||||
|
||||
updateLicenseInDirectory(
|
||||
directoryPath,
|
||||
licenseTemplate,
|
||||
startLineStartWith,
|
||||
endLineStartWith
|
||||
);
|
||||
// Exit with success code
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
main();
|
||||
|
|
@ -1,132 +0,0 @@
|
|||
# ========= Copyright 2025-2026 @ Eigent.ai. All Rights Reserved. =========
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ========= Copyright 2025-2026 @ Eigent.ai. All Rights Reserved. =========
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from typing import List
|
||||
|
||||
|
||||
# The license template file is hard-coded with specific start and end lines
|
||||
def fine_license_start_line(lines: List[str], start_with: str) -> int:
|
||||
for i in range(len(lines)):
|
||||
if lines[i].startswith(start_with):
|
||||
return i
|
||||
return None
|
||||
|
||||
|
||||
def find_license_end_line(lines: List[str], start_with: str) -> int:
|
||||
for i in range(len(lines) - 1, -1, -1):
|
||||
if lines[i].startswith(start_with):
|
||||
return i
|
||||
return None
|
||||
|
||||
|
||||
def update_license_in_file(
|
||||
file_path: str,
|
||||
license_template_path: str,
|
||||
start_line_start_with: str,
|
||||
end_line_start_with: str,
|
||||
) -> bool:
|
||||
with open(
|
||||
file_path, 'r', encoding='utf-8'
|
||||
) as f: # for windows compatibility
|
||||
content = f.read()
|
||||
|
||||
with open(license_template_path, 'r', encoding='utf-8') as f:
|
||||
new_license = f.read().strip()
|
||||
|
||||
maybe_existing_licenses = re.findall(
|
||||
r'^#.*?(?=\n)', content, re.MULTILINE | re.DOTALL
|
||||
)
|
||||
start_index = fine_license_start_line(
|
||||
maybe_existing_licenses, start_line_start_with
|
||||
)
|
||||
end_index = find_license_end_line(
|
||||
maybe_existing_licenses, end_line_start_with
|
||||
)
|
||||
if start_index is not None and end_index is not None:
|
||||
maybe_existing_licenses = maybe_existing_licenses[
|
||||
start_index : end_index + 1
|
||||
]
|
||||
else:
|
||||
maybe_existing_licenses = None
|
||||
if maybe_existing_licenses:
|
||||
maybe_old_licenses = '\n'.join(maybe_existing_licenses)
|
||||
if maybe_old_licenses.strip() != new_license.strip():
|
||||
replaced_content = content.replace(maybe_old_licenses, new_license)
|
||||
with open(file_path, 'w') as f:
|
||||
f.write(replaced_content)
|
||||
print(f'Replaced license in {file_path}')
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
else:
|
||||
with open(file_path, 'w') as f:
|
||||
f.write(new_license + '\n' + content)
|
||||
print(f'Added license to {file_path}')
|
||||
return True
|
||||
|
||||
|
||||
def update_license_in_directory(
|
||||
directory_path: str,
|
||||
license_template_path: str,
|
||||
start_line_start_with: str,
|
||||
end_line_start_with: str,
|
||||
) -> None:
|
||||
# Check if directory exists
|
||||
if not os.path.isdir(directory_path):
|
||||
raise NotADirectoryError(f'{directory_path} is not a directory')
|
||||
# Check if license template exists
|
||||
if not os.path.isfile(license_template_path):
|
||||
raise FileNotFoundError(f'{license_template_path} not found')
|
||||
|
||||
file_count = 0
|
||||
for py_files in Path(directory_path).rglob("*.py"):
|
||||
if py_files.name.startswith('.'):
|
||||
continue
|
||||
if any(part.startswith('.') for part in py_files.parts):
|
||||
continue
|
||||
if update_license_in_file(
|
||||
py_files,
|
||||
license_template_path,
|
||||
start_line_start_with,
|
||||
end_line_start_with,
|
||||
):
|
||||
file_count += 1
|
||||
|
||||
print(f'License updated in {file_count} files')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if len(sys.argv) < 3:
|
||||
print(
|
||||
"Usage from command line: "
|
||||
"python update_license.py <directory_path> <license_template_path>"
|
||||
"No valid input arguments found, please enter manually."
|
||||
)
|
||||
directory_path = input("Enter directory path: ")
|
||||
license_template_path = input("Enter license template path: ")
|
||||
else:
|
||||
directory_path = sys.argv[1]
|
||||
license_template_path = sys.argv[2]
|
||||
|
||||
start_line_start_with = "# ========= Copyright"
|
||||
end_line_start_with = "# ========= Copyright"
|
||||
update_license_in_directory(
|
||||
directory_path,
|
||||
license_template_path,
|
||||
start_line_start_with,
|
||||
end_line_start_with,
|
||||
)
|
||||
Loading…
Add table
Add a link
Reference in a new issue