gptsync
Version:
Seamlessly sync the latest AI output in a flash.
93 lines (92 loc) • 4.13 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const unzipper_1 = __importDefault(require("unzipper"));
const os_1 = __importDefault(require("os"));
const commander_1 = require("commander");
const child_process_1 = require("child_process"); // For executing git commands
const program = new commander_1.Command();
// Get version from package.json
// @ts-ignore
const package_json_1 = require("../package.json");
// Get the path to the Downloads directory
const downloadsDir = path_1.default.join(os_1.default.homedir(), 'Downloads');
// Function to check if the git history is clean
function isGitHistoryClean() {
try {
// Check if the current directory is a git repository
(0, child_process_1.execSync)('git rev-parse --is-inside-work-tree', { stdio: 'ignore' });
// If it's a git repository, check the status
const status = (0, child_process_1.execSync)('git status --porcelain').toString();
return status.length === 0; // If empty, no changes or untracked files
}
catch (error) {
// If any error occurs, it means it's not a Git repository
console.error('Error: Not a git repository or unable to check status.');
return false; // Assume not clean if error occurs
}
}
// Function to find the most recent .zip file in the Downloads directory
function findMostRecentZip() {
const files = fs_1.default.readdirSync(downloadsDir);
const zipFiles = files.filter(file => file.endsWith('.zip'));
if (zipFiles.length === 0) {
console.log('No .zip files found in Downloads.');
return null;
}
// Get the most recent zip file based on the creation time
const mostRecentFile = zipFiles
.map(file => ({
file,
time: fs_1.default.statSync(path_1.default.join(downloadsDir, file)).mtime
}))
.sort((a, b) => b.time.getTime() - a.time.getTime())[0]; // Sort in descending order by modification time
// Print the name of the zip file found
console.log(`Most recent zip file: ${mostRecentFile.file}`);
return mostRecentFile.file;
}
// Function to extract the most recent .zip file
function extractZip(zipFile) {
const zipFilePath = path_1.default.join(downloadsDir, zipFile);
const extractPath = path_1.default.resolve(process.cwd()); // Extract to the current working directory
fs_1.default.createReadStream(zipFilePath)
.pipe(unzipper_1.default.Extract({ path: extractPath }))
.on('entry', (entry) => {
console.log(`Updating ${entry.path}`); // Print the name of each file being extracted
})
.on('error', (err) => {
console.error('Extraction failed:', err);
});
}
// Command handler for unzip
program
.name('aisync')
.description('Seamlessly sync the latest AI output in a flash.')
.version(package_json_1.version) // Set version from package.json
.helpOption('-h, --help', 'Show help') // Ensure help works
.option('-f, --force', 'Force operation even if git history is not clean.')
.command('unzip')
.description('Finds the most recent .zip file from Downloads and extracts it in the current directory.')
.option('-f, --force', 'Force operation even if git history is not clean.')
.action((options) => {
if (!options.force && !isGitHistoryClean()) {
console.error('Error: Git history is not clean. Use --force only if you are sure.');
process.exit(1); // Exit with error status
}
const recentZip = findMostRecentZip();
if (recentZip) {
extractZip(recentZip);
}
});
// Default behavior: if no command is provided, invoke 'unzip'
program.action(() => {
var _a;
(_a = program.commands.find(c => c.name() === 'unzip')) === null || _a === void 0 ? void 0 : _a.parse(process.argv);
});
// Parse the command line arguments
program.parse(process.argv);