D
Deno

help

Anyway to use deno fmt within a script itself without having to call deno again?

Lleigna9/30/2023
I am working on a code generator and I'd like to run a string through deno fmt before I save to file. I've seen how to do it by calling deno to format the file, but wondering if I could do this within the script itself? See this function here:
export function generateTSContent({
schema,
compressedSchema,
propertyMappingTable,
enumMappingTables,
compressedInterfaceName,
originalTSInterface,
compressedTSInterface,
}: {
schema: any;
compressedSchema: any;
propertyMappingTable: KeyValue;
enumMappingTables: { [key: string]: KeyValue };
interfaceName: string;
compressedInterfaceName: string;
originalTSInterface: string;
compressedTSInterface: string;
}): string {
return `import Ajv from "https://esm.sh/ajv@8.12.0";
import addFormats from "https://esm.sh/ajv-formats@2.1.1";
${originalTSInterface}
${compressedTSInterface}
export const schemaString = ${JSON.stringify(schema)};
export const schemaCompressedString = ${JSON.stringify(compressedSchema)};
export const propertyMappingTable = ${JSON.stringify(propertyMappingTable)};
export const enumMappingTables = ${JSON.stringify(enumMappingTables)};
const ajv = new Ajv({ allErrors: true });
addFormats(ajv);
const validate = ajv.compile(schemaCompressedString);
export function validateData(data: ${compressedInterfaceName}): { valid: boolean; errors: any } {
const valid = validate(data);
return {
valid,
errors: validate.errors,
};
}
`;
}
export function generateTSContent({
schema,
compressedSchema,
propertyMappingTable,
enumMappingTables,
compressedInterfaceName,
originalTSInterface,
compressedTSInterface,
}: {
schema: any;
compressedSchema: any;
propertyMappingTable: KeyValue;
enumMappingTables: { [key: string]: KeyValue };
interfaceName: string;
compressedInterfaceName: string;
originalTSInterface: string;
compressedTSInterface: string;
}): string {
return `import Ajv from "https://esm.sh/ajv@8.12.0";
import addFormats from "https://esm.sh/ajv-formats@2.1.1";
${originalTSInterface}
${compressedTSInterface}
export const schemaString = ${JSON.stringify(schema)};
export const schemaCompressedString = ${JSON.stringify(compressedSchema)};
export const propertyMappingTable = ${JSON.stringify(propertyMappingTable)};
export const enumMappingTables = ${JSON.stringify(enumMappingTables)};
const ajv = new Ajv({ allErrors: true });
addFormats(ajv);
const validate = ajv.compile(schemaCompressedString);
export function validateData(data: ${compressedInterfaceName}): { valid: boolean; errors: any } {
const valid = validate(data);
return {
valid,
errors: validate.errors,
};
}
`;
}
I'd like this string passed through deno fmt before saving, within the deno Script itself. I then process that string content like this:
const tempFilePath = "./temp.ts";
await Deno.writeTextFile(tempFilePath, tsContent);
const command = new Deno.Command("deno", {
args: ["fmt", tempFilePath],
});
const child = command.spawn();
await child.status;
const formattedTsContent = await Deno.readTextFile(tempFilePath);
await Deno.remove(tempFilePath);
const tempFilePath = "./temp.ts";
await Deno.writeTextFile(tempFilePath, tsContent);
const command = new Deno.Command("deno", {
args: ["fmt", tempFilePath],
});
const child = command.spawn();
await child.status;
const formattedTsContent = await Deno.readTextFile(tempFilePath);
await Deno.remove(tempFilePath);
I dont want to allow Deno.Command, so was wondering if another way. Thank you.
MMrcool9/30/2023
nope unfortunately there are issues on the deno repo about adding such functions (exposing deno cli to scripts) but it never gained any traction
Lleigna10/1/2023
Okay thank you! This was the workaround i ended up with, no temp file and just fmt the whole directory
// After the loop has finished processing all schema files, format them all at once
const command = new Deno.Command("deno", {
args: ["fmt", "./schemas"],
});
const child = command.spawn();
await child.status;
// After the loop has finished processing all schema files, format them all at once
const command = new Deno.Command("deno", {
args: ["fmt", "./schemas"],
});
const child = command.spawn();
await child.status;
Lleigna10/1/2023
Im really not too familiar with this stuff I find Deno great for prototyping ideas as its brilliant but not too familiar with internals etc, but does my logic sound correct on this? Here is the rust code for fmt: https://github.com/denoland/deno/blob/main/cli/tools/fmt.rs I found this blog: https://deno.com/blog/wasmbuild maybe i could just try this with the deno cli code i will have a look
GitHub
deno/cli/tools/fmt.rs at main ยท denoland/deno
A modern runtime for JavaScript and TypeScript. Contribute to denoland/deno development by creating an account on GitHub.
Deno Blog
wasmbuild - Using Rust in Deno and Web Apps
An easier way to build Rust for Deno and the Web.
MMrcool10/1/2023
Deno uses dprint internally which uses wasm plugins to format Turns out the author already expose deno wasm wrapper
MMrcool10/1/2023
GitHub
GitHub - dprint/js-formatter: JS formatter for dprint Wasm plugins.
JS formatter for dprint Wasm plugins. Contribute to dprint/js-formatter development by creating an account on GitHub.
Lleigna10/1/2023
great thank you I will have a look at that! ๐Ÿ˜„

Looking for more? Join the community!

Recommended Posts
interaction_endpoint_url: couldn't be verifiedI'm trying to create an http interactions bot using deno deploy, but sometimes my url fails to be veLost access to deno land module, how to reset the repo?My repository was accidentally destroyed on GitHub and I recreated it the same exact way. The webhooBDD with async testsI'd like to run some tests that all call the same helper function that wraps the bdd `it` function. Is there a schedule / pattern for which Deno releases get new Docker files?We'd love to update to Deno 1.37.1 in production, but deno_docker is still at 1.37.0. I've noticed tIs the Fresh compornents test function enabled?Attempting to write a test for Fresh compornents. I was able to verify the rendered content. I am Inquiry Regarding Deno Security Model and Command Injection VulnerabilityI am currently instructing a class on software security and have been exploring Deno's security mode`WARNING: v8::OwnedIsolate for snapshot was leaked` and/or crash after using snapshotOn startup, I create a runtime: ```rust let js_runtime = JsRuntimeForSnapshot::new(deno_corOak: Remove HTML extensions when serving filesUsing Oak, how can I serve `home.html` as `/home` (or `/home/`). I'm aware that `Context#send` existdeno_console not being properly instantiated?Not sure if it's indended, or if I am missing a step, but several core extensions, for example the dTailwind nonfunctional on fresh-update from 1.3.1 -> 1.4.3After running `deno run -A -r https://fresh.deno.dev/update .` on my project, the tailwind styles doassertEquals deprecated in vscode?Since upgrading to std@0.202.0 I'm seeing that assertEquals is marked deprecated, but I can't see whForce await import() to import a fresh copy?I have a Deno instance that dynamically loads a module with: `await import("some/module.js")` The mFresh ClassList Brokenhttps://github.com/Leave TLS connection open in between testsWhen I have 2 tests that use a database connection and attempt to close the connection `afterAll` thNvim tsserver clash with denolsI have read the docs and added a root_dir in my lua lsp file but for some reason tsserver still lanccircular type reference nonsensewhy does removing an array nest cause this to be a circular dependency?? shouldn't it be functionallGetting exports from an evaluated module with get_module_namespace always results in null valuesTrying to get module exports like the example [here](https://github.com/denoland/deno/pull/14026/fiIs there any way to define a signal inside a route component?Is there any way to define a signal inside a route component? I am trying to do it but an error rai