DenoDDeno
Powered by
leignaL
Denoβ€’3y agoβ€’
8 replies
leigna

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

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.
Deno banner
DenoJoin
Chat about Deno, a modern runtime for JavaScript and TypeScript.
20,934Members
Resources
Recent Announcements

Similar Threads

Was this page helpful?

Similar Threads

deno fmt vs prettier
Gary KGGary K / help
9mo ago
Deno fmt with verbatimModuleSyntax?
vicaryVvicary / help
3y ago
Can Deno list all functions within a script without explicit registration or annotation?
BairdyBBairdy / help
3y ago
Can I fmt a string with Deno.<...>(...) ?
PaxiliusPPaxilius / help
16mo ago