diff --git a/package-lock.json b/package-lock.json index 7f45791..54b1e14 100644 --- a/package-lock.json +++ b/package-lock.json @@ -21,6 +21,7 @@ "sern": "src/index.js" }, "devDependencies": { + "@types/prompts": "^2.0.14", "cz-conventional-changelog": "3.0.1", "eslint": "8.18.0", "prettier": "2.7.1", @@ -420,8 +421,7 @@ "version": "17.0.41", "resolved": "https://registry.npmjs.org/@types/node/-/node-17.0.41.tgz", "integrity": "sha512-xA6drNNeqb5YyV5fO3OAEsnXLfO7uF0whiOfPTz5AeDo8KeZFmODKnvwPymMNO8qE/an8pVY/O50tig2SQCrGw==", - "dev": true, - "optional": true + "dev": true }, "node_modules/@types/normalize-package-data": { "version": "2.4.1", @@ -436,6 +436,15 @@ "dev": true, "optional": true }, + "node_modules/@types/prompts": { + "version": "2.0.14", + "resolved": "https://registry.npmjs.org/@types/prompts/-/prompts-2.0.14.tgz", + "integrity": "sha512-HZBd99fKxRWpYCErtm2/yxUZv6/PBI9J7N4TNFffl5JbrYMHBwF25DjQGTW3b3jmXq+9P6/8fCIb2ee57BFfYA==", + "dev": true, + "dependencies": { + "@types/node": "*" + } + }, "node_modules/acorn": { "version": "8.7.1", "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.7.1.tgz", @@ -4470,8 +4479,7 @@ "version": "17.0.41", "resolved": "https://registry.npmjs.org/@types/node/-/node-17.0.41.tgz", "integrity": "sha512-xA6drNNeqb5YyV5fO3OAEsnXLfO7uF0whiOfPTz5AeDo8KeZFmODKnvwPymMNO8qE/an8pVY/O50tig2SQCrGw==", - "dev": true, - "optional": true + "dev": true }, "@types/normalize-package-data": { "version": "2.4.1", @@ -4486,6 +4494,15 @@ "dev": true, "optional": true }, + "@types/prompts": { + "version": "2.0.14", + "resolved": "https://registry.npmjs.org/@types/prompts/-/prompts-2.0.14.tgz", + "integrity": "sha512-HZBd99fKxRWpYCErtm2/yxUZv6/PBI9J7N4TNFffl5JbrYMHBwF25DjQGTW3b3jmXq+9P6/8fCIb2ee57BFfYA==", + "dev": true, + "requires": { + "@types/node": "*" + } + }, "acorn": { "version": "8.7.1", "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.7.1.tgz", diff --git a/package.json b/package.json index 0e0a69c..f8effb9 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,7 @@ "prompts": "2.4.2" }, "devDependencies": { + "@types/prompts": "^2.0.14", "cz-conventional-changelog": "3.0.1", "eslint": "8.18.0", "prettier": "2.7.1", diff --git a/src/commands/init.js b/src/commands/init.js index 38eadb3..e62413e 100644 --- a/src/commands/init.js +++ b/src/commands/init.js @@ -19,7 +19,7 @@ import { editDirs, editMain } from '../utilities/edits.js'; import { writeFile } from 'fs/promises'; const { prompt } = prompts; -export async function init(flags, options) { +export async function init(flags) { // * Check if node version is valid const node = await execa('node', ['--version']); if (/v1(([0-6]\.[2-9])|([0-5]\.[0-9]))/gm.test(node.stdout)) { @@ -77,7 +77,7 @@ export async function init(flags, options) { git_init ? await git(data) : console.log(`Skipping git init...\n`); - let choice = ''; + let choice; if (pm === 'both') { const chosen = await prompt([which_manager]); diff --git a/src/commands/plugins.js b/src/commands/plugins.js index f63de5d..38716b5 100644 --- a/src/commands/plugins.js +++ b/src/commands/plugins.js @@ -4,11 +4,11 @@ import axios from 'axios'; import fs from 'fs'; import { greenBright } from 'colorette'; const { prompt } = prompts; + /** * Installs plugins to project - * @param flags */ -export async function plugins(flags) { +export async function plugins() { /** * @type {string[]} */ @@ -26,8 +26,12 @@ export async function plugins(flags) { ); } +/** + * @param {string} url + * @returns File + */ async function download(url) { - const res = await axios.get(url); + const res = await axios.default.get(url); const data = res.data; const dir = `${process.cwd()}/src/plugins`; const filedir = `${process.cwd()}/src/plugins/${url.split('/').pop()}`; diff --git a/src/prompts/init.js b/src/prompts/init.js index 0e6cab3..f9296f9 100644 --- a/src/prompts/init.js +++ b/src/prompts/init.js @@ -30,7 +30,7 @@ export const cmds_dir = { name: 'cmds_dir', type: 'text', initial: 'commands', - validate: (dir) => + validate: (/** @type {string} */ dir) => dir === 'src' ? 'You can not use src as a directory' : true, }; @@ -89,7 +89,7 @@ export const name = { message: 'What is your project name?', name: 'name', type: 'text', - validate: (name) => + validate: (/**@type {string}*/ name) => name.match('^(?:@[a-z0-9-*~][a-z0-9-*._~]*/)?[a-z0-9-~][a-z0-9-._~]*$') ? true : 'Invalid name', diff --git a/src/utilities/create.js b/src/utilities/create.js index 64ca972..53eee38 100644 --- a/src/utilities/create.js +++ b/src/utilities/create.js @@ -7,6 +7,15 @@ const templates = new URL('./templates/', root); const extraURL = new URL('./extra/', templates); const extraFolder = fileURLToPath(extraURL); + +/** + * It creates a file with the name `name.lang.sern` in the `location` directory + * @param {string} name - The name of the file. + * @param {string} lang - The language you want to use. + * @param {string} location - The location of the file to be created. + * @param {boolean} no_ext - If true, the file will be created without an extension. + * @returns File + */ export async function create(name, lang, location, no_ext) { const file = `${name}.${lang}.sern`; @@ -17,6 +26,11 @@ export async function create(name, lang, location, no_ext) { return createFile(file, target); } +/** + * It reads a file from a template folder, and writes it to a target folder + * @param {string} template - The name of the file to be created. + * @param {string} target - The location of the file to be created. + */ async function createFile(template, target) { const location = `${extraFolder}${template}`; @@ -25,6 +39,12 @@ async function createFile(template, target) { await writeFileRecursive(target, file); } +/** + * It creates a directory recursively, then writes a file to it + * @param {string} target - The path to the file you want to write to. + * @param {string} data - The data to write to the file. + * @returns A promise that resolves to the result of the writeFile function. + */ async function writeFileRecursive(target, data) { const resolvedTarget = resolve(target); const dir = dirname(resolvedTarget); diff --git a/src/utilities/edits.js b/src/utilities/edits.js index 484cb1f..fe6b80d 100644 --- a/src/utilities/edits.js +++ b/src/utilities/edits.js @@ -20,6 +20,15 @@ export async function editMain(name) { return writeFile(pjLocation, JSON.stringify(output, null, 2)); } +/** + * It renames the `src` and `commands` directories, and edits the `index.ts` file to reflect the + * changes + * @param {string} srcName - The name of the folder that will contain your main files. + * @param {string} cmds_dirName - The name of the directory where your commands will be stored. + * @param {string} name - The name of the folder you want to edit. + * @param {'javascript' | 'typescript'} lang - The language you want to use. + * @returns void + */ export async function editDirs( srcName, cmds_dirName, diff --git a/src/utilities/getLang.js b/src/utilities/getLang.js index c170b10..72899ba 100644 --- a/src/utilities/getLang.js +++ b/src/utilities/getLang.js @@ -1,6 +1,10 @@ import { findUp } from 'find-up'; import { readFile } from 'node:fs/promises'; +/** + * It finds the sern.config.json file, reads it, and returns the language property + * @returns {Promise} The language of the project. + */ export async function getLang() { const sernLocation = await findUp('sern.config.json'); diff --git a/src/utilities/install.js b/src/utilities/install.js index 64fe218..5ff8d06 100644 --- a/src/utilities/install.js +++ b/src/utilities/install.js @@ -8,8 +8,8 @@ import ora from 'ora'; /** * It installs dependencies from a package.json file - * @param choice - The package manager to use. - * @param name - The name of the project + * @param {'skip' | 'npm' | 'yarn'} choice - The package manager to use. + * @param {string} name - The name of the project * @returns a promise. */ export async function installDeps(choice, name) { @@ -46,8 +46,6 @@ export async function installDeps(choice, name) { * Clone the repo, copy the files from the repo to the new project directory, and delete the repo * @param {string} lang - The language of the template * @param {string} name - The name of the project - * @param {string} location - Location of repository - * @param {string} subDirs - path of sub-directory of location, if any */ export async function cloneRepo(lang, name) { await execa('git', [ diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..e93aaad --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,17 @@ +{ + "compilerOptions": { + "target": "ES2022", + "module": "ESNext", + "moduleResolution": "NodeNext", + "lib": ["es2017", "dom"], + "allowJs": true, + "checkJs": true, + "noEmit": true, + "strict": false, + "noImplicitThis": true, + "alwaysStrict": true, + "esModuleInterop": true, + "resolveJsonModule": true + }, + "include": ["test", "src"] +}