From e73c435585002b374d08b8c4f4667f0665c40161 Mon Sep 17 00:00:00 2001 From: Thomas Cruveilher <38007824+Sorikairox@users.noreply.github.com> Date: Tue, 1 Oct 2024 14:59:43 +0000 Subject: [PATCH] feat: clean errors --- bundle.ts | 20 ++++++++------- deno.json | 13 +++------- deployToDenoDeploy.ts | 14 +++++----- generate.ts | 59 ++++++++++++++++++++++++------------------- main.ts | 10 +++++--- run.ts | 20 +++++++-------- 6 files changed, 72 insertions(+), 64 deletions(-) diff --git a/bundle.ts b/bundle.ts index 2338706..d25b47d 100644 --- a/bundle.ts +++ b/bundle.ts @@ -8,15 +8,17 @@ export const bundleProject = async ( options: { entrypoint: string, bundlePath?: string}, name: string, ) => { - if (!bundlePath) - bundlePath = './bundle'; - if (await bundleExists(bundlePath)) { - await Deno.remove(bundlePath, { recursive: true }); + if (!options.bundlePath) + options.bundlePath = './bundle'; + if (await bundleFolderExists(options.bundlePath)) { + await Deno.remove(options.bundlePath, { recursive: true }); } - await Deno.mkdir(bundlePath); - const p = Deno.run({ - cmd: ['deno', 'bundle', options.entrypoint, `${bundlePath}/${name}`], - }); - await p.status(); + await Deno.mkdir(options.bundlePath); + + + const bundleCommand = new Deno.Command("deno", { + args: [ 'bundle', options.entrypoint, `${options.bundlePath}/${name}`] + }); + await bundleCommand.output(); }; diff --git a/deno.json b/deno.json index 77b7be6..6feb59b 100644 --- a/deno.json +++ b/deno.json @@ -2,13 +2,7 @@ "name": "@danet/cli", "version": "0.9.0", "exports": { - "./bundle.ts": "./bundle.ts", - "./generate.ts": "./generate.ts", - "./main.ts": "./main.ts", - "./deployToDenoDeploy.ts": "./deployToDenoDeploy.ts", - "./deps.ts": "./deps.ts", - "./run.ts": "./run.ts", - "./deno.json": "./deno.json" + ".": "./main.ts" }, "fmt": { "options": { @@ -28,7 +22,8 @@ }, "imports": { "@cliffy/command": "jsr:@cliffy/command@1.0.0-rc.7", - "@ts-morph/ts-morph": "jsr:@ts-morph/ts-morph", + "@ts-morph/ts-morph": "jsr:@ts-morph/ts-morph@23.0.0", "@danet/core": "jsr:@danet/core@2" - } + }, + "license": "MIT" } diff --git a/deployToDenoDeploy.ts b/deployToDenoDeploy.ts index e9a36c0..3bce298 100644 --- a/deployToDenoDeploy.ts +++ b/deployToDenoDeploy.ts @@ -2,14 +2,16 @@ import { bundleProject } from "./bundle.ts"; export async function deployToDenoDeploy(options: { project: string, bundle: string, entrypoint: string }) { await bundleProject(options, options.bundle); - await Deno.run({ cmd: ['deno', 'install', '-Arf', 'jsr:@deno/deployctl'] }).status(); + const installCommand = new Deno.Command("deno", { + args: [ 'install', '-Arf', 'jsr:@deno/deployctl'] + }); + await installCommand.output(); let projectOptions: string[] = []; if (options.project) { projectOptions = [`--project`, options.project]; } - const p = Deno.run({ - cmd: ['deployctl', 'deploy', ...projectOptions , `--entrypoint`, `${options.bundle}`], - cwd: './bundle', - }) - await p.status(); + const deployCommand = new Deno.Command("deployctl", { + args: [ 'deployctl', 'deploy', ...projectOptions , `--entrypoint`, `${options.bundle}`] + }); + await deployCommand.output(); } \ No newline at end of file diff --git a/generate.ts b/generate.ts index 911da4f..c7ca356 100644 --- a/generate.ts +++ b/generate.ts @@ -18,7 +18,8 @@ export async function generateProject(options: GenerateOption, name: string) { logger.log(`Danet's project creation done ! You can run it with the following commands: cd ${name} && danet develop Tests can be run with: deno task test`); - } catch (e) { + // deno-lint-ignore no-explicit-any + } catch (e: any) { logger.error(e.toString()); } } @@ -67,8 +68,9 @@ function getDatabaseFromOptionOrAskUser(options: GenerateOption) { } function modifyDatabaseModule(baseDirectory: Directory, databaseName: string) { - const dbDirectory: Directory = baseDirectory?.getDirectory('src/database'); - const dbModuleFile: SourceFile = dbDirectory?.getSourceFile('module.ts'); + const dbDirectory: Directory = baseDirectory?.getDirectory('src/database')!; + const dbModuleFile: SourceFile = dbDirectory?.getSourceFile('module.ts')!; + const importDeclaration = dbModuleFile.getImportDeclarations()[1]; importDeclaration.remove(); dbModuleFile.addImportDeclaration({ @@ -80,10 +82,12 @@ function modifyDatabaseModule(baseDirectory: Directory, databaseName: string) { ); const ModuleDeclaration = DatabaseClass.getDecorators().find((d) => d.getName() === 'Module' - ); + )!; const moduleDeclarationArgument = ModuleDeclaration.getArguments()[0]; - const argumentProperties = moduleDeclarationArgument.getProperties(); - const moduleInjectables = argumentProperties.find((p) => + // deno-lint-ignore no-explicit-any + const argumentProperties = (moduleDeclarationArgument as any).getProperties(); + // deno-lint-ignore no-explicit-any + const moduleInjectables = argumentProperties.find((p: any) => p.getName() === 'injectables' ); moduleInjectables.set( @@ -100,11 +104,11 @@ function modifyDatabaseModule(baseDirectory: Directory, databaseName: string) { capitalize(databaseName) }Service from DatabaseModule`, ); -} + } -function modifyTodoModule(baseDirectory, databaseName: string) { - const dbDirectory: Directory = baseDirectory?.getDirectory('src/todo'); - const todoModule: SourceFile = dbDirectory?.getSourceFile('module.ts'); +function modifyTodoModule(baseDirectory: Directory, databaseName: string) { + const dbDirectory: Directory = baseDirectory?.getDirectory('src/todo')!; + const todoModule: SourceFile = dbDirectory?.getSourceFile('module.ts')!; const importDeclaration = todoModule.getImportDeclarations()[4]; importDeclaration.remove(); todoModule.addImportDeclaration({ @@ -120,15 +124,18 @@ function modifyTodoModule(baseDirectory, databaseName: string) { ); const ModuleDeclaration = DatabaseClass.getDecorators().find((d) => d.getName() === 'Module' - ); + )!; const moduleDeclarationArgument = ModuleDeclaration.getArguments()[0]; - const argumentProperties = moduleDeclarationArgument.getProperties(); - moduleDeclarationArgument.addProperty({ + // deno-lint-ignore no-explicit-any + const argumentProperties = (moduleDeclarationArgument as any).getProperties(); + // deno-lint-ignore no-explicit-any + (moduleDeclarationArgument as any).addProperty({ name: 'imports', kind: StructureKind.PropertyAssignment, initializer: `[DatabaseModule]`, }); - const moduleInjectables = argumentProperties.find((p) => + // deno-lint-ignore no-explicit-any + const moduleInjectables = argumentProperties.find((p: any) => p.getName() === 'injectables' ); moduleInjectables.set( @@ -154,9 +161,10 @@ async function modifyModulesWithDatabaseService( const project = new Project({ resolutionHost: ResolutionHosts.deno, indentationText: IndentationText.TwoSpaces, - }); + // deno-lint-ignore no-explicit-any + } as any); project.addSourceFilesAtPaths(`${projectDirectory}/**/*.ts`); - const baseDirectory = project.getDirectory(`${projectDirectory}`); + const baseDirectory = project.getDirectory(`${projectDirectory}`)!; modifyDatabaseModule(baseDirectory, databaseName); modifyTodoModule(baseDirectory, databaseName); await project.save(); @@ -188,14 +196,15 @@ function askWhichDBUserWants() { } async function overwriteIfPossibleOrQuit(name: string) { - const overwrite: string = prompt( + const overwrite: string | null = prompt( `${name} folder may already exists, do you want to completely overwrite its content ? (y/N)`, 'N', ); - if (overwrite.toLowerCase() === 'y') { + if (overwrite?.toLowerCase() === 'y') { try { await Deno.remove(name, {recursive: true}); - } catch (e) { + // deno-lint-ignore no-explicit-any + } catch (e: any) { if (e.name !== 'NotFound') console.log(e); } @@ -205,13 +214,11 @@ async function overwriteIfPossibleOrQuit(name: string) { async function cloneRepositoryAndDeleteGitFolder(name: string) { const repository = `https://github.com/Savory/Danet-Starter.git`; logger.log(`Cloning starter project from ${repository} into ${name}`); - const p = Deno.run({ - cmd: ['git', 'clone', '-b', 'jsr', repository, `${name}`], - stdout: 'null', - stderr: 'null', - }); - const status = await p.status(); - if (!status.success) { + const cloneCommand = new Deno.Command("git", { + args: [ 'clone', '-b', 'jsr', repository, `${name}`] + }); + const { success, stdout } = await cloneCommand.output(); + if (!success) { throw new Error('Clone Failed'); } await Deno.remove(`./${name}/.git`, { recursive: true }); diff --git a/main.ts b/main.ts index 60226c7..0729af4 100644 --- a/main.ts +++ b/main.ts @@ -1,3 +1,5 @@ +#!/usr/bin/env -S deno run --allow-read --allow-write --allow-env --allow-net --allow-run --allow-sys + import { Command } from './deps.ts'; import { generateProject } from './generate.ts'; import { runProject, runProjectWithWatch } from "./run.ts"; @@ -27,15 +29,15 @@ program.command('start') program.command('bundle') .description('Bundle project into a single file') - .option('-e, --entrypoint ', { + .option('-e , --entrypoint ', "Your danet entry file", { default: 'run.ts', }) .arguments('').action(bundleProject); -program.command('deploy').description('Deploy your project to Deno Deploy').option('-p, --project ', 'Deno deploy project name. If no value is given, Deno deploy will generate a random name') -.option('-e, --entrypoint ', 'Bundle entrypoint file', { +program.command('deploy').description('Deploy your project to Deno Deploy').option('-p , --project ', 'Deno deploy project name.', { required: true }) +.option('-e , --entrypoint ', 'Bundle entrypoint file', { default: 'run.ts', }) - .option('-b, --bundle ', 'Bundle output file name, also used as deployctl entrypoint', { default: 'bundle.js' }).action(deployToDenoDeploy); + .option('-b , --bundle ', 'Bundle output file name, also used as deployctl entrypoint', { default: 'bundle.js' }).action(deployToDenoDeploy); await program.parse(Deno.args); diff --git a/run.ts b/run.ts index 6dd3a55..c30dd6b 100644 --- a/run.ts +++ b/run.ts @@ -1,13 +1,13 @@ -export const runProjectWithWatch = async () => { - const p = Deno.run({ - cmd: ['deno', 'run', '--watch', '--allow-net', '--allow-read', '--unstable', '--allow-env', 'run.ts'] - }) - await p.status(); +export const runProjectWithWatch: () => void = async () => { + const command = new Deno.Command("deno", { + args: ['run', '--watch', '--allow-net', '--allow-read', '--unstable', '--allow-env', 'run.ts'] + }); + await command.output(); } -export const runProject = async () => { - const p = Deno.run({ - cmd: ['deno', 'run', '--allow-net', '--allow-read', '--unstable', '--allow-env', 'run.ts'] - }) - await p.status(); +export const runProject: () => void = async () => { + const command = new Deno.Command("deno", { + args: ['run', '--allow-net', '--allow-read', '--unstable', '--allow-env', 'run.ts'] + }); + await command.output(); } \ No newline at end of file