-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwindsurf-task-manager-workflow.js
More file actions
executable file
·169 lines (158 loc) · 6.01 KB
/
Copy pathwindsurf-task-manager-workflow.js
File metadata and controls
executable file
·169 lines (158 loc) · 6.01 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env node
// windsurf-task-manager-workflow.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 marked = require('marked')
// marked-terminal v7+ is ESM-only. Use require and .default for CJS compatibility.
const TerminalRenderer = require('marked-terminal').default || require('marked-terminal')
const REMOTE_REPO = 'edsadr/windsurf-task-manager-workflow'
// Use the correct branch ref for raw URLs
const RAW_BASE = `https://raw.githubusercontent.com/${REMOTE_REPO}/refs/heads/master`
const REMOTE_WORKFLOWS_PATH = 'workflows' // remote workflows directory
const LOCAL_WORKFLOWS_DIR = '.windsurf/workflows' // local workflows 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/${REMOTE_WORKFLOWS_PATH}?ref=master`
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() {
// Use the correct URL for the README
const readmeUrl = `${RAW_BASE}/README.md`
try {
const readme = await fetchRemoteFile(readmeUrl)
// Render markdown to terminal-friendly output using marked-terminal
marked.setOptions({
renderer: new TerminalRenderer(),
mangle: false,
headerIds: false
})
const rendered = marked.parse(readme)
console.log(rendered)
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 localDir = LOCAL_WORKFLOWS_DIR
// Ensure local workflows directory exists
if (!fs.existsSync(localDir)) {
fs.mkdirSync(localDir, { recursive: true })
}
// Use the correct raw URL for workflows
const url = `${RAW_BASE}/${REMOTE_WORKFLOWS_PATH}/${file.name}`
const dest = path.join(localDir, file.name)
try {
const data = await fetchRemoteFile(url)
fs.writeFileSync(dest, data)
console.log(green(`Copied: ${LOCAL_WORKFLOWS_DIR}/${file.name}`))
} catch (err) {
console.log(red(`Failed to copy ${file.name}: ${err.message}`))
}
}
// Extract the workflow instructions section from README.md and write to .windsurf/workflows/instructions.md in the current working directory
async function copyInstructionsFromReadme () {
// Read README.md from the script's directory
const readmePath = path.join(__dirname, 'README.md')
// Write instructions.md to the docs/ subdirectory of the current working directory
const docsDir = path.join(process.cwd(), 'docs')
const instructionsPath = path.join(docsDir, 'instructions.md')
try {
const content = await fs.promises.readFile(readmePath, 'utf8')
const lines = content.split(/\r?\n/)
// Find the start of the workflow section
const startIdx = lines.findIndex(line => line.trim().startsWith('## Windsurf Task Manager Workflow'))
if (startIdx === -1) throw new Error('Workflow section not found in README.md')
// Find the end of the workflow section: next major header or EOF
let endIdx = lines.findIndex((line, i) => i > startIdx && line.startsWith('## '))
if (endIdx === -1) {
endIdx = lines.length
}
const instructions = lines.slice(startIdx, endIdx).join('\n').trim()
if (!instructions) {
throw new Error('No instructions found in the workflow section of README.md')
}
// Ensure docs directory exists in cwd
if (!fs.existsSync(docsDir)) {
fs.mkdirSync(docsDir, { recursive: true })
}
await fs.promises.writeFile(instructionsPath, instructions, 'utf8')
console.log(green(`Instructions copied to docs/instructions.md in the current directory`))
} catch (err) {
console.log(red('Failed to extract/write instructions from README.md:'), err.message)
}
}
// Main logic
(async () => {
await printReadme()
const confirmed = await askConfirmation()
if (!confirmed) {
console.log(red('Aborted by user.'))
process.exit(1)
}
// Copy instructions from local README.md to workflows/instructions.md
await copyInstructionsFromReadme()
console.log(cyan('\nFetching workflows 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 workflows copied successfully!')))
})()