Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use cli-tools #102

Merged
merged 4 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@dfinity/identity": "^1.0.1",
"@dfinity/principal": "^1.0.1",
"@junobuild/admin": "^0.0.46",
"@junobuild/cli-tools": "^0.0.3",
"@junobuild/config": "^0.0.2",
"@junobuild/core-peer": "^0.0.11",
"@junobuild/utils": "^0.0.19",
Expand Down
101 changes: 17 additions & 84 deletions src/configs/juno.config.ts
Original file line number Diff line number Diff line change
@@ -1,84 +1,29 @@
import {
junoConfigExist as junoConfigExistTools,
junoConfigFile as junoConfigFileTools,
readJunoConfig as readJunoConfigTools,
type ConfigFilename
} from '@junobuild/cli-tools';
import type {JunoConfig, JunoConfigEnv, JunoConfigFnOrObject} from '@junobuild/config';
import {nonNullish} from '@junobuild/utils';
import {existsSync} from 'node:fs';
import {access, readFile, writeFile} from 'node:fs/promises';
import {join} from 'node:path';
import {writeFile} from 'node:fs/promises';
import {
TEMPLATE_INIT_PATH,
TEMPLATE_SATELLITE_CONFIG_FILENAME
} from '../constants/config.constants';
import {JUNO_CONFIG_FILENAME, JUNO_JSON} from '../constants/constants';
import {JUNO_CONFIG_FILENAME} from '../constants/constants';
import {DEPLOY_DEFAULT_SOURCE} from '../constants/deploy.constants';
import type {ConfigType, JunoConfigWithSatelliteId} from '../types/config';
import {readTemplateFile} from '../utils/fs.utils';
import {nodeRequire} from '../utils/node.utils';

const JUNO_CONFIG_FILE: {filename: ConfigFilename} = {filename: JUNO_CONFIG_FILENAME};

export const junoConfigExist = async (): Promise<boolean> => {
try {
const {configPath} = junoConfigFile();
await access(configPath);
return true;
} catch (err: unknown) {
if (err instanceof Error && 'code' in err && (err as NodeJS.ErrnoException).code === 'ENOENT') {
return false;
} else {
throw err;
}
}
return await junoConfigExistTools(JUNO_CONFIG_FILE);
};

export const junoConfigFile = (): {configPath: string; configType: ConfigType} => {
const junoTs = join(process.cwd(), `${JUNO_CONFIG_FILENAME}.ts`);

if (existsSync(junoTs)) {
return {
configPath: junoTs,
configType: 'ts'
};
}

const junoJs = join(process.cwd(), `${JUNO_CONFIG_FILENAME}.js`);

if (existsSync(junoJs)) {
return {
configPath: junoJs,
configType: 'js'
};
}

const junoMjs = join(process.cwd(), `${JUNO_CONFIG_FILENAME}.mjs`);

if (existsSync(junoMjs)) {
return {
configPath: junoMjs,
configType: 'js'
};
}

const junoCjs = join(process.cwd(), `${JUNO_CONFIG_FILENAME}.cjs`);

if (existsSync(junoCjs)) {
return {
configPath: junoCjs,
configType: 'js'
};
}

// Support for original juno.json file
const junoJsonDeprecated = join(process.cwd(), JUNO_JSON);

if (existsSync(junoJsonDeprecated)) {
return {
configPath: junoJsonDeprecated,
configType: 'json'
};
}

return {
configPath: join(process.cwd(), `${JUNO_CONFIG_FILENAME}.json`),
configType: 'json'
};
};
export const junoConfigFile = (): {configPath: string; configType: ConfigType} =>
junoConfigFileTools(JUNO_CONFIG_FILE);

export const writeJunoConfig = async ({
config,
Expand Down Expand Up @@ -117,23 +62,11 @@ export const writeJunoConfig = async ({
};

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

const config = (userConfig: JunoConfigFnOrObject): JunoConfig =>
typeof userConfig === 'function' ? userConfig(env) : userConfig;

switch (configType) {
case 'ts': {
const {default: userConfig} = nodeRequire<JunoConfigFnOrObject>(configPath);
return config(userConfig);
}
case 'js': {
const {default: userConfig} = await import(configPath);
return config(userConfig as JunoConfigFnOrObject);
}
default: {
const buffer = await readFile(configPath);
return JSON.parse(buffer.toString('utf-8'));
}
}
return await readJunoConfigTools({
...JUNO_CONFIG_FILE,
config
});
};
78 changes: 11 additions & 67 deletions src/configs/juno.dev.config.ts
Original file line number Diff line number Diff line change
@@ -1,72 +1,16 @@
import {existsSync} from 'node:fs';
import {access} from 'node:fs/promises';
import {join} from 'node:path';
import {JUNO_DEV_CONFIG_FILENAME, JUNO_DEV_JSON} from '../constants/constants';
import type {ConfigFilename} from '@junobuild/cli-tools';
import {
junoConfigExist as junoConfigExistTools,
junoConfigFile as junoConfigFileTools
} from '@junobuild/cli-tools';
import {JUNO_DEV_CONFIG_FILENAME} from '../constants/constants';
import type {ConfigType} from '../types/config';

const JUNO_DEV_CONFIG_FILE: {filename: ConfigFilename} = {filename: JUNO_DEV_CONFIG_FILENAME};

export const junoDevConfigExist = async (): Promise<boolean> => {
try {
const {configPath} = junoDevConfigFile();
await access(configPath);
return true;
} catch (err: unknown) {
if (err instanceof Error && 'code' in err && (err as NodeJS.ErrnoException).code === 'ENOENT') {
return false;
} else {
throw err;
}
}
return await junoConfigExistTools(JUNO_DEV_CONFIG_FILE);
};

export const junoDevConfigFile = (): {configPath: string; configType: ConfigType} => {
const junoTs = join(process.cwd(), `${JUNO_DEV_CONFIG_FILENAME}.ts`);

if (existsSync(junoTs)) {
return {
configPath: junoTs,
configType: 'ts'
};
}

const junoJs = join(process.cwd(), `${JUNO_DEV_CONFIG_FILENAME}.js`);

if (existsSync(junoJs)) {
return {
configPath: junoJs,
configType: 'js'
};
}

const junoMjs = join(process.cwd(), `${JUNO_DEV_CONFIG_FILENAME}.mjs`);

if (existsSync(junoMjs)) {
return {
configPath: junoMjs,
configType: 'js'
};
}

const junoCjs = join(process.cwd(), `${JUNO_DEV_CONFIG_FILENAME}.cjs`);

if (existsSync(junoCjs)) {
return {
configPath: junoCjs,
configType: 'js'
};
}

// Support for original juno.json file
const junoJsonDeprecated = join(process.cwd(), JUNO_DEV_JSON);

if (existsSync(junoJsonDeprecated)) {
return {
configPath: junoJsonDeprecated,
configType: 'json'
};
}

return {
configPath: join(process.cwd(), `${JUNO_DEV_CONFIG_FILENAME}.json`),
configType: 'json'
};
};
export const junoDevConfigFile = (): {configPath: string; configType: ConfigType} =>
junoConfigFileTools(JUNO_DEV_CONFIG_FILE);
8 changes: 0 additions & 8 deletions src/constants/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,6 @@ export const CLI_PROJECT_NAME = process.env.CLI_PROJECT_NAME;
export const REDIRECT_URL = 'http://localhost:{port}';
export const JUNO_CONFIG_FILENAME = 'juno.config'; // .json | .js | .mjs | .cjs | .ts
export const JUNO_DEV_CONFIG_FILENAME = 'juno.dev.config'; // .json | .js | .mjs | .cjs | .ts
/**
* @deprecated juno.json is deprecated but still supported. We are now using juno.config.xxx
*/
export const JUNO_JSON = 'juno.json';
/**
* @deprecated juno.dev.json is deprecated but still supported. We are now using juno.dev.config.xxx
*/
export const JUNO_DEV_JSON = 'juno.dev.json';
export const DAPP_COLLECTION = '#dapp';
export const SATELLITE_WASM_NAME = 'satellite';
export const MISSION_CONTROL_WASM_NAME = 'mission_control';
Expand Down
73 changes: 0 additions & 73 deletions src/utils/node.utils.ts

This file was deleted.

Loading