mirror of
https://github.com/PR0M3TH3AN/TORCH.git
synced 2026-10-27 07:51:52 +00:00
Merge pull request #359 from PR0M3TH3AN/fix/torch-init-gitignore-13639849350884611583
Ensure torch init adds .gitignore with node_modules
This commit is contained in:
2 files changed
+89
No files matched your search
+24
@@ -304,6 +304,29 @@ function configureTorch(cwd, paths, installDir, namespace, relays, hashtag) {
|
||||
console.log(`Saved configuration to ${path.relative(cwd, configPath)}`);
|
||||
}
|
||||
|
||||
function ensureGitIgnore(targetDir) {
|
||||
const gitIgnorePath = path.join(targetDir, '.gitignore');
|
||||
let content = '';
|
||||
const exists = fs.existsSync(gitIgnorePath);
|
||||
|
||||
if (exists) {
|
||||
content = fs.readFileSync(gitIgnorePath, 'utf8');
|
||||
}
|
||||
|
||||
const lines = content.split('\n').map((l) => l.trim());
|
||||
const hasNodeModules = lines.some((l) => l === 'node_modules' || l === '/node_modules' || l === 'node_modules/');
|
||||
|
||||
if (!hasNodeModules) {
|
||||
const prefix = exists && content.length > 0 && !content.endsWith('\n') ? '\n' : '';
|
||||
fs.appendFileSync(gitIgnorePath, `${prefix}node_modules\n`, 'utf8');
|
||||
if (exists) {
|
||||
console.log(`Updated ${path.relative(process.cwd(), gitIgnorePath)}: added node_modules`);
|
||||
} else {
|
||||
console.log(`Created ${path.relative(process.cwd(), gitIgnorePath)} with node_modules`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function injectHostScriptsIfNeeded(paths, installDir) {
|
||||
// 7. Inject Scripts into Host Package.json
|
||||
// If we are NOT installing to '.', the host package.json is in paths.root
|
||||
@@ -327,6 +350,7 @@ export async function cmdInit(force = false, cwd = process.cwd(), mockAnswers =
|
||||
|
||||
installAppAssets(paths.torchDir, installDir);
|
||||
installTorchAssets(paths, installDir);
|
||||
ensureGitIgnore(paths.torchDir);
|
||||
configureTorch(cwd, paths, installDir, namespace, relays, hashtag);
|
||||
const dashboardUrl = createDashboardLinkFile(paths, namespace, relays, hashtag);
|
||||
injectHostScriptsIfNeeded(paths, installDir);
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert';
|
||||
import { cmdInit } from '../src/ops.mjs';
|
||||
|
||||
test('ops.mjs: cmdInit creates .gitignore with node_modules', async (t) => {
|
||||
const tmpDir = fs.mkdtempSync(path.join(process.cwd(), 'test-gitignore-'));
|
||||
|
||||
// Clean up afterwards
|
||||
t.after(() => {
|
||||
fs.rmSync(tmpDir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
const mockAnswers = {
|
||||
installDir: 'torch',
|
||||
namespace: 'test-ns',
|
||||
hashtag: 'test-hash',
|
||||
relays: ['wss://relay.damus.io'],
|
||||
};
|
||||
|
||||
// Run init
|
||||
// We silence console logs to keep test output clean, if possible, but it's fine.
|
||||
await cmdInit(false, tmpDir, mockAnswers);
|
||||
|
||||
const gitIgnorePath = path.join(tmpDir, 'torch', '.gitignore');
|
||||
|
||||
if (fs.existsSync(gitIgnorePath)) {
|
||||
const content = fs.readFileSync(gitIgnorePath, 'utf8');
|
||||
assert.match(content, /node_modules/, '.gitignore should contain node_modules');
|
||||
} else {
|
||||
assert.fail('.gitignore was not created');
|
||||
}
|
||||
});
|
||||
|
||||
test('ops.mjs: cmdInit appends to existing .gitignore', async (t) => {
|
||||
const tmpDir = fs.mkdtempSync(path.join(process.cwd(), 'test-gitignore-existing-'));
|
||||
|
||||
// Clean up afterwards
|
||||
t.after(() => {
|
||||
fs.rmSync(tmpDir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
const torchDir = path.join(tmpDir, 'torch');
|
||||
fs.mkdirSync(torchDir, { recursive: true });
|
||||
|
||||
// Create existing .gitignore without node_modules
|
||||
fs.writeFileSync(path.join(torchDir, '.gitignore'), 'existing-pattern\n', 'utf8');
|
||||
|
||||
const mockAnswers = {
|
||||
installDir: 'torch',
|
||||
namespace: 'test-ns',
|
||||
hashtag: 'test-hash',
|
||||
relays: ['wss://relay.damus.io'],
|
||||
};
|
||||
|
||||
// Run init (force=true because directory exists)
|
||||
await cmdInit(true, tmpDir, mockAnswers);
|
||||
|
||||
const gitIgnorePath = path.join(torchDir, '.gitignore');
|
||||
const content = fs.readFileSync(gitIgnorePath, 'utf8');
|
||||
|
||||
assert.match(content, /existing-pattern/, 'Should preserve existing content');
|
||||
assert.match(content, /node_modules/, 'Should append node_modules');
|
||||
});
|
||||
Reference in new issue
Block a user