Skip to content

Commit

Permalink
feat: use cli-config
Browse files Browse the repository at this point in the history
  • Loading branch information
peterpeterparker committed Feb 14, 2024
1 parent b305de9 commit b7b224e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 33 deletions.
21 changes: 13 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
"@dfinity/identity": "^0.20.2",
"@dfinity/principal": "^0.20.2",
"@junobuild/admin": "^0.0.45",
"@junobuild/cli-config": "file:../juno-js/packages/cli-config",
"@junobuild/core-peer": "^0.0.10",
"@junobuild/types": "file:../juno-js/packages/types",
"@junobuild/utils": "^0.0.18",
"conf": "^12.0.0",
"file-type": "^19.0.0",
Expand Down
25 changes: 5 additions & 20 deletions src/configs/juno.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {existsSync, readFileSync} from 'node:fs';
import type {JunoConfigFnOrObject} from '@junobuild/cli-config';

Check failure on line 1 in src/configs/juno.config.ts

View workflow job for this annotation

GitHub Actions / build

Cannot find module '@junobuild/cli-config' or its corresponding type declarations.
import {existsSync} from 'node:fs';
import {access, readFile, writeFile} from 'node:fs/promises';
import {join} from 'node:path';
import {JUNO_CONFIG_FILENAME} from '../constants/constants';
Expand Down Expand Up @@ -97,30 +98,14 @@ const writeJunoConfig = async (config: JunoConfig): Promise<void> => {
await writeFile(JUNO_CONFIG_FILENAME, JSON.stringify(config, null, 2), 'utf-8');
};

// TODO: replace
export function defineConfig(config: JunoConfig): JunoConfig;
export function defineConfig(config: JunoConfig): JunoConfig {
return config;
}

const readJunoConfig = async (): Promise<JunoConfig> => {
const {configPath, configType} = junoConfigFile();

switch (configType) {
case 'ts': {

// const module = await import(`data:text/javascript;base64,${Buffer.from(readFileSync(configPath).toString()).toString(`base64`)}`);
// console.log('MODUE', module)
//
// console.log(readFileSync(configPath).toString());
// const tmp = `data:text/javascript,${readFileSync(configPath).toString()}`;
// const result = await import(tmp);
//
//
// console.log('instance', result);

const userConfig = nodeRequire<JunoConfig | typeof defineConfig>(configPath).default;
const config = typeof userConfig === 'function' ? userConfig : userConfig;
const userConfig = nodeRequire<JunoConfigFnOrObject>(configPath).default;
const config =
typeof userConfig === 'function' ? userConfig({mode: 'production'}) : userConfig;

console.log('Config', config);

Expand Down
24 changes: 20 additions & 4 deletions src/utils/node.utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {transformFileSync} from '@babel/core';
import * as mod from '@babel/plugin-transform-modules-commonjs';
import * as ts from '@babel/preset-typescript';
import * as tst from "@junobuild/types";
import {defineConfig, type JunoConfig} from '@junobuild/cli-config';

Check failure on line 4 in src/utils/node.utils.ts

View workflow job for this annotation

GitHub Actions / build

Cannot find module '@junobuild/cli-config' or its corresponding type declarations.
import {readFileSync} from 'node:fs';

/**
Expand All @@ -12,6 +12,9 @@ export const nodeRequire = <T>(id: string): {default: T} => {
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete require.cache[id];

const Module = require('module');

Check failure on line 15 in src/utils/node.utils.ts

View workflow job for this annotation

GitHub Actions / lint

Require statement not part of import statement
const originalLoad = Module._load;

try {
// let's override node's require for a second
// don't worry, we'll revert this when we're done
Expand All @@ -26,9 +29,6 @@ export const nodeRequire = <T>(id: string): {default: T} => {
presets: [ts.default],
plugins: [mod.default]
}).code;

console.log(sourceText)

} else {
// quick hack to turn a modern es module
// into and old school commonjs module
Expand All @@ -45,11 +45,27 @@ export const nodeRequire = <T>(id: string): {default: T} => {
(module as NodeModuleWithCompile)._compile(sourceText, fileName);
};

// We override defineConfig because the library is unknown in the module we are trying to load.
// This need to be a function and not an arrow function because of the "arguments"
Module._load = function (request: string): unknown {
if (request === '@junobuild/cli-config') {
return {
defineConfig: (config: JunoConfig) => defineConfig(config)
};
}

// @ts-ignore arguments are passed by NodeJS

Check failure on line 57 in src/utils/node.utils.ts

View workflow job for this annotation

GitHub Actions / lint

Use "@ts-expect-error" to ensure an error is actually being suppressed
return originalLoad.apply(this, arguments);
};

// let's do this!
return require(id);
} finally {
// all set, let's go ahead and reset the require back to the default
// eslint-disable-next-line n/no-deprecated-api
require.extensions['.ts'] = undefined;

// Redo our hack
Module._load = originalLoad;
}
};

0 comments on commit b7b224e

Please sign in to comment.