Skip to content

Commit 4bae940

Browse files
committed
fix(example): prevent error in initial execution of generated example
1 parent 7c3db4c commit 4bae940

File tree

4 files changed

+16
-53
lines changed

4 files changed

+16
-53
lines changed

src/project-type/package/build-details.js

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,28 @@
1-
import {promises as fs} from 'fs';
1+
import {promises as fs} from 'node:fs';
22
import deepmerge from 'deepmerge';
3-
import mustache from 'mustache';
43
import touch from 'touch';
5-
import camelcase from 'camelcase';
64
import {dialects, projectTypes} from '@form8ion/javascript-core';
75

86
import {scaffold as scaffoldBundler} from '../publishable/bundler/index.js';
9-
import determinePathToTemplateFile from '../../template-path.js';
107

118
const defaultBuildDirectory = 'lib';
129

13-
async function createExample(projectRoot, projectName, dialect) {
14-
return fs.writeFile(
15-
`${projectRoot}/example.js`,
16-
mustache.render(
17-
await fs.readFile(determinePathToTemplateFile('example.mustache'), 'utf8'),
18-
{projectName: camelcase(projectName), esm: dialect === dialects.ESM}
19-
)
20-
);
10+
async function createExample(projectRoot) {
11+
return fs.writeFile(`${projectRoot}/example.js`, "import {} from './lib/index.js';\n");
2112
}
2213

23-
async function buildDetailsForCommonJsProject({projectRoot, projectName, provideExample}) {
14+
async function buildDetailsForCommonJsProject({projectRoot, provideExample}) {
2415
await Promise.all([
2516
touch(`${projectRoot}/index.js`),
2617
provideExample
27-
? fs.writeFile(`${projectRoot}/example.js`, `const ${camelcase(projectName)} = require('.');\n`)
18+
? fs.writeFile(`${projectRoot}/example.js`, "const {} = require('.');\n")
2819
: Promise.resolve()
2920
]);
3021

3122
return {};
3223
}
3324

34-
export default async function ({
25+
export default async function buildDetails({
3526
projectRoot,
3627
projectName,
3728
visibility,
@@ -41,7 +32,7 @@ export default async function ({
4132
provideExample,
4233
decisions
4334
}) {
44-
if (dialects.COMMON_JS === dialect) return buildDetailsForCommonJsProject({projectRoot, projectName, provideExample});
35+
if (dialects.COMMON_JS === dialect) return buildDetailsForCommonJsProject({projectRoot, provideExample});
4536

4637
await fs.mkdir(`${projectRoot}/src`, {recursive: true});
4738
const [bundlerResults] = await Promise.all([

src/project-type/package/build-details.test.js

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,27 @@
1-
import {promises as fs} from 'fs';
1+
import {promises as fs} from 'node:fs';
22
import touch from 'touch';
3-
import camelcase from 'camelcase';
4-
import mustache from 'mustache';
53
import {dialects, projectTypes} from '@form8ion/javascript-core';
64

7-
import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
5+
import {describe, expect, it, vi} from 'vitest';
86
import any from '@travi/any';
97
import {when} from 'jest-when';
108

11-
import templatePath from '../../template-path.js';
129
import {scaffold as scaffoldBundler} from '../publishable/bundler/index.js';
1310
import buildDetails from './build-details.js';
1411

15-
vi.mock('fs');
12+
vi.mock('node:fs');
1613
vi.mock('make-dir');
17-
vi.mock('camelcase');
18-
vi.mock('mustache');
1914
vi.mock('touch');
20-
vi.mock('../../template-path');
2115
vi.mock('../publishable/bundler');
2216

2317
describe('package build details', () => {
2418
const projectRoot = any.string();
2519
const projectName = any.word();
2620
const pathToExample = `${projectRoot}/example.js`;
2721
const bundlerResults = any.simpleObject();
28-
const exampleContent = any.string();
29-
const pathToExampleTemplate = any.string();
30-
const exampleTemplateContent = any.string();
31-
const camelizedProjectName = any.word();
3222
const packageBundlers = any.simpleObject();
3323
const decisions = any.simpleObject();
3424

35-
beforeEach(() => {
36-
when(templatePath).calledWith('example.mustache').mockReturnValue(pathToExampleTemplate);
37-
when(fs.readFile).calledWith(pathToExampleTemplate, 'utf8').mockResolvedValue(exampleTemplateContent);
38-
when(camelcase).calledWith(projectName).mockReturnValue(camelizedProjectName);
39-
});
40-
41-
afterEach(() => {
42-
vi.clearAllMocks();
43-
});
44-
4525
it('should correctly define a common-js project', async () => {
4626
const results = await buildDetails({
4727
dialect: dialects.COMMON_JS,
@@ -51,7 +31,7 @@ describe('package build details', () => {
5131
});
5232

5333
expect(results).toEqual({});
54-
expect(fs.writeFile).toHaveBeenCalledWith(pathToExample, `const ${camelizedProjectName} = require('.');\n`);
34+
expect(fs.writeFile).toHaveBeenCalledWith(pathToExample, "const {} = require('.');\n");
5535
expect(touch).toHaveBeenCalledWith(`${projectRoot}/index.js`);
5636
});
5737

@@ -71,9 +51,6 @@ describe('package build details', () => {
7151
when(scaffoldBundler)
7252
.calledWith({bundlers: packageBundlers, decisions, projectRoot, dialect, projectType: projectTypes.PACKAGE})
7353
.mockResolvedValue(bundlerResults);
74-
when(mustache.render)
75-
.calledWith(exampleTemplateContent, {projectName: camelizedProjectName, esm: false})
76-
.mockReturnValue(exampleContent);
7754

7855
const results = await buildDetails({
7956
dialect,
@@ -100,7 +77,7 @@ describe('package build details', () => {
10077
});
10178
expect(fs.mkdir).toHaveBeenCalledWith(`${projectRoot}/src`, {recursive: true});
10279
expect(touch).toHaveBeenCalledWith(`${projectRoot}/src/index.js`);
103-
expect(fs.writeFile).toHaveBeenCalledWith(pathToExample, exampleContent);
80+
expect(fs.writeFile).toHaveBeenCalledWith(pathToExample, "import {} from './lib/index.js';\n");
10481
});
10582

10683
it('should not create the example file for a modern-js project when `provideExample` is `false`', async () => {

templates/example.mustache

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
// remark-usage-ignore-next
2-
/* eslint-disable-next-line no-unused-vars */
3-
import {{ projectName }} from './lib/index.js';
1+
import {} from './lib/index.js';

test/integration/features/step_definitions/documentation-steps.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import {promises as fs} from 'fs';
2-
import camelcase from 'camelcase';
1+
import {promises as fs} from 'node:fs';
32
import {fileExists} from '@form8ion/core';
43
import {dialects, packageManagers, projectTypes} from '@form8ion/javascript-core';
54

@@ -19,16 +18,14 @@ export async function assertThatDocumentationIsDefinedAppropriately(
1918
if (projectTypes.PACKAGE === projectType && exampleShouldBeProvided && dialects.COMMON_JS === dialect) {
2019
const exampleContents = (await fs.readFile(pathToExampleFile)).toString();
2120

22-
assert.equal(exampleContents, `const ${camelcase(projectName)} = require('.');\n`);
21+
assert.equal(exampleContents, "const {} = require('.');\n");
2322
assert.isTrue(await fileExists(`${process.cwd()}/index.js`));
2423
assert.isDefined(packageDetails.scripts['generate:md']);
2524
assert.isUndefined(packageDetails.scripts['pregenerate:md']);
2625
} else if (projectTypes.PACKAGE === projectType && exampleShouldBeProvided) {
2726
const exampleContents = (await fs.readFile(pathToExampleFile)).toString();
2827

29-
assert.equal(exampleContents, `// remark-usage-ignore-next
30-
/* eslint-disable-next-line no-unused-vars */
31-
import ${camelcase(projectName)} from './lib/index.js';
28+
assert.equal(exampleContents, `import {} from './lib/index.js';
3229
`);
3330
assert.isTrue(await fileExists(`${projectRoot}/src/index.js`));
3431
assert.isDefined(packageDetails.scripts['generate:md']);

0 commit comments

Comments
 (0)