Skip to content

Commit 8ec63c9

Browse files
authored
refactor(addon-verify): some improvements (#175)
* refactor(addon-verify): nit * feat: use dedent
1 parent 5c5884c commit 8ec63c9

File tree

6 files changed

+130
-68
lines changed

6 files changed

+130
-68
lines changed

package-lock.json

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
},
3131
"dependencies": {
3232
"commander": "^13.0.0",
33+
"dedent": "^1.5.3",
3334
"github-slugger": "^2.0.0",
3435
"glob": "^11.0.0",
3536
"hast-util-to-string": "^3.0.1",

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: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import dedent from 'dedent';
2+
3+
/**
4+
* Updates JavaScript files with correct require paths for the build tree.
5+
*
6+
* @param {string} content Original code
7+
* @returns {string}
8+
*/
9+
const updateJsRequirePaths = content => {
10+
return dedent`'use strict';
11+
const common = require('../../common');
12+
${content.replace(
13+
"'./build/Release/addon'",
14+
'`./build/${common.buildType}/addon`'
15+
)}
16+
`;
17+
};
18+
19+
/**
20+
* Creates a binding.gyp configuration for C++ addon compilation.
21+
*
22+
* @param {string[]} sourceFiles List of source file names
23+
* @returns {string}
24+
*/
25+
const createBindingGyp = sourceFiles => {
26+
const config = {
27+
targets: [
28+
{
29+
target_name: 'addon',
30+
sources: sourceFiles,
31+
includes: ['../common.gypi'],
32+
},
33+
],
34+
};
35+
36+
return JSON.stringify(config);
37+
};
38+
39+
/**
40+
* Generates required files list from section's code blocks for C++ addon
41+
* compilation.
42+
*
43+
* @param {{name: string, content: string}[]} codeBlocks Array of code blocks
44+
* @returns {{name: string, content: string}[]}
45+
*/
46+
export const generateFileList = codeBlocks => {
47+
const files = codeBlocks.map(({ name, content }) => {
48+
return {
49+
name,
50+
content: name === 'test.js' ? updateJsRequirePaths(content) : content,
51+
};
52+
});
53+
54+
files.push({
55+
name: 'binding.gyp',
56+
content: createBindingGyp(files.map(({ name }) => name)),
57+
});
58+
59+
return files;
60+
};
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)