Skip to content

Commit 1010a2d

Browse files
committed
Add script to test that prettier examples are transformed properly
1 parent bc04748 commit 1010a2d

8 files changed

Lines changed: 155 additions & 3 deletions

File tree

examples/bin/test.mjs

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
#!/usr/bin/env node
2+
import chalk from 'chalk';
3+
import { spawn } from 'child_process';
4+
import fs from 'fs/promises';
5+
import path from 'path';
6+
7+
const debug = process.argv.includes('--debug');
8+
9+
const logDebug = (message) => {
10+
if (debug) {
11+
console.log(`[${chalk.blue('debug')}]`, message);
12+
}
13+
};
14+
15+
const newLine = () => console.log();
16+
17+
const inputDir = './input';
18+
const expectedOutputDir = './expected-output';
19+
20+
const run = async () => {
21+
logDebug('Reading input directory...');
22+
23+
try {
24+
const inputFiles = await fs.readdir(inputDir);
25+
const originalFiles = new Map();
26+
27+
logDebug('Backing up input files...');
28+
for (const file of inputFiles) {
29+
const filePath = path.join(inputDir, file);
30+
const fileContent = await fs.readFile(filePath, 'utf-8');
31+
originalFiles.set(file, fileContent);
32+
}
33+
34+
if (debug) {
35+
newLine();
36+
}
37+
console.log('💅 Running Prettier...');
38+
const prettier = spawn(
39+
'prettier',
40+
[
41+
'.',
42+
'--write',
43+
'--plugin',
44+
'prettier-plugin-ember-template-tag',
45+
debug ? '--log-level' : null,
46+
debug ? 'debug' : null,
47+
].filter(Boolean),
48+
{
49+
env: {
50+
...process.env,
51+
FORCE_COLOR: true,
52+
},
53+
},
54+
);
55+
56+
prettier.stdout.pipe(process.stdout);
57+
prettier.stderr.pipe(process.stderr);
58+
59+
prettier.on('close', async (code) => {
60+
if (code !== 0) {
61+
console.error(chalk.red(`💩 Prettier exited with code ${code}`));
62+
process.exit(code);
63+
}
64+
65+
newLine();
66+
console.log('👯 Comparing files...');
67+
68+
let allFilesMatch = true;
69+
70+
for (const file of inputFiles) {
71+
const inputFilePath = path.join(inputDir, file);
72+
const expectedOutputFilePath = path.join(expectedOutputDir, file);
73+
74+
try {
75+
const [inputContent, expectedOutputContent] = await Promise.all([
76+
fs.readFile(inputFilePath, 'utf-8'),
77+
fs.readFile(expectedOutputFilePath, 'utf-8'),
78+
]);
79+
80+
if (inputContent === expectedOutputContent) {
81+
console.log(
82+
chalk.green('Pass'),
83+
`${file}: Formatted content matches expected output.`,
84+
);
85+
logDebug(`Resetting ${file}...`);
86+
await fs.writeFile(inputFilePath, originalFiles.get(file));
87+
} else {
88+
console.log(
89+
chalk.red('Fail'),
90+
`${file}: Formatted content does not match expected output.`,
91+
);
92+
allFilesMatch = false;
93+
}
94+
} catch (err) {
95+
console.error(chalk.red(`Error processing ${file}:`), err);
96+
allFilesMatch = false;
97+
}
98+
}
99+
100+
newLine();
101+
102+
if (allFilesMatch) {
103+
console.log('👋 Exiting...', chalk.green('SUCCESS'));
104+
process.exit(0);
105+
} else {
106+
console.log('👋 Exiting...', chalk.red('FAIL'));
107+
process.exit(1);
108+
}
109+
});
110+
} catch (err) {
111+
console.error(chalk.red('💩 Error:'), err);
112+
process.exit(2);
113+
}
114+
};
115+
116+
run();
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import Component from '@glimmer/component';
2+
3+
/**
4+
* An example GJS file on which we can run the Prettier for GJS plugin.
5+
*/
6+
class MyComponent extends Component {
7+
<template>
8+
<h1>
9+
Class top level template. Class top level template. Class top level
10+
template. Class top level template. Class top level template.
11+
</h1>
12+
</template>
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import type { TemplateOnlyComponent } from '@ember/component/template-only';
2+
3+
export interface Signature {
4+
Element: HTMLElement;
5+
Args: {};
6+
Yields: [];
7+
}
8+
9+
<template>
10+
Explicit default export module top level component. Explicit default export
11+
module top level component. Explicit default export module top level
12+
component. Explicit default export module top level component. Explicit
13+
default export module top level component.
14+
</template> as TemplateOnlyComponent<Signature>

examples/package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33
"private": true,
44
"version": "0.0.0",
55
"scripts": {
6-
"example": "pnpm exec prettier . --write --plugin prettier-plugin-ember-template-tag --log-level debug",
7-
"example-debug": "pnpm exec prettier . --write --plugin prettier-plugin-ember-template-tag --log-level debug"
6+
"example": "pnpm exec prettier . --write --plugin prettier-plugin-ember-template-tag",
7+
"example-debug": "pnpm exec prettier . --write --plugin prettier-plugin-ember-template-tag --log-level debug",
8+
"test": "./bin/test.mjs"
89
},
910
"dependencies": {
1011
"prettier": "^3.0.3",
1112
"prettier-plugin-ember-template-tag": "workspace:^"
13+
},
14+
"devDependencies": {
15+
"chalk": "^5.3.0"
1216
}
1317
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@
4242
"release": "release-it",
4343
"release:ci": "release-it --ci",
4444
"release:debug": "release-it --verbose --dry-run",
45-
"test:all": "concurrently \"pnpm:test:run\" \"pnpm:example\" \"pnpm:example-ts\"",
45+
"test:all": "concurrently \"pnpm:test:run\" \"pnpm:test:example\"",
4646
"test": "vitest",
47+
"test:example": "pnpm preexample && cd examples && pnpm test",
4748
"test:ui": "vitest --ui",
4849
"test:run": "vitest run"
4950
},

pnpm-lock.yaml

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)