Skip to content

Commit f41bf9f

Browse files
authored
Migrate to ESM format (#215)
* Migrate to ESM format Signed-off-by: Gonzalo Gomez Gracia <gonzalo.gomez@broadcom.com> --------- Signed-off-by: Gonzalo Gomez Gracia <gonzalo.gomez@broadcom.com>
1 parent 79d7bbf commit f41bf9f

File tree

15 files changed

+6555
-3426
lines changed

15 files changed

+6555
-3426
lines changed

.eslintrc.js

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

bin/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
* SPDX-License-Identifier: Apache-2.0
66
*/
77

8-
const { program } = require('commander');
9-
const { runReadmeGenerator } = require('../index');
8+
import { program } from 'commander';
9+
import runReadmeGenerator from '../index.js';
1010

1111
program
1212
.option('-v, --values <path>', 'Path to the values.yaml file')

eslint.config.js

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import js from '@eslint/js';
2+
import globals from 'globals';
3+
import json from '@eslint/json';
4+
import markdown from '@eslint/markdown';
5+
import { globalIgnores, defineConfig } from 'eslint/config';
6+
import jest from 'eslint-plugin-jest';
7+
import node from 'eslint-plugin-node';
8+
import unicorn from 'eslint-plugin-unicorn';
9+
import { fixupPluginRules } from '@eslint/compat';
10+
import _import from 'eslint-plugin-import';
11+
12+
export default defineConfig([
13+
// Non-required paths
14+
globalIgnores(['**/node_modules/', '.git/']),
15+
{
16+
files: ['**/*.{js,mjs,cjs}'],
17+
plugins: {
18+
js,
19+
node,
20+
unicorn,
21+
import: fixupPluginRules(_import),
22+
},
23+
extends: ['js/recommended'],
24+
languageOptions: {
25+
ecmaVersion: 'latest',
26+
globals: {
27+
...globals.node,
28+
...globals.browser,
29+
},
30+
},
31+
rules: {
32+
'semi': ['error', 'always'],
33+
'prefer-const': 'error',
34+
'no-unused-vars': 'error',
35+
'max-len': ['error', { 'code': 120 }],
36+
// eslint-plugin-node
37+
'node/no-missing-import': 'error',
38+
'node/no-extraneous-import': 'error',
39+
'node/file-extension-in-import': 'error',
40+
'node/no-sync': 'off',
41+
// eslint-plugin-import
42+
'import/no-unresolved': 'error',
43+
'import/no-useless-path-segments': 'error',
44+
'import/no-extraneous-dependencies': 'error',
45+
'import/no-commonjs': 'error',
46+
// eslint-plugin-unicorn
47+
'unicorn/prefer-module': 'error',
48+
'unicorn/prefer-node-protocol': 'error',
49+
'unicorn/prefer-top-level-await': 'error',
50+
},
51+
},
52+
{
53+
files: ['eslint.config.js'],
54+
rules: {
55+
// Disables the eslint/config not found message
56+
'node/no-missing-import': 'off',
57+
'import/no-unresolved': 'off',
58+
'max-len': 'off',
59+
},
60+
},
61+
{
62+
files: ['**/*.json'],
63+
plugins: { json },
64+
language: 'json/json',
65+
extends: ['json/recommended']
66+
},
67+
{
68+
files: ['package-lock.json'],
69+
rules: {
70+
'json/no-empty-keys': 'off',
71+
},
72+
},
73+
{
74+
files: ['**/*.md'],
75+
plugins: { markdown },
76+
language: 'markdown/gfm',
77+
extends: ['markdown/recommended'],
78+
// Allow special labels: alerts
79+
// https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax#alerts
80+
rules: {
81+
'markdown/no-missing-label-refs': [
82+
'error',
83+
{
84+
allowLabels: [
85+
'!NOTE',
86+
'!TIP',
87+
'!IMPORTANT',
88+
'!WARNING',
89+
'!CAUTION',
90+
],
91+
},
92+
],
93+
},
94+
},
95+
{
96+
files: ['tests/*.js'],
97+
plugins: {
98+
js,
99+
jest
100+
},
101+
extends: [
102+
'js/recommended',
103+
'jest/recommended',
104+
],
105+
rules: {
106+
'max-len': 'off',
107+
},
108+
},
109+
]);

index.js

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
/* eslint-disable global-require */
7-
/* eslint-disable import/no-dynamic-require */
6+
import fs from 'node:fs';
7+
import pjson from './package.json' with { type: "json" };
88

9-
const fs = require('fs');
10-
const pjson = require('./package.json');
9+
import { createValuesObject, parseMetadataComments } from './lib/parser.js';
10+
import checkKeys from './lib/checker.js';
11+
import { combineMetadataAndValues, buildParamsToRenderList } from './lib/builder.js';
12+
import { insertReadmeTable, renderOpenAPISchema } from './lib/render.js';
1113

12-
const { createValuesObject, parseMetadataComments } = require('./lib/parser');
13-
const { checkKeys } = require('./lib/checker');
14-
const { combineMetadataAndValues, buildParamsToRenderList } = require('./lib/builder');
15-
const { insertReadmeTable, renderOpenAPISchema } = require('./lib/render');
14+
// Common alias for __dirname not available in ESM
15+
const __dirname = import.meta.dirname;
1616

1717
function getParsedMetadata(valuesFilePath, config) {
1818
const valuesObject = createValuesObject(valuesFilePath);
@@ -28,14 +28,14 @@ function getParsedMetadata(valuesFilePath, config) {
2828
return valuesMetadata;
2929
}
3030

31-
function runReadmeGenerator(options) {
31+
export default function runReadmeGenerator(options) {
3232
const valuesFilePath = options.values;
3333
const readmeFilePath = options.readme;
3434
const schemaFilePath = options.schema;
3535
const versionFlag = options.version;
3636

3737
if (versionFlag) {
38-
console.log('Version:', pjson.version); // eslint-disable-line no-console
38+
console.log('Version:', pjson.version);
3939
} else {
4040
if (!readmeFilePath && !schemaFilePath) {
4141
throw new Error('Nothing to do. Please provide the --readme or --schema options.');
@@ -62,6 +62,3 @@ function runReadmeGenerator(options) {
6262
}
6363
}
6464

65-
module.exports = {
66-
runReadmeGenerator,
67-
};

lib/builder.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
/* eslint-disable no-restricted-syntax */
7-
8-
const { cloneDeep } = require('lodash');
6+
import cloneDeep from 'lodash/cloneDeep.js';
97

108
/*
119
* Sets the proper value for the provided parameter taking into account its modifiers.
@@ -65,7 +63,7 @@ function applyModifiers(param, config) {
6563
* IMPORTANT: the array returned will have fields that should not be rendered on the README and
6664
* fields that should not be rendered in the schema. They will be selected later.
6765
*/
68-
function combineMetadataAndValues(valuesObject, valuesMetadata) {
66+
export function combineMetadataAndValues(valuesObject, valuesMetadata) {
6967
for (const param of valuesMetadata) {
7068
// The parameters with extra do not appear in the actual object and don't have a value
7169
if (!param.extra) {
@@ -100,7 +98,7 @@ function combineMetadataAndValues(valuesObject, valuesMetadata) {
10098
/*
10199
* Returns the Parameter list that will be rendered in the README
102100
*/
103-
function buildParamsToRenderList(parametersList, config) {
101+
export function buildParamsToRenderList(parametersList, config) {
104102
let returnList = cloneDeep(parametersList);
105103
for (const param of returnList) {
106104
// Modify values following modifiers, except for nullable parameters
@@ -114,8 +112,3 @@ function buildParamsToRenderList(parametersList, config) {
114112

115113
return returnList;
116114
}
117-
118-
module.exports = {
119-
combineMetadataAndValues,
120-
buildParamsToRenderList,
121-
};

lib/checker.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
/* eslint-disable no-restricted-syntax */
7-
/* eslint-disable no-console */
8-
9-
const { cloneDeep } = require('lodash');
10-
const utils = require('./utils');
6+
import cloneDeep from 'lodash/cloneDeep.js';
7+
import * as utils from './utils.js';
118

129
/*
1310
* Filter sections and objects
@@ -71,7 +68,7 @@ function getValuesToCheck(valuesObject, valuesMetadata) {
7168
* - realKeys: Array with the real YAML keys
7269
* - parsedKeys: Array with the keys to check against the real ones
7370
*/
74-
function checkKeys(valuesObject, valuesMetadata) {
71+
export default function checkKeys(valuesObject, valuesMetadata) {
7572
const valuesToCheck = getValuesToCheck(valuesObject, valuesMetadata);
7673
const realKeys = valuesToCheck[0];
7774
const parsedKeys = valuesToCheck[1];
@@ -95,7 +92,3 @@ function checkKeys(valuesObject, valuesMetadata) {
9592
console.log('INFO: Metadata is correct!');
9693
}
9794
}
98-
99-
module.exports = {
100-
checkKeys,
101-
};

lib/metadata.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
class Metadata {
6+
export default class Metadata {
77
constructor() {
88
/* Parsed metadata comments */
99
// List of available sections
@@ -21,4 +21,3 @@ class Metadata {
2121
}
2222
}
2323

24-
module.exports = Metadata;

lib/parameter.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
class Parameter {
6+
export default class Parameter {
77
constructor(name) {
88
/* Parameter information */
99
// The parameter path using dot notation
@@ -57,4 +57,3 @@ class Parameter {
5757
}
5858
}
5959

60-
module.exports = Parameter;

lib/parser.js

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,36 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
/* eslint-disable no-restricted-syntax */
6+
import * as fs from 'node:fs';
7+
import dot from 'dot-object';
8+
import * as YAML from 'yaml';
9+
import _ from 'lodash';
710

8-
const fs = require('fs');
9-
const dot = require('dot-object');
10-
const YAML = require('yaml');
11-
const _ = require('lodash');
12-
13-
const utils = require('./utils');
14-
const Parameter = require('./parameter');
15-
const Section = require('./section');
16-
const Metadata = require('./metadata');
11+
import * as utils from './utils.js';
12+
import Parameter from './parameter.js';
13+
import Section from './section.js';
14+
import Metadata from './metadata.js';
1715

1816
/*
1917
* Returns a Metadata object
2018
* The objects within that wrapper object are parsed from the comments metadata
2119
* See metadata.js
2220
*/
23-
function parseMetadataComments(valuesFilePath, config) {
24-
/* eslint-disable prefer-destructuring */
21+
export function parseMetadataComments(valuesFilePath, config) {
22+
2523

2624
const data = fs.readFileSync(valuesFilePath, 'UTF-8');
2725
const lines = data.split(/\r?\n/);
2826

2927
const parsedValues = new Metadata();
28+
// eslint-disable-next-line max-len
3029
const paramRegex = new RegExp(`^\\s*${config.comments.format}\\s*${config.tags.param}\\s*([^\\s]+)\\s*(\\[.*?\\])?\\s*(.*)$`);
3130
const sectionRegex = new RegExp(`^\\s*${config.comments.format}\\s*${config.tags.section}\\s*(.*)$`);
3231
const descriptionStartRegex = new RegExp(`^\\s*${config.comments.format}\\s*${config.tags.descriptionStart}\\s*(.*)`);
3332
const descriptionContentRegex = new RegExp(`^\\s*${config.comments.format}\\s?(.*)`);
3433
const descriptionEndRegex = new RegExp(`^\\s*${config.comments.format}\\s*${config.tags.descriptionEnd}\\s*(.*)`);
3534
const skipRegex = new RegExp(`^\\s*${config.comments.format}\\s*${config.tags.skip}\\s*([^\\s]+)\\s*(.*)$`);
35+
// eslint-disable-next-line max-len
3636
const extraRegex = new RegExp(`^\\s*${config.comments.format}\\s*${config.tags.extra}\\s*([^\\s]+)\\s*(\\[.*?\\])?\\s*(.*)$`);
3737

3838
// We assume there will always be a section before any parameter. At least one section is required
@@ -117,7 +117,7 @@ function parseMetadataComments(valuesFilePath, config) {
117117
* Returns an array of Parameters parsed from the actual YAML content
118118
* This object contains the actual type and value of the object
119119
*/
120-
function createValuesObject(valuesFilePath) {
120+
export function createValuesObject(valuesFilePath) {
121121
const resultValues = [];
122122
const valuesJSON = YAML.parse(fs.readFileSync(valuesFilePath, 'utf8'));
123123
const dottedFormatProperties = dot.dot(valuesJSON);
@@ -180,8 +180,3 @@ function createValuesObject(valuesFilePath) {
180180

181181
return resultValues;
182182
}
183-
184-
module.exports = {
185-
parseMetadataComments,
186-
createValuesObject,
187-
};

0 commit comments

Comments
 (0)