Skip to content

Commit f643ab7

Browse files
Merge pull request #10 from ubiquity-os/meniole-main
feat: esm + cjs
2 parents 4d26f35 + 33147cc commit f643ab7

File tree

1 file changed

+60
-28
lines changed

1 file changed

+60
-28
lines changed

action.yml

+60-28
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ inputs:
88
schemaPath:
99
description: "The path to the plugin settings schema."
1010
required: false
11-
default: "${{ github.workspace }}/src/types/plugin-input.js"
11+
default: "${{ github.workspace }}/src/types/plugin-input.ts"
1212
pluginEntry:
1313
description: "The path to the plugin entry file."
1414
required: false
@@ -37,11 +37,6 @@ runs:
3737
run: |
3838
yarn install --immutable --immutable-cache --check-cache
3939
40-
- name: Compile TypeScript files
41-
shell: bash
42-
run: |
43-
yarn tsc --project tsconfig.json -m commonjs
44-
4540
- name: Build project
4641
shell: bash
4742
run: |
@@ -55,38 +50,75 @@ runs:
5550
uses: actions/github-script@v7
5651
with:
5752
script: |
58-
const fs = require('fs');
53+
const fs = require('fs').promises;
5954
const path = require('path');
6055
61-
const { pluginSettingsSchema } = require('${{ github.workspace }}/plugin/index');
56+
async function updateManifest() {
57+
const manifestPath = '${{ inputs.manifestPath }}';
58+
const pluginPath = path.resolve('${{ github.workspace }}', 'plugin', 'index.js');
6259
63-
const manifestPath = '${{ inputs.manifestPath }}'
64-
const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8'));
60+
let pluginSettingsSchema;
61+
try {
62+
// First, try to load as ESM
63+
try {
64+
const pluginModule = await import(`file://${pluginPath}`);
65+
pluginSettingsSchema = pluginModule.pluginSettingsSchema;
6566
66-
const configuration = JSON.stringify(pluginSettingsSchema);
67-
68-
manifest["configuration"] = JSON.parse(configuration);
67+
if (!pluginSettingsSchema) {
68+
throw new Error('pluginSettingsSchema not found in the ESM module');
69+
}
70+
} catch (esmError) {
71+
// If ESM import fails, try loading as CJS
72+
try {
73+
const pluginModule = require(pluginPath);
74+
pluginSettingsSchema = pluginModule.pluginSettingsSchema;
6975
70-
function customReviver(key, value) {
71-
if (typeof value === "object" && value !== null) {
72-
if ("default" in value && "required" in value) {
73-
delete value.required;
76+
if (!pluginSettingsSchema) {
77+
throw new Error('pluginSettingsSchema not found in the CJS module');
78+
}
79+
} catch (cjsError) {
80+
console.error('Error loading module as ESM and CJS:', esmError, cjsError);
81+
process.exit(1);
82+
}
7483
}
75-
if (Array.isArray(value.required)) {
76-
value.required = value.required.filter(
77-
(prop) => !(value.properties[prop] && "default" in value.properties[prop])
78-
);
79-
if (!value.required.length) {
80-
delete value.required;
84+
} catch (error) {
85+
console.error('Error loading module:', error);
86+
process.exit(1);
87+
}
88+
89+
const manifest = JSON.parse(await fs.readFile(manifestPath, 'utf8'));
90+
manifest["configuration"] = pluginSettingsSchema;
91+
92+
function customReviver(key, value) {
93+
if (typeof value === "object" && value !== null) {
94+
if ("properties" in value && "required" in value) {
95+
const requiredFields = new Set(value.required);
96+
for (const [propKey, propValue] of Object.entries(value.properties)) {
97+
if (typeof propValue === 'object' && 'default' in propValue) {
98+
requiredFields.delete(propKey);
99+
}
100+
}
101+
value.required = Array.from(requiredFields);
102+
if (value.required.length === 0) {
103+
delete value.required;
104+
}
105+
}
106+
107+
// Recursively apply to nested objects and arrays
108+
if (Array.isArray(value)) {
109+
return value.map(item => JSON.parse(JSON.stringify(item), customReviver));
110+
} else {
111+
return Object.fromEntries(
112+
Object.entries(value).map(([k, v]) => [k, JSON.parse(JSON.stringify(v), customReviver)])
113+
);
81114
}
82115
}
116+
return value;
83117
}
84-
return value;
118+
119+
const updatedManifest = JSON.stringify(manifest, customReviver, 2);
120+
await fs.writeFile(manifestPath, updatedManifest, 'utf8');
85121
}
86-
87-
const updatedManifest = JSON.stringify(manifest, customReviver, 2);
88-
console.log('Updated manifest:', updatedManifest);
89-
fs.writeFileSync(manifestPath, updatedManifest);
90122
91123
- name: Format manifest using Prettier
92124
shell: bash

0 commit comments

Comments
 (0)