Skip to content

Commit

Permalink
Adding formatting on save for ts files (#24646)
Browse files Browse the repository at this point in the history
* Adding formatting on save for ts files

* scoping to eng folder

* cleanup

* aligning prettier with typespec repo

* update tsv.ts formatting

* scoping typescript prettier to entire repo

* revert package-lock
  • Loading branch information
ckairen authored Jul 6, 2023
1 parent 12ad6af commit 310a010
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 74 deletions.
14 changes: 14 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@
"excludeFiles": [
"specification/**/*.json"
]
},
{
"files": [
"**/*.ts"
],
"options": {
"arrowParens": "always",
"bracketSpacing": true,
"endOfLine": "lf",
"printWidth": 100,
"semi": true,
"singleQuote": false,
"tabWidth": 2
}
}
]
}
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,9 @@
"typescript.tsdk": "node_modules\\typescript\\lib",
"[json]": {
"editor.defaultFormatter": "vscode.json-language-features"
},
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true
}
}
143 changes: 69 additions & 74 deletions eng/tools/TypeSpecValidation/src/TypeSpecValidation.ts
Original file line number Diff line number Diff line change
@@ -1,91 +1,86 @@
import { exec } from "child_process";
import debug from "debug";
import { access } from "fs/promises"
import { parseArgs, ParseArgsConfig } from 'node:util';
import { access } from "fs/promises";
import { parseArgs, ParseArgsConfig } from "node:util";
import path from "path";
import { simpleGit } from 'simple-git';
import { simpleGit } from "simple-git";

debug.enable('simple-git');
debug.enable("simple-git");

async function runCmd(cmd: string, cwd: string) {
console.log(`run command:${cmd}`)
const { err, stdout, stderr } = await new Promise((res) =>
exec(
cmd,
{ encoding: "utf8", maxBuffer: 1024 * 1024 * 64, cwd: cwd },
(err: unknown, stdout: unknown, stderr: unknown) =>
res({ err: err, stdout: stdout, stderr: stderr })
)
) as any;
let resultString = stderr + stdout;
console.log("Stdout output:")
console.log(stdout)
if (stderr) {
console.log("Stderr output:")
console.log(stderr)
}
if (stderr || err) {
throw new Error(err);
}
return resultString as string;
console.log(`run command:${cmd}`);
const { err, stdout, stderr } = (await new Promise((res) =>
exec(
cmd,
{ encoding: "utf8", maxBuffer: 1024 * 1024 * 64, cwd: cwd },
(err: unknown, stdout: unknown, stderr: unknown) =>
res({ err: err, stdout: stdout, stderr: stderr })
)
)) as any;
let resultString = stderr + stdout;
console.log("Stdout output:");
console.log(stdout);
if (stderr) {
console.log("Stderr output:");
console.log(stderr);
}
if (stderr || err) {
throw new Error(err);
}
return resultString as string;
}

async function checkFileExists(file: string) {
return access(file)
.then(() => true)
.catch(() => false)
return access(file)
.then(() => true)
.catch(() => false);
}

export async function main() {
const args = process.argv.slice(2);
const options = {
folder: {
type: 'string',
short: 'f',
},
};
const parsedArgs = parseArgs({ args, options, allowPositionals: true } as ParseArgsConfig);
const folder = parsedArgs.positionals[0];
console.log("Running TypeSpecValidation on folder:", folder);
const args = process.argv.slice(2);
const options = {
folder: {
type: "string",
short: "f",
},
};
const parsedArgs = parseArgs({ args, options, allowPositionals: true } as ParseArgsConfig);
const folder = parsedArgs.positionals[0];
console.log("Running TypeSpecValidation on folder:", folder);

// Verify all specs are using root level pacakge.json
let expected_npm_prefix = process.cwd()
const actual_npm_prefix = (await runCmd(`npm prefix`, folder)).trim()
if (expected_npm_prefix !== actual_npm_prefix) {
console.log("ERROR: TypeSpec folders MUST NOT contain a package.json, and instead MUST rely on the package.json at repo root.")
throw new Error("Expected npm prefix: " + expected_npm_prefix + "\nActual npm prefix: " + actual_npm_prefix)
}
// Verify all specs are using root level pacakge.json
let expected_npm_prefix = process.cwd();
const actual_npm_prefix = (await runCmd(`npm prefix`, folder)).trim();
if (expected_npm_prefix !== actual_npm_prefix) {
console.log(
"ERROR: TypeSpec folders MUST NOT contain a package.json, and instead MUST rely on the package.json at repo root."
);
throw new Error(
"Expected npm prefix: " + expected_npm_prefix + "\nActual npm prefix: " + actual_npm_prefix
);
}

// Spec compilation check
if (await checkFileExists(path.join(folder, "main.tsp"))) {
await runCmd(
`npx --no tsp compile . --warn-as-error`,
folder
);
}
if (await checkFileExists(path.join(folder, "client.tsp"))) {
await runCmd(
`npx --no tsp compile client.tsp --no-emit --warn-as-error`,
folder
);
}
// Spec compilation check
if (await checkFileExists(path.join(folder, "main.tsp"))) {
await runCmd(`npx --no tsp compile . --warn-as-error`, folder);
}
if (await checkFileExists(path.join(folder, "client.tsp"))) {
await runCmd(`npx --no tsp compile client.tsp --no-emit --warn-as-error`, folder);
}

// Format parent folder to include shared files
await runCmd(
`npx tsp format ../**/*.tsp`,
folder
);
// Format parent folder to include shared files
await runCmd(`npx tsp format ../**/*.tsp`, folder);

// Verify generated swagger file is in sync with one on disk
const git = simpleGit();
let gitStatusIsClean = await (await git.status(['--porcelain'])).isClean()
if (!gitStatusIsClean) {
let gitStatus = await git.status()
let gitDiff = await git.diff()
console.log("git status")
console.log(gitStatus)
console.log("git diff")
console.log(gitDiff)
throw new Error("Generated swagger file does not match swagger file on disk")
}
// Verify generated swagger file is in sync with one on disk
const git = simpleGit();
let gitStatusIsClean = await (await git.status(["--porcelain"])).isClean();
if (!gitStatusIsClean) {
let gitStatus = await git.status();
let gitDiff = await git.diff();
console.log("git status");
console.log(gitStatus);
console.log("git diff");
console.log(gitDiff);
throw new Error("Generated swagger file does not match swagger file on disk");
}
}

0 comments on commit 310a010

Please sign in to comment.