Skip to content

Commit ac55225

Browse files
authored
Merge pull request #124 from gitKrystan/prettier-write
2 parents e97eb70 + e69ad3f commit ac55225

9 files changed

Lines changed: 159 additions & 5 deletions

File tree

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ Prettier 3.0.0 and above
5353

5454
_If you already have a `"prettier"` section in `package.json`, remember that takes precedence over the `.prettierrc.js` file!_
5555

56-
1. Run `npm prettier --write .`
56+
1. Run `npm prettier --write . --plugin prettier-plugin-ember-template-tag`
57+
58+
See <https://github.com/gitKrystan/prettier-plugin-ember-template-tag/issues/113> and <https://github.com/prettier/prettier/issues/15351> for details on why using the `--plugin` flag is required here.
5759

5860
## Opinions
5961

examples/bin/test.mjs

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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+
// NOTE: Run with `--debug` to get debug output (from both this script and prettier)
21+
const run = async () => {
22+
logDebug('Reading input directory...');
23+
24+
try {
25+
const inputFiles = await fs.readdir(inputDir);
26+
const originalFiles = new Map();
27+
28+
logDebug('Backing up input files...');
29+
for (const file of inputFiles) {
30+
const filePath = path.join(inputDir, file);
31+
const fileContent = await fs.readFile(filePath, 'utf-8');
32+
originalFiles.set(file, fileContent);
33+
}
34+
35+
if (debug) {
36+
newLine();
37+
}
38+
console.log('💅 Running Prettier...');
39+
const prettier = spawn(
40+
'prettier',
41+
[
42+
'.',
43+
'--write',
44+
'--plugin',
45+
'prettier-plugin-ember-template-tag',
46+
debug ? '--log-level' : null,
47+
debug ? 'debug' : null,
48+
].filter(Boolean),
49+
{
50+
env: {
51+
...process.env,
52+
FORCE_COLOR: true,
53+
},
54+
},
55+
);
56+
57+
prettier.stdout.pipe(process.stdout);
58+
prettier.stderr.pipe(process.stderr);
59+
60+
prettier.on('close', async (code) => {
61+
if (code !== 0) {
62+
console.error(chalk.red(`💩 Prettier exited with code ${code}`));
63+
process.exit(code);
64+
}
65+
66+
newLine();
67+
console.log('👯 Comparing files...');
68+
69+
let allFilesMatch = true;
70+
71+
for (const file of inputFiles) {
72+
const inputFilePath = path.join(inputDir, file);
73+
const expectedOutputFilePath = path.join(expectedOutputDir, file);
74+
75+
try {
76+
const [inputContent, expectedOutputContent] = await Promise.all([
77+
fs.readFile(inputFilePath, 'utf-8'),
78+
fs.readFile(expectedOutputFilePath, 'utf-8'),
79+
]);
80+
81+
if (inputContent === expectedOutputContent) {
82+
console.log(
83+
chalk.green('Pass'),
84+
`${file}: Formatted content matches expected output.`,
85+
);
86+
logDebug(`Resetting ${file}...`);
87+
await fs.writeFile(inputFilePath, originalFiles.get(file));
88+
} else {
89+
console.log(
90+
chalk.red('Fail'),
91+
`${file}: Formatted content does not match expected output.`,
92+
);
93+
allFilesMatch = false;
94+
}
95+
} catch (err) {
96+
console.error(chalk.red(`Error processing ${file}:`), err);
97+
allFilesMatch = false;
98+
}
99+
}
100+
101+
newLine();
102+
103+
if (allFilesMatch) {
104+
console.log('👋 Exiting...', chalk.green('SUCCESS'));
105+
process.exit(0);
106+
} else {
107+
console.log('👋 Exiting...', chalk.red('FAIL'));
108+
process.exit(1);
109+
}
110+
});
111+
} catch (err) {
112+
console.error(chalk.red('💩 Error:'), err);
113+
process.exit(2);
114+
}
115+
};
116+
117+
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 ./example.gjs",
7-
"example-ts": "pnpm exec prettier --write ./example.gts"
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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
"build": "tsc && vite build",
3333
"preexample": "vite build",
3434
"example": "pnpm preexample && cd examples && pnpm example",
35-
"example-ts": "pnpm preexample && cd examples && pnpm example-ts",
3635
"lint": "concurrently \"pnpm:lint:*(!fix)\" --names \"lint:\"",
3736
"lint:fix": "concurrently \"pnpm:lint:*:fix\" --names \"fix:\"",
3837
"lint:eslint": "eslint . --cache",
@@ -43,8 +42,9 @@
4342
"release": "release-it",
4443
"release:ci": "release-it --ci",
4544
"release:debug": "release-it --verbose --dry-run",
46-
"test:all": "concurrently \"pnpm:test:run\" \"pnpm:example\" \"pnpm:example-ts\"",
45+
"test:all": "concurrently \"pnpm:test:run\" \"pnpm:test:example\"",
4746
"test": "vitest",
47+
"test:example": "pnpm preexample && cd examples && pnpm test",
4848
"test:ui": "vitest --ui",
4949
"test:run": "vitest run"
5050
},

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)