diff --git a/.github/workflows/linter.yml b/.github/workflows/linter.yml index 6c1bf94..e0be3d4 100644 --- a/.github/workflows/linter.yml +++ b/.github/workflows/linter.yml @@ -6,10 +6,15 @@ on: push: branches: - main - pull_request: + pull_request_target: branches: - main +# Down scope as necessary via https://docs.github.com/en/actions/security-guides/automatic-token-authentication#modifying-the-permissions-for-the-github_token +permissions: + checks: write + contents: write + jobs: run-linters: name: Run linters diff --git a/src/commands/init.js b/src/commands/init.js index 9460b2d..4ebff08 100644 --- a/src/commands/init.js +++ b/src/commands/init.js @@ -6,13 +6,15 @@ import { execa } from 'execa'; import { cmds_dir, default_prefix, - intent, lang, main_dir, - token, - npmInit, gitInit, + which_manager, + name, } from '../prompts/init.js'; +import { npm } from '../utilities/npm.js'; +import { cloneRepo, installDeps } from '../utilities/install.js'; +import { editMain } from '../utilities/edits.js'; const { prompt } = prompts; // TODO make this functional and better! @@ -41,8 +43,8 @@ export async function init({ flags }) { `${redBright('Failed')} to initialize Sern!` + '\nMaybe you should run npm init?' ); - process.exit(1); - } + process.exit(1); + } const spin = ora({ text: 'Initializing npm...', spinner: 'aesthetic', @@ -56,52 +58,52 @@ export async function init({ flags }) { `${redBright('Failed')} to initialize npm!` + '\nMaybe you should run npm init?' ); - process.exit(1); - } - spin.succeed('Npm initialized!'); - } - const git = await findUp('.git/config'); - if (!git) { - console.log(`No ${redBright('Git Repository')} found!`); - const git_init = await prompt([gitInit]); - if (!git_init.gitinit) { - console.log( - 'Maybe you should run git init?\n' + `Alright Moving on...` - ); - } else { - const spin = ora({ - text: 'Initializing git...', - spinner: 'aesthetic', - }).start(); - const exe = await execa('git', [ - 'init', - ]); /* .stdout.pipe(process.stdout) */ - await wait(300); - if (!exe || exe?.failed) { - spin.fail( - `${redBright('Failed')} to initialize git!` + - '\nMaybe you should run git init?' - ); - process.exit(1); - } - spin.succeed('Git initialized!'); - return; + process.exit(1); } + spin.succeed('Npm initialized!'); + } + /** + * TODO edit main_dir and cmds_dir according to user input as well as default_prefix + * will need help @Allyedge + */ + const data = await prompt([name, lang, main_dir, cmds_dir, default_prefix]); + + await cloneRepo(data.lang, data.name); + const git_init = await prompt([gitInit]); + if (!git_init.gitinit) { + console.log(`\nAlright\n`); + } else { + const spin = ora({ + text: 'Initializing git...', + spinner: 'aesthetic', + }).start(); + const exe = await execa('git', ['init', data.name]); + await wait(300); + if (!exe || exe?.failed) { + spin.fail( + `${redBright('Failed')} to initialize git!` + + '\nMaybe you should run git init?' + ); + process.exit(1); + } + spin.succeed('Git initialized!'); } - const data = await prompt([ - lang, - main_dir, - cmds_dir, - default_prefix, - token, - intent, - ]); - console.log(data); + const pm = await npm(); + let choice = ''; + if (pm === 'both') { + const chosen = await prompt([which_manager]); + choice = chosen.manager; + } else choice = pm; + await installDeps(choice, data.name); + await editMain(data.name); } /** - * @param {number} ms + * Wait for a specified number of milliseconds, then return a promise that resolves to undefined. + * @param {number} ms - The number of milliseconds to wait. + * @returns A function that takes a single argument, ms, and returns a promise that resolves after ms + * milliseconds. */ async function wait(ms) { const wait = (await import('util')).promisify(setTimeout); diff --git a/src/prompts/init.js b/src/prompts/init.js index 9dcf741..56eec0e 100644 --- a/src/prompts/init.js +++ b/src/prompts/init.js @@ -29,14 +29,21 @@ export const lang = { { title: 'JavaScript', description: 'JS', + value: 'javascript', }, { title: 'TypeScript', description: 'TS', + value: 'typescript', + selected: true, }, ], }; +/** + * @deprecated + * ! Will be removed ! + */ export const intent = { message: 'What intents do you want to keep?', type: 'multiselect', @@ -56,13 +63,15 @@ export const default_prefix = { }; export const token = { - message: 'What is your bot token?', + message: 'What is your bot token? Type "no" to skip', name: 'token', type: 'password', - validate: (/** @type {string} */ token) => - (/(?mfa\.[a-z0-9_-]{20,})|(?[a-z0-9_-]{23,28}\.[a-z0-9_-]{6,7}\.[a-z0-9_-]{27})/i).test(token) + validate: (/** @type {string} */ token) => { + if (token === 'no') return true; + return (/(?mfa\.[a-z0-9_-]{20,})|(?[a-z0-9_-]{23,28}\.[a-z0-9_-]{6,7}\.[a-z0-9_-]{27})/i).test(token) ? true - : 'Invalid token', + : 'Invalid token' + } }; export const main_dir = { @@ -90,12 +99,37 @@ export const npmInit = { message: `Do you want to ${blueBright('me')} to initialize npm?`, initial: true, }; -/** - * @type {import('prompts').PromptObject} - */ export const gitInit = { name: 'gitinit', type: 'confirm', message: `Do you want to ${blueBright('me')} to initialize git?`, initial: true, }; + +export const which_manager = { + message: `Which manager do you want to use?`, + name: 'manager', + type: 'select', + choices: [ + { + title: 'NPM', + description: 'Default Package Manager', + selected: true, + value: 'npm', + }, + { + title: 'Yarn', + description: 'Yarn Package Manager', + value: 'yarn', + }, + ], +}; + +/** + * @type {import('prompts').PromptObject} + */ +export const name = { + message: 'What is your project name?', + name: 'name', + type: 'text', +}; diff --git a/src/utilities/edits.js b/src/utilities/edits.js index 9bac1ba..8bc5fa8 100644 --- a/src/utilities/edits.js +++ b/src/utilities/edits.js @@ -13,6 +13,12 @@ export async function editMain(name) { }); const output = JSON.parse(await readFile(pjLocation, 'utf8')); + if (!output) throw new Error("Can't read file."); + + output.name = name; + + const result = () => writeFile(pjLocation, JSON.stringify(output, null, 2)); + return result(); if (!output) throw new Error("Can't read your package.json."); output.name = name;