feat: now edits the directories as per user's choice

This commit is contained in:
EvolutionX
2022-05-11 11:23:38 +05:30
parent 3a767987f4
commit fec1c8e24c
3 changed files with 63 additions and 41 deletions

View File

@@ -14,7 +14,7 @@ import {
} from '../prompts/init.js';
import { npm } from '../utilities/npm.js';
import { cloneRepo, installDeps } from '../utilities/install.js';
import { editMain } from '../utilities/edits.js';
import { editDirs, editMain } from '../utilities/edits.js';
const { prompt } = prompts;
// TODO make this functional and better!
@@ -34,40 +34,10 @@ export async function init({ flags }) {
process.exit(1);
}
const pkg = await findUp('package.json');
if (!pkg) {
console.log(`No ${redBright('package.json')} found!`);
const npm = await prompt([npmInit]);
if (!npm.npminit) {
console.log(
`${redBright('Failed')} to initialize Sern!` +
'\nMaybe you should run npm init?'
);
process.exit(1);
}
const spin = ora({
text: 'Initializing npm...',
spinner: 'aesthetic',
}).start();
const exee = await execa('npm', ['init', '-y']).catch(
() => null
); /* .stdout.pipe(process.stdout) */
await wait(300);
if (!exee || exee?.failed) {
spin.fail(
`${redBright('Failed')} to initialize npm!` +
'\nMaybe you should run npm init?'
);
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]);
if (Object.keys(data).length < 5) process.exit(1);
await cloneRepo(data.lang, data.name);
const git_init = await prompt([gitInit]);
if (!git_init.gitinit) {
@@ -95,8 +65,9 @@ export async function init({ flags }) {
const chosen = await prompt([which_manager]);
choice = chosen.manager;
} else choice = pm;
await installDeps(choice, data.name);
// await installDeps(choice, data.name);
await editMain(data.name);
await editDirs(data.main_dir, data.cmds_dir, data.name);
}
/**

View File

@@ -86,4 +86,8 @@ export const name = {
message: 'What is your project name?',
name: 'name',
type: 'text',
validate: (name) =>
name.match('^(?:@[a-z0-9-*~][a-z0-9-*._~]*/)?[a-z0-9-~][a-z0-9-._~]*$')
? true
: 'Invalid name',
};

View File

@@ -1,4 +1,4 @@
import { readFile, writeFile } from 'node:fs/promises';
import { readFile, rename, writeFile } from 'node:fs/promises';
import { findUp } from 'find-up';
/**
@@ -13,15 +13,62 @@ 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;
return writeFile(pjLocation, JSON.stringify(output, null, 2));
}
export async function editDirs(
srcName,
cmds_dirName,
name,
lang = 'typescript'
) {
const path = await findUp('src', {
cwd: process.cwd() + '/' + name,
type: 'directory',
});
const ext = lang === 'typescript' ? 'ts' : 'js';
const newMainDir = path?.replace('src', srcName);
await rename(path, newMainDir);
const cmdsPath = await findUp('commands', {
cwd: process.cwd() + '/' + name + '/' + srcName,
type: 'directory',
});
const index = await findUp(`index.${ext}`, {
cwd: process.cwd() + '/' + name + '/' + srcName,
});
const newCmdsPath = cmdsPath?.replace('commands', cmds_dirName);
await rename(cmdsPath, newCmdsPath);
const tsconfig = await findUp('tsconfig.json', {
cwd: process.cwd() + '/' + name,
});
if (tsconfig) {
const output = JSON.parse(await readFile(tsconfig, 'utf8'));
if (!output) throw new Error("Can't read your tsconfig.json.");
output.compilerOptions.rootDir = srcName;
writeFile(tsconfig, JSON.stringify(output, null, 2));
}
const output = await readFile(index, 'utf8');
const oldfold = ext === 'ts' ? 'dist' : 'src';
const newfold = ext === 'ts' ? 'dist' : srcName;
const regex = new RegExp(`commands: '${oldfold}/commands'`);
const edit = output.replace(
regex,
`commands: '${newfold}/${cmds_dirName}'`
);
return writeFile(index, edit);
}