diff --git a/src/commands/init.js b/src/commands/init.js index a0840ba..821c653 100644 --- a/src/commands/init.js +++ b/src/commands/init.js @@ -23,7 +23,6 @@ const { prompt } = prompts; * @param {{ y: string; sync: string; }} flags */ export async function init(flags) { - let data; let git_init; let pm; @@ -45,15 +44,14 @@ export async function init(flags) { git_init = (await prompt([gitInit])).gitinit; pm = await npm(); } - + const language = data.lang === 'javascript-esm' ? 'javascript' : data.lang; const config = { - language: data.lang, + language, paths: { base: data.main_dir, commands: data.cmds_dir, }, }; - const file = JSON.stringify(config, null, 2); const requiredData = flags.sync !== undefined ? 3 : 4; diff --git a/src/commands/plugins.js b/src/commands/plugins.js index b8abd6a..f7c2741 100644 --- a/src/commands/plugins.js +++ b/src/commands/plugins.js @@ -3,6 +3,7 @@ import prompts from 'prompts'; import { fetch } from 'undici'; import fs from 'fs'; import { greenBright } from 'colorette'; +import { fromCwd } from '../utilities/fromCwd.js'; const { prompt } = prompts; /** @@ -37,12 +38,11 @@ async function download(url) { if (!data) throw new Error('Download failed! Kindly contact developers'); - const dir = `${process.cwd()}/src/plugins`; + const dir = `${fromCwd('/src/plugins')}`; const filedir = `${process.cwd()}/src/plugins/${url.split('/').pop()}`; if (!fs.existsSync(dir)) { fs.mkdirSync(dir, { recursive: true }); } - const file = fs.writeFileSync(filedir, data); - return file; + fs.writeFileSync(filedir, data); } diff --git a/src/prompts/init.js b/src/prompts/init.js index f9296f9..e8a06f6 100644 --- a/src/prompts/init.js +++ b/src/prompts/init.js @@ -10,6 +10,11 @@ export const lang = { description: 'JS', value: 'javascript', }, + { + title: 'JavaScript (ESM)', + description: 'JS', + value: 'javascript-esm', + }, { title: 'TypeScript', description: 'TS - (Recommended)', diff --git a/src/prompts/plugin.js b/src/prompts/plugin.js index 5ec6945..43fb7f8 100644 --- a/src/prompts/plugin.js +++ b/src/prompts/plugin.js @@ -1,24 +1,29 @@ import { fetch } from 'undici'; import { getLang } from '../utilities/getLang.js'; + +function upperCase(string) { + if (string === null) { + console.error('Lang property not found!'); + process.exit(0); + } + return string === 'typescript' ? 'TypeScript' : 'JavaScript'; +} + async function gimmechoices() { - const lang = - (await getLang().catch(() => null)) === 'typescript' - ? 'TypeScript' - : 'JavaScript'; + const lang = upperCase(await getLang().catch(() => null)); + const link = `https://api.github.com/repos/sern-handler/awesome-plugins/contents/${lang}`; const resp = await fetch(link).catch(() => null); - if (!resp) return { title: 'No plugins found!', value: '', disabled: true }; const data = await resp.json(); - const choices = data.map( + return data.map( (/** @type {{ name: string; download_url: string; }} */ e) => ({ title: e.name, value: e.download_url, }) ); - return choices; } export async function pluginsQ() { diff --git a/src/utilities/edits.js b/src/utilities/edits.js index fe6b80d..cf1efc3 100644 --- a/src/utilities/edits.js +++ b/src/utilities/edits.js @@ -1,5 +1,6 @@ import { readFile, rename, writeFile } from 'node:fs/promises'; import { findUp } from 'find-up'; +import { fromCwd } from './fromCwd.js'; /** * It takes a string, finds the package.json file in the directory of the string, and changes the name @@ -9,7 +10,7 @@ import { findUp } from 'find-up'; */ export async function editMain(name) { const pjLocation = await findUp('package.json', { - cwd: process.cwd() + '/' + name, + cwd: fromCwd('/' + name), }); const output = JSON.parse(await readFile(pjLocation, 'utf8')); @@ -36,7 +37,7 @@ export async function editDirs( lang = 'typescript' ) { const path = await findUp('src', { - cwd: process.cwd() + '/' + name, + cwd: fromCwd(name), type: 'directory', }); @@ -46,12 +47,12 @@ export async function editDirs( await rename(path, newMainDir); const cmdsPath = await findUp('commands', { - cwd: process.cwd() + '/' + name + '/' + srcName, + cwd: fromCwd(name, srcName), type: 'directory', }); const index = await findUp(`index.${ext}`, { - cwd: process.cwd() + '/' + name + '/' + srcName, + cwd: fromCwd(name, srcName), }); const newCmdsPath = cmdsPath?.replace('commands', cmds_dirName); @@ -66,7 +67,7 @@ export async function editDirs( if (!output) throw new Error("Can't read your tsconfig.json."); output.compilerOptions.rootDir = srcName; - writeFile(tsconfig, JSON.stringify(output, null, 2)); + await writeFile(tsconfig, JSON.stringify(output, null, 2)); } const output = await readFile(index, 'utf8'); diff --git a/src/utilities/fromCwd.js b/src/utilities/fromCwd.js new file mode 100644 index 0000000..0669e0e --- /dev/null +++ b/src/utilities/fromCwd.js @@ -0,0 +1,8 @@ +import path from 'path'; + +/** + * @param {string[]} dir + */ +export function fromCwd(...dir) { + return path.join(...[process.cwd(), ...dir]); +}