Repository navigation
Expand file tree
/
Copy pathfetch-remote-docs.js
More file actions
executable file
·115 lines (106 loc) · 3.42 KB
/
Copy pathfetch-remote-docs.js
File metadata and controls
executable file
·115 lines (106 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env node
// fetch-remote-docs.js
// CLI script to fetch docs from a remote GitHub repo, print README instructions, and copy docs after user confirmation
const fs = require('fs')
const path = require('path')
const https = require('https')
const readline = require('readline')
const { cyan, yellow, green, red, bold } = require('colorette')
const REMOTE_REPO = 'edsadr/windsurf-task-manager-workflow'
const RAW_BASE = `https://raw.githubusercontent.com/${REMOTE_REPO}/main`
const DOCS_PATH = 'docs' // remote docs directory
const LOCAL_DEST = process.cwd()
// Helper to fetch a remote file as string
function fetchRemoteFile(url) {
return new Promise((resolve, reject) => {
https.get(url, res => {
if (res.statusCode !== 200) {
reject(new Error(`Failed to fetch ${url} (status ${res.statusCode})`))
return
}
let data = ''
res.on('data', chunk => { data += chunk })
res.on('end', () => resolve(data))
}).on('error', reject)
})
}
// Helper to fetch remote directory listing via GitHub API
function fetchDocsList() {
const apiUrl = `https://api.github.com/repos/${REMOTE_REPO}/contents/${DOCS_PATH}`
return new Promise((resolve, reject) => {
https.get(apiUrl, {
headers: { 'User-Agent': 'fetch-remote-docs-script' }
}, res => {
if (res.statusCode !== 200) {
reject(new Error(`Failed to fetch docs list (status ${res.statusCode})`))
return
}
let data = ''
res.on('data', chunk => { data += chunk })
res.on('end', () => {
try {
const files = JSON.parse(data)
resolve(files.filter(f => f.type === 'file'))
} catch (e) {
reject(e)
}
})
}).on('error', reject)
})
}
// Print README instructions
async function printReadme() {
const readmeUrl = `${RAW_BASE}/README.md`
try {
const readme = await fetchRemoteFile(readmeUrl)
console.log(bold(cyan('\n--- Remote README Instructions ---\n')))
console.log(yellow(readme))
console.log(bold(cyan('\n--- End of Instructions ---\n')))
} catch (err) {
console.log(red('Could not fetch remote README.md:'), err.message)
}
}
// Ask user for confirmation
function askConfirmation() {
return new Promise(resolve => {
const rl = readline.createInterface({ input: process.stdin, output: process.stdout })
rl.question(bold(green('Continue copying docs to this folder? [y/N]: ')), answer => {
rl.close()
resolve(answer.trim().toLowerCase() === 'y')
})
})
}
// Download and write a file
async function downloadDoc(file) {
const url = file.download_url
const dest = path.join(LOCAL_DEST, file.name)
try {
const data = await fetchRemoteFile(url)
fs.writeFileSync(dest, data)
console.log(green(`Copied: ${file.name}`))
} catch (err) {
console.log(red(`Failed to copy ${file.name}: ${err.message}`))
}
}
// Main logic
(async () => {
await printReadme()
const confirmed = await askConfirmation()
if (!confirmed) {
console.log(red('Aborted by user.'))
process.exit(1)
}
console.log(cyan('\nFetching docs from remote repo...'))
let docs
try {
docs = await fetchDocsList()
if (!docs.length) throw new Error('No docs found in remote repo.')
} catch (err) {
console.log(red('Error fetching docs list:'), err.message)
process.exit(1)
}
for (const file of docs) {
await downloadDoc(file)
}
console.log(bold(green('\nAll docs copied successfully!')))
})()