feat: adds the esm template option + refactors (#73)

* feat: inline gimmeChoices returned value

* feat: add esm option

* feat: add config support for javascript-esm

* feat: add fromCwd.js util

* feat: transform repetitive paths to fromCwd

* feat: remove conditional branch in favor of transform

* refactor: extract condition into function

* style: prettier
This commit is contained in:
Jacob Nguyen
2022-08-13 12:45:44 -05:00
committed by GitHub
parent 2009d1fdca
commit 9271da3076
6 changed files with 36 additions and 19 deletions

View File

@@ -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;

View File

@@ -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);
}

View File

@@ -10,6 +10,11 @@ export const lang = {
description: 'JS',
value: 'javascript',
},
{
title: 'JavaScript (ESM)',
description: 'JS',
value: 'javascript-esm',
},
{
title: 'TypeScript',
description: 'TS - (Recommended)',

View File

@@ -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() {

View File

@@ -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');

8
src/utilities/fromCwd.js Normal file
View File

@@ -0,0 +1,8 @@
import path from 'path';
/**
* @param {string[]} dir
*/
export function fromCwd(...dir) {
return path.join(...[process.cwd(), ...dir]);
}