vovk-cli 0.0.1-beta.59 → 0.0.1-beta.60

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.
@@ -1,26 +1,62 @@
1
+ import { readFile } from 'node:fs/promises';
2
+ import { createRequire } from 'node:module';
3
+ import { dirname } from 'node:path';
4
+ import { runInNewContext } from 'node:vm';
1
5
  import { pathToFileURL } from 'node:url';
2
6
  import { getConfigAbsolutePaths } from './getConfigAbsolutePaths.mjs';
3
- import { importUncachedModule } from './importUncachedModule.mjs';
4
7
  export async function getUserConfig({ configPath: givenConfigPath, cwd, }) {
5
8
  const configAbsolutePaths = await getConfigAbsolutePaths({ configPath: givenConfigPath, cwd });
6
- let userConfig;
7
9
  if (!configAbsolutePaths.length) {
8
10
  return { userConfig: null, configAbsolutePaths };
9
11
  }
10
12
  const configPath = configAbsolutePaths[0];
11
- // TODO explain
12
- try {
13
- userConfig = await importUncachedModule(configPath);
14
- }
15
- catch {
13
+ let userConfig;
14
+ let lastError;
15
+ const loaders = [
16
+ () => loadConfigWithVm(configPath),
17
+ () => importWithCacheBuster(configPath),
18
+ ];
19
+ for (const loader of loaders) {
16
20
  try {
17
- const cacheBuster = Date.now();
18
- const configPathUrl = pathToFileURL(configPath).href;
19
- ({ default: userConfig } = (await import(`${configPathUrl}?cache=${cacheBuster}`)));
21
+ userConfig = await loader();
22
+ return { userConfig, configAbsolutePaths };
20
23
  }
21
24
  catch (e) {
22
- return { userConfig: null, configAbsolutePaths, error: e };
25
+ lastError = e;
23
26
  }
24
27
  }
25
- return { userConfig, configAbsolutePaths };
28
+ return { userConfig: null, configAbsolutePaths, error: lastError };
29
+ }
30
+ async function importWithCacheBuster(configPath) {
31
+ const cacheBuster = Date.now();
32
+ const configPathUrl = pathToFileURL(configPath).href;
33
+ const { default: userConfig } = (await import(`${configPathUrl}?cache=${cacheBuster}`));
34
+ return userConfig;
35
+ }
36
+ async function loadConfigWithVm(configPath) {
37
+ const source = await readFile(configPath, 'utf8');
38
+ const moduleUrl = pathToFileURL(configPath).href;
39
+ const cjsSource = transformDefaultExportToCjs(source);
40
+ const moduleObj = { exports: {} };
41
+ const context = {
42
+ module: moduleObj,
43
+ exports: moduleObj.exports,
44
+ require: createRequire(moduleUrl),
45
+ __dirname: dirname(configPath),
46
+ __filename: configPath,
47
+ console,
48
+ process,
49
+ Buffer,
50
+ setTimeout,
51
+ clearTimeout,
52
+ setInterval,
53
+ clearInterval,
54
+ URL,
55
+ };
56
+ runInNewContext(cjsSource, context, { filename: moduleUrl, displayErrors: true });
57
+ const result = moduleObj.exports;
58
+ return result?.default ?? result;
59
+ }
60
+ function transformDefaultExportToCjs(source) {
61
+ return source.replace(/^\s*export\s+default\s+/m, 'module.exports = ');
26
62
  }
@@ -1,5 +1,7 @@
1
+ import type { VovkStrictConfig } from 'vovk/internal';
1
2
  import type { getLogger } from '../utils/getLogger.mjs';
2
3
  import type { InitOptions } from '../types.mjs';
4
+ export declare const BUNDLE_BUILD_TSDOWN: ({ entry, outDir }: Parameters<VovkStrictConfig["bundle"]["build"]>[0]) => Promise<void>;
3
5
  export declare function createConfig({ root, options: { validationLibrary, bundle, lang, dryRun }, }: {
4
6
  root: string;
5
7
  log: ReturnType<typeof getLogger>;
@@ -3,6 +3,19 @@ import fs from 'node:fs/promises';
3
3
  import { prettify } from '../utils/prettify.mjs';
4
4
  import { getFileSystemEntryType, FileSystemEntryType } from '../utils/getFileSystemEntryType.mjs';
5
5
  import { updateConfigProperty } from '../utils/updateConfigProperty.mjs';
6
+ export const BUNDLE_BUILD_TSDOWN = async ({ entry, outDir }) => {
7
+ const { build } = await import('tsdown');
8
+ await build({
9
+ entry,
10
+ dts: true,
11
+ format: ['cjs', 'esm'],
12
+ hash: false,
13
+ fixedExtension: true,
14
+ clean: true,
15
+ outDir,
16
+ tsconfig: './tsconfig.bundle.json',
17
+ });
18
+ };
6
19
  export async function createConfig({ root, options: { validationLibrary, bundle, lang, dryRun }, }) {
7
20
  const config = {};
8
21
  const dotConfigPath = path.join(root, '.config');
@@ -42,21 +55,9 @@ export async function createConfig({ root, options: { validationLibrary, bundle,
42
55
  let configStr = await prettify(`// @ts-check
43
56
  /** @type {import('vovk').VovkConfig} */
44
57
  const config = ${JSON.stringify(config, null, 2)};
45
- ${isModule ? '\nexport config;' : 'module.exports = config;'}`, configAbsolutePath);
58
+ ${isModule ? '\nexport default config;' : 'module.exports = config;'}`, configAbsolutePath);
46
59
  if (bundle) {
47
- configStr = await updateConfigProperty(configStr, ['bundle', 'build'], async ({ entry, outDir }) => {
48
- const { build } = await import('tsdown');
49
- await build({
50
- entry,
51
- dts: true,
52
- format: ['cjs', 'esm'],
53
- hash: false,
54
- fixedExtension: true,
55
- clean: true,
56
- outDir,
57
- tsconfig: './tsconfig.bundle.json',
58
- });
59
- });
60
+ configStr = await updateConfigProperty(configStr, ['bundle', 'build'], BUNDLE_BUILD_TSDOWN);
60
61
  }
61
62
  if (!dryRun)
62
63
  await fs.writeFile(configAbsolutePath, configStr, 'utf-8');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vovk-cli",
3
- "version": "0.0.1-beta.59",
3
+ "version": "0.0.1-beta.60",
4
4
  "bin": {
5
5
  "vovk": "./dist/index.mjs"
6
6
  },
@@ -35,7 +35,7 @@
35
35
  },
36
36
  "homepage": "https://vovk.dev",
37
37
  "peerDependencies": {
38
- "vovk": "^3.0.0-beta.87",
38
+ "vovk": "^3.0.0-beta.89",
39
39
  "vovk-ajv": "^0.0.0-beta.4",
40
40
  "vovk-client": "^0.0.4-beta.4",
41
41
  "vovk-python": "^0.0.1-beta.4",
@@ -1,2 +0,0 @@
1
- import './importUncachedModuleWorker.mjs';
2
- export declare function importUncachedModule<T>(modulePath: string): Promise<T>;
@@ -1,36 +0,0 @@
1
- // importUncachedModule.js
2
- import { Worker } from 'node:worker_threads';
3
- import path from 'node:path';
4
- import './importUncachedModuleWorker.mjs'; // required for TS compilation
5
- export function importUncachedModule(modulePath) {
6
- return new Promise((resolve, reject) => {
7
- // Resolve the path to the worker script
8
- const workerPath = path.resolve(import.meta.dirname, 'importUncachedModuleWorker.mjs');
9
- // Initialize the worker thread
10
- const worker = new Worker(workerPath, {
11
- workerData: { modulePath },
12
- });
13
- // Listen for messages from the worker
14
- worker.on('message', (d) => {
15
- const data = d;
16
- if (data.success) {
17
- resolve(data.exportDefault);
18
- }
19
- else {
20
- reject(new Error(data.error));
21
- }
22
- void worker.terminate();
23
- });
24
- // Handle errors from the worker
25
- worker.on('error', (err) => {
26
- reject(err);
27
- void worker.terminate();
28
- });
29
- // Handle worker exit
30
- worker.on('exit', (code) => {
31
- if (code !== 0) {
32
- reject(new Error(`Worker stopped with exit code ${code}`));
33
- }
34
- });
35
- });
36
- }
@@ -1,25 +0,0 @@
1
- import { parentPort, workerData as wData } from 'node:worker_threads';
2
- import { pathToFileURL } from 'node:url';
3
- void (async () => {
4
- if (!parentPort)
5
- return;
6
- const workerData = wData;
7
- try {
8
- // Convert the module path to a file URL
9
- const moduleUrl = pathToFileURL(workerData.modulePath).href;
10
- // Dynamically import the module
11
- const importedModule = (await import(moduleUrl));
12
- // Send the module's exports back to the main thread
13
- parentPort?.postMessage({
14
- success: true,
15
- exportDefault: importedModule.default,
16
- });
17
- }
18
- catch (error) {
19
- // Send the error back to the main thread
20
- parentPort?.postMessage({
21
- success: false,
22
- error: error.message,
23
- });
24
- }
25
- })();