Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add format option to gen files #324

Merged
merged 3 commits into from
May 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# changelog

## 0.55.1

- add `format` option to gen files, defaults to `true`
([#324](https://github.com/feltcoop/gro/pull/324))

## 0.55.0

- **break**: remove the dev server, including `gro serve` and project metadata
Expand Down
1 change: 1 addition & 0 deletions src/docs/gen.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ export const gen: Gen = () => {
return {
content: `console.log('${message}')`,
filename: '../elsewhere/otherName.ts',
format: optional_boolean_that_defaults_to_true,
};
};
```
Expand Down
3 changes: 2 additions & 1 deletion src/format/formatFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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': {
Expand Down
35 changes: 20 additions & 15 deletions src/gen/gen.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}],
});
});

Expand All @@ -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}],
},
);
});
Expand All @@ -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}],
},
);
});
Expand Down Expand Up @@ -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,
},
],
});
Expand All @@ -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}],
},
);
});
Expand All @@ -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,
},
],
});
Expand All @@ -127,6 +129,7 @@ test__toGenResult('implicit empty file extension', () => {
id: resolve('src/foo'),
content: '[/**/]',
originId: resolve('src/foo.gen..ts'),
format: true,
},
],
});
Expand All @@ -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,
},
],
});
Expand All @@ -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,
},
],
});
Expand Down Expand Up @@ -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}],
},
);
});
Expand All @@ -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}],
},
);
});
Expand All @@ -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}],
},
);
});
Expand All @@ -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},
],
},
);
Expand All @@ -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},
],
},
);
Expand Down
6 changes: 4 additions & 2 deletions src/gen/gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface GenFile {
id: string;
content: string;
originId: string;
format: boolean;
}

export interface Gen {
Expand All @@ -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 = {
Expand Down Expand Up @@ -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 => {
Expand Down
5 changes: 5 additions & 0 deletions src/gen/runGen.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ test__gen('basic behavior', async () => {
content: fileA.content,
id: join(modA.id, '../', fileA.filename),
originId: modA.id,
format: true,
},
]);

Expand All @@ -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];
Expand All @@ -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,
},
]);
});
Expand Down Expand Up @@ -177,6 +181,7 @@ test__gen('failing gen function', async () => {
content: fileB.content,
id: join(modB.id, '../', fileB.filename),
originId: modB.id,
format: true,
},
]);
});
Expand Down
1 change: 1 addition & 0 deletions src/gen/runGen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down