const fs = require('fs'); const path = require('path'); const os = require('os'); const { getApiResult } = require('../utils/requestUtils'); const url = { list: 'https://cdn-static-devbit.csdn.net/devbit-static/plugin/linux-command/dist/data.min.json', content: (command) => `https://cdn-static-devbit.csdn.net/devbit-static/plugin/linux-command/command/${command}.md` } async function get_list() { let saveFilePath = './list.json' if (fs.existsSync(saveFilePath) && fs.statSync(saveFilePath).isFile()) { console.log('File exists') let list = fs.readFileSync(saveFilePath, 'utf8') let list_data = JSON.parse(list) return list_data } else { console.log('File not exists') let list = await getApiResult(url.list) let list_data = JSON.parse(list) fs.writeFileSync(saveFilePath, JSON.stringify(list_data, null, 4)) return list_data } } async function get_content_by_command(command) { // console.log(element) let saveFilePath = `./commands/${command}.md` if (fs.existsSync(saveFilePath) && fs.statSync(saveFilePath).isFile()) { console.log(`[${command}] File exists, skip ...`) } else { console.log(`[${command}] File not exists, get content ...`) let content = await getApiResult(url.content(command)) fs.writeFileSync(saveFilePath, content) } } async function main() { let list = await get_list() let commands = Object.keys(list) // console.log(commands.join(', ')) let folderPath = './commands' if (!fs.existsSync(folderPath) || !fs.statSync(folderPath).isDirectory()) { fs.mkdirSync(folderPath, { recursive: true }) } for (let i = 0; i < commands.length; i++) { const element = commands[i] get_content_by_command(element) } } main()