mirror of
https://github.com/sern-handler/cli
synced 2026-06-28 02:32:20 +00:00
feat: publish command (#105)
Co-authored-by: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> Co-authored-by: jacob <jacoobes@sern.dev>
This commit is contained in:
@@ -15,19 +15,12 @@ const extraFolder = fileURLToPath(extraURL);
|
||||
* @param location - The location of the file to be created.
|
||||
* @param no_ext - If true, the file will be created without an extension.
|
||||
*/
|
||||
export async function create(
|
||||
name: string,
|
||||
lang: string,
|
||||
location: string,
|
||||
no_ext: boolean
|
||||
) {
|
||||
const file = `${name}.${lang}.sern`;
|
||||
export async function create(name: string, lang: string, location: string, no_ext: boolean) {
|
||||
const file = `${name}.${lang}.sern`;
|
||||
|
||||
const target = no_ext
|
||||
? `${location}/${name}`
|
||||
: `${location}/${name}.${lang}`;
|
||||
const target = no_ext ? `${location}/${name}` : `${location}/${name}.${lang}`;
|
||||
|
||||
return createFile(file, target);
|
||||
return createFile(file, target);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -36,11 +29,11 @@ export async function create(
|
||||
* @param target - The location of the file to be created.
|
||||
*/
|
||||
async function createFile(template: string, target: string) {
|
||||
const location = `${extraFolder}${template}`;
|
||||
const location = `${extraFolder}${template}`;
|
||||
|
||||
const file = await readFile(location, 'utf8');
|
||||
const file = await readFile(location, 'utf8');
|
||||
|
||||
await writeFileRecursive(target, file);
|
||||
await writeFileRecursive(target, file);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -50,10 +43,10 @@ async function createFile(template: string, target: string) {
|
||||
* @returns A promise that resolves to the result of the writeFile function.
|
||||
*/
|
||||
async function writeFileRecursive(target: string, data: string) {
|
||||
const resolvedTarget = resolve(target);
|
||||
const dir = dirname(resolvedTarget);
|
||||
const resolvedTarget = resolve(target);
|
||||
const dir = dirname(resolvedTarget);
|
||||
|
||||
await mkdir(dir, { recursive: true });
|
||||
await mkdir(dir, { recursive: true });
|
||||
|
||||
return writeFile(resolvedTarget, data);
|
||||
return writeFile(resolvedTarget, data);
|
||||
}
|
||||
|
||||
@@ -8,16 +8,16 @@ import { fromCwd } from './fromCwd.js';
|
||||
* @param name - The name of the project.
|
||||
*/
|
||||
export async function editMain(name: string) {
|
||||
const pjLocation = (await findUp('package.json', {
|
||||
cwd: fromCwd('/' + name),
|
||||
})) as string;
|
||||
const pjLocation = (await findUp('package.json', {
|
||||
cwd: fromCwd('/' + name),
|
||||
})) as string;
|
||||
|
||||
const output = JSON.parse(await readFile(pjLocation, 'utf8'));
|
||||
if (!output) throw new Error("Can't read your package.json.");
|
||||
const output = JSON.parse(await readFile(pjLocation, 'utf8'));
|
||||
if (!output) throw new Error("Can't read your package.json.");
|
||||
|
||||
output.name = name;
|
||||
output.name = name;
|
||||
|
||||
return writeFile(pjLocation, JSON.stringify(output, null, 2));
|
||||
return writeFile(pjLocation, JSON.stringify(output, null, 2));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -29,55 +29,52 @@ export async function editMain(name: string) {
|
||||
* @param lang - The language you want to use.
|
||||
*/
|
||||
export async function editDirs(
|
||||
srcName: string,
|
||||
cmds_dirName: string,
|
||||
name: string,
|
||||
lang: 'javascript' | 'typescript' | 'javascript-esm' = 'typescript'
|
||||
srcName: string,
|
||||
cmds_dirName: string,
|
||||
name: string,
|
||||
lang: 'javascript' | 'typescript' | 'javascript-esm' = 'typescript'
|
||||
) {
|
||||
const path = (await findUp('src', {
|
||||
cwd: fromCwd(name),
|
||||
type: 'directory',
|
||||
})) as string;
|
||||
const path = (await findUp('src', {
|
||||
cwd: fromCwd(name),
|
||||
type: 'directory',
|
||||
})) as string;
|
||||
|
||||
const ext = lang === 'typescript' ? 'ts' : 'js';
|
||||
const ext = lang === 'typescript' ? 'ts' : 'js';
|
||||
|
||||
const newMainDir = path?.replace('src', srcName);
|
||||
await rename(path, newMainDir);
|
||||
const newMainDir = path?.replace('src', srcName);
|
||||
await rename(path, newMainDir);
|
||||
|
||||
const cmdsPath = (await findUp('commands', {
|
||||
cwd: fromCwd(name, srcName),
|
||||
type: 'directory',
|
||||
})) as string;
|
||||
const cmdsPath = (await findUp('commands', {
|
||||
cwd: fromCwd(name, srcName),
|
||||
type: 'directory',
|
||||
})) as string;
|
||||
|
||||
const index = (await findUp(`index.${ext}`, {
|
||||
cwd: fromCwd(name, srcName),
|
||||
})) as string;
|
||||
const index = (await findUp(`index.${ext}`, {
|
||||
cwd: fromCwd(name, srcName),
|
||||
})) as string;
|
||||
|
||||
const newCmdsPath = cmdsPath?.replace('commands', cmds_dirName);
|
||||
await rename(cmdsPath, newCmdsPath);
|
||||
const newCmdsPath = cmdsPath?.replace('commands', cmds_dirName);
|
||||
await rename(cmdsPath, newCmdsPath);
|
||||
|
||||
const tsconfig = await findUp('tsconfig.json', {
|
||||
cwd: process.cwd() + '/' + name,
|
||||
});
|
||||
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;
|
||||
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;
|
||||
|
||||
await writeFile(tsconfig, JSON.stringify(output, null, 2));
|
||||
}
|
||||
await writeFile(tsconfig, JSON.stringify(output, null, 2));
|
||||
}
|
||||
|
||||
const output = await readFile(index, 'utf8');
|
||||
const output = await readFile(index, 'utf8');
|
||||
|
||||
const oldfold = ext === 'ts' ? 'dist' : 'src';
|
||||
const newfold = ext === 'ts' ? 'dist' : srcName;
|
||||
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}'`
|
||||
);
|
||||
const regex = new RegExp(`commands: '${oldfold}/commands'`);
|
||||
const edit = output.replace(regex, `commands: '${newfold}/${cmds_dirName}'`);
|
||||
|
||||
return writeFile(index, edit);
|
||||
return writeFile(index, edit);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import path from 'path';
|
||||
|
||||
export function fromCwd(...dir: string[]) {
|
||||
return path.join(...[process.cwd(), ...dir]);
|
||||
return path.join(...[process.cwd(), ...dir]);
|
||||
}
|
||||
|
||||
23
src/utilities/getConfig.ts
Normal file
23
src/utilities/getConfig.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { readFile } from 'node:fs/promises';
|
||||
import { findUp } from 'find-up';
|
||||
import assert from 'node:assert';
|
||||
export async function getConfig(): Promise<sernConfig> {
|
||||
const sernLocation = await findUp('sern.config.json');
|
||||
assert(sernLocation, "Can't find sern.config.json");
|
||||
|
||||
const output = JSON.parse(await readFile(sernLocation, 'utf8')) as sernConfig;
|
||||
|
||||
assert(output, "Can't read your sern.config.json.");
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
export interface sernConfig {
|
||||
language: 'typescript' | 'javascript';
|
||||
defaultPrefix?: string;
|
||||
paths: {
|
||||
base: string;
|
||||
commands: string;
|
||||
events?: string;
|
||||
};
|
||||
}
|
||||
@@ -6,13 +6,13 @@ import { readFile } from 'node:fs/promises';
|
||||
* @returns The language of the project.
|
||||
*/
|
||||
export async function getLang(): Promise<'typescript' | 'javascript'> {
|
||||
const sernLocation = await findUp('sern.config.json');
|
||||
const sernLocation = await findUp('sern.config.json');
|
||||
|
||||
if (!sernLocation) throw new Error("Can't find sern.config.json");
|
||||
if (!sernLocation) throw new Error("Can't find sern.config.json");
|
||||
|
||||
const output = JSON.parse(await readFile(sernLocation, 'utf8'));
|
||||
const output = JSON.parse(await readFile(sernLocation, 'utf8'));
|
||||
|
||||
if (!output) throw new Error("Can't read your sern.config.json.");
|
||||
if (!output) throw new Error("Can't read your sern.config.json.");
|
||||
|
||||
return output.language;
|
||||
return output.language;
|
||||
}
|
||||
|
||||
@@ -13,34 +13,34 @@ import type { PackageManagerChoice } from './types';
|
||||
* @param name - The name of the project
|
||||
*/
|
||||
export async function installDeps(choice: PackageManagerChoice, name: string) {
|
||||
const pkg = await findUp('package.json', {
|
||||
cwd: process.cwd() + '/' + name,
|
||||
});
|
||||
if (!pkg) throw new Error('No package.json found!');
|
||||
const pkg = await findUp('package.json', {
|
||||
cwd: process.cwd() + '/' + name,
|
||||
});
|
||||
if (!pkg) throw new Error('No package.json found!');
|
||||
|
||||
const output = JSON.parse(await readFile(pkg, 'utf8'));
|
||||
if (!output) throw new Error("Can't read file.");
|
||||
const output = JSON.parse(await readFile(pkg, 'utf8'));
|
||||
if (!output) throw new Error("Can't read file.");
|
||||
|
||||
const deps = output.dependencies;
|
||||
if (!deps) throw new Error("Can't find dependencies.");
|
||||
const deps = output.dependencies;
|
||||
if (!deps) throw new Error("Can't find dependencies.");
|
||||
|
||||
if (choice === 'skip') {
|
||||
return console.log('Dependency installation skipped...');
|
||||
}
|
||||
if (choice === 'skip') {
|
||||
return console.log('Dependency installation skipped...');
|
||||
}
|
||||
|
||||
const spin = ora({
|
||||
text: `Installing dependencies...`,
|
||||
spinner: 'aesthetic',
|
||||
}).start();
|
||||
const spin = ora({
|
||||
text: `Installing dependencies...`,
|
||||
spinner: 'aesthetic',
|
||||
}).start();
|
||||
|
||||
const result = await execa(choice, ['install'], {
|
||||
cwd: process.cwd() + '/' + name,
|
||||
}).catch(() => null);
|
||||
const result = await execa(choice, ['install'], {
|
||||
cwd: process.cwd() + '/' + name,
|
||||
}).catch(() => null);
|
||||
|
||||
if (!result || result?.failed) {
|
||||
spin.fail(`${redBright('Failed')} to install dependencies!`);
|
||||
process.exit(1);
|
||||
} else spin.succeed(`Dependencies installed!`);
|
||||
if (!result || result?.failed) {
|
||||
spin.fail(`${redBright('Failed')} to install dependencies!`);
|
||||
process.exit(1);
|
||||
} else spin.succeed(`Dependencies installed!`);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -49,21 +49,14 @@ export async function installDeps(choice: PackageManagerChoice, name: string) {
|
||||
* @param name - The name of the project
|
||||
*/
|
||||
export async function cloneRepo(lang: string, name: string) {
|
||||
try {
|
||||
await execa('git', [
|
||||
'clone',
|
||||
`https://github.com/sern-handler/templates.git`,
|
||||
]);
|
||||
copyRecursiveSync(`templates/templates/${lang}`, name);
|
||||
fs.rmSync(`templates/`, { recursive: true, force: true });
|
||||
} catch (error) {
|
||||
console.log(
|
||||
`${redBright(
|
||||
'✖ Failed'
|
||||
)} to clone github templates repo. Install git and try again!`
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
try {
|
||||
await execa('git', ['clone', `https://github.com/sern-handler/templates.git`]);
|
||||
copyRecursiveSync(`templates/templates/${lang}`, name);
|
||||
fs.rmSync(`templates/`, { recursive: true, force: true });
|
||||
} catch (error) {
|
||||
console.log(`${redBright('✖ Failed')} to clone github templates repo. Install git and try again!`);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -74,21 +67,18 @@ export async function cloneRepo(lang: string, name: string) {
|
||||
* @param dest - The destination folder where the files will be copied to.
|
||||
*/
|
||||
export function copyRecursiveSync(src: string, dest: string) {
|
||||
const exists = fs.existsSync(src);
|
||||
const exists = fs.existsSync(src);
|
||||
|
||||
const stats = (exists && fs.statSync(src)) as fs.Stats;
|
||||
const stats = (exists && fs.statSync(src)) as fs.Stats;
|
||||
|
||||
const isDirectory = exists && stats.isDirectory();
|
||||
if (isDirectory) {
|
||||
fs.mkdirSync(dest);
|
||||
const isDirectory = exists && stats.isDirectory();
|
||||
if (isDirectory) {
|
||||
fs.mkdirSync(dest);
|
||||
|
||||
fs.readdirSync(src).forEach(function (childItemName) {
|
||||
copyRecursiveSync(
|
||||
path.join(src, childItemName),
|
||||
path.join(dest, childItemName)
|
||||
);
|
||||
});
|
||||
} else {
|
||||
fs.copyFileSync(src, dest);
|
||||
}
|
||||
fs.readdirSync(src).forEach(function (childItemName) {
|
||||
copyRecursiveSync(path.join(src, childItemName), path.join(dest, childItemName));
|
||||
});
|
||||
} else {
|
||||
fs.copyFileSync(src, dest);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,21 +5,21 @@ import { execa } from 'execa';
|
||||
* @returns A promise that resolves to a string.
|
||||
*/
|
||||
export async function npm() {
|
||||
const npm = await execa('npm', ['-v']).catch(() => null);
|
||||
const npm_version = npm?.stdout;
|
||||
const npm = await execa('npm', ['-v']).catch(() => null);
|
||||
const npm_version = npm?.stdout;
|
||||
|
||||
const yarn = await execa('yarn', ['-v']).catch(() => null);
|
||||
const yarn_version = yarn?.stdout;
|
||||
const yarn = await execa('yarn', ['-v']).catch(() => null);
|
||||
const yarn_version = yarn?.stdout;
|
||||
|
||||
if (npm_version && !yarn_version) {
|
||||
return 'npm';
|
||||
}
|
||||
if (npm_version && !yarn_version) {
|
||||
return 'npm';
|
||||
}
|
||||
|
||||
if (!npm_version && yarn_version) {
|
||||
return 'yarn';
|
||||
}
|
||||
if (!npm_version && yarn_version) {
|
||||
return 'yarn';
|
||||
}
|
||||
|
||||
if (npm_version && yarn_version) {
|
||||
return 'both';
|
||||
}
|
||||
if (npm_version && yarn_version) {
|
||||
return 'both';
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user