Skip to content

Commit

Permalink
feat(babelrc): included the file extension on the file for better edi…
Browse files Browse the repository at this point in the history
…tor support

and leveraged the new config-file writer to simplify the details
  • Loading branch information
travi committed Jun 3, 2022
1 parent 18daef0 commit 8f64272
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 27 deletions.
47 changes: 33 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
"dependencies": {
"@form8ion/codecov": "^3.0.0",
"@form8ion/commit-convention": "^2.0.0",
"@form8ion/config-file": "^1.0.0-alpha.1",
"@form8ion/core": "^1.5.0",
"@form8ion/eslint": "^2.0.1",
"@form8ion/husky": "^2.3.0",
Expand Down
19 changes: 13 additions & 6 deletions src/scaffolder/dialects/babel-test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import {promises as fs} from 'fs';
import {fileTypes} from '@form8ion/core';
import * as configFile from '@form8ion/config-file';

import sinon from 'sinon';
import {assert} from 'chai';
import any from '@travi/any';

import scaffoldBabel from './babel';

suite('babel config', () => {
Expand All @@ -12,7 +15,7 @@ suite('babel config', () => {
setup(() => {
sandbox = sinon.createSandbox();

sandbox.stub(fs, 'writeFile');
sandbox.stub(configFile, 'write');
});

teardown(() => sandbox.restore());
Expand All @@ -28,9 +31,13 @@ suite('babel config', () => {
);

assert.calledWith(
fs.writeFile,
`${projectRoot}/.babelrc`,
JSON.stringify({presets: [babelPresetName], ignore: [`./${buildDirectory}/`]})
configFile.write,
{
path: projectRoot,
name: 'babel',
format: fileTypes.JSON,
config: {presets: [babelPresetName], ignore: [`./${buildDirectory}/`]}
}
);
});

Expand All @@ -40,7 +47,7 @@ suite('babel config', () => {

throw new Error('test should have failed, but didnt');
} catch (e) {
assert.notCalled(fs.writeFile);
assert.notCalled(configFile.write);
assert.equal(e.message, 'No babel preset provided. Cannot configure babel transpilation');
}
});
Expand Down
13 changes: 8 additions & 5 deletions src/scaffolder/dialects/babel.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import {promises as fsPromises} from 'fs';
import {write} from '@form8ion/config-file';
import {fileTypes} from '@form8ion/core';

export default async function ({projectRoot, preset, buildDirectory}) {
if (!preset) {
throw new Error('No babel preset provided. Cannot configure babel transpilation');
}

await fsPromises.writeFile(
`${projectRoot}/.babelrc`,
JSON.stringify({presets: [preset.name], ignore: [`./${buildDirectory}/`]})
);
await write({
path: projectRoot,
name: 'babel',
format: fileTypes.JSON,
config: {presets: [preset.name], ignore: [`./${buildDirectory}/`]}
});

return {
devDependencies: ['@babel/register', preset.packageName],
Expand Down
4 changes: 2 additions & 2 deletions test/integration/features/step_definitions/dialect-steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import any from '@travi/any';
import {assertDevDependencyIsInstalled} from './common-steps';

async function assertBabelIsNotConfigured() {
assert.isFalse(await fileExists(`${process.cwd()}/.babelrc`));
assert.isFalse(await fileExists(`${process.cwd()}/.babelrc.json`));
}

async function assertBabelDialectDetailsAreCorrect(babelPreset, buildDirectory, execa) {
const [babelRcContents, packageContents] = await Promise.all([
fs.readFile(`${process.cwd()}/.babelrc`, 'utf-8'),
fs.readFile(`${process.cwd()}/.babelrc.json`, 'utf-8'),
fs.readFile(`${process.cwd()}/package.json`, 'utf-8')
]);
const {presets, ignore} = JSON.parse(babelRcContents);
Expand Down

0 comments on commit 8f64272

Please sign in to comment.