Skip to content

Commit 18ca0a4

Browse files
committed
refactor(addon-verify): nit
1 parent ba071f5 commit 18ca0a4

File tree

4 files changed

+112
-68
lines changed

4 files changed

+112
-68
lines changed

src/generators/addon-verify/index.mjs

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,13 @@ import { join } from 'node:path';
55

66
import { visit } from 'unist-util-visit';
77

8-
import { updateFilesForBuild } from './utils/updateFilesForBuild.mjs';
8+
import { generateFileList } from './utils/generateFileList.mjs';
99
import { EXTRACT_CODE_FILENAME_COMMENT } from './constants.mjs';
10-
11-
/**
12-
* Normalizes a section name.
13-
*
14-
* @param {string} sectionName Section name
15-
* @returns {string}
16-
*/
17-
export function normalizeSectionName(sectionName) {
18-
return sectionName.toLowerCase().replace(/\s/g, '_').replace(/\W/g, '');
19-
}
10+
import {
11+
generateSectionFolderName,
12+
isBuildableSection,
13+
normalizeSectionName,
14+
} from './utils/section.mjs';
2015

2116
/**
2217
* This generator generates a file list from code blocks extracted from
@@ -73,31 +68,26 @@ export default {
7368

7469
const files = await Promise.all(
7570
Object.entries(sectionsCodeBlocks)
76-
.filter(([, files]) => {
77-
// Must have a .cc and a .js to be a valid test.
78-
return (
79-
files.some(file => file.name.endsWith('.cc')) &&
80-
files.some(file => file.name.endsWith('.js'))
81-
);
82-
})
83-
.flatMap(async ([sectionName, files], index) => {
84-
const newFiles = updateFilesForBuild(files);
71+
.filter(([, codeBlocks]) => isBuildableSection(codeBlocks))
72+
.flatMap(async ([sectionName, codeBlocks], index) => {
73+
const files = generateFileList(codeBlocks);
8574

8675
if (output) {
8776
const normalizedSectionName = normalizeSectionName(sectionName);
8877

89-
const identifier = String(index + 1).padStart(2, '0');
90-
91-
const folder = `${identifier}_${normalizedSectionName}`;
78+
const folderName = generateSectionFolderName(
79+
normalizedSectionName,
80+
index
81+
);
9282

93-
await mkdir(join(output, folder), { recursive: true });
83+
await mkdir(join(output, folderName), { recursive: true });
9484

95-
newFiles.forEach(async ({ name, content }) => {
96-
await writeFile(join(output, folder, name), content);
85+
files.forEach(async ({ name, content }) => {
86+
await writeFile(join(output, folderName, name), content);
9787
});
9888
}
9989

100-
return newFiles;
90+
return files;
10191
})
10292
);
10393

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* Updates JavaScript files with correct require paths for the build tree.
3+
*
4+
* @param {string} content Original code
5+
* @returns {string}
6+
*/
7+
const updateJsRequirePaths = content => {
8+
return `'use strict';
9+
const common = require('../../common');
10+
${content.replace(
11+
"'./build/Release/addon'",
12+
'`./build/${common.buildType}/addon`'
13+
)}
14+
`;
15+
};
16+
17+
/**
18+
* Creates a binding.gyp configuration for C++ addon compilation.
19+
*
20+
* @param {string[]} sourceFiles List of source file names
21+
* @returns {string}
22+
*/
23+
const createBindingGyp = sourceFiles => {
24+
const config = {
25+
targets: [
26+
{
27+
target_name: 'addon',
28+
sources: sourceFiles,
29+
includes: ['../common.gypi'],
30+
},
31+
],
32+
};
33+
34+
return JSON.stringify(config);
35+
};
36+
37+
/**
38+
* Generates required files list from section's code blocks for C++ addon
39+
* compilation.
40+
*
41+
* @param {{name: string, content: string}[]} codeBlocks Array of code blocks
42+
* @returns {{name: string, content: string}[]}
43+
*/
44+
export const generateFileList = codeBlocks => {
45+
const files = codeBlocks.map(({ name, content }) => {
46+
return {
47+
name,
48+
content: name === 'test.js' ? updateJsRequirePaths(content) : content,
49+
};
50+
});
51+
52+
files.push({
53+
name: 'binding.gyp',
54+
content: createBindingGyp(files.map(({ name }) => name)),
55+
});
56+
57+
return files;
58+
};
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Checks if a section contains the required code blocks for building.
3+
* A buildable section must contain at least one C++ (.cc) file and one
4+
* JavaScript (.js) file.
5+
*
6+
* @param {Array<{name: string; content: string}>} codeBlocks Array of code blocks
7+
* @returns {boolean}
8+
*/
9+
export const isBuildableSection = codeBlocks => {
10+
return (
11+
codeBlocks.some(codeBlock => codeBlock.name.endsWith('.cc')) &&
12+
codeBlocks.some(codeBlock => codeBlock.name.endsWith('.js'))
13+
);
14+
};
15+
16+
/**
17+
* Normalizes a section name.
18+
*
19+
* @param {string} sectionName Original section name
20+
* @returns {string}
21+
*/
22+
export const normalizeSectionName = sectionName => {
23+
return sectionName.toLowerCase().replace(/\s/g, '_').replace(/\W/g, '');
24+
};
25+
26+
/**
27+
* Generates a standardized folder name for a section.
28+
*
29+
* @param {string} sectionName Normalized section name
30+
* @param {number} index Zero-based section index
31+
* @returns {string}
32+
*/
33+
export const generateSectionFolderName = (sectionName, index) => {
34+
const identifier = String(index + 1).padStart(2, '0');
35+
36+
return `${identifier}_${sectionName}`;
37+
};

src/generators/addon-verify/utils/updateFilesForBuild.mjs

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)