@xuda.io/drive_module 1.1.1054 → 1.1.1056

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +218 -7
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -1430,13 +1430,10 @@ exports.check_drive_file = async function (req, job_id, headers, file_obj) {
1430
1430
  }
1431
1431
  case "workspace": {
1432
1432
  selector.docType = "workspace_drive";
1433
- const workspace_drive_res = await db_module.find_app_couch_query(
1434
- master_app_id,
1435
- {
1436
- selector,
1437
- limit: 99,
1438
- }
1439
- );
1433
+ const workspace_drive_res = await db_module.find_app_couch_query(app_id, {
1434
+ selector,
1435
+ limit: 99,
1436
+ });
1440
1437
  if (workspace_drive_res.docs.length) {
1441
1438
  return {
1442
1439
  code: -764,
@@ -2100,6 +2097,220 @@ const save_drive_doc = async function (drive_type, app_id, doc) {
2100
2097
  return save_ret;
2101
2098
  };
2102
2099
 
2100
+ exports.compile_javascript_program_in_studio_drive = async function (
2101
+ app_id,
2102
+ prog_id
2103
+ ) {
2104
+ try {
2105
+ const doc = await db_module.get_app_couch_doc(app_id, prog_id);
2106
+
2107
+ const studio_drive_script_folder = path.join(
2108
+ _conf.studio_drive_path,
2109
+ app_id,
2110
+ "scripts"
2111
+ );
2112
+ const fs_module = require(`${module_path}/fs_module`);
2113
+ if (!(await fs_module.file_exists(studio_drive_script_folder))) {
2114
+ await fs_module.mkdir(studio_drive_script_folder, 0o775);
2115
+ }
2116
+
2117
+ const studio_drive_script_program_folder = path.join(
2118
+ studio_drive_script_folder,
2119
+ doc._id
2120
+ );
2121
+
2122
+ const src = path.join(studio_drive_script_program_folder, "src");
2123
+ const dist = path.join(studio_drive_script_program_folder, "dist");
2124
+
2125
+ if (!(await fs_module.file_exists(studio_drive_script_program_folder))) {
2126
+ await fs_module.mkdir(studio_drive_script_program_folder, 0o775);
2127
+ await fs_module.mkdir(src, 0o775);
2128
+ await fs_module.mkdir(dist, 0o775);
2129
+ }
2130
+
2131
+ const inputCode = doc.scriptData?.value || "";
2132
+
2133
+ function wrapCodeWithFunction(inputCode) {
2134
+ const lines = inputCode.split("\n");
2135
+ const importLines = [];
2136
+ const codeLines = [];
2137
+
2138
+ // Separate import statements from the rest of the code
2139
+ for (const line of lines) {
2140
+ if (line.trim().startsWith("import ")) {
2141
+ importLines.push(line);
2142
+ } else {
2143
+ codeLines.push(line);
2144
+ }
2145
+ }
2146
+
2147
+ // Wrap the non-import lines with the function
2148
+ const wrappedCode = [
2149
+ ...importLines,
2150
+ "",
2151
+ "export default async function (xu, SESSION_ID, SESSION_OBJ, job_id) {",
2152
+ ...codeLines.map((line) => ` ${line}`), // Indent the code inside the function
2153
+ "}",
2154
+ ];
2155
+
2156
+ // Join the lines back into a single string
2157
+ return wrappedCode.join("\n");
2158
+ }
2159
+
2160
+ let content = wrapCodeWithFunction(inputCode);
2161
+ ///// insert the css
2162
+ const index_css = path.join(dist, "index.css");
2163
+ if (await fs_module.file_exists(index_css)) {
2164
+ // const index_css_content = fs_module.read_file(index_css);
2165
+ content = ` export const css = true \n` + content;
2166
+ }
2167
+
2168
+ ////////////////////////
2169
+
2170
+ const index_js = path.join(src, "index.mjs");
2171
+ await fs_module.save_file(index_js, content);
2172
+
2173
+ ///////////////////////
2174
+
2175
+ let packageJSON = {
2176
+ name: doc.properties?.menuName || doc._id,
2177
+ version: "1.0.0",
2178
+ description: "",
2179
+ main: "index.js",
2180
+ scripts: { dev: "vite", build: "vite build", preview: "vite preview" },
2181
+ license: "ISC",
2182
+ dependencies: {},
2183
+ devDependencies: { vite: "^5.4.10" },
2184
+ author:
2185
+ doc?.studio_meta?.checkedInUserName ||
2186
+ doc?.studio_meta?.createdByUid ||
2187
+ "",
2188
+ };
2189
+ const package_json = path.join(
2190
+ studio_drive_script_program_folder,
2191
+ "package.json"
2192
+ );
2193
+
2194
+ /////////////////
2195
+
2196
+ // Match `import` and `require` statements
2197
+ const importRegex = /import\s+(?:.*?from\s+)?['"](.+?)['"]/g;
2198
+ const requireRegex = /require\(['"](.+?)['"]\)/g;
2199
+
2200
+ let devDependencies = packageJSON.devDependencies || {};
2201
+ let allDependencies = new Set();
2202
+
2203
+ const extractDependencies = (fileContent, regex) => {
2204
+ const matches = [];
2205
+ let match;
2206
+ while ((match = regex.exec(fileContent)) !== null) {
2207
+ const dep = match[1];
2208
+ // Ignore relative imports and CSS files
2209
+ if (
2210
+ !dep.startsWith(".") &&
2211
+ !dep.startsWith("/") &&
2212
+ !dep.endsWith(".css")
2213
+ ) {
2214
+ matches.push(dep);
2215
+ }
2216
+ }
2217
+ return matches;
2218
+ };
2219
+
2220
+ const imports = extractDependencies(content, importRegex);
2221
+ const requires = extractDependencies(content, requireRegex);
2222
+ imports.concat(requires).forEach((dep) => allDependencies.add(dep));
2223
+
2224
+ // Add missing dependencies to package.json
2225
+ allDependencies.forEach((dep) => {
2226
+ if (!devDependencies[dep]) {
2227
+ console.log(`Adding dependency: ${dep}`);
2228
+ devDependencies[dep] = "latest"; // Use "latest" as the version or fetch actual versions if needed
2229
+ }
2230
+ });
2231
+
2232
+ // Update package.json
2233
+ packageJSON.devDependencies = devDependencies;
2234
+
2235
+ await fs_module.save_file(
2236
+ package_json,
2237
+ JSON.stringify(packageJSON, null, 2)
2238
+ );
2239
+ /////////////////////////
2240
+
2241
+ const vite_config = path.join(
2242
+ studio_drive_script_program_folder,
2243
+ "vite.config.js"
2244
+ );
2245
+
2246
+ const code = `
2247
+
2248
+ export default {
2249
+ build: {
2250
+ lib: {
2251
+ entry: "src/index.mjs",
2252
+ formats: ["es"],
2253
+ name: "MyLibrary", // Global variable for UMD/IIFE formats
2254
+ },
2255
+ rollupOptions: {
2256
+ output: {
2257
+ manualChunks(id) {
2258
+ if (id.includes("node_modules")) {
2259
+ return "vendor";
2260
+ }
2261
+
2262
+ },
2263
+ entryFileNames: "[name].mjs",
2264
+ chunkFileNames: "[name]-[hash].mjs",
2265
+ assetFileNames: (assetInfo) => {
2266
+ if (assetInfo.name && assetInfo.name.endsWith(".css")) {
2267
+ return "index[extname]";
2268
+ }
2269
+ return "[name]-[hash][extname]";
2270
+ },
2271
+
2272
+
2273
+ },
2274
+ },
2275
+ minify: false, // Disable minification for easier debugging
2276
+ },
2277
+
2278
+ define: {
2279
+ "process.env": process.env, // Pass environment variables
2280
+ },
2281
+ };
2282
+
2283
+
2284
+
2285
+ `;
2286
+
2287
+ await fs_module.save_file(vite_config, code);
2288
+ //////////////////////
2289
+
2290
+ await _utils.run_ssh_script(
2291
+ `npm i --prefix ${studio_drive_script_program_folder};`
2292
+ );
2293
+ const build_ret = await _utils.run_ssh_script(
2294
+ `npm run build --prefix ${studio_drive_script_program_folder};`
2295
+ );
2296
+
2297
+ //////////////////////
2298
+ doc.vite_build_res = {
2299
+ code: 1,
2300
+ data: build_ret,
2301
+ dependencies: devDependencies,
2302
+ };
2303
+ db_module.save_app_couch_doc(app_id, doc);
2304
+ } catch (err) {
2305
+ doc.vite_build_res = {
2306
+ code: -1,
2307
+ data: err.message,
2308
+ };
2309
+ console.error(doc);
2310
+ db_module.save_app_couch_doc(app_id, doc);
2311
+ }
2312
+ };
2313
+
2103
2314
  setTimeout(async function () {
2104
2315
  // console.log(
2105
2316
  // await exports.search_drive_files({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xuda.io/drive_module",
3
- "version": "1.1.1054",
3
+ "version": "1.1.1056",
4
4
  "description": "Xuda Drive Server Module",
5
5
  "main": "index.js",
6
6
  "dependencies": {