From 570d86e0fefeb4dcaef2f2e9235c16f6d5243d62 Mon Sep 17 00:00:00 2001 From: Ryan Atkinson Date: Mon, 9 May 2022 15:47:49 -0500 Subject: [PATCH 1/3] add format option to gen files --- changelog.md | 5 +++++ src/docs/gen.md | 1 + src/format/formatFile.ts | 3 ++- src/gen/gen.ts | 6 ++++-- src/gen/runGen.ts | 1 + 5 files changed, 13 insertions(+), 3 deletions(-) diff --git a/changelog.md b/changelog.md index 7fa8bcd8c1..8b167f2481 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,10 @@ # changelog +## 0.55.1 + +- add `format` option to gen files, defaults to `true` + ([#322](https://github.com/feltcoop/gro/pull/322)) + ## 0.55.0 - **break**: remove the dev server, including `gro serve` and project metadata diff --git a/src/docs/gen.md b/src/docs/gen.md index ddf01d90fe..1373c10f91 100644 --- a/src/docs/gen.md +++ b/src/docs/gen.md @@ -200,6 +200,7 @@ export const gen: Gen = () => { return { content: `console.log('${message}')`, filename: '../elsewhere/otherName.ts', + format: optional_boolean_that_defaults_to_true, }; }; ``` diff --git a/src/format/formatFile.ts b/src/format/formatFile.ts index c9fb0e9d15..ccbdd77bbe 100644 --- a/src/format/formatFile.ts +++ b/src/format/formatFile.ts @@ -25,7 +25,8 @@ const inferParser = (id: string): prettier.BuiltInParserName | null => { case 'css': { return extension; } - case 'svelte': { + case 'svelte': + case 'xml': { return extension as any; } case 'md': { diff --git a/src/gen/gen.ts b/src/gen/gen.ts index 9977f18f7d..0109a465e2 100644 --- a/src/gen/gen.ts +++ b/src/gen/gen.ts @@ -13,6 +13,7 @@ export interface GenFile { id: string; content: string; originId: string; + format: boolean; } export interface Gen { @@ -30,6 +31,7 @@ export interface RawGenFile { // Defaults to file name without the `.gen` or `.schema`, and can be a relative path. // TODO maybe support a transform pattern or callback fn? like '[stem].thing.[ext]' filename?: string; + format?: boolean; // defaults to `true` } export type GenResults = { @@ -77,9 +79,9 @@ const toGenFiles = (originId: string, rawResult: RawGenResult): GenFile[] => { }; const toGenFile = (originId: string, rawGenFile: RawGenFile): GenFile => { - const {content, filename} = rawGenFile; + const {content, filename, format = true} = rawGenFile; const id = toOutputFileId(originId, filename); - return {id, content, originId}; + return {id, content, originId, format}; }; const toOutputFileId = (originId: string, rawFileName: string | undefined): string => { diff --git a/src/gen/runGen.ts b/src/gen/runGen.ts index 78a5916d90..257545779c 100644 --- a/src/gen/runGen.ts +++ b/src/gen/runGen.ts @@ -71,6 +71,7 @@ export const runGen = async ( const files = formatFile ? await Promise.all( genResult.files.map(async (file) => { + if (!file.format) return file; try { return {...file, content: await formatFile(fs, file.id, file.content)}; } catch (err) { From d8ef6a638a7a879f4ba65e484abe884e5632731b Mon Sep 17 00:00:00 2001 From: Ryan Atkinson Date: Mon, 9 May 2022 15:48:59 -0500 Subject: [PATCH 2/3] fix changelog --- changelog.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog.md b/changelog.md index 8b167f2481..3a0866f0e4 100644 --- a/changelog.md +++ b/changelog.md @@ -3,7 +3,7 @@ ## 0.55.1 - add `format` option to gen files, defaults to `true` - ([#322](https://github.com/feltcoop/gro/pull/322)) + ([#324](https://github.com/feltcoop/gro/pull/324)) ## 0.55.0 From 19ff1781932645346cd6269671a0838357bd37ba Mon Sep 17 00:00:00 2001 From: Ryan Atkinson Date: Mon, 9 May 2022 15:56:17 -0500 Subject: [PATCH 3/3] fix tests --- src/gen/gen.test.ts | 35 ++++++++++++++++++++--------------- src/gen/runGen.test.ts | 5 +++++ 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/src/gen/gen.test.ts b/src/gen/gen.test.ts index c87fb58eae..8e669a2e16 100644 --- a/src/gen/gen.test.ts +++ b/src/gen/gen.test.ts @@ -12,14 +12,14 @@ const test__toGenResult = suite('toGenResult'); test__toGenResult('plain string', () => { assert.equal(toGenResult(originId, '/**/'), { originId, - files: [{id: resolve('src/foo.ts'), content: '/**/', originId}], + files: [{id: resolve('src/foo.ts'), content: '/**/', originId, format: true}], }); }); test__toGenResult('object with a content string', () => { assert.equal(toGenResult(originId, {content: '/**/'}), { originId, - files: [{id: resolve('src/foo.ts'), content: '/**/', originId}], + files: [{id: resolve('src/foo.ts'), content: '/**/', originId, format: true}], }); }); @@ -43,7 +43,7 @@ test__toGenResult('custom file name', () => { }), { originId, - files: [{id: resolve('src/fooz.ts'), content: '/**/', originId}], + files: [{id: resolve('src/fooz.ts'), content: '/**/', originId, format: true}], }, ); }); @@ -56,7 +56,7 @@ test__toGenResult('custom file name that matches the default file name', () => { }), { originId, - files: [{id: resolve('src/foo.ts'), content: '/**/', originId}], + files: [{id: resolve('src/foo.ts'), content: '/**/', originId, format: true}], }, ); }); @@ -88,6 +88,7 @@ test__toGenResult('additional file name parts', () => { id: resolve('src/foo.bar.ts'), content: '/**/', originId: resolve('src/foo.bar.gen.ts'), + format: true, }, ], }); @@ -101,7 +102,7 @@ test__toGenResult('js', () => { }), { originId, - files: [{id: resolve('src/foo.js'), content: '/**/', originId}], + files: [{id: resolve('src/foo.js'), content: '/**/', originId, format: true}], }, ); }); @@ -114,6 +115,7 @@ test__toGenResult('implicit custom file extension', () => { id: resolve('src/foo.json'), content: '[/**/]', originId: resolve('src/foo.gen.json.ts'), + format: true, }, ], }); @@ -127,6 +129,7 @@ test__toGenResult('implicit empty file extension', () => { id: resolve('src/foo'), content: '[/**/]', originId: resolve('src/foo.gen..ts'), + format: true, }, ], }); @@ -140,6 +143,7 @@ test__toGenResult('implicit custom file extension with additional file name part id: resolve('src/foo.bar.json'), content: '[/**/]', originId: resolve('src/foo.bar.gen.json.ts'), + format: true, }, ], }); @@ -153,6 +157,7 @@ test__toGenResult('implicit custom file extension with many dots in between', () id: resolve('src/foo...ts'), content: '[/**/]', originId: resolve('src/foo...gen.ts'), + format: true, }, ], }); @@ -203,7 +208,7 @@ test__toGenResult('explicit custom file extension', () => { }), { originId, - files: [{id: resolve('src/foo.json'), content: '[/**/]', originId}], + files: [{id: resolve('src/foo.json'), content: '[/**/]', originId, format: true}], }, ); }); @@ -216,7 +221,7 @@ test__toGenResult('explicit custom empty file extension', () => { }), { originId, - files: [{id: resolve('src/foo'), content: '[/**/]', originId}], + files: [{id: resolve('src/foo'), content: '[/**/]', originId, format: true}], }, ); }); @@ -229,7 +234,7 @@ test__toGenResult('explicit custom file extension ending with a dot', () => { }), { originId, - files: [{id: resolve('src/foo.'), content: '[/**/]', originId}], + files: [{id: resolve('src/foo.'), content: '[/**/]', originId, format: true}], }, ); }); @@ -240,8 +245,8 @@ test__toGenResult('simple array of raw files', () => { { originId, files: [ - {id: resolve('src/foo.ts'), content: '/*1*/', originId}, - {id: resolve('src/foo2.ts'), content: '/*2*/', originId}, + {id: resolve('src/foo.ts'), content: '/*1*/', originId, format: true}, + {id: resolve('src/foo2.ts'), content: '/*2*/', originId, format: true}, ], }, ); @@ -259,11 +264,11 @@ test__toGenResult('complex array of raw files', () => { { originId, files: [ - {id: resolve('src/foo.ts'), content: '/*1*/', originId}, - {id: resolve('src/foo2.ts'), content: '/*2*/', originId}, - {id: resolve('src/foo3.ts'), content: '/*3*/', originId}, - {id: resolve('src/foo4.ts'), content: '/*4*/', originId}, - {id: resolve('src/foo5.json'), content: '[/*5*/]', originId}, + {id: resolve('src/foo.ts'), content: '/*1*/', originId, format: true}, + {id: resolve('src/foo2.ts'), content: '/*2*/', originId, format: true}, + {id: resolve('src/foo3.ts'), content: '/*3*/', originId, format: true}, + {id: resolve('src/foo4.ts'), content: '/*4*/', originId, format: true}, + {id: resolve('src/foo5.json'), content: '[/*5*/]', originId, format: true}, ], }, ); diff --git a/src/gen/runGen.test.ts b/src/gen/runGen.test.ts index bdbc0d418c..03e916aae1 100644 --- a/src/gen/runGen.test.ts +++ b/src/gen/runGen.test.ts @@ -90,6 +90,7 @@ test__gen('basic behavior', async () => { content: fileA.content, id: join(modA.id, '../', fileA.filename), originId: modA.id, + format: true, }, ]); @@ -101,6 +102,7 @@ test__gen('basic behavior', async () => { content: `${fileB.content}/*FORMATTED*/`, id: join(modB.id, '../', fileB.filename), originId: modB.id, + format: true, }, ]); const resultC = genResults.results[2]; @@ -112,11 +114,13 @@ test__gen('basic behavior', async () => { content: fileC1.content, id: join(modC.id, '../', fileC1.filename), originId: modC.id, + format: true, }, { content: fileC2.content, id: join(modC.id, '../', fileC2.filename), originId: modC.id, + format: true, }, ]); }); @@ -177,6 +181,7 @@ test__gen('failing gen function', async () => { content: fileB.content, id: join(modB.id, '../', fileB.filename), originId: modB.id, + format: true, }, ]); });