Skip to content

Commit

Permalink
Merge pull request #8 from Savory/jsr
Browse files Browse the repository at this point in the history
feat: clean errors
  • Loading branch information
Sorikairox authored Oct 1, 2024
2 parents dfabfbc + e73c435 commit f74cb98
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 64 deletions.
20 changes: 11 additions & 9 deletions bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};
13 changes: 4 additions & 9 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -28,7 +22,8 @@
},
"imports": {
"@cliffy/command": "jsr:@cliffy/[email protected]",
"@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"
}
14 changes: 8 additions & 6 deletions deployToDenoDeploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
59 changes: 33 additions & 26 deletions generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Expand Down Expand Up @@ -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({
Expand All @@ -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(
Expand All @@ -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({
Expand All @@ -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(
Expand All @@ -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();
Expand Down Expand Up @@ -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);
}
Expand All @@ -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 });
Expand Down
10 changes: 6 additions & 4 deletions main.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -27,15 +29,15 @@ program.command('start')

program.command('bundle')
.description('Bundle project into a single file')
.option('-e, --entrypoint <entrypoint:string>', {
.option('-e <entrypoint:string>, --entrypoint <entrypoint:string>', "Your danet entry file", {
default: 'run.ts',
})
.arguments('<name:string>').action(bundleProject);

program.command('deploy').description('Deploy your project to Deno Deploy').option('-p, --project <project:string>', 'Deno deploy project name. If no value is given, Deno deploy will generate a random name')
.option('-e, --entrypoint <entrypoint:string>', 'Bundle entrypoint file', {
program.command('deploy').description('Deploy your project to Deno Deploy').option('-p <bundle:string>, --project <project:string>', 'Deno deploy project name.', { required: true })
.option('-e <entrypoint:string>, --entrypoint <entrypoint:string>', 'Bundle entrypoint file', {
default: 'run.ts',
})
.option('-b, --bundle <bundle:string>', 'Bundle output file name, also used as deployctl entrypoint', { default: 'bundle.js' }).action(deployToDenoDeploy);
.option('-b <bundle:string>, --bundle <bundle:string>', 'Bundle output file name, also used as deployctl entrypoint', { default: 'bundle.js' }).action(deployToDenoDeploy);

await program.parse(Deno.args);
20 changes: 10 additions & 10 deletions run.ts
Original file line number Diff line number Diff line change
@@ -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();
}

0 comments on commit f74cb98

Please sign in to comment.